Completed
Push — master ( ec9928...7e945e )
by Paweł
56:53 queued 42:10
created

ManagingProductsContext   D

Complexity

Total Complexity 59

Size/Duplication

Total Lines 769
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 14

Importance

Changes 0
Metric Value
wmc 59
lcom 3
cbo 14
dl 0
loc 769
rs 4.2105
c 0
b 0
f 0

56 Methods

Rating   Name   Duplication   Size   Complexity  
A iWantToCreateANewSimpleProduct() 0 4 1
A iWantToCreateANewConfigurableProduct() 0 4 1
A iSpecifyItsCodeAs() 0 9 1
A iNameItIn() 0 9 1
A iRenameItToIn() 0 9 1
A __construct() 0 21 1
A iAddIt() 0 12 1
A iSetItsPriceTo() 0 4 1
A theProductShouldAppearInTheShop() 0 9 1
A iDisableItsTracking() 0 4 1
A iEnableItsTracking() 0 4 1
A iWantToBrowseProducts() 0 4 1
A iFilterThemByTaxon() 0 4 1
A iShouldSeeProductWith() 0 7 1
A iShouldNotSeeAnyProductWith() 0 7 1
A theFirstProductOnTheListShouldHave() 0 10 1
A iSortProductsBy() 0 4 1
A iShouldSeeProductsInTheList() 0 10 1
A iDeleteProduct() 0 7 1
A productShouldNotExist() 0 9 1
A iShouldBeNotifiedOfFailure() 0 7 1
A productShouldExistInTheProductCatalog() 0 4 1
A iWantToModifyAProduct() 0 11 2
A theCodeFieldShouldBeDisabled() 0 13 1
A thisProductPriceShouldBeEqualTo() 0 4 1
A thisProductElementShouldBe() 0 4 1
A iShouldBeNotifiedThatIsRequired() 0 4 1
A iShouldBeNotifiedThatPriceIsRequired() 0 4 1
A iSaveMyChanges() 0 12 1
A iChangeItsPriceTo() 0 4 1
A iAddTheOptionToIt() 0 4 1
A iSetItsAttributeTo() 0 4 1
A iRemoveItsAttribute() 0 4 1
A itsAttributeShouldBe() 0 10 1
A productShouldNotHaveAttribute() 0 9 1
A productWithNameShouldNotBeAdded() 0 9 1
A iRemoveItsNameFromTranslation() 0 9 1
A thisProductShouldHaveOption() 0 4 1
A theOptionFieldShouldBeDisabled() 0 7 1
A iChooseMainTaxon() 0 9 1
A thisProductMainTaxonShouldBe() 0 15 1
A thisProductShouldNotBeTracked() 0 9 1
A thisProductShouldBeTracked() 0 9 1
A iAttachImageWithACode() 0 12 2
A thisProductShouldHaveAnImageWithCode() 0 15 1
A thisProductShouldNotHaveAnImageWithCode() 0 13 1
A iChangeItsImageToPathForTheCode() 0 10 1
A iRemoveAnImageWithACode() 0 10 1
A iRemoveTheFirstImage() 0 10 1
A thisProductShouldNotHaveImages() 0 14 1
A theImageCodeFieldShouldBeDisabled() 0 13 1
A iShouldBeNotifiedThatTheImageWithThisCodeAlreadyExists() 0 4 1
A thereShouldStillBeOnlyOneImageInThisTaxon() 0 16 1
A thereAreNoProductReviews() 0 9 1
A assertElementValue() 0 17 1
A assertValidationMessage() 0 14 2

How to fix   Complexity   

Complex Class

Complex classes like ManagingProductsContext often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ManagingProductsContext, and based on these observations, apply Extract Interface, too.

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\Admin;
13
14
use Behat\Behat\Context\Context;
15
use Sylius\Behat\NotificationType;
16
use Sylius\Behat\Page\Admin\Crud\CreatePageInterface;
17
use Sylius\Behat\Page\Admin\Crud\UpdatePageInterface;
18
use Sylius\Behat\Page\Admin\Product\CreateConfigurableProductPageInterface;
19
use Sylius\Behat\Page\Admin\Product\CreateSimpleProductPageInterface;
20
use Sylius\Behat\Page\Admin\Product\IndexPageInterface;
21
use Sylius\Behat\Page\Admin\Product\UpdateConfigurableProductPageInterface;
22
use Sylius\Behat\Page\Admin\Product\UpdateSimpleProductPageInterface;
23
use Sylius\Behat\Page\Admin\ProductReview\IndexPageInterface as ProductReviewIndexPageInterface;
24
use Sylius\Behat\Service\NotificationCheckerInterface;
25
use Sylius\Behat\Service\Resolver\CurrentProductPageResolverInterface;
26
use Sylius\Component\Core\Model\ProductInterface;
27
use Sylius\Behat\Service\SharedStorageInterface;
28
use Sylius\Component\Taxonomy\Model\TaxonInterface;
29
use Webmozart\Assert\Assert;
30
31
/**
32
 * @author Kamil Kokot <[email protected]>
33
 * @author Magdalena Banasiak <[email protected]>
34
 * @author Łukasz Chruściel <[email protected]>
35
 */
36
final class ManagingProductsContext implements Context
37
{
38
    /**
39
     * @var SharedStorageInterface
40
     */
41
    private $sharedStorage;
42
43
    /**x
44
     * @var CreateSimpleProductPageInterface
45
     */
46
    private $createSimpleProductPage;
47
48
    /**
49
     * @var CreateConfigurableProductPageInterface
50
     */
51
    private $createConfigurableProductPage;
52
53
    /**
54
     * @var IndexPageInterface
55
     */
56
    private $indexPage;
57
58
    /**
59
     * @var UpdateSimpleProductPageInterface
60
     */
61
    private $updateSimpleProductPage;
62
63
    /**
64
     * @var UpdateConfigurableProductPageInterface
65
     */
66
    private $updateConfigurableProductPage;
67
68
    /**
69
     * @var ProductReviewIndexPageInterface
70
     */
71
    private $productReviewIndexPage;
72
73
    /**
74
     * @var CurrentProductPageResolverInterface
75
     */
76
    private $currentPageResolver;
77
78
    /**
79
     * @var NotificationCheckerInterface
80
     */
81
    private $notificationChecker;
82
83
    /**
84
     * @param SharedStorageInterface $sharedStorage
85
     * @param CreateSimpleProductPageInterface $createSimpleProductPage
86
     * @param CreateConfigurableProductPageInterface $createConfigurableProductPage
87
     * @param IndexPageInterface $indexPage
88
     * @param UpdateSimpleProductPageInterface $updateSimpleProductPage
89
     * @param UpdateConfigurableProductPageInterface $updateConfigurableProductPage
90
     * @param ProductReviewIndexPageInterface $productReviewIndexPage
91
     * @param CurrentProductPageResolverInterface $currentPageResolver
92
     * @param NotificationCheckerInterface $notificationChecker
93
     */
94
    public function __construct(
95
        SharedStorageInterface $sharedStorage,
96
        CreateSimpleProductPageInterface $createSimpleProductPage,
97
        CreateConfigurableProductPageInterface $createConfigurableProductPage,
98
        IndexPageInterface $indexPage,
99
        UpdateSimpleProductPageInterface $updateSimpleProductPage,
100
        UpdateConfigurableProductPageInterface $updateConfigurableProductPage,
101
        ProductReviewIndexPageInterface $productReviewIndexPage,
102
        CurrentProductPageResolverInterface $currentPageResolver,
103
        NotificationCheckerInterface $notificationChecker
104
    ) {
105
        $this->sharedStorage = $sharedStorage;
106
        $this->createSimpleProductPage = $createSimpleProductPage;
107
        $this->createConfigurableProductPage = $createConfigurableProductPage;
108
        $this->indexPage = $indexPage;
109
        $this->updateSimpleProductPage = $updateSimpleProductPage;
110
        $this->updateConfigurableProductPage = $updateConfigurableProductPage;
111
        $this->productReviewIndexPage = $productReviewIndexPage;
112
        $this->currentPageResolver = $currentPageResolver;
113
        $this->notificationChecker = $notificationChecker;
114
    }
115
116
    /**
117
     * @Given I want to create a new simple product
118
     */
119
    public function iWantToCreateANewSimpleProduct()
120
    {
121
        $this->createSimpleProductPage->open();
122
    }
123
124
    /**
125
     * @Given I want to create a new configurable product
126
     */
127
    public function iWantToCreateANewConfigurableProduct()
128
    {
129
        $this->createConfigurableProductPage->open();
130
    }
131
132
    /**
133
     * @When I specify its code as :code
134
     * @When I do not specify its code
135
     */
136
    public function iSpecifyItsCodeAs($code = null)
137
    {
138
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([
139
            $this->createSimpleProductPage,
140
            $this->createConfigurableProductPage,
141
        ]);
142
143
        $currentPage->specifyCode($code);
144
    }
145
146
    /**
147
     * @When I name it :name in :language
148
     */
149
    public function iNameItIn($name, $language)
150
    {
151
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([
152
            $this->createSimpleProductPage,
153
            $this->createConfigurableProductPage,
154
        ]);
155
156
        $currentPage->nameItIn($name, $language);
157
    }
158
159
    /**
160
     * @When I rename it to :name in :language
161
     */
162
    public function iRenameItToIn($name, $language)
163
    {
164
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([
165
            $this->updateSimpleProductPage,
166
            $this->updateConfigurableProductPage,
167
        ], $this->sharedStorage->get('product'));
168
169
        $currentPage->nameItIn($name, $language);
170
    }
171
172
    /**
173
     * @When I add it
174
     * @When I try to add it
175
     */
176
    public function iAddIt()
177
    {
178
        /** @var CreatePageInterface $currentPage */
179
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([
180
            $this->createSimpleProductPage,
181
            $this->createConfigurableProductPage,
182
        ]);
183
184
        Assert::isInstanceOf($currentPage, CreatePageInterface::class);
185
186
        $currentPage->create();
187
    }
188
189
    /**
190
     * @When I disable its inventory tracking
191
     */
192
    public function iDisableItsTracking()
193
    {
194
        $this->updateSimpleProductPage->disableTracking();
195
    }
196
197
    /**
198
     * @When I enable its inventory tracking
199
     */
200
    public function iEnableItsTracking()
201
    {
202
        $this->updateSimpleProductPage->enableTracking();
203
    }
204
205
    /**
206
     * @When /^I set its price to ("(?:€|£|\$)[^"]+")$/
207
     */
208
    public function iSetItsPriceTo($price)
209
    {
210
        $this->createSimpleProductPage->specifyPrice($price);
211
    }
212
213
    /**
214
     * @Then the product :productName should appear in the shop
215
     * @Then the product :productName should be in the shop
216
     * @Then this product should still be named :productName
217
     */
218
    public function theProductShouldAppearInTheShop($productName)
219
    {
220
        $this->iWantToBrowseProducts();
221
222
        Assert::true(
223
            $this->indexPage->isSingleResourceOnPage(['name' => $productName]),
224
            sprintf('The product with name %s has not been found.', $productName)
225
        );
226
    }
227
228
    /**
229
     * @Given I am browsing products
230
     * @When I want to browse products
231
     */
232
    public function iWantToBrowseProducts()
233
    {
234
        $this->indexPage->open();
235
    }
236
237
    /**
238
     * @When I filter them by :taxonName taxon
239
     */
240
    public function iFilterThemByTaxon($taxonName)
241
    {
242
        $this->indexPage->filterByTaxon($taxonName);
243
    }
244
245
    /**
246
     * @Then I should( still) see a product with :field :value
247
     */
248
    public function iShouldSeeProductWith($field, $value)
249
    {
250
        Assert::true(
251
            $this->indexPage->isSingleResourceOnPage([$field => $value]),
252
            sprintf('The product with %s "%s" has not been found.', $field, $value)
253
        );
254
    }
255
256
    /**
257
     * @Then I should not see any product with :field :value
258
     */
259
    public function iShouldNotSeeAnyProductWith($field, $value)
260
    {
261
        Assert::false(
262
            $this->indexPage->isSingleResourceOnPage([$field => $value]),
263
            sprintf('The product with %s "%s" has been found.', $field, $value)
264
        );
265
    }
266
267
    /**
268
     * @Then the first product on the list should have :field :value
269
     */
270
    public function theFirstProductOnTheListShouldHave($field, $value)
271
    {
272
        $actualValue = $this->indexPage->getColumnFields($field)[0];
273
274
        Assert::same(
275
            $actualValue,
276
            $value,
277
            sprintf('Expected first product\'s %s to be "%s", but it is "%s".', $field, $value, $actualValue)
278
        );
279
    }
280
281
    /**
282
     * @When I switch the way products are sorted by :field
283
     * @When I start sorting products by :field
284
     * @Given the products are already sorted by :field
285
     */
286
    public function iSortProductsBy($field)
287
    {
288
        $this->indexPage->sortBy($field);
289
    }
290
291
    /**
292
     * @Then I should see :numberOfProducts products in the list
293
     */
294
    public function iShouldSeeProductsInTheList($numberOfProducts)
295
    {
296
        $foundRows = $this->indexPage->countItems();
297
298
        Assert::same(
299
            (int) $numberOfProducts,
300
            $foundRows,
301
            '%s rows with products should appear on page, %s rows has been found'
302
        );
303
    }
304
305
    /**
306
     * @When I delete the :product product
307
     * @When I try to delete the :product product
308
     */
309
    public function iDeleteProduct(ProductInterface $product)
310
    {
311
        $this->sharedStorage->set('product', $product);
312
313
        $this->iWantToBrowseProducts();
314
        $this->indexPage->deleteResourceOnPage(['name' => $product->getName()]);
315
    }
316
317
    /**
318
     * @Then /^(this product) should not exist in the product catalog$/
319
     */
320
    public function productShouldNotExist(ProductInterface $product)
321
    {
322
        $this->iWantToBrowseProducts();
323
324
        Assert::false(
325
            $this->indexPage->isSingleResourceOnPage(['code' => $product->getCode()]),
326
            sprintf('Product with code %s exists but should not.', $product->getCode())
327
        );
328
    }
329
330
    /**
331
     * @Then I should be notified that this product is in use and cannot be deleted
332
     */
333
    public function iShouldBeNotifiedOfFailure()
334
    {
335
        $this->notificationChecker->checkNotification(
336
            "Cannot delete, the product is in use.",
337
            NotificationType::failure()
338
        );
339
    }
340
341
    /**
342
     * @Then /^(this product) should still exist in the product catalog$/
343
     */
344
    public function productShouldExistInTheProductCatalog(ProductInterface $product)
345
    {
346
        $this->theProductShouldAppearInTheShop($product->getName());
347
    }
348
349
    /**
350
     * @When I want to modify the :product product
351
     * @When /^I want to modify (this product)$/
352
     */
353
    public function iWantToModifyAProduct(ProductInterface $product)
354
    {
355
        $this->sharedStorage->set('product', $product);
356
357
        if ($product->isSimple()) {
358
            $this->updateSimpleProductPage->open(['id' => $product->getId()]);
359
            return;
360
        }
361
362
        $this->updateConfigurableProductPage->open(['id' => $product->getId()]);
363
    }
364
365
    /**
366
     * @Then the code field should be disabled
367
     */
368
    public function theCodeFieldShouldBeDisabled()
369
    {
370
        /** @var UpdatePageInterface $currentPage */
371
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([
372
            $this->updateSimpleProductPage,
373
            $this->updateConfigurableProductPage,
374
        ], $this->sharedStorage->get('product'));
375
376
        Assert::true(
377
            $currentPage->isCodeDisabled(),
378
            'Code should be immutable, but it does not.'
379
        );
380
    }
381
382
    /**
383
     * @Then /^this product price should be "(?:€|£|\$)([^"]+)"$/
384
     */
385
    public function thisProductPriceShouldBeEqualTo($price)
386
    {
387
        $this->assertElementValue('price', $price);
388
    }
389
390
    /**
391
     * @Then this product name should be :name
392
     */
393
    public function thisProductElementShouldBe($name)
394
    {
395
        $this->assertElementValue('name', $name);
396
    }
397
398
    /**
399
     * @Then /^I should be notified that (code|name) is required$/
400
     */
401
    public function iShouldBeNotifiedThatIsRequired($element)
402
    {
403
        $this->assertValidationMessage($element, sprintf('Please enter product %s.', $element));
404
    }
405
406
    /**
407
     * @Then I should be notified that price is required
408
     */
409
    public function iShouldBeNotifiedThatPriceIsRequired()
410
    {
411
        $this->assertValidationMessage('price', 'Please enter the price.');
412
    }
413
414
    /**
415
     * @When I save my changes
416
     * @When I try to save my changes
417
     */
418
    public function iSaveMyChanges()
419
    {
420
        /** @var UpdatePageInterface $currentPage */
421
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([
422
            $this->updateSimpleProductPage,
423
            $this->updateConfigurableProductPage,
424
        ], $this->sharedStorage->get('product'));
425
426
        Assert::isInstanceOf($currentPage, UpdatePageInterface::class);
427
428
        $currentPage->saveChanges();
429
    }
430
431
    /**
432
     * @When /^I change its price to "(?:€|£|\$)([^"]+)"$/
433
     */
434
    public function iChangeItsPriceTo($price)
435
    {
436
        $this->updateSimpleProductPage->specifyPrice($price);
437
    }
438
439
    /**
440
     * @Given I add the :optionName option to it
441
     */
442
    public function iAddTheOptionToIt($optionName)
443
    {
444
        $this->createConfigurableProductPage->selectOption($optionName);
445
    }
446
447
    /**
448
     * @When I set its :attribute attribute to :value
449
     */
450
    public function iSetItsAttributeTo($attribute, $value)
451
    {
452
        $this->createSimpleProductPage->addAttribute($attribute, $value);
453
    }
454
455
    /**
456
     * @When I remove its :attribute attribute
457
     */
458
    public function iRemoveItsAttribute($attribute)
459
    {
460
        $this->createSimpleProductPage->removeAttribute($attribute);
461
    }
462
463
    /**
464
     * @Then /^attribute "([^"]+)" of (product "[^"]+") should be "([^"]+)"$/
465
     */
466
    public function itsAttributeShouldBe($attribute, ProductInterface $product, $value)
467
    {
468
        $this->updateSimpleProductPage->open(['id' => $product->getId()]);
469
470
        Assert::same(
471
            $value,
472
            $this->updateSimpleProductPage->getAttributeValue($attribute),
473
            sprintf('ProductAttribute "%s" should have value "%s" but it does not.', $attribute, $value)
474
        );
475
    }
476
477
    /**
478
     * @Then /^(product "[^"]+") should not have a "([^"]+)" attribute$/
479
     */
480
    public function productShouldNotHaveAttribute(ProductInterface $product, $attribute)
481
    {
482
        $this->updateSimpleProductPage->open(['id' => $product->getId()]);
483
484
        Assert::false(
485
            $this->updateSimpleProductPage->hasAttribute($attribute),
486
            sprintf('Product "%s" should not have attribute "%s" but it does.', $product->getName(), $attribute)
487
        );
488
    }
489
490
    /**
491
     * @Given product with :element :value should not be added
492
     */
493
    public function productWithNameShouldNotBeAdded($element, $value)
494
    {
495
        $this->iWantToBrowseProducts();
496
497
        Assert::false(
498
            $this->indexPage->isSingleResourceOnPage([$element => $value]),
499
            sprintf('Product with %s %s was created, but it should not.', $element, $value)
500
        );
501
    }
502
503
    /**
504
     * @When I remove its name from :language translation
505
     */
506
    public function iRemoveItsNameFromTranslation($language)
507
    {
508
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([
509
            $this->updateSimpleProductPage,
510
            $this->updateConfigurableProductPage,
511
        ], $this->sharedStorage->get('product'));
512
513
        $currentPage->nameItIn('', $language);
514
    }
515
516
    /**
517
     * @Then /^this product should have (?:a|an) "([^"]+)" option$/
518
     */
519
    public function thisProductShouldHaveOption($productOption)
520
    {
521
        $this->updateConfigurableProductPage->isProductOptionChosen($productOption);
522
    }
523
524
    /**
525
     * @Then the option field should be disabled
526
     */
527
    public function theOptionFieldShouldBeDisabled()
528
    {
529
        Assert::true(
530
            $this->updateConfigurableProductPage->isProductOptionsDisabled(),
531
            'Options field should be immutable, but it does not.'
532
        );
533
    }
534
535
    /**
536
     * @When /^I choose main (taxon "([^"]+)")$/
537
     */
538
    public function iChooseMainTaxon(TaxonInterface $taxon)
539
    {
540
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([
541
            $this->updateSimpleProductPage,
542
            $this->updateConfigurableProductPage,
543
        ], $this->sharedStorage->get('product'));
544
545
        $currentPage->selectMainTaxon($taxon);
546
    }
547
548
    /**
549
     * @Then /^(this product) main taxon should be "([^"]+)"$/
550
     */
551
    public function thisProductMainTaxonShouldBe(ProductInterface $product, $taxonName)
552
    {
553
        /** @var UpdatePageInterface $currentPage */
554
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([
555
            $this->updateSimpleProductPage,
556
            $this->updateConfigurableProductPage,
557
        ], $this->sharedStorage->get('product'));
558
559
        $currentPage->open(['id' => $product->getId()]);
560
561
        Assert::true(
562
            $this->updateConfigurableProductPage->isMainTaxonChosen($taxonName),
563
            sprintf('The main taxon %s should be chosen, but it does not.', $taxonName)
564
        );
565
    }
566
567
    /**
568
     * @Then /^inventory of (this product) should not be tracked$/
569
     */
570
    public function thisProductShouldNotBeTracked(ProductInterface $product)
571
    {
572
        $this->iWantToModifyAProduct($product);
573
574
        Assert::false(
575
            $this->updateSimpleProductPage->isTracked(),
576
            '"%s" should not be tracked, but it is.'
577
        );
578
    }
579
580
    /**
581
     * @Then /^inventory of (this product) should be tracked$/
582
     */
583
    public function thisProductShouldBeTracked(ProductInterface $product)
584
    {
585
        $this->iWantToModifyAProduct($product);
586
587
        Assert::true(
588
            $this->updateSimpleProductPage->isTracked(),
589
            '"%s" should be tracked, but it is not.'
590
        );
591
    }
592
593
    /**
594
     * @When I attach the :path image with a code :code
595
     */
596
    public function iAttachImageWithACode($path, $code)
597
    {
598
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
599
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([
600
            $this->createSimpleProductPage,
601
            $this->createConfigurableProductPage,
602
            $this->updateSimpleProductPage,
603
            $this->updateConfigurableProductPage,
604
        ], $this->sharedStorage->has('product') ? $this->sharedStorage->get('product') : null);
605
606
        $currentPage->attachImageWithCode($code, $path);
607
    }
608
609
    /**
610
     * @Then /^(this product) should have(?:| also) an image with a code "([^"]*)"$/
611
     * @Then /^the (product "[^"]+") should have(?:| also) an image with a code "([^"]*)"$/
612
     */
613
    public function thisProductShouldHaveAnImageWithCode(ProductInterface $product, $code)
614
    {
615
        $this->sharedStorage->set('product', $product);
616
617
        /** @var UpdateSimpleProductPageInterface|UpdateConfigurableProductPageInterface $currentPage */
618
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([
619
            $this->updateSimpleProductPage,
620
            $this->updateConfigurableProductPage,
621
        ], $product);
622
623
        Assert::true(
624
            $currentPage->isImageWithCodeDisplayed($code),
625
            sprintf('Image with a code %s should have been displayed.', $code)
626
        );
627
    }
628
629
    /**
630
     * @Then /^(this product) should not have(?:| also) an image with a code "([^"]*)"$/
631
     */
632
    public function thisProductShouldNotHaveAnImageWithCode(ProductInterface $product, $code)
633
    {
634
        /** @var UpdateSimpleProductPageInterface|UpdateConfigurableProductPageInterface $currentPage */
635
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([
636
            $this->updateSimpleProductPage,
637
            $this->updateConfigurableProductPage,
638
        ], $product);
639
640
        Assert::false(
641
            $currentPage->isImageWithCodeDisplayed($code),
642
            sprintf('Image with a code %s should not have been displayed.', $code)
643
        );
644
    }
645
646
    /**
647
     * @When I change the image with the :code code to :path
648
     */
649
    public function iChangeItsImageToPathForTheCode($path, $code)
650
    {
651
        /** @var UpdateSimpleProductPageInterface|UpdateConfigurableProductPageInterface $currentPage */
652
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([
653
            $this->updateSimpleProductPage,
654
            $this->updateConfigurableProductPage,
655
        ], $this->sharedStorage->get('product'));
656
657
        $currentPage->changeImageWithCode($code, $path);
658
    }
659
660
    /**
661
     * @When /^I remove(?:| also) an image with a code "([^"]*)"$/
662
     */
663
    public function iRemoveAnImageWithACode($code)
664
    {
665
        /** @var UpdateSimpleProductPageInterface|UpdateConfigurableProductPageInterface $currentPage */
666
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([
667
            $this->updateSimpleProductPage,
668
            $this->updateConfigurableProductPage,
669
        ], $this->sharedStorage->get('product'));
670
671
        $currentPage->removeImageWithCode($code);
672
    }
673
674
    /**
675
     * @When I remove the first image
676
     */
677
    public function iRemoveTheFirstImage()
678
    {
679
        /** @var UpdateSimpleProductPageInterface|UpdateConfigurableProductPageInterface $currentPage */
680
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([
681
            $this->updateSimpleProductPage,
682
            $this->updateConfigurableProductPage,
683
        ], $this->sharedStorage->get('product'));
684
685
        $currentPage->removeFirstImage();
686
    }
687
688
    /**
689
     * @Then this product should not have images
690
     */
691
    public function thisProductShouldNotHaveImages()
692
    {
693
        /** @var UpdateSimpleProductPageInterface|UpdateConfigurableProductPageInterface $currentPage */
694
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([
695
            $this->updateSimpleProductPage,
696
            $this->updateConfigurableProductPage,
697
        ], $this->sharedStorage->get('product'));
698
699
        Assert::same(
700
            0,
701
            $currentPage->countImages(),
702
            'This product has %2$s, but it should not have.'
703
        );
704
    }
705
706
    /**
707
     * @Then the image code field should be disabled
708
     */
709
    public function theImageCodeFieldShouldBeDisabled()
710
    {
711
        /** @var UpdateSimpleProductPageInterface|UpdateConfigurableProductPageInterface $currentPage */
712
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([
713
            $this->updateSimpleProductPage,
714
            $this->updateConfigurableProductPage,
715
        ], $this->sharedStorage->get('product'));
716
717
        Assert::true(
718
            $currentPage->isImageCodeDisabled(),
719
            'Image code field should be disabled but it is not.'
720
        );
721
    }
722
723
    /**
724
     * @Then I should be notified that the image with this code already exists
725
     */
726
    public function iShouldBeNotifiedThatTheImageWithThisCodeAlreadyExists()
727
    {
728
        Assert::same($this->updateSimpleProductPage->getValidationMessageForImage('code'), 'Image code must be unique within this product.');
0 ignored issues
show
Unused Code introduced by
The call to UpdateSimpleProductPageI...dationMessageForImage() has too many arguments starting with 'code'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
729
    }
730
731
    /**
732
     * @Then there should still be only one image in the :product product
733
     */
734
    public function thereShouldStillBeOnlyOneImageInThisTaxon(ProductInterface $product)
735
    {
736
        $this->iWantToModifyAProduct($product);
737
738
        /** @var UpdateSimpleProductPageInterface|UpdateConfigurableProductPageInterface $currentPage */
739
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([
740
            $this->updateSimpleProductPage,
741
            $this->updateConfigurableProductPage,
742
        ], $product);
743
744
        Assert::same(
745
            1,
746
            $currentPage->countImages(),
747
            'This product has %2$s images, but it should have only one.'
748
        );
749
    }
750
751
    /**
752
     * @Then /^there should be no reviews of (this product)$/
753
     */
754
    public function thereAreNoProductReviews(ProductInterface $product)
755
    {
756
        $this->productReviewIndexPage->open();
757
758
        Assert::false(
759
            $this->productReviewIndexPage->isSingleResourceOnPage(['reviewSubject' => $product->getName()]),
760
            sprintf('There should be no reviews of %s.', $product->getName())
761
        );
762
    }
763
764
    /**
765
     * @param string $element
766
     * @param string $value
767
     */
768
    private function assertElementValue($element, $value)
769
    {
770
        /** @var UpdatePageInterface $currentPage */
771
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([
772
            $this->updateSimpleProductPage,
773
            $this->updateConfigurableProductPage,
774
        ], $this->sharedStorage->get('product'));
775
776
        Assert::isInstanceOf($currentPage, UpdatePageInterface::class);
777
778
        Assert::true(
779
            $currentPage->hasResourceValues(
780
                [$element => $value]
781
            ),
782
            sprintf('Product should have %s with %s value.', $element, $value)
783
        );
784
    }
785
786
    /**
787
     * @param string $element
788
     * @param string $message
789
     */
790
    private function assertValidationMessage($element, $message)
791
    {
792
        $product = $this->sharedStorage->has('product') ? $this->sharedStorage->get('product') : null;
793
794
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
795
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([
796
            $this->createSimpleProductPage,
797
            $this->createConfigurableProductPage,
798
            $this->updateSimpleProductPage,
799
            $this->updateConfigurableProductPage,
800
        ], $product);
801
802
        Assert::same($currentPage->getValidationMessage($element), $message);
803
    }
804
}
805