|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sylius package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Paweł Jędrzejewski |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sylius\Behat\Context\Transform; |
|
13
|
|
|
|
|
14
|
|
|
use Behat\Behat\Context\Context; |
|
15
|
|
|
use Sylius\Component\Core\Repository\ProductRepositoryInterface; |
|
16
|
|
|
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @author Łukasz Chruściel <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
final class ProductVariantContext implements Context |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var ProductRepositoryInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
private $productRepository; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var ProductVariantRepositoryInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $productVariantRepository; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param ProductRepositoryInterface $productRepository |
|
35
|
|
|
* @param ProductVariantRepositoryInterface $productVariantRepository |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(ProductRepositoryInterface $productRepository, ProductVariantRepositoryInterface $productVariantRepository) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->productRepository = $productRepository; |
|
40
|
|
|
$this->productVariantRepository = $productVariantRepository; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @Transform /^"([^"]+)" variant of product "([^"]+)"$/ |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getProductVariantByNameAndProduct($variantName, $productName) |
|
47
|
|
|
{ |
|
48
|
|
|
$product = $this->productRepository->findOneByName($productName); |
|
49
|
|
|
if (null === $product) { |
|
50
|
|
|
throw new \InvalidArgumentException(sprintf('Product with name "%s" does not exist', $productName)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$productVariant = $this->productVariantRepository->findOneBy(['presentation' => $variantName, 'object' => $product]); |
|
54
|
|
|
if (null === $productVariant) { |
|
55
|
|
|
throw new \InvalidArgumentException(sprintf('Product variant with name "%s" of product "%s" does not exist', $variantName, $productName)); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $productVariant; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|