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
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
18
|
|
|
use Webmozart\Assert\Assert; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Łukasz Chruściel <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
final class ProductVariantContext implements Context |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var ProductRepositoryInterface |
27
|
|
|
*/ |
28
|
|
|
private $productRepository; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var RepositoryInterface |
32
|
|
|
*/ |
33
|
|
|
private $productVariantRepository; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param ProductRepositoryInterface $productRepository |
37
|
|
|
* @param RepositoryInterface $productVariantRepository |
38
|
|
|
*/ |
39
|
|
|
public function __construct(ProductRepositoryInterface $productRepository, RepositoryInterface $productVariantRepository) |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
$this->productRepository = $productRepository; |
42
|
|
|
$this->productVariantRepository = $productVariantRepository; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @Transform /^"([^"]+)" variant of product "([^"]+)"$/ |
47
|
|
|
*/ |
48
|
|
|
public function getProductVariantByNameAndProduct($variantName, $productName) |
49
|
|
|
{ |
50
|
|
|
$products = $this->productRepository->findByName($productName, 'en_US'); |
51
|
|
|
|
52
|
|
|
Assert::eq( |
53
|
|
|
1, |
54
|
|
|
count($products), |
55
|
|
|
sprintf('%d products has been found with name "%s".', count($products), $productName) |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
$productVariant = $this->productVariantRepository->findOneBy(['name' => $variantName, 'product' => $products[0]]); |
59
|
|
|
if (null === $productVariant) { |
60
|
|
|
throw new \InvalidArgumentException(sprintf('Product variant with name "%s" of product "%s" does not exist', $variantName, $productName)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $productVariant; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @Transform /^"([^"]+)" product variant$/ |
68
|
|
|
* @Transform /^"([^"]+)" variant$/ |
69
|
|
|
* @Transform :variant |
70
|
|
|
*/ |
71
|
|
|
public function getProductVariantByName($name) |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$productVariants = $this->productVariantRepository->findByName($name); |
|
|
|
|
74
|
|
|
|
75
|
|
|
Assert::eq( |
76
|
|
|
1, |
77
|
|
|
count($productVariants), |
78
|
|
|
sprintf('%d product variants has been found with name "%s".', count($productVariants), $name) |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
return $productVariants[0]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @Transform /^variant with code "([^"]+)"$/ |
86
|
|
|
*/ |
87
|
|
|
public function getProductVariantByCode($code) |
88
|
|
|
{ |
89
|
|
|
$productVariant = $this->productVariantRepository->findOneBy(['code' => $code]); |
90
|
|
|
|
91
|
|
|
Assert::notNull($productVariant, sprintf('Cannot find product variant with code %s', $code)); |
92
|
|
|
|
93
|
|
|
return $productVariant; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.