Completed
Push — master ( 1ee1a0...f8ebe1 )
by Paweł
11:32 queued 02:14
created

iAddTheCustomerGroupRuleConfiguredForGroup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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