Completed
Push — master ( 549eec...6716d6 )
by Paweł
44:16 queued 28:58
created

iMakeItAvailableFromTo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Behat\Context\Ui\Admin;
13
14
use Behat\Behat\Context\Context;
15
use Sylius\Behat\NotificationType;
16
use Sylius\Behat\Page\Admin\Crud\IndexPageInterface;
17
use Sylius\Behat\Page\Admin\Promotion\CreatePageInterface;
18
use Sylius\Behat\Page\Admin\Promotion\UpdatePageInterface;
19
use Sylius\Behat\Service\CurrentPageResolverInterface;
20
use Sylius\Behat\Service\NotificationCheckerInterface;
21
use Sylius\Component\Core\Model\PromotionInterface;
22
use Sylius\Component\Core\Test\Services\SharedStorageInterface;
23
use Webmozart\Assert\Assert;
24
25
/**
26
 * @author Mateusz Zalewski <[email protected]>
27
 */
28
final class ManagingPromotionsContext implements Context
29
{
30
    /**
31
     * @var SharedStorageInterface
32
     */
33
    private $sharedStorage;
34
35
    /**
36
     * @var IndexPageInterface
37
     */
38
    private $indexPage;
39
40
    /**
41
     * @var CreatePageInterface
42
     */
43
    private $createPage;
44
45
    /**
46
     * @var UpdatePageInterface
47
     */
48
    private $updatePage;
49
50
    /**
51
     * @var CurrentPageResolverInterface
52
     */
53
    private $currentPageResolver;
54
55
    /**
56
     * @var NotificationCheckerInterface
57
     */
58
    private $notificationChecker;
59
60
    /**
61
     * @param SharedStorageInterface $sharedStorage
62
     * @param IndexPageInterface $indexPage
63
     * @param CreatePageInterface $createPage
64
     * @param UpdatePageInterface $updatePage
65
     * @param CurrentPageResolverInterface $currentPageResolver
66
     * @param NotificationCheckerInterface $notificationChecker
67
     */
68
    public function __construct(
69
        SharedStorageInterface $sharedStorage,
70
        IndexPageInterface $indexPage,
71
        CreatePageInterface $createPage,
72
        UpdatePageInterface $updatePage,
73
        CurrentPageResolverInterface $currentPageResolver,
74
        NotificationCheckerInterface $notificationChecker
75
    ) {
76
        $this->sharedStorage = $sharedStorage;
77
        $this->indexPage = $indexPage;
78
        $this->createPage = $createPage;
79
        $this->updatePage = $updatePage;
80
        $this->currentPageResolver = $currentPageResolver;
81
        $this->notificationChecker = $notificationChecker;
82
    }
83
84
    /**
85
     * @Given I want to create a new promotion
86
     */
87
    public function iWantToCreateANewPromotion()
88
    {
89
        $this->createPage->open();
90
    }
91
92
    /**
93
     * @When I specify its code as :code
94
     * @When I do not specify its code
95
     */
96
    public function iSpecifyItsCodeAs($code = null)
97
    {
98
        $this->createPage->specifyCode($code);
99
    }
100
101
    /**
102
     * @When I name it :name
103
     * @When I do not name it
104
     * @When I remove its name
105
     */
106
    public function iNameIt($name = null)
107
    {
108
        $this->createPage->nameIt($name);
109
    }
110
111
    /**
112
     * @Then the :promotionName promotion should appear in the registry
113
     * @Then this promotion should still be named :promotionName
114
     * @Then promotion :promotionName should still exist in the registry
115
     */
116
    public function thePromotionShouldAppearInTheRegistry($promotionName)
117
    {
118
        $this->indexPage->open();
119
120
        Assert::true(
121
            $this->indexPage->isSingleResourceOnPage(['name' => $promotionName]),
122
            sprintf('Promotion with name %s has not been found.', $promotionName)
123
        );
124
    }
125
126
    /**
127
     * @When I add it
128
     * @When I try to add it
129
     */
130
    public function iAddIt()
131
    {
132
        $this->createPage->create();
133
    }
134
135
    /**
136
     * @Given I add the "Contains taxon" rule configured with :count :taxonName
137
     */
138
    public function iAddTheContainsTaxonRuleConfiguredWith($count, $taxonName)
139
    {
140
        $this->createPage->addRule('Contains taxon');
141
        $this->createPage->selectRuleOption('Taxon', $taxonName);
142
        $this->createPage->fillRuleOption('Count', $count);
143
    }
144
145
    /**
146
     * @Given I add the "Taxon" rule configured with :firstTaxon
147
     * @Given I add the "Taxon" rule configured with :firstTaxon and :secondTaxon
148
     */
149
    public function iAddTheTaxonRuleConfiguredWith($firstTaxon, $secondTaxon = null)
150
    {
151
        $this->createPage->addRule('Taxon');
152
        $this->createPage->selectRuleOption('Taxons', $firstTaxon, true);
153
154
        if (null !== $secondTaxon) {
155
            $this->createPage->selectRuleOption('Taxons', $secondTaxon, true);
156
        }
157
    }
158
159
    /**
160
     * @Given I add the "Total of items from taxon" rule configured with :count :taxonName
161
     */
162
    public function iAddTheRuleConfiguredWith($count, $taxonName)
163
    {
164
        $this->createPage->addRule('Total of items from taxon');
165
        $this->createPage->selectRuleOption('Taxon', $taxonName);
166
        $this->createPage->fillRuleOption('Amount', $count);
167
    }
168
169
    /**
170
     * @Given I add the "Order fixed discount" action configured with €:amount
171
     */
172
    public function stepDefinition($amount)
173
    {
174
        $this->createPage->addAction('Order fixed discount');
175
        $this->createPage->fillActionOption('Amount', $amount);
176
    }
177
178
    /**
179
     * @Then I should be notified that :element is required
180
     */
181
    public function iShouldBeNotifiedThatIsRequired($element)
182
    {
183
        $this->assertFieldValidationMessage($element, sprintf('Please enter promotion %s.', $element));
184
    }
185
186
    /**
187
     * @Then I should be notified that promotion with this code already exists
188
     */
189
    public function iShouldBeNotifiedThatPromotionWithThisCodeAlreadyExists()
190
    {
191
        Assert::true(
192
            $this->createPage->checkValidationMessageFor('code', 'The promotion with given code already exists.'),
193
            'Unique code violation message should appear on page, but it does not.'
194
        );
195
    }
196
197
    /**
198
     * @Then promotion with :element :name should not be added
199
     */
200
    public function promotionWithElementValueShouldNotBeAdded($element, $name)
201
    {
202
        $this->indexPage->open();
203
204
        Assert::false(
205
            $this->indexPage->isSingleResourceOnPage([$element => $name]),
206
            sprintf('Promotion with %s "%s" has been created, but it should not.', $element, $name)
207
        );
208
    }
209
210
    /**
211
     * @Then there should still be only one promotion with :element :value
212
     */
213
    public function thereShouldStillBeOnlyOnePromotionWith($element, $value)
214
    {
215
        $this->indexPage->open();
216
217
        Assert::true(
218
            $this->indexPage->isSingleResourceOnPage([$element => $value]),
219
            sprintf('Promotion with %s "%s" cannot be found.', $element, $value)
220
        );
221
    }
222
223
    /**
224
     * @When I set its usage limit to :usageLimit
225
     */
226
    public function iSetItsUsageLimitTo($usageLimit)
227
    {
228
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm($this->createPage, $this->updatePage);
229
230
        $currentPage->fillUsageLimit($usageLimit);
231
    }
232
233
    /**
234
     * @Then the :promotion promotion should be available to be used only :usageLimit times
235
     */
236
    public function thePromotionShouldBeAvailableToUseOnlyTimes(PromotionInterface $promotion, $usageLimit)
237
    {
238
        $this->iWantToModifyAPromotion($promotion);
239
240
        Assert::true(
241
            $this->updatePage->hasResourceValues(['usage_limit' => $usageLimit]),
242
            sprintf('Promotion %s does not have usage limit set to %s.', $promotion->getName(), $usageLimit)
243
        );
244
    }
245
246
    /**
247
     * @When I make it exclusive
248
     */
249
    public function iMakeItExclusive()
250
    {
251
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm($this->createPage, $this->updatePage);
252
253
        $currentPage->makeExclusive();
254
    }
255
256
    /**
257
     * @Then the :promotion promotion should be exclusive
258
     */
259
    public function thePromotionShouldBeExclusive(PromotionInterface $promotion)
260
    {
261
        $this->assertIfFieldIsTrue($promotion, 'exclusive');
262
    }
263
264
    /**
265
     * @When I make it coupon based
266
     */
267
    public function iMakeItCouponBased()
268
    {
269
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm($this->createPage, $this->updatePage);
270
271
        $currentPage->checkCouponBased();
272
    }
273
274
    /**
275
     * @Then the :promotion promotion should be coupon based
276
     */
277
    public function thePromotionShouldBeCouponBased(PromotionInterface $promotion)
278
    {
279
        $this->assertIfFieldIsTrue($promotion, 'coupon_based');
280
    }
281
282
    /**
283
     * @When I make it applicable for the :channelName channel
284
     */
285
    public function iMakeItApplicableForTheChannel($channelName)
286
    {
287
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm($this->createPage, $this->updatePage);
288
289
        $currentPage->checkChannel($channelName);
290
    }
291
292
    /**
293
     * @Then the :promotion promotion should be applicable for the :channelName channel
294
     */
295
    public function thePromotionShouldBeApplicatableForTheChannel(PromotionInterface $promotion, $channelName)
296
    {
297
        $this->iWantToModifyAPromotion($promotion);
298
299
        Assert::true(
300
            $this->updatePage->checkChannelsState($channelName),
301
            sprintf('Promotion %s is not %s, but it should be.', $promotion->getName(), $channelName)
302
        );
303
    }
304
305
    /**
306
     * @Given I want to modify a :promotion promotion
307
     * @Given /^I want to modify (this promotion)$/
308
     */
309
    public function iWantToModifyAPromotion(PromotionInterface $promotion)
310
    {
311
        $this->updatePage->open(['id' => $promotion->getId()]);
312
    }
313
314
    /**
315
     * @Then the code field should be disabled
316
     */
317
    public function theCodeFieldShouldBeDisabled()
318
    {
319
        Assert::true(
320
            $this->updatePage->isCodeDisabled(),
321
            'Code should be immutable, but it does not.'
322
        );
323
    }
324
325
    /**
326
     * @When I save my changes
327
     * @When I try to save my changes
328
     */
329
    public function iSaveMyChanges()
330
    {
331
        $this->updatePage->saveChanges();
332
    }
333
334
    /**
335
     * @When /^I delete a ("([^"]+)" promotion)$/
336
     * @When /^I try to delete a ("([^"]+)" promotion)$/
337
     */
338
    public function iDeletePromotion(PromotionInterface $promotion)
339
    {
340
        $this->sharedStorage->set('promotion', $promotion);
341
342
        $this->indexPage->open();
343
        $this->indexPage->deleteResourceOnPage(['name' => $promotion->getName()]);
344
    }
345
346
    /**
347
     * @Then /^(this promotion) should no longer exist in the promotion registry$/
348
     */
349
    public function promotionShouldNotExistInTheRegistry(PromotionInterface $promotion)
350
    {
351
        $this->indexPage->open();
352
353
        Assert::false(
354
            $this->indexPage->isSingleResourceOnPage(['code' => $promotion->getCode()]),
355
            sprintf('Promotion with code %s exists but should not.', $promotion->getCode())
356
        );
357
    }
358
359
    /**
360
     * @Then I should be notified that it is in use and cannot be deleted
361
     */
362
    public function iShouldBeNotifiedOfFailure()
363
    {
364
        $this->notificationChecker->checkNotification(
365
            "Cannot delete, the promotion is in use.",
366
            NotificationType::failure()
367
        );
368
    }
369
370
    /**
371
     * @When I make it available from :startsDate to :endsDate
372
     */
373
    public function iMakeItAvailableFromTo(\DateTime $startsDate, \DateTime $endsDate)
374
    {
375
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm($this->createPage, $this->updatePage);
376
377
        $currentPage->setStartsAt($startsDate);
378
        $currentPage->setEndsAt($endsDate);
379
    }
380
381
    /**
382
     * @Then the :promotion promotion should be available from :startsDate to :endsDate
383
     */
384
    public function thePromotionShouldBeAvailableFromTo(PromotionInterface $promotion, \DateTime $startsDate, \DateTime $endsDate)
385
    {
386
        $this->iWantToModifyAPromotion($promotion);
387
388
        Assert::true(
389
            $this->updatePage->hasStartsAt($startsDate),
390
            sprintf('Promotion %s should starts at %s, but it isn\'t.', $promotion->getName(), date('D, d M Y H:i:s', $startsDate->getTimestamp()))
391
        );
392
393
        Assert::true(
394
            $this->updatePage->hasEndsAt($endsDate),
395
            sprintf('Promotion %s should ends at %s, but it isn\'t.', $promotion->getName(), date('D, d M Y H:i:s', $endsDate->getTimestamp()))
396
        );
397
    }
398
399
    /**
400
     * @Then I should be notified that promotion cannot end before it start
401
     */
402
    public function iShouldBeNotifiedThatPromotionCannotEndBeforeItsEvenStart()
403
    {
404
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm($this->createPage, $this->updatePage);
405
406
        Assert::true(
407
            $currentPage->checkValidationMessageFor('ends_at', 'End date cannot be set prior start date.'),
408
            'Start date was set after ends date, but it should not be possible.'
409
        );
410
    }
411
412
    /**
413
     * @param string $element
414
     * @param string $expectedMessage
415
     */
416
    private function assertFieldValidationMessage($element, $expectedMessage)
417
    {
418
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm($this->createPage, $this->updatePage);
419
420
        Assert::true(
421
            $currentPage->checkValidationMessageFor($element, $expectedMessage),
422
            sprintf('Promotion %s should be required.', $element)
423
        );
424
    }
425
426
    /**
427
     * @param PromotionInterface $promotion
428
     * @param string $field
429
     */
430
    private function assertIfFieldIsTrue(PromotionInterface $promotion, $field)
431
    {
432
        $this->iWantToModifyAPromotion($promotion);
433
434
        Assert::true(
435
            $this->updatePage->hasResourceValues([$field => 1]),
436
            sprintf('Promotion %s is not %s, but it should be.', $promotion->getName(), str_replace('_', ' ', $field))
437
        );
438
    }
439
}
440