Completed
Push — master ( 78fd01...e243d3 )
by Paweł
11:06
created

iShouldSeeTheCouponInTheList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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\Crud\IndexPageInterface;
19
use Sylius\Behat\Page\Admin\PromotionCoupon\CreatePageInterface;
20
use Sylius\Behat\Page\Admin\PromotionCoupon\GeneratePageInterface;
21
use Sylius\Behat\Page\Admin\PromotionCoupon\UpdatePageInterface;
22
use Sylius\Behat\Service\NotificationCheckerInterface;
23
use Sylius\Behat\Service\Resolver\CurrentPageResolverInterface;
24
use Sylius\Component\Core\Model\PromotionCouponInterface;
25
use Sylius\Component\Promotion\Model\PromotionInterface;
26
use Webmozart\Assert\Assert;
27
28
final class ManagingPromotionCouponsContext implements Context
29
{
30
    /**
31
     * @var CreatePageInterface
32
     */
33
    private $createPage;
34
35
    /**
36
     * @var GeneratePageInterface
37
     */
38
    private $generatePage;
39
40
    /**
41
     * @var IndexPageInterface
42
     */
43
    private $indexPage;
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 CreatePageInterface $createPage
62
     * @param GeneratePageInterface $generatePage
63
     * @param IndexPageInterface $indexPage
64
     * @param UpdatePageInterface $updatePage
65
     * @param CurrentPageResolverInterface $currentPageResolver
66
     * @param NotificationCheckerInterface $notificationChecker
67
     */
68
    public function __construct(
69
        CreatePageInterface $createPage,
70
        GeneratePageInterface $generatePage,
71
        IndexPageInterface $indexPage,
72
        UpdatePageInterface $updatePage,
73
        CurrentPageResolverInterface $currentPageResolver,
74
        NotificationCheckerInterface $notificationChecker
75
    ) {
76
        $this->createPage = $createPage;
77
        $this->generatePage = $generatePage;
78
        $this->indexPage = $indexPage;
79
        $this->updatePage = $updatePage;
80
        $this->currentPageResolver = $currentPageResolver;
81
        $this->notificationChecker = $notificationChecker;
82
    }
83
84
    /**
85
     * @Given /^I browse coupons of (this promotion)$/
86
     * @Given /^I want to view all coupons of (this promotion)$/
87
     * @When /^I browse all coupons of ("[^"]+" promotion)$/
88
     */
89
    public function iWantToViewAllCouponsOfThisPromotion(PromotionInterface $promotion)
90
    {
91
        $this->indexPage->open(['promotionId' => $promotion->getId()]);
92
    }
93
94
    /**
95
     * @Given /^I want to create a new coupon for (this promotion)$/
96
     */
97
    public function iWantToCreateANewCouponForThisPromotion(PromotionInterface $promotion)
98
    {
99
        $this->createPage->open(['promotionId' => $promotion->getId()]);
100
    }
101
102
    /**
103
     * @Given /^I want to modify the ("[^"]+" coupon) for (this promotion)$/
104
     */
105
    public function iWantToModifyTheCoupon(PromotionCouponInterface $coupon, PromotionInterface $promotion)
106
    {
107
        $this->updatePage->open(['id' => $coupon->getId(), 'promotionId' => $promotion->getId()]);
108
    }
109
110
    /**
111
     * @Given /^I want to generate a new coupons for (this promotion)$/
112
     */
113
    public function iWantToGenerateANewCouponsForThisPromotion(PromotionInterface $promotion)
114
    {
115
        $this->generatePage->open(['promotionId' => $promotion->getId()]);
116
    }
117
118
    /**
119
     * @When /^I specify its code length as (\d+)$/
120
     * @When I do not specify its code length
121
     */
122
    public function iSpecifyItsCodeLengthAs($codeLength = null)
123
    {
124
        $this->generatePage->specifyCodeLength($codeLength);
125
    }
126
127
    /**
128
     * @When /^I limit generated coupons usage to (\d+) times$/
129
     */
130
    public function iSetGeneratedCouponsUsageLimitTo($limit)
131
    {
132
        $this->generatePage->setUsageLimit($limit);
133
    }
134
135
    /**
136
     * @When I make generated coupons valid until :date
137
     */
138
    public function iMakeGeneratedCouponsValidUntil(\DateTimeInterface $date)
139
    {
140
        $this->generatePage->setExpiresAt($date);
141
    }
142
143
    /**
144
     * @When I specify its code as :code
145
     * @When I do not specify its code
146
     */
147
    public function iSpecifyItsCodeAs($code = null)
148
    {
149
        $this->createPage->specifyCode($code);
150
    }
151
152
    /**
153
     * @When I limit its usage to :limit times
154
     */
155
    public function iLimitItsUsageLimitTo($limit)
156
    {
157
        $this->createPage->setUsageLimit($limit);
158
    }
159
160
    /**
161
     * @When I change its usage limit to :limit
162
     */
163
    public function iChangeItsUsageLimitTo($limit)
164
    {
165
        $this->updatePage->setUsageLimit($limit);
166
    }
167
168
    /**
169
     * @When I specify its amount as :amount
170
     * @When I do not specify its amount
171
     */
172
    public function iSpecifyItsAmountAs($amount = null)
173
    {
174
        $this->generatePage->specifyAmount($amount);
175
    }
176
177
    /**
178
     * @When I limit its per customer usage to :limit times
179
     */
180
    public function iLimitItsPerCustomerUsageLimitTo($limit)
181
    {
182
        $this->createPage->setCustomerUsageLimit($limit);
183
    }
184
185
    /**
186
     * @When I change its per customer usage limit to :limit
187
     */
188
    public function iChangeItsPerCustomerUsageLimitTo($limit)
189
    {
190
        $this->updatePage->setCustomerUsageLimit($limit);
191
    }
192
193
    /**
194
     * @When I make it valid until :date
195
     */
196
    public function iMakeItValidUntil(\DateTimeInterface $date)
197
    {
198
        $this->createPage->setExpiresAt($date);
199
    }
200
201
    /**
202
     * @When I change expires date to :date
203
     */
204
    public function iChangeExpiresDateTo(\DateTimeInterface $date)
205
    {
206
        $this->updatePage->setExpiresAt($date);
207
    }
208
209
    /**
210
     * @When I add it
211
     * @When I try to add it
212
     */
213
    public function iAddIt()
214
    {
215
        $this->createPage->create();
216
    }
217
218
    /**
219
     * @When I save my changes
220
     */
221
    public function iSaveMyChanges()
222
    {
223
        $this->updatePage->saveChanges();
224
    }
225
226
    /**
227
     * @When I generate it
228
     * @When I try to generate it
229
     */
230
    public function iGenerateIt()
231
    {
232
        $this->generatePage->generate();
233
    }
234
235
    /**
236
     * @When /^I delete ("[^"]+" coupon) related to (this promotion)$/
237
     * @When /^I try to delete ("[^"]+" coupon) related to (this promotion)$/
238
     */
239
    public function iDeleteCouponRelatedToThisPromotion(PromotionCouponInterface $coupon, PromotionInterface $promotion)
240
    {
241
        $this->indexPage->open(['promotionId' => $promotion->getId()]);
242
        $this->indexPage->deleteResourceOnPage(['code' => $coupon->getCode()]);
243
    }
244
245
    /**
246
     * @When I check (also) the :couponCode coupon
247
     */
248
    public function iCheckTheCoupon(string $couponCode): void
249
    {
250
        $this->indexPage->checkResourceOnPage(['code' => $couponCode]);
251
    }
252
253
    /**
254
     * @When I delete them
255
     */
256
    public function iDeleteThem(): void
257
    {
258
        $this->indexPage->bulkDelete();
259
    }
260
261
    /**
262
     * @Then /^there should be (\d+) coupon related to (this promotion)$/
263
     */
264
    public function thereShouldBeCouponRelatedTo($number, PromotionInterface $promotion)
265
    {
266
        $this->indexPage->open(['promotionId' => $promotion->getId()]);
267
268
        Assert::same($this->indexPage->countItems(), (int) $number);
269
    }
270
271
    /**
272
     * @Then I should see a single coupon in the list
273
     */
274
    public function iShouldSeeASingleCouponInTheList(): void
275
    {
276
        Assert::same($this->indexPage->countItems(), 1);
277
    }
278
279
    /**
280
     * @Then /^there should be coupon with code "([^"]+)"$/
281
     */
282
    public function thereShouldBeCouponWithCode($code)
283
    {
284
        Assert::true($this->indexPage->isSingleResourceOnPage(['code' => $code]));
285
    }
286
287
    /**
288
     * @Then this coupon should be valid until :date
289
     */
290
    public function thisCouponShouldBeValidUntil(\DateTimeInterface $date)
291
    {
292
        Assert::true($this->indexPage->isSingleResourceOnPage(['expiresAt' => date('d-m-Y', $date->getTimestamp())]));
293
    }
294
295
    /**
296
     * @Then /^this coupon should have (\d+) usage limit$/
297
     */
298
    public function thisCouponShouldHaveUsageLimit($limit)
299
    {
300
        Assert::true($this->indexPage->isSingleResourceOnPage(['usageLimit' => $limit]));
301
    }
302
303
    /**
304
     * @Then /^("[^"]+" coupon) should be used (\d+) time(?:|s)$/
305
     */
306
    public function couponShouldHaveUsageLimit(PromotionCouponInterface $promotionCoupon, $used)
307
    {
308
        Assert::true($this->indexPage->isSingleResourceOnPage(['code' => $promotionCoupon->getCode(), 'used' => $used]));
309
    }
310
311
    /**
312
     * @Then /^this coupon should have (\d+) per customer usage limit$/
313
     */
314
    public function thisCouponShouldHavePerCustomerUsageLimit($limit)
315
    {
316
        Assert::true($this->indexPage->isSingleResourceOnPage(['perCustomerUsageLimit' => $limit]));
317
    }
318
319
    /**
320
     * @Then the code field should be disabled
321
     */
322
    public function theCodeFieldShouldBeDisabled()
323
    {
324
        Assert::true($this->updatePage->isCodeDisabled());
325
    }
326
327
    /**
328
     * @Then /^I should be notified that coupon with this code already exists$/
329
     */
330
    public function iShouldBeNotifiedThatCouponWithThisCodeAlreadyExists()
331
    {
332
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
333
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
334
335
        Assert::same($currentPage->getValidationMessage('code'), 'This coupon already exists.');
336
    }
337
338
    /**
339
     * @Then I should be notified that :element is required
340
     */
341
    public function iShouldBeNotifiedThatIsRequired($element)
342
    {
343
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
344
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
345
346
        Assert::same($currentPage->getValidationMessage($element), sprintf('Please enter coupon %s.', $element));
347
    }
348
349
    /**
350
     * @Then I should be notified that generate amount is required
351
     */
352
    public function iShouldBeNotifiedThatGenerateAmountIsRequired()
353
    {
354
        Assert::true($this->generatePage->checkAmountValidation('Please enter amount of coupons to generate.'));
355
    }
356
357
    /**
358
     * @Then I should be notified that generate code length is required
359
     */
360
    public function iShouldBeNotifiedThatCodeLengthIsRequired()
361
    {
362
        Assert::true($this->generatePage->checkCodeLengthValidation('Please enter coupon code length.'));
363
    }
364
365
    /**
366
     * @Then /^there should still be only one coupon with code "([^"]+)" related to (this promotion)$/
367
     */
368
    public function thereShouldStillBeOnlyOneCouponWithCodeRelatedTo($code, PromotionInterface $promotion)
369
    {
370
        $this->indexPage->open(['promotionId' => $promotion->getId()]);
371
372
        Assert::true($this->indexPage->isSingleResourceOnPage(['code' => $code]));
373
    }
374
375
    /**
376
     * @Then I should be notified that coupon usage limit must be at least one
377
     */
378
    public function iShouldBeNotifiedThatCouponUsageLimitMustBeAtLeast()
379
    {
380
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
381
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
382
383
        Assert::same($currentPage->getValidationMessage('usage_limit'), 'Coupon usage limit must be at least 1.');
384
    }
385
386
    /**
387
     * @Then /^(this coupon) should no longer exist in the coupon registry$/
388
     */
389
    public function couponShouldNotExistInTheRegistry(PromotionCouponInterface $coupon)
390
    {
391
        Assert::false($this->indexPage->isSingleResourceOnPage(['code' => $coupon->getCode()]));
392
    }
393
394
    /**
395
     * @Then I should be notified that it has been successfully generated
396
     */
397
    public function iShouldBeNotifiedThatItHasBeenSuccessfullyGenerated()
398
    {
399
        $this->notificationChecker->checkNotification('Success Promotion coupons have been successfully generated.', NotificationType::success());
400
    }
401
402
    /**
403
     * @Then I should be notified that it is in use and cannot be deleted
404
     */
405
    public function iShouldBeNotifiedOfFailure()
406
    {
407
        $this->notificationChecker->checkNotification(
408
            'Error Cannot delete, the promotion coupon is in use.',
409
            NotificationType::failure()
410
        );
411
    }
412
413
    /**
414
     * @Then /^(this coupon) should still exist in the registry$/
415
     */
416
    public function couponShouldStillExistInTheRegistry(PromotionCouponInterface $coupon)
417
    {
418
        Assert::true($this->indexPage->isSingleResourceOnPage(['code' => $coupon->getCode()]));
419
    }
420
421
    /**
422
     * @Then /^I should be notified that generating (\d+) coupons with code length equal to (\d+) is not possible$/
423
     */
424
    public function iShouldBeNotifiedThatGeneratingCouponsWithCodeLengthIsNotPossible($amount, $codeLength)
425
    {
426
        $message = sprintf('Invalid coupons code length or coupons amount. It is not possible to generate %d unique coupons with code length equals %d. Possible generate amount is 8.', $amount, $codeLength);
427
428
        Assert::true($this->generatePage->checkGenerationValidation($message));
429
    }
430
431
    /**
432
     * @Then I should see the coupon :couponCode in the list
433
     */
434
    public function iShouldSeeTheCouponInTheList(string $couponCode): void
435
    {
436
        Assert::true($this->indexPage->isSingleResourceOnPage(['code' => $couponCode]));
437
    }
438
}
439