Completed
Push — master ( 6d05bc...71ecc8 )
by Kamil
25:05
created

iShouldSeeProductReviewsInTheList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
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 Sylius\Behat\Page\Shop\Product\ShowPageInterface;
16
use Sylius\Behat\Page\Shop\ProductReview\IndexPageInterface;
17
use Sylius\Behat\Page\Shop\Taxon\ShowPageInterface as TaxonShowPageInterface;
18
use Sylius\Behat\Page\SymfonyPageInterface;
19
use Sylius\Component\Core\Model\ProductInterface;
20
use Sylius\Component\Core\Model\TaxonInterface;
21
use Webmozart\Assert\Assert;
22
23
/**
24
 * @author Kamil Kokot <[email protected]>
25
 * @author Magdalena Banasiak <[email protected]>
26
 * @author Anna Walasek <[email protected]>
27
 */
28
final class ProductContext implements Context
29
{
30
    /**
31
     * @var ShowPageInterface
32
     */
33
    private $showPage;
34
35
    /**
36
     * @var TaxonShowPageInterface
37
     */
38
    private $taxonShowPage;
39
40
    /**
41
     * @var IndexPageInterface
42
     */
43
    private $productReviewsIndexPage;
44
45
    /**
46
     * @param ShowPageInterface $showPage
47
     * @param TaxonShowPageInterface $taxonShowPage
48
     * @param IndexPageInterface $productReviewsIndexPage
49
     */
50
    public function __construct(
51
        ShowPageInterface $showPage,
52
        TaxonShowPageInterface $taxonShowPage,
53
        IndexPageInterface $productReviewsIndexPage
54
    ) {
55
        $this->showPage = $showPage;
56
        $this->taxonShowPage = $taxonShowPage;
57
        $this->productReviewsIndexPage = $productReviewsIndexPage;
58
    }
59
60
    /**
61
     * @Then I should be able to access product :product
62
     */
63
    public function iShouldBeAbleToAccessProduct(ProductInterface $product)
64
    {
65
        $this->showPage->tryToOpen(['slug' => $product->getSlug()]);
66
67
        Assert::true(
68
            $this->showPage->isOpen(['slug' => $product->getSlug()]),
69
            'Product show page should be open, but it does not.'
70
        );
71
    }
72
73
    /**
74
     * @Then I should not be able to access product :product
75
     */
76
    public function iShouldNotBeAbleToAccessProduct(ProductInterface $product)
77
    {
78
        $this->showPage->tryToOpen(['slug' => $product->getSlug()]);
79
80
        Assert::false(
81
            $this->showPage->isOpen(['slug' => $product->getSlug()]),
82
            'Product show page should not be open, but it does.'
83
        );
84
    }
85
86
    /**
87
     * @When /^I check (this product)'s details/
88
     * @When I view product :product
89
     */
90
    public function iOpenProductPage(ProductInterface $product)
91
    {
92
        $this->showPage->open(['slug' => $product->getSlug()]);
93
    }
94
95
    /**
96
     * @Given I should see the product name :name
97
     */
98
    public function iShouldSeeProductName($name)
99
    {
100
        Assert::same(
101
            $name,
102
            $this->showPage->getName(),
103
            'Product should have name %2$s, but it has %s'
104
        );
105
    }
106
107
    /**
108
     * @When I open page :url
109
     */
110
    public function iOpenPage($url)
111
    {
112
        $this->showPage->visit($url);
113
    }
114
115
    /**
116
     * @Then I should be on :product product detailed page
117
     * @Then I should still be on product :product page
118
     */
119
    public function iShouldBeOnProductDetailedPage(ProductInterface $product)
120
    {
121
        Assert::true(
122
            $this->showPage->isOpen(['slug' => $product->getSlug()]),
123
            sprintf('Product %s show page should be open, but it does not.', $product->getName())
124
        );
125
    }
126
127
    /**
128
     * @Then I should see the product attribute :attributeName with value :AttributeValue
129
     */
130
    public function iShouldSeeTheProductAttributeWithValue($attributeName, $AttributeValue)
0 ignored issues
show
Coding Style introduced by
$AttributeValue does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
131
    {
132
        Assert::true(
133
            $this->showPage->hasAttributeWithValue($attributeName, $AttributeValue),
0 ignored issues
show
Coding Style introduced by
$AttributeValue does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
134
            sprintf('Product should have attribute %s with value %s, but it does not.', $attributeName, $AttributeValue)
0 ignored issues
show
Coding Style introduced by
$AttributeValue does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
135
        );
136
    }
137
138
    /**
139
     * @When /^I browse products from (taxon "([^"]+)")$/
140
     */
141
    public function iCheckListOfProductsForTaxon(TaxonInterface $taxon)
142
    {
143
        $this->taxonShowPage->open(['permalink' => $taxon->getPermalink()]);
144
    }
145
146
    /**
147
     * @Then I should see the product :productName
148
     */
149
    public function iShouldSeeProduct($productName)
150
    {
151
        Assert::true(
152
            $this->taxonShowPage->isProductOnList($productName),
153
            sprintf("The product %s should appear on page, but it does not.", $productName)
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal The product %s should ap... page, but it does not. does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
154
        );
155
    }
156
157
    /**
158
     * @Then I should not see the product :productName
159
     */
160
    public function iShouldNotSeeProduct($productName)
161
    {
162
        Assert::false(
163
            $this->taxonShowPage->isProductOnList($productName),
164
            sprintf("The product %s should not appear on page, but it does.", $productName)
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal The product %s should no...r on page, but it does. does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
165
        );
166
    }
167
168
    /**
169
     * @Then I should see empty list of products
170
     */
171
    public function iShouldSeeEmptyListOfProducts()
172
    {
173
        Assert::true(
174
            $this->taxonShowPage->isEmpty(),
175
            'There should appear information about empty list of products, but it does not.'
176
        );
177
    }
178
179
    /**
180
     * @Then I should see that it is out of stock
181
     */
182
    public function iShouldSeeItIsOutOfStock()
183
    {
184
        Assert::true(
185
            $this->showPage->isOutOfStock(),
186
            'Out of stock label should be visible.'
187
        );
188
    }
189
190
    /**
191
     * @Then I should be unable to add it to the cart
192
     */
193
    public function iShouldBeUnableToAddItToTheCart()
194
    {
195
        Assert::false(
196
            $this->showPage->hasAddToCartButton(),
197
            'Add to cart button should not be visible.'
198
        );
199
    }
200
201
    /**
202
     * @Then the product price should be :price
203
     * @Then I should see the product price :price
204
     */
205
    public function iShouldSeeTheProductPrice($price)
206
    {
207
        Assert::same(
208
            $price,
209
            $this->showPage->getPrice(),
210
            'Product should have price %2$s, but it has %s'
211
        );
212
    }
213
214
    /**
215
     * @When I set its :optionName to :optionValue
216
     */
217
    public function iSetItsOptionTo($optionName, $optionValue)
218
    {
219
        $this->showPage->selectOption($optionName, $optionValue);
220
    }
221
222
    /**
223
     * @When I select :variantName variant
224
     */
225
    public function iSelectVariant($variantName)
226
    {
227
        $this->showPage->selectVariant($variantName);
228
    }
229
230
    /**
231
     * @Then I should see the product :productName with price :productPrice
232
     */
233
    public function iShouldSeeTheProductWithPrice($productName, $productPrice)
234
    {
235
        Assert::true(
236
            $this->taxonShowPage->isProductWithPriceOnList($productName, $productPrice),
237
            sprintf("The product %s with price %s should appear on page, but it does not.", $productName, $productPrice)
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal The product %s with pric... page, but it does not. does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
238
        );
239
    }
240
241
    /**
242
     * @Then /^I should be notified that (this product) does not have sufficient stock$/
243
     */
244
    public function iShouldBeNotifiedThatThisProductDoesNotHaveSufficientStock(ProductInterface $product)
245
    {
246
       $this->showPage->waitForValidationErrors(3);
247
248
        Assert::true(
249
            $this->showPage->hasProductOutOfStockValidationMessage($product),
250
            sprintf('I should see validation message for %s product', $product->getName())
251
        );
252
    }
253
254
    /**
255
     * @Then /^I should not be notified that (this product) does not have sufficient stock$/
256
     */
257
    public function iShouldNotBeNotifiedThatThisProductDoesNotHaveSufficientStock(ProductInterface $product)
258
    {
259
        Assert::false(
260
            $this->showPage->hasProductOutOfStockValidationMessage($product),
261
            sprintf('I should see validation message for %s product', $product->getName())
262
        );
263
    }
264
265
    /**
266
     * @Then I should see a main image
267
     */
268
    public function iShouldSeeAMainImage()
269
    {
270
        Assert::true(
271
            $this->showPage->isMainImageDisplayed(),
272
            'The main image should have been displayed.'
273
        );
274
    }
275
276
    /**
277
     * @When /^I view (oldest|newest) products from (taxon "([^"]+)")$/
278
     */
279
    public function iViewSortedProductsFromTaxon($sortDirection, TaxonInterface $taxon)
280
    {
281
        $sorting = ['createdAt' => 'oldest' === $sortDirection ? 'asc' : 'desc'];
282
283
        $this->taxonShowPage->open(['permalink' => $taxon->getPermalink(), 'sorting' => $sorting]);
284
    }
285
286
    /**
287
     * @Then I should see :numberOfProducts products in the list
288
     */
289
    public function iShouldSeeProductsInTheList($numberOfProducts)
290
    {
291
        $foundRows = $this->taxonShowPage->countProductsItems();
292
293
        Assert::same(
294
            (int) $numberOfProducts,
295
            $foundRows,
296
            '%s rows with products should appear on page, %s rows has been found'
297
        );
298
    }
299
300
    /**
301
     * @Then I should see a product with name :name
302
     */
303
    public function iShouldSeeProductWithName($name)
304
    {
305
        Assert::true(
306
            $this->taxonShowPage->isProductOnPageWithName($name),
307
            sprintf('The product with name "%s" has not been found.', $name)
308
        );
309
    }
310
311
    /**
312
     * @Then the first product on the list should have name :name
313
     */
314
    public function theFirstProductOnTheListShouldHaveName($name)
315
    {
316
        $actualName = $this->taxonShowPage->getFirstProductNameFromList();
317
318
        Assert::same(
319
            $actualName,
320
            $name,
321
            sprintf('Expected first product\'s name to be "%s", but it is "%s".', $name, $actualName)
322
        );
323
    }
324
325
    /**
326
     * @Then I should see :count product reviews
327
     */
328
    public function iShouldSeeProductReviews($count)
329
    {
330
        Assert::same(
331
            (int) $count,
332
            $this->showPage->countReviews(),
333
            'Product has %2$s reviews, but should have %s.'
334
        );
335
    }
336
337
    /**
338
     * @Then I should see reviews titled :firstReview, :secondReview and :thirdReview
339
     */
340
    public function iShouldSeeReviewsTitled(...$reviews)
341
    {
342
        foreach ($reviews as $review) {
343
            Assert::true(
344
                $this->showPage->hasReviewTitled($review),
345
                sprintf('Product should have review titled "%s" but it does not.', $review)
346
            );
347
        }
348
    }
349
350
    /**
351
     * @Then I should not see review titled :title
352
     */
353
    public function iShouldNotSeeReviewTitled($title)
354
    {
355
        Assert::false(
356
            $this->showPage->hasReviewTitled($title),
357
            sprintf('Product should not have review titled "%s" but it does.', $title)
358
        );
359
    }
360
361
    /**
362
     * @When /^I check (this product)'s reviews$/
363
     */
364
    public function iCheckThisProductSReviews(ProductInterface $product)
365
    {
366
        $this->productReviewsIndexPage->open(['slug' => $product->getSlug()]);
367
    }
368
369
    /**
370
     * @Then I should see :count product reviews in the list
371
     */
372
    public function iShouldSeeProductReviewsInTheList($count)
373
    {
374
        Assert::same(
375
            (int) $count,
376
            $this->productReviewsIndexPage->countReviews(),
377
            'Product has %2$s reviews in the list, but should have %s.'
378
        );
379
    }
380
381
    /**
382
     * @Then I should not see review titled :title in the list
383
     */
384
    public function iShouldNotSeeReviewTitledInTheList($title)
385
    {
386
        Assert::false(
387
            $this->productReviewsIndexPage->hasReviewTitled($title),
388
            sprintf('Product should not have review titled "%s" but it does.', $title)
389
        );
390
    }
391
392
    /**
393
     * @Then /^I should be notified that there are no reviews$/
394
     */
395
    public function iShouldBeNotifiedThatThereAreNoReviews()
396
    {
397
        Assert::true(
398
            $this->productReviewsIndexPage->hasNoReviewMessage(),
399
            'There should be message about no reviews but there is not.'
400
        );
401
    }
402
403
    /**
404
     * @Then I should see :rating as its average rating
405
     */
406
    public function iShouldSeeAsItsAverageRating($rating)
407
    {
408
        Assert::same(
409
            (float) $rating,
410
            $this->showPage->getAverageRating(),
411
            'Product should have average rating %2$s but has %s.'
412
        );
413
    }
414
}
415