Completed
Push — master ( 2ec63c...d54afb )
by Kamil
21:51
created

CartContext   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 338
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 7

Importance

Changes 6
Bugs 1 Features 4
Metric Value
wmc 27
lcom 3
cbo 7
dl 0
loc 338
rs 10
c 6
b 1
f 4

26 Methods

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