|
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
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Sylius\Behat\Context\Transform; |
|
15
|
|
|
|
|
16
|
|
|
use Behat\Behat\Context\Context; |
|
17
|
|
|
use Sylius\Component\Core\Formatter\StringInflector; |
|
18
|
|
|
use Sylius\Component\Core\Repository\ProductRepositoryInterface; |
|
19
|
|
|
use Sylius\Component\Product\Repository\ProductVariantRepositoryInterface; |
|
20
|
|
|
use Webmozart\Assert\Assert; |
|
21
|
|
|
|
|
22
|
|
|
final class ProductVariantContext implements Context |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var ProductRepositoryInterface */ |
|
25
|
|
|
private $productRepository; |
|
26
|
|
|
|
|
27
|
|
|
/** @var ProductVariantRepositoryInterface */ |
|
28
|
|
|
private $productVariantRepository; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct( |
|
31
|
|
|
ProductRepositoryInterface $productRepository, |
|
32
|
|
|
ProductVariantRepositoryInterface $productVariantRepository |
|
33
|
|
|
) { |
|
34
|
|
|
$this->productRepository = $productRepository; |
|
35
|
|
|
$this->productVariantRepository = $productVariantRepository; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @Transform /^"([^"]+)" variant of product "([^"]+)"$/ |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getProductVariantByNameAndProduct($variantName, $productName) |
|
|
|
|
|
|
42
|
|
|
{ |
|
43
|
|
|
$products = $this->productRepository->findByName($productName, 'en_US'); |
|
44
|
|
|
|
|
45
|
|
|
Assert::eq( |
|
46
|
|
|
count($products), |
|
47
|
|
|
1, |
|
48
|
|
|
sprintf('%d products has been found with name "%s".', count($products), $productName) |
|
49
|
|
|
); |
|
50
|
|
|
|
|
51
|
|
|
$productVariants = $this->productVariantRepository->findByNameAndProduct($variantName, 'en_US', $products[0]); |
|
52
|
|
|
Assert::notEmpty( |
|
53
|
|
|
$productVariants, |
|
54
|
|
|
sprintf('Product variant with name "%s" of product "%s" does not exist', $variantName, $productName) |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
return $productVariants[0]; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @Transform /^"([^"]+)" product variant$/ |
|
62
|
|
|
* @Transform /^"([^"]+)" variant$/ |
|
63
|
|
|
* @Transform :variant |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getProductVariantByName($name) |
|
|
|
|
|
|
66
|
|
|
{ |
|
67
|
|
|
$productVariants = $this->productVariantRepository->findByName($name, 'en_US'); |
|
68
|
|
|
|
|
69
|
|
|
Assert::eq( |
|
70
|
|
|
count($productVariants), |
|
71
|
|
|
1, |
|
72
|
|
|
sprintf('%d product variants has been found with name "%s".', count($productVariants), $name) |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
|
|
return $productVariants[0]; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @Transform /^variant with code "([^"]+)"$/ |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getProductVariantByCode($code) |
|
|
|
|
|
|
82
|
|
|
{ |
|
83
|
|
|
$productVariant = $this->productVariantRepository->findOneBy(['code' => $code]); |
|
84
|
|
|
|
|
85
|
|
|
Assert::notNull($productVariant, sprintf('Cannot find product variant with code %s', $code)); |
|
86
|
|
|
|
|
87
|
|
|
return $productVariant; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @Transform /^"([^"]*)" (\w+) \/ "([^"]*)" (\w+) variant of product "([^"]+)"$/ |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getVariantByOptionValuesAndProduct( |
|
|
|
|
|
|
94
|
|
|
string $value1, |
|
95
|
|
|
string $option1, |
|
96
|
|
|
string $value2, |
|
97
|
|
|
string $option2, |
|
98
|
|
|
string $productName |
|
99
|
|
|
) { |
|
100
|
|
|
$products = $this->productRepository->findByName($productName, 'en_US'); |
|
101
|
|
|
|
|
102
|
|
|
Assert::eq( |
|
103
|
|
|
count($products), |
|
104
|
|
|
1, |
|
105
|
|
|
sprintf('%d products has been found with name "%s".', count($products), $productName) |
|
106
|
|
|
); |
|
107
|
|
|
$product = $products[0]; |
|
108
|
|
|
|
|
109
|
|
|
$option1 = StringInflector::nameToUppercaseCode($option1); |
|
110
|
|
|
$option2 = StringInflector::nameToUppercaseCode($option2); |
|
111
|
|
|
foreach ($product->getVariants() as $variant) { |
|
112
|
|
|
$options = []; |
|
113
|
|
|
foreach ($variant->getOptionValues() as $optionValue) { |
|
114
|
|
|
$options[$optionValue->getOption()->getCode()] = $optionValue->getValue(); |
|
115
|
|
|
} |
|
116
|
|
|
if (array_key_exists($option1, $options) && $options[$option1] === $value1 && |
|
117
|
|
|
array_key_exists($option2, $options) && $options[$option2] === $value2) { |
|
118
|
|
|
return $variant; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
throw new \InvalidArgumentException( |
|
123
|
|
|
sprintf( |
|
124
|
|
|
'Cannot find variant "%s" %s / "%s" %s within product "%s"', |
|
125
|
|
|
$value1, |
|
126
|
|
|
$option1, |
|
127
|
|
|
$value2, |
|
128
|
|
|
$option2, |
|
129
|
|
|
$product->getCode() |
|
130
|
|
|
) |
|
131
|
|
|
); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.