Completed
Push — master ( 3e7fd7...789110 )
by Paweł
162:07 queued 147:09
created

CartContext::myCartShippingFeeShouldBe()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
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\Ui\Shop;
13
14
use Behat\Behat\Context\Context;
15
use Behat\Mink\Exception\ElementNotFoundException;
16
use Sylius\Behat\NotificationType;
17
use Sylius\Behat\Page\Shop\Cart\SummaryPageInterface;
18
use Sylius\Behat\Page\Shop\Product\ShowPageInterface;
19
use Sylius\Behat\Service\NotificationCheckerInterface;
20
use Sylius\Component\Core\Test\Services\SharedStorageInterface;
21
use Sylius\Component\Product\Model\ProductInterface;
22
use Webmozart\Assert\Assert;
23
24
/**
25
 * @author Mateusz Zalewski <[email protected]>
26
 * @author Anna Walasek <[email protected]>
27
 */
28
final class CartContext implements Context
29
{
30
    /**
31
     * @var SharedStorageInterface
32
     */
33
    private $sharedStorage;
34
35
    /**
36
     * @var SummaryPageInterface
37
     */
38
    private $summaryPage;
39
40
    /**
41
     * @var ShowPageInterface
42
     */
43
    private $productShowPage;
44
45
    /**
46
     * @var NotificationCheckerInterface
47
     */
48
    private $notificationChecker;
49
50
    /**
51
     * @param SharedStorageInterface $sharedStorage
52
     * @param SummaryPageInterface $summaryPage
53
     * @param ShowPageInterface $productShowPage
54
     * @param NotificationCheckerInterface $notificationChecker
55
     */
56
    public function __construct(
57
        SharedStorageInterface $sharedStorage,
58
        SummaryPageInterface $summaryPage,
59
        ShowPageInterface $productShowPage,
60
        NotificationCheckerInterface $notificationChecker
61
    ) {
62
        $this->sharedStorage = $sharedStorage;
63
        $this->summaryPage = $summaryPage;
64
        $this->productShowPage = $productShowPage;
65
        $this->notificationChecker = $notificationChecker;
66
    }
67
68
    /**
69
     * @When I see the summary of my cart
70
     */
71
    public function iOpenCartSummaryPage()
72
    {
73
        $this->summaryPage->open();
74
    }
75
76
    /**
77
     * @Then my cart should be empty
78
     * @Then cart should be empty with no value
79
     */
80
    public function iShouldBeNotifiedThatMyCartIsEmpty()
81
    {
82
        $this->summaryPage->open();
83
84
        Assert::true(
85
             $this->summaryPage->isEmpty(),
86
            'There should appear information about empty cart, but it does not.'
87
        );
88
    }
89
90
    /**
91
     * @Given /^I (?:remove|removed) product "([^"]+)" from the cart$/
92
     */
93
    public function iRemoveProductFromTheCart($productName)
94
    {
95
        $this->summaryPage->open();
96
        $this->summaryPage->removeProduct($productName);
97
    }
98
99
    /**
100
     * @Given I change :productName quantity to :quantity
101
     */
102
    public function iChangeQuantityTo($productName, $quantity)
103
    {
104
        $this->summaryPage->open();
105
        $this->summaryPage->changeQuantity($productName, $quantity);
106
    }
107
108
    /**
109
     * @Then grand total value should be :total
110
     * @Then my cart total should be :total
111
     */
112
    public function myCartTotalShouldBe($total)
113
    {
114
        $this->summaryPage->open();
115
        Assert::same(
116
            $this->summaryPage->getGrandTotal(),
117
            $total,
118
            'Grand total should be %2$s, but it is %s.'
119
        );
120
    }
121
122
    /**
123
     * @Then tax total value should be :taxTotal
124
     * @Then my cart taxes should be :taxTotal
125
     */
126
    public function myCartTaxesShouldBe($taxTotal)
127
    {
128
        $this->summaryPage->open();
129
130
        Assert::same(
131
            $this->summaryPage->getTaxTotal(),
132
            $taxTotal,
133
            'Tax total value should be %2$s, but it is %s.'
134
        );
135
    }
136
137
    /**
138
     * @Then shipping total value should be :shippingTotal
139
     * @Then my cart shipping fee should be :shippingTotal
140
     */
141
    public function myCartShippingFeeShouldBe($shippingTotal)
142
    {
143
        $this->summaryPage->open();
144
145
        Assert::same(
146
            $this->summaryPage->getShippingTotal(),
147
            $shippingTotal,
148
            'Shipping total value should be %2$s, but it is %s.'
149
        );
150
    }
151
152
    /**
153
     * @Then my cart promotions should be :promotionsTotal
154
     * @Then my discount should be :promotionsTotal
155
     */
156
    public function myDiscountShouldBe($promotionsTotal)
157
    {
158
        $this->summaryPage->open();
159
160
        Assert::same(
161
            $this->summaryPage->getPromotionTotal(),
162
            $promotionsTotal,
163
            'Promotion total value should be %2$s, but it is %s.'
164
        );
165
    }
166
167
    /**
168
     * @Given /^there should be no shipping fee$/
169
     */
170
    public function thereShouldBeNoShippingFee()
171
    {
172
        $this->summaryPage->open();
173
174
        expect($this->summaryPage)->toThrow(ElementNotFoundException::class)->during('getShippingTotal', []);
175
    }
176
177
    /**
178
     * @Given /^there should be no discount$/
179
     */
180
    public function thereShouldBeNoDiscount()
181
    {
182
        $this->summaryPage->open();
183
184
        expect($this->summaryPage)->toThrow(ElementNotFoundException::class)->during('getPromotionTotal', []);
185
    }
186
187
    /**
188
     * @Then /^(its|theirs) price should be decreased by ("[^"]+")$/
189
     * @Then /^(product "[^"]+") price should be decreased by ("[^"]+")$/
190
     */
191
    public function itsPriceShouldBeDecreasedBy(ProductInterface $product, $amount)
192
    {
193
        $this->summaryPage->open();
194
195
        $discountPrice = $this->summaryPage->getItemDiscountPrice($product->getName());
196
        $regularPrice = $this->summaryPage->getItemRegularPrice($product->getName());
197
198
        Assert::same(
199
            $discountPrice,
200
            ($regularPrice - $amount),
201
            'Price after discount should be %2$s, but it is %s.'
202
        );
203
    }
204
205
    /**
206
     * @Given /^(product "[^"]+") price should not be decreased$/
207
     */
208
    public function productPriceShouldNotBeDecreased(ProductInterface $product)
209
    {
210
        $this->summaryPage->open();
211
212
        Assert::false(
213
            $this->summaryPage->isItemDiscounted($product->getName()),
214
            'The price should not be decreased, but it is.'
215
        );
216
    }
217
218
219
    /**
220
     * @Given /^I add (this product) to the cart$/
221
     * @Given I added product :product to the cart
222
     * @Given /^I (?:have|had) (product "([^"]+)") in the cart$/
223
     * @When I add product :product to the cart
224
     */
225
    public function iAddProductToTheCart(ProductInterface $product)
226
    {
227
        $this->productShowPage->open(['slug' => $product->getSlug()]);
228
        $this->productShowPage->addToCart();
229
230
        $this->sharedStorage->set('product', $product);
231
    }
232
233
    /**
234
     * @Given /^I added (products "([^"]+)" and "([^"]+)") to the cart$/
235
     * @When /^I add (products "([^"]+)" and "([^"]+)") to the cart$/
236
     * @Given /^I added (products "([^"]+)", "([^"]+)" and "([^"]+)") to the cart$/
237
     * @When /^I add (products "([^"]+)", "([^"]+)" and "([^"]+)") to the cart$/
238
     */
239
    public function iAddMultipleProductsToTheCart(array $products)
240
    {
241
        foreach ($products as $product) {
242
            $this->iAddProductToTheCart($product);
243
        }
244
    }
245
246
    /**
247
     * @Given I added :variant variant of product :product to the cart
248
     * @When I add :variant variant of product :product to the cart
249
     * @When I have :variant variant of product :product in the cart
250
     * @When /^I add "([^"]+)" variant of (this product) to the cart$/
251
     */
252
    public function iAddProductToTheCartSelectingVariant($variant, ProductInterface $product)
253
    {
254
        $this->productShowPage->open(['slug' => $product->getSlug()]);
255
        $this->productShowPage->addToCartWithVariant($variant);
256
257
        $this->sharedStorage->set('product', $product);
258
    }
259
260
    /**
261
     * @Given I have :quantity products :product in the cart
262
     * @When I add :quantity products :product to the cart
263
     */
264
    public function iAddProductsToTheCart(ProductInterface $product, $quantity)
265
    {
266
        $this->productShowPage->open(['slug' => $product->getSlug()]);
267
        $this->productShowPage->addToCartWithQuantity($quantity);
268
269
        $this->sharedStorage->set('product', $product);
270
    }
271
272
    /**
273
     * @Then I should be on my cart summary page
274
     */
275
    public function shouldBeOnMyCartSummaryPage()
276
    {
277
        Assert::true(
278
            $this->summaryPage->isOpen(),
279
            'Cart summary page should be open, but it does not.'
280
        );
281
    }
282
283
    /**
284
     * @Then I should be notified that the product has been successfully added
285
     */
286
    public function iShouldBeNotifiedThatItHasBeenSuccessfullyAdded()
287
    {
288
        $this->notificationChecker->checkNotification('Item has been added to cart.', NotificationType::success());
289
    }
290
291
    /**
292
     * @Then there should be one item in my cart
293
     */
294
    public function thereShouldBeOneItemInMyCart()
295
    {
296
        Assert::true(
297
            $this->summaryPage->isSingleItemOnPage(),
298
            'There should be only one item on list, but it does not.'
299
        );
300
    }
301
302
    /**
303
     * @Then this item should have name :itemName
304
     */
305
    public function thisProductShouldHaveName($itemName)
306
    {
307
        Assert::true(
308
            $this->summaryPage->isItemWithName($itemName),
309
            sprintf('The product with name %s should appear on the list, but it does not.', $itemName)
310
        );
311
    }
312
313
    /**
314
     * @Then this item should have variant :variantName
315
     */
316
    public function thisItemShouldHaveVariant($variantName)
317
    {
318
        Assert::true(
319
            $this->summaryPage->isItemWithVariant($variantName),
320
            sprintf('The product with variant %s should appear on the list, but it does not.', $variantName)
321
        );
322
    }
323
324
    /**
325
     * @When /^I add (this product) with ([^"]+) "([^"]+)" to the cart$/
326
     */
327
    public function iAddThisProductWithSizeToTheCart(ProductInterface $product, $optionName, $optionValue)
328
    {
329
        $this->productShowPage->open(['slug' => $product->getSlug()]);
330
331
        $option = $this->sharedStorage->get(sprintf('%s_option', $optionName));
332
333
        $this->productShowPage->addToCartWithOption($option, $optionValue);
334
    }
335
336
    /**
337
     * @Given /^(this product) should have ([^"]+) "([^"]+)"$/
338
     */
339
    public function thisItemShouldHaveSize(ProductInterface $product, $optionName, $optionValue)
340
    {
341
        Assert::contains(
342
            $this->summaryPage->getProductOption($product->getName(), $optionName),
343
            $optionValue,
344
            'The product should have option with value %2$s, but it has option with value %s.'
345
        );
346
    }
347
}
348