|
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\Setup; |
|
13
|
|
|
|
|
14
|
|
|
use Behat\Behat\Context\Context; |
|
15
|
|
|
use Sylius\Behat\Service\SharedStorageInterface; |
|
16
|
|
|
use Sylius\Component\Association\Model\AssociationTypeInterface; |
|
17
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
|
18
|
|
|
use Sylius\Component\Product\Model\ProductAssociationInterface; |
|
19
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
|
20
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @author Grzegorz Sadowski <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
final class ProductAssociationContext implements Context |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var SharedStorageInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
private $sharedStorage; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var FactoryInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
private $productAssociationTypeFactory; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var FactoryInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
private $productAssociationFactory; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var RepositoryInterface |
|
44
|
|
|
*/ |
|
45
|
|
|
private $productAssociationTypeRepository; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var RepositoryInterface |
|
49
|
|
|
*/ |
|
50
|
|
|
private $productAssociationRepository; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param SharedStorageInterface $sharedStorage |
|
54
|
|
|
* @param FactoryInterface $productAssociationTypeFactory |
|
55
|
|
|
* @param FactoryInterface $productAssociationFactory |
|
56
|
|
|
* @param RepositoryInterface $productAssociationTypeRepository |
|
57
|
|
|
* @param RepositoryInterface $productAssociationRepository |
|
58
|
|
|
*/ |
|
59
|
|
|
public function __construct( |
|
60
|
|
|
SharedStorageInterface $sharedStorage, |
|
61
|
|
|
FactoryInterface $productAssociationTypeFactory, |
|
|
|
|
|
|
62
|
|
|
FactoryInterface $productAssociationFactory, |
|
|
|
|
|
|
63
|
|
|
RepositoryInterface $productAssociationTypeRepository, |
|
|
|
|
|
|
64
|
|
|
RepositoryInterface $productAssociationRepository |
|
|
|
|
|
|
65
|
|
|
) { |
|
66
|
|
|
$this->sharedStorage = $sharedStorage; |
|
67
|
|
|
$this->productAssociationTypeFactory = $productAssociationTypeFactory; |
|
68
|
|
|
$this->productAssociationFactory = $productAssociationFactory; |
|
69
|
|
|
$this->productAssociationTypeRepository = $productAssociationTypeRepository; |
|
70
|
|
|
$this->productAssociationRepository = $productAssociationRepository; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @Given the store has (also) a product association type :name |
|
75
|
|
|
* @Given the store has (also) a product association type :name with a code :code |
|
76
|
|
|
*/ |
|
77
|
|
|
public function theStoreHasAProductAssociationType($name, $code = null) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->createProductAssociationType($name, $code); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @Given /^the (product "[^"]+") has(?:| also) an (association "[^"]+") with (products "[^"]+" and "[^"]+")$/ |
|
84
|
|
|
*/ |
|
85
|
|
|
public function theProductHasAnAssociationWithProducts( |
|
86
|
|
|
ProductInterface $product, |
|
87
|
|
|
AssociationTypeInterface $productAssociationType, |
|
|
|
|
|
|
88
|
|
|
array $products |
|
89
|
|
|
) { |
|
90
|
|
|
$this->createProductAssociation($product, $productAssociationType, $products); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param string $name |
|
95
|
|
|
* @param string|null $code |
|
96
|
|
|
*/ |
|
97
|
|
|
private function createProductAssociationType($name, $code = null) |
|
98
|
|
|
{ |
|
99
|
|
|
if (null === $code) { |
|
100
|
|
|
$code = $this->generateCodeFromName($name); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** @var AssociationTypeInterface $productAssociationType */ |
|
104
|
|
|
$productAssociationType = $this->productAssociationTypeFactory->createNew(); |
|
|
|
|
|
|
105
|
|
|
$productAssociationType->setCode($code); |
|
106
|
|
|
$productAssociationType->setName($name); |
|
107
|
|
|
|
|
108
|
|
|
$this->productAssociationTypeRepository->add($productAssociationType); |
|
109
|
|
|
$this->sharedStorage->set('product_association_type', $productAssociationType); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param ProductInterface $product |
|
114
|
|
|
* @param AssociationTypeInterface $productAssociationType |
|
115
|
|
|
* @param array $associatedProducts |
|
116
|
|
|
*/ |
|
117
|
|
|
private function createProductAssociation( |
|
118
|
|
|
ProductInterface $product, |
|
119
|
|
|
AssociationTypeInterface $productAssociationType, |
|
|
|
|
|
|
120
|
|
|
array $associatedProducts |
|
121
|
|
|
) { |
|
122
|
|
|
/** @var ProductAssociationInterface $productAssociation */ |
|
123
|
|
|
$productAssociation = $this->productAssociationFactory->createNew(); |
|
124
|
|
|
$productAssociation->setType($productAssociationType); |
|
125
|
|
|
|
|
126
|
|
|
foreach ($associatedProducts as $associatedProduct) { |
|
127
|
|
|
$productAssociation->addAssociatedObject($associatedProduct); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
$product->addAssociation($productAssociation); |
|
131
|
|
|
|
|
132
|
|
|
$this->productAssociationRepository->add($productAssociation); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param string $name |
|
137
|
|
|
* |
|
138
|
|
|
* @return string |
|
139
|
|
|
*/ |
|
140
|
|
|
private function generateCodeFromName($name) |
|
141
|
|
|
{ |
|
142
|
|
|
return str_replace([' ', '-'], '_', strtolower($name)); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.