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\Bundle\PromotionBundle\Behat; |
13
|
|
|
|
14
|
|
|
use Behat\Gherkin\Node\TableNode; |
15
|
|
|
use Behat\Mink\Element\NodeElement; |
16
|
|
|
use Sylius\Bundle\ResourceBundle\Behat\DefaultContext; |
17
|
|
|
|
18
|
|
|
class PromotionContext extends DefaultContext |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @Given /^promotion "([^""]*)" has following coupons defined:$/ |
22
|
|
|
* @Given /^promotion "([^""]*)" has following coupons:$/ |
23
|
|
|
*/ |
24
|
|
|
public function theFollowingPromotionCouponsAreDefined($name, TableNode $table) |
25
|
|
|
{ |
26
|
|
|
$promotion = $this->findOneByName('promotion', $name); |
27
|
|
|
|
28
|
|
|
$manager = $this->getEntityManager(); |
29
|
|
|
$factory = $this->getFactory('promotion_coupon'); |
30
|
|
|
|
31
|
|
|
foreach ($table->getHash() as $data) { |
32
|
|
|
$coupon = $factory->createNew(); |
33
|
|
|
$coupon->setCode($data['code']); |
34
|
|
|
$coupon->setUsageLimit(isset($data['usage limit']) ? $data['usage limit'] : 0); |
35
|
|
|
$coupon->setUsed(isset($data['used']) ? $data['used'] : 0); |
36
|
|
|
|
37
|
|
|
$promotion->addCoupon($coupon); |
38
|
|
|
|
39
|
|
|
$manager->persist($coupon); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$promotion->setCouponBased(true); |
43
|
|
|
|
44
|
|
|
$manager->flush(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @Given /^promotion "([^""]*)" has following rules defined:$/ |
49
|
|
|
*/ |
50
|
|
|
public function theFollowingPromotionRulesAreDefined($name, TableNode $table) |
51
|
|
|
{ |
52
|
|
|
$promotion = $this->findOneByName('promotion', $name); |
53
|
|
|
|
54
|
|
|
$manager = $this->getEntityManager(); |
55
|
|
|
$factory = $this->getFactory('promotion_rule'); |
56
|
|
|
|
57
|
|
|
foreach ($table->getHash() as $data) { |
58
|
|
|
$configuration = $this->cleanPromotionConfiguration($this->getConfiguration($data['configuration'])); |
59
|
|
|
|
60
|
|
|
$rule = $factory->createNew(); |
61
|
|
|
$rule->setType(strtolower(str_replace(' ', '_', $data['type']))); |
62
|
|
|
$rule->setConfiguration($configuration); |
63
|
|
|
|
64
|
|
|
$promotion->addRule($rule); |
65
|
|
|
|
66
|
|
|
$manager->persist($rule); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$manager->flush(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @Given /^promotion "([^""]*)" has following actions defined:$/ |
74
|
|
|
*/ |
75
|
|
|
public function theFollowingPromotionActionsAreDefined($name, TableNode $table) |
76
|
|
|
{ |
77
|
|
|
$promotion = $this->findOneByName('promotion', $name); |
78
|
|
|
|
79
|
|
|
$manager = $this->getEntityManager(); |
80
|
|
|
$factory = $this->getFactory('promotion_action'); |
81
|
|
|
|
82
|
|
|
foreach ($table->getHash() as $data) { |
83
|
|
|
$configuration = $this->cleanPromotionConfiguration($this->getConfiguration($data['configuration'])); |
84
|
|
|
|
85
|
|
|
$action = $factory->createNew(); |
86
|
|
|
$action->setType(strtolower(str_replace(' ', '_', $data['type']))); |
87
|
|
|
$action->setConfiguration($configuration); |
88
|
|
|
|
89
|
|
|
$promotion->addAction($action); |
90
|
|
|
|
91
|
|
|
$manager->persist($action); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$manager->flush(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @Given /^the following promotions exist:$/ |
99
|
|
|
* @Given /^there are following promotions configured:$/ |
100
|
|
|
*/ |
101
|
|
|
public function theFollowingPromotionsExist(TableNode $table) |
102
|
|
|
{ |
103
|
|
|
$manager = $this->getEntityManager(); |
104
|
|
|
$factory = $this->getFactory('promotion'); |
105
|
|
|
|
106
|
|
|
foreach ($table->getHash() as $data) { |
107
|
|
|
$promotion = $factory->createNew(); |
108
|
|
|
|
109
|
|
|
$promotion->setName($data['name']); |
110
|
|
|
$promotion->setDescription($data['description']); |
111
|
|
|
|
112
|
|
|
if (array_key_exists('usage limit', $data) && '' !== $data['usage limit']) { |
113
|
|
|
$promotion->setUsageLimit((int) $data['usage limit']); |
114
|
|
|
} |
115
|
|
|
if (array_key_exists('used', $data) && '' !== $data['used']) { |
116
|
|
|
$promotion->setUsed((int) $data['used']); |
117
|
|
|
} |
118
|
|
|
if (array_key_exists('starts', $data)) { |
119
|
|
|
$promotion->setStartsAt(new \DateTime($data['starts'])); |
120
|
|
|
} |
121
|
|
|
if (array_key_exists('ends', $data)) { |
122
|
|
|
$promotion->setEndsAt(new \DateTime($data['ends'])); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$manager->persist($promotion); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$manager->flush(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @Then I should see product :productName in the cart summary |
133
|
|
|
*/ |
134
|
|
|
public function iShouldSeeProductInCartSummary($productName) |
135
|
|
|
{ |
136
|
|
|
$cartSummary = $this->getCartSummaryPageElement(); |
137
|
|
|
if (false === strpos($cartSummary->getText(), $productName)) { |
138
|
|
|
throw new \InvalidArgumentException(sprintf('Product "%s" was not found in cart summary', $productName)); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @Then I should not see product :productName in the cart summary |
144
|
|
|
*/ |
145
|
|
|
public function iShouldNotSeeProductInCartSummary($productName) |
146
|
|
|
{ |
147
|
|
|
$cartSummary = $this->getCartSummaryPageElement(); |
148
|
|
|
if (false !== strpos($cartSummary->getText(), $productName)) { |
149
|
|
|
throw new \InvalidArgumentException(sprintf('Product "%s" was found in cart summary', $productName)); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return NodeElement |
155
|
|
|
* |
156
|
|
|
* @throws \Exception |
157
|
|
|
*/ |
158
|
|
|
private function getCartSummaryPageElement() |
159
|
|
|
{ |
160
|
|
|
$element = $this->getSession()->getPage()->find('css', 'div:contains("Cart summary") > form > table'); |
161
|
|
|
if (null === $element) { |
162
|
|
|
throw new \Exception("Cart summary element cannot be found!"); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $element; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Cleaning promotion configuration that is serialized in database. |
170
|
|
|
* |
171
|
|
|
* @param array $configuration |
172
|
|
|
* |
173
|
|
|
* @return array |
174
|
|
|
*/ |
175
|
|
|
private function cleanPromotionConfiguration(array $configuration) |
176
|
|
|
{ |
177
|
|
|
foreach ($configuration as $key => $value) { |
178
|
|
|
switch ($key) { |
179
|
|
|
case 'amount': |
180
|
|
|
case 'price': |
181
|
|
|
$configuration[$key] = (int) $value * 100; |
182
|
|
|
break; |
183
|
|
|
case 'count': |
184
|
|
|
$configuration[$key] = (int) $value; |
185
|
|
|
break; |
186
|
|
|
case 'percentage': |
187
|
|
|
$configuration[$key] = (int) $value / 100; |
188
|
|
|
break; |
189
|
|
|
case 'equal': |
190
|
|
|
$configuration[$key] = (Boolean) $value; |
191
|
|
|
break; |
192
|
|
|
default: |
193
|
|
|
break; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return $configuration; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|