Completed
Push — master ( 7fc704...cfa808 )
by Paweł
33:04 queued 17:47
created

CartContext::thisUserHasProductInTheCart()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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