Completed
Push — 1.3 ( ea96f1...08f6de )
by Kamil
13:44
created

iWantToModifyAPromotion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
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
declare(strict_types=1);
13
14
namespace Sylius\Behat\Context\Ui\Admin;
15
16
use Behat\Behat\Context\Context;
17
use Sylius\Behat\NotificationType;
18
use Sylius\Behat\Page\Admin\Promotion\CreatePageInterface;
19
use Sylius\Behat\Page\Admin\Promotion\IndexPageInterface;
20
use Sylius\Behat\Page\Admin\Promotion\UpdatePageInterface;
21
use Sylius\Behat\Service\NotificationCheckerInterface;
22
use Sylius\Behat\Service\Resolver\CurrentPageResolverInterface;
23
use Sylius\Behat\Service\SharedStorageInterface;
24
use Sylius\Component\Core\Model\PromotionInterface;
25
use Webmozart\Assert\Assert;
26
27
final class ManagingPromotionsContext implements Context
28
{
29
    /** @var SharedStorageInterface */
30
    private $sharedStorage;
31
32
    /** @var IndexPageInterface */
33
    private $indexPage;
34
35
    /** @var CreatePageInterface */
36
    private $createPage;
37
38
    /** @var UpdatePageInterface */
39
    private $updatePage;
40
41
    /** @var CurrentPageResolverInterface */
42
    private $currentPageResolver;
43
44
    /** @var NotificationCheckerInterface */
45
    private $notificationChecker;
46
47
    public function __construct(
48
        SharedStorageInterface $sharedStorage,
49
        IndexPageInterface $indexPage,
50
        CreatePageInterface $createPage,
51
        UpdatePageInterface $updatePage,
52
        CurrentPageResolverInterface $currentPageResolver,
53
        NotificationCheckerInterface $notificationChecker
54
    ) {
55
        $this->sharedStorage = $sharedStorage;
56
        $this->indexPage = $indexPage;
57
        $this->createPage = $createPage;
58
        $this->updatePage = $updatePage;
59
        $this->currentPageResolver = $currentPageResolver;
60
        $this->notificationChecker = $notificationChecker;
61
    }
62
63
    /**
64
     * @When I want to create a new promotion
65
     */
66
    public function iWantToCreateANewPromotion()
67
    {
68
        $this->createPage->open();
69
    }
70
71
    /**
72
     * @Given I want to browse promotions
73
     * @When I browse promotions
74
     */
75
    public function iWantToBrowsePromotions()
76
    {
77
        $this->indexPage->open();
78
    }
79
80
    /**
81
     * @When I specify its code as :code
82
     * @When I do not specify its code
83
     */
84
    public function iSpecifyItsCodeAs($code = null)
85
    {
86
        $this->createPage->specifyCode($code ?? '');
87
    }
88
89
    /**
90
     * @When I name it :name
91
     * @When I do not name it
92
     * @When I remove its name
93
     */
94
    public function iNameIt($name = null)
95
    {
96
        $this->createPage->nameIt($name ?? '');
97
    }
98
99
    /**
100
     * @When I remove its priority
101
     */
102
    public function iRemoveItsPriority()
103
    {
104
        $this->updatePage->setPriority(null);
105
    }
106
107
    /**
108
     * @Then I should see the promotion :promotionName in the list
109
     * @Then the :promotionName promotion should appear in the registry
110
     * @Then the :promotionName promotion should exist in the registry
111
     * @Then this promotion should still be named :promotionName
112
     * @Then promotion :promotionName should still exist in the registry
113
     */
114
    public function thePromotionShouldAppearInTheRegistry(string $promotionName): void
115
    {
116
        $this->indexPage->open();
117
118
        Assert::true($this->indexPage->isSingleResourceOnPage(['name' => $promotionName]));
119
    }
120
121
    /**
122
     * @When I add it
123
     * @When I try to add it
124
     */
125
    public function iAddIt()
126
    {
127
        $this->createPage->create();
128
    }
129
130
    /**
131
     * @When I add the "Has at least one from taxons" rule configured with :firstTaxon
132
     * @When I add the "Has at least one from taxons" rule configured with :firstTaxon and :secondTaxon
133
     */
134
    public function iAddTheHasTaxonRuleConfiguredWith(...$taxons)
135
    {
136
        $this->createPage->addRule('Has at least one from taxons');
137
138
        $this->createPage->selectAutocompleteRuleOption('Taxons', $taxons, true);
139
    }
140
141
    /**
142
     * @When /^I add the "Total price of items from taxon" rule configured with "([^"]+)" taxon and (?:€|£|\$)([^"]+) amount for "([^"]+)" channel$/
143
     */
144
    public function iAddTheRuleConfiguredWith($taxonName, $amount, $channelName)
145
    {
146
        $this->createPage->addRule('Total price of items from taxon');
147
        $this->createPage->selectAutocompleteRuleOption('Taxon', $taxonName);
148
        $this->createPage->fillRuleOptionForChannel($channelName, 'Amount', $amount);
149
    }
150
151
    /**
152
     * @When /^I add the "Item total" rule configured with (?:€|£|\$)([^"]+) amount for "([^"]+)" channel and (?:€|£|\$)([^"]+) amount for "([^"]+)" channel$/
153
     */
154
    public function iAddTheItemTotalRuleConfiguredWithTwoChannel(
155
        $firstAmount,
156
        $firstChannelName,
157
        $secondAmount,
158
        $secondChannelName
159
    ) {
160
        $this->createPage->addRule('Item total');
161
        $this->createPage->fillRuleOptionForChannel($firstChannelName, 'Amount', $firstAmount);
162
        $this->createPage->fillRuleOptionForChannel($secondChannelName, 'Amount', $secondAmount);
163
    }
164
165
    /**
166
     * @When /^I add the "([^"]+)" action configured with amount of "(?:€|£|\$)([^"]+)" for "([^"]+)" channel$/
167
     */
168
    public function iAddTheActionConfiguredWithAmountForChannel($actionType, $amount, $channelName)
169
    {
170
        $this->createPage->addAction($actionType);
171
        $this->createPage->fillActionOptionForChannel($channelName, 'Amount', $amount);
172
    }
173
174
    /**
175
     * @When /^it is(?:| also) configured with amount of "(?:€|£|\$)([^"]+)" for "([^"]+)" channel$/
176
     */
177
    public function itIsConfiguredWithAmountForChannel($amount, $channelName)
178
    {
179
        $this->createPage->fillActionOptionForChannel($channelName, 'Amount', $amount);
180
    }
181
182
    /**
183
     * @When /^I specify that on "([^"]+)" channel this action should be applied to items with price greater then "(?:€|£|\$)([^"]+)"$/
184
     */
185
    public function iAddAMinPriceFilterRangeForChannel($channelName, $minimum)
186
    {
187
        $this->createPage->fillActionOptionForChannel($channelName, 'Min', $minimum);
188
    }
189
190
    /**
191
     * @When /^I specify that on "([^"]+)" channel this action should be applied to items with price lesser then "(?:€|£|\$)([^"]+)"$/
192
     */
193
    public function iAddAMaxPriceFilterRangeForChannel($channelName, $maximum)
194
    {
195
        $this->createPage->fillActionOptionForChannel($channelName, 'Max', $maximum);
196
    }
197
198
    /**
199
     * @When /^I specify that on "([^"]+)" channel this action should be applied to items with price between "(?:€|£|\$)([^"]+)" and "(?:€|£|\$)([^"]+)"$/
200
     */
201
    public function iAddAMinMaxPriceFilterRangeForChannel($channelName, $minimum, $maximum)
202
    {
203
        $this->iAddAMinPriceFilterRangeForChannel($channelName, $minimum);
204
        $this->iAddAMaxPriceFilterRangeForChannel($channelName, $maximum);
205
    }
206
207
    /**
208
     * @When I specify that this action should be applied to items from :taxonName category
209
     */
210
    public function iSpecifyThatThisActionShouldBeAppliedToItemsFromCategory($taxonName)
211
    {
212
        $this->createPage->selectAutoCompleteFilterOption('Taxons', $taxonName);
213
    }
214
215
    /**
216
     * @When /^I add the "([^"]+)" action configured with a percentage value of (?:|-)([^"]+)% for ("[^"]+") channel$/
217
     */
218
    public function iAddTheActionConfiguredWithAPercentageValueForChannel(
219
        string $actionType,
220
        string $percentage = null,
221
        string $channelName
222
    ): void {
223
        $this->createPage->addAction($actionType);
224
        $this->createPage->fillActionOptionForChannel($channelName, 'Percentage', $percentage);
225
    }
226
227
    /**
228
     * @When /^I add the "([^"]+)" action configured with a percentage value of (?:|-)([^"]+)%$/
229
     * @When I add the :actionType action configured without a percentage value
230
     */
231
    public function iAddTheActionConfiguredWithAPercentageValue($actionType, $percentage = null)
232
    {
233
        $this->createPage->addAction($actionType);
234
        $this->createPage->fillActionOption('Percentage', $percentage ?? '');
235
    }
236
237
    /**
238
     * @When I add the "Customer group" rule for :customerGroupName group
239
     */
240
    public function iAddTheCustomerGroupRuleConfiguredForGroup($customerGroupName)
241
    {
242
        $this->createPage->addRule('Customer group');
243
        $this->createPage->selectRuleOption('Customer group', $customerGroupName);
244
    }
245
246
    /**
247
     * @When I check (also) the :promotionName promotion
248
     */
249
    public function iCheckThePromotion(string $promotionName): void
250
    {
251
        $this->indexPage->checkResourceOnPage(['name' => $promotionName]);
252
    }
253
254
    /**
255
     * @When I delete them
256
     */
257
    public function iDeleteThem(): void
258
    {
259
        $this->indexPage->bulkDelete();
260
    }
261
262
    /**
263
     * @Then I should see a single promotion in the list
264
     * @Then there should be :amount promotions
265
     */
266
    public function thereShouldBePromotion(int $amount = 1): void
267
    {
268
        Assert::same($amount, $this->indexPage->countItems());
269
    }
270
271
    /**
272
     * @Then /^(this promotion) should be coupon based$/
273
     */
274
    public function thisPromotionShouldBeCouponBased(PromotionInterface $promotion)
275
    {
276
        Assert::true($this->indexPage->isCouponBasedFor($promotion));
277
    }
278
279
    /**
280
     * @Then /^I should be able to manage coupons for (this promotion)$/
281
     */
282
    public function iShouldBeAbleToManageCouponsForThisPromotion(PromotionInterface $promotion)
283
    {
284
        Assert::true($this->indexPage->isAbleToManageCouponsFor($promotion));
285
    }
286
287
    /**
288
     * @Then I should be notified that :element is required
289
     */
290
    public function iShouldBeNotifiedThatIsRequired($element)
291
    {
292
        $this->assertFieldValidationMessage($element, sprintf('Please enter promotion %s.', $element));
293
    }
294
295
    /**
296
     * @Then I should be notified that a :element value should be a numeric value
297
     */
298
    public function iShouldBeNotifiedThatAMinimalValueShouldBeNumeric($element)
299
    {
300
        $this->assertFieldValidationMessage($element, 'This value is not valid.');
301
    }
302
303
    /**
304
     * @Then I should be notified that promotion with this code already exists
305
     */
306
    public function iShouldBeNotifiedThatPromotionWithThisCodeAlreadyExists()
307
    {
308
        Assert::same($this->createPage->getValidationMessage('code'), 'The promotion with given code already exists.');
309
    }
310
311
    /**
312
     * @Then promotion with :element :name should not be added
313
     */
314
    public function promotionWithElementValueShouldNotBeAdded($element, $name)
315
    {
316
        $this->indexPage->open();
317
318
        Assert::false($this->indexPage->isSingleResourceOnPage([$element => $name]));
319
    }
320
321
    /**
322
     * @Then there should still be only one promotion with :element :value
323
     */
324
    public function thereShouldStillBeOnlyOnePromotionWith($element, $value)
325
    {
326
        $this->indexPage->open();
327
328
        Assert::true($this->indexPage->isSingleResourceOnPage([$element => $value]));
329
    }
330
331
    /**
332
     * @When I set its usage limit to :usageLimit
333
     */
334
    public function iSetItsUsageLimitTo($usageLimit)
335
    {
336
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
337
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
338
339
        $currentPage->fillUsageLimit($usageLimit);
340
    }
341
342
    /**
343
     * @Then the :promotion promotion should be available to be used only :usageLimit times
344
     */
345
    public function thePromotionShouldBeAvailableToUseOnlyTimes(PromotionInterface $promotion, $usageLimit)
346
    {
347
        $this->iWantToModifyAPromotion($promotion);
348
349
        Assert::true($this->updatePage->hasResourceValues(['usage_limit' => $usageLimit]));
350
    }
351
352
    /**
353
     * @When I make it exclusive
354
     */
355
    public function iMakeItExclusive()
356
    {
357
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
358
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
359
360
        $currentPage->makeExclusive();
361
    }
362
363
    /**
364
     * @Then the :promotion promotion should be exclusive
365
     */
366
    public function thePromotionShouldBeExclusive(PromotionInterface $promotion)
367
    {
368
        $this->assertIfFieldIsTrue($promotion, 'exclusive');
369
    }
370
371
    /**
372
     * @When I make it coupon based
373
     */
374
    public function iMakeItCouponBased()
375
    {
376
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
377
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
378
379
        $currentPage->checkCouponBased();
380
    }
381
382
    /**
383
     * @Then the :promotion promotion should be coupon based
384
     */
385
    public function thePromotionShouldBeCouponBased(PromotionInterface $promotion)
386
    {
387
        $this->assertIfFieldIsTrue($promotion, 'coupon_based');
388
    }
389
390
    /**
391
     * @When I make it applicable for the :channelName channel
392
     */
393
    public function iMakeItApplicableForTheChannel($channelName)
394
    {
395
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
396
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
397
398
        $currentPage->checkChannel($channelName);
399
    }
400
401
    /**
402
     * @Then the :promotion promotion should be applicable for the :channelName channel
403
     */
404
    public function thePromotionShouldBeApplicableForTheChannel(PromotionInterface $promotion, $channelName)
405
    {
406
        $this->iWantToModifyAPromotion($promotion);
407
408
        Assert::true($this->updatePage->checkChannelsState($channelName));
409
    }
410
411
    /**
412
     * @Given I want to modify a :promotion promotion
413
     * @Given /^I want to modify (this promotion)$/
414
     */
415
    public function iWantToModifyAPromotion(PromotionInterface $promotion)
416
    {
417
        $this->updatePage->open(['id' => $promotion->getId()]);
418
    }
419
420
    /**
421
     * @Then the code field should be disabled
422
     */
423
    public function theCodeFieldShouldBeDisabled()
424
    {
425
        Assert::true($this->updatePage->isCodeDisabled());
426
    }
427
428
    /**
429
     * @When I save my changes
430
     * @When I try to save my changes
431
     */
432
    public function iSaveMyChanges()
433
    {
434
        $this->updatePage->saveChanges();
435
    }
436
437
    /**
438
     * @When /^I delete a ("([^"]+)" promotion)$/
439
     * @When /^I try to delete a ("([^"]+)" promotion)$/
440
     */
441
    public function iDeletePromotion(PromotionInterface $promotion)
442
    {
443
        $this->sharedStorage->set('promotion', $promotion);
444
445
        $this->indexPage->open();
446
        $this->indexPage->deleteResourceOnPage(['name' => $promotion->getName()]);
447
    }
448
449
    /**
450
     * @Then /^(this promotion) should no longer exist in the promotion registry$/
451
     */
452
    public function promotionShouldNotExistInTheRegistry(PromotionInterface $promotion)
453
    {
454
        $this->indexPage->open();
455
456
        Assert::false($this->indexPage->isSingleResourceOnPage(['code' => $promotion->getCode()]));
457
    }
458
459
    /**
460
     * @Then I should be notified that it is in use and cannot be deleted
461
     */
462
    public function iShouldBeNotifiedOfFailure()
463
    {
464
        $this->notificationChecker->checkNotification(
465
            'Cannot delete, the promotion is in use.',
466
            NotificationType::failure()
467
        );
468
    }
469
470
    /**
471
     * @When I make it available from :startsDate to :endsDate
472
     */
473
    public function iMakeItAvailableFromTo(\DateTimeInterface $startsDate, \DateTimeInterface $endsDate)
474
    {
475
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
476
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
477
478
        $currentPage->setStartsAt($startsDate);
479
        $currentPage->setEndsAt($endsDate);
480
    }
481
482
    /**
483
     * @Then the :promotion promotion should be available from :startsDate to :endsDate
484
     */
485
    public function thePromotionShouldBeAvailableFromTo(PromotionInterface $promotion, \DateTimeInterface $startsDate, \DateTimeInterface $endsDate)
486
    {
487
        $this->iWantToModifyAPromotion($promotion);
488
489
        Assert::true($this->updatePage->hasStartsAt($startsDate));
490
491
        Assert::true($this->updatePage->hasEndsAt($endsDate));
492
    }
493
494
    /**
495
     * @Then I should be notified that promotion cannot end before it start
496
     */
497
    public function iShouldBeNotifiedThatPromotionCannotEndBeforeItsEvenStart()
498
    {
499
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
500
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
501
502
        Assert::same($currentPage->getValidationMessage('ends_at'), 'End date cannot be set prior start date.');
503
    }
504
505
    /**
506
     * @Then I should be notified that this value should not be blank
507
     */
508
    public function iShouldBeNotifiedThatThisValueShouldNotBeBlank()
509
    {
510
        Assert::same(
511
            $this->createPage->getValidationMessageForAction(),
512
            'This value should not be blank.'
513
        );
514
    }
515
516
    /**
517
     * @Then I should be notified that the maximum value of a percentage discount is 100%
518
     */
519
    public function iShouldBeNotifiedThatTheMaximumValueOfAPercentageDiscountIs100()
520
    {
521
        Assert::same(
522
            $this->createPage->getValidationMessageForAction(),
523
            'The maximum value of a percentage discount is 100%.'
524
        );
525
    }
526
527
    /**
528
     * @Then I should be notified that a percentage discount value must be at least 0%
529
     */
530
    public function iShouldBeNotifiedThatAPercentageDiscountValueMustBeAtLeast0()
531
    {
532
        Assert::same(
533
            $this->createPage->getValidationMessageForAction(),
534
            'The value of a percentage discount must be at least 0%.'
535
        );
536
    }
537
538
    /**
539
     * @Then the promotion :promotion should be used :usage time(s)
540
     * @Then the promotion :promotion should not be used
541
     */
542
    public function thePromotionShouldBeUsedTime(PromotionInterface $promotion, $usage = 0)
543
    {
544
        Assert::same(
545
            (int) $usage,
546
            $this->indexPage->getUsageNumber($promotion),
547
            'Promotion should be used %s times, but is %2$s.'
548
        );
549
    }
550
551
    /**
552
     * @When I add the "Contains product" rule configured with the :productName product
553
     */
554
    public function iAddTheRuleConfiguredWithTheProduct($productName)
555
    {
556
        $this->createPage->addRule('Contains product');
557
        $this->createPage->selectAutocompleteRuleOption('Product code', $productName);
558
    }
559
560
    /**
561
     * @When I specify that this action should be applied to the :productName product
562
     */
563
    public function iSpecifyThatThisActionShouldBeAppliedToTheProduct($productName)
564
    {
565
        $this->createPage->selectAutoCompleteFilterOption('Products', $productName);
566
    }
567
568
    /**
569
     * @Then I should see :count promotions on the list
570
     */
571
    public function iShouldSeePromotionsOnTheList($count)
572
    {
573
        $actualCount = $this->indexPage->countItems();
574
575
        Assert::same(
576
            (int) $count,
577
            $actualCount,
578
            'There should be %s promotion, but there\'s %2$s.'
579
        );
580
    }
581
582
    /**
583
     * @Then the first promotion on the list should have :field :value
584
     */
585
    public function theFirstPromotionOnTheListShouldHave($field, $value)
586
    {
587
        $fields = $this->indexPage->getColumnFields($field);
588
        $actualValue = reset($fields);
589
590
        Assert::same(
591
            $actualValue,
592
            $value,
593
            sprintf('Expected first promotion\'s %s to be "%s", but it is "%s".', $field, $value, $actualValue)
594
        );
595
    }
596
597
    /**
598
     * @Then the last promotion on the list should have :field :value
599
     */
600
    public function theLastPromotionOnTheListShouldHave($field, $value)
601
    {
602
        $fields = $this->indexPage->getColumnFields($field);
603
        $actualValue = end($fields);
604
605
        Assert::same(
606
            $actualValue,
607
            $value,
608
            sprintf('Expected last promotion\'s %s to be "%s", but it is "%s".', $field, $value, $actualValue)
609
        );
610
    }
611
612
    /**
613
     * @Given the :promotion promotion should have priority :priority
614
     */
615
    public function thePromotionsShouldHavePriority(PromotionInterface $promotion, int $priority)
616
    {
617
        $this->iWantToModifyAPromotion($promotion);
618
619
        Assert::same($this->updatePage->getPriority(), $priority);
620
    }
621
622
    /**
623
     * @Then I should be able to modify a :promotion promotion
624
     */
625
    public function iShouldBeAbleToModifyAPromotion(PromotionInterface $promotion): void
626
    {
627
        $this->iWantToModifyAPromotion($promotion);
628
        $this->updatePage->saveChanges();
629
    }
630
631
    /**
632
     * @Then the :promotion promotion should have :ruleName rule configured
633
     */
634
    public function thePromotionShouldHaveRuleConfigured(PromotionInterface $promotion, string $ruleName): void
635
    {
636
        $this->iWantToModifyAPromotion($promotion);
637
        $this->updatePage->saveChanges();
638
639
        Assert::true($this->updatePage->hasRule($ruleName));
640
    }
641
642
    /**
643
     * @Then the :promotion promotion should not have any rule configured
644
     */
645
    public function thePromotionShouldNotHaveAnyRuleConfigured(PromotionInterface $promotion): void
646
    {
647
        $this->iWantToModifyAPromotion($promotion);
648
649
        Assert::false($this->updatePage->hasAnyRule());
650
    }
651
652
    /**
653
     * @param string $element
654
     * @param string $expectedMessage
655
     */
656
    private function assertFieldValidationMessage($element, $expectedMessage)
657
    {
658
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
659
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
660
661
        Assert::same($currentPage->getValidationMessage($element), $expectedMessage);
662
    }
663
664
    /**
665
     * @param string $field
666
     */
667
    private function assertIfFieldIsTrue(PromotionInterface $promotion, $field)
668
    {
669
        $this->iWantToModifyAPromotion($promotion);
670
671
        Assert::true($this->updatePage->hasResourceValues([$field => 1]));
672
    }
673
}
674