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 Behat\Gherkin\Node\TableNode; |
16
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
17
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
18
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
19
|
|
|
use Sylius\Component\Core\Model\ProductVariantInterface; |
20
|
|
|
use Sylius\Component\Core\Test\Services\SharedStorageInterface; |
21
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
22
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
23
|
|
|
use Sylius\Component\Taxation\Model\TaxCategoryInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @author Arkadiusz Krakowiak <[email protected]> |
27
|
|
|
* @author Mateusz Zalewski <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
final class ProductContext implements Context |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var SharedStorageInterface |
33
|
|
|
*/ |
34
|
|
|
private $sharedStorage; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var RepositoryInterface |
38
|
|
|
*/ |
39
|
|
|
private $productRepository; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var FactoryInterface |
43
|
|
|
*/ |
44
|
|
|
private $productFactory; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var FactoryInterface |
48
|
|
|
*/ |
49
|
|
|
private $productVariantFactory; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var ObjectManager |
53
|
|
|
*/ |
54
|
|
|
private $objectManager; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param SharedStorageInterface $sharedStorage |
58
|
|
|
* @param RepositoryInterface $productRepository |
59
|
|
|
* @param FactoryInterface $productFactory |
60
|
|
|
* @param FactoryInterface $productVariantFactory |
61
|
|
|
* @param ObjectManager $objectManager |
62
|
|
|
*/ |
63
|
|
|
public function __construct( |
64
|
|
|
SharedStorageInterface $sharedStorage, |
65
|
|
|
RepositoryInterface $productRepository, |
66
|
|
|
FactoryInterface $productFactory, |
67
|
|
|
FactoryInterface $productVariantFactory, |
68
|
|
|
ObjectManager $objectManager |
69
|
|
|
) { |
70
|
|
|
$this->sharedStorage = $sharedStorage; |
71
|
|
|
$this->productRepository = $productRepository; |
72
|
|
|
$this->productFactory = $productFactory; |
73
|
|
|
$this->productVariantFactory = $productVariantFactory; |
74
|
|
|
$this->objectManager = $objectManager; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @Given /^the store has a product "([^"]+)"$/ |
79
|
|
|
* @Given /^the store has a product "([^"]+)" priced at ("[^"]+")$/ |
80
|
|
|
*/ |
81
|
|
|
public function storeHasAProductPricedAt($productName, $price = 0) |
82
|
|
|
{ |
83
|
|
|
$product = $this->productFactory->createNew(); |
84
|
|
|
$product->setName($productName); |
85
|
|
|
$product->setPrice($price); |
86
|
|
|
$product->setDescription('Awesome '.$productName); |
87
|
|
|
|
88
|
|
|
$channel = $this->sharedStorage->get('channel'); |
89
|
|
|
$product->addChannel($channel); |
90
|
|
|
|
91
|
|
|
$this->productRepository->add($product); |
92
|
|
|
|
93
|
|
|
$this->sharedStorage->set('product', $product); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @Given /^there is product "([^"]+)" available in ((?:this|that|"[^"]+") channel)$/ |
98
|
|
|
*/ |
99
|
|
|
public function thereIsProductAvailableInGivenChannel($productName, ChannelInterface $channel) |
100
|
|
|
{ |
101
|
|
|
/** @var ProductInterface $product */ |
102
|
|
|
$product = $this->productFactory->createNew(); |
103
|
|
|
$product->setName($productName); |
104
|
|
|
$product->setPrice(0); |
105
|
|
|
$product->setDescription('Awesome ' . $productName); |
106
|
|
|
$product->addChannel($channel); |
107
|
|
|
|
108
|
|
|
$this->productRepository->add($product); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @Given /^([^"]+) belongs to ("[^"]+" tax category)$/ |
113
|
|
|
*/ |
114
|
|
|
public function productBelongsToTaxCategory(ProductInterface $product, TaxCategoryInterface $taxCategory) |
115
|
|
|
{ |
116
|
|
|
$product->getMasterVariant()->setTaxCategory($taxCategory); |
117
|
|
|
$this->objectManager->flush(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @Given /^(it) comes in the following variations:$/ |
122
|
|
|
*/ |
123
|
|
|
public function itComesInTheFollowingVariations(ProductInterface $product, TableNode $table) |
124
|
|
|
{ |
125
|
|
|
foreach ($table->getHash() as $variantHash) { |
126
|
|
|
$variant = $this->productVariantFactory->createNew(); |
127
|
|
|
$variant->setPresentation($variantHash['name']); |
128
|
|
|
$variant->setPrice($this->getPriceFromString(str_replace(['$', '€', '£'], '', $variantHash['price']))); |
129
|
|
|
$variant->setProduct($product); |
130
|
|
|
|
131
|
|
|
$this->productRepository->add($variant); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$this->objectManager->flush(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @Given /^("[^"]+" variant of product "[^"]+") belongs to ("[^"]+" tax category)$/ |
139
|
|
|
*/ |
140
|
|
|
public function productVariantBelongsToTaxCategory(ProductVariantInterface $productVariant, TaxCategoryInterface $taxCategory) |
141
|
|
|
{ |
142
|
|
|
$productVariant->setTaxCategory($taxCategory); |
143
|
|
|
$this->objectManager->flush($productVariant); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string $price |
148
|
|
|
* |
149
|
|
|
* @return int |
150
|
|
|
*/ |
151
|
|
|
private function getPriceFromString($price) |
152
|
|
|
{ |
153
|
|
|
return (int) round(($price * 100), 2); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|