Completed
Push — master ( 99e244...bf2eb3 )
by Paweł
204:07 queued 189:10
created

iShouldBeNotifiedThatCodeLengthIsRequired()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
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\PromotionCoupon\CreatePageInterface;
17
use Sylius\Behat\Page\Admin\Crud\IndexPageInterface;
18
use Sylius\Behat\Page\Admin\PromotionCoupon\GeneratePageInterface;
19
use Sylius\Behat\Page\Admin\PromotionCoupon\UpdatePageInterface;
20
use Sylius\Behat\Service\CurrentPageResolverInterface;
21
use Sylius\Behat\Service\NotificationCheckerInterface;
22
use Sylius\Component\Core\Model\CouponInterface;
23
use Sylius\Component\Promotion\Model\PromotionInterface;
24
use Webmozart\Assert\Assert;
25
26
/**
27
 * @author Arkadiusz Krakowiak <[email protected]>
28
 */
29
final class ManagingPromotionCouponsContext implements Context
30
{
31
    /**
32
     * @var CreatePageInterface
33
     */
34
    private $createPage;
35
36
    /**
37
     * @var GeneratePageInterface
38
     */
39
    private $generatePage;
40
41
    /**
42
     * @var IndexPageInterface
43
     */
44
    private $indexPage;
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
     * @param CreatePageInterface $createPage
63
     * @param GeneratePageInterface $generatePage
64
     * @param IndexPageInterface $indexPage
65
     * @param UpdatePageInterface $updatePage
66
     * @param CurrentPageResolverInterface $currentPageResolver
67
     * @param NotificationCheckerInterface $notificationChecker
68
     */
69
    public function __construct(
70
        CreatePageInterface $createPage,
71
        GeneratePageInterface $generatePage,
72
        IndexPageInterface $indexPage,
73
        UpdatePageInterface $updatePage,
74
        CurrentPageResolverInterface $currentPageResolver,
75
        NotificationCheckerInterface $notificationChecker
76
    ) {
77
        $this->createPage = $createPage;
78
        $this->generatePage = $generatePage;
79
        $this->indexPage = $indexPage;
80
        $this->updatePage = $updatePage;
81
        $this->currentPageResolver = $currentPageResolver;
82
        $this->notificationChecker = $notificationChecker;
83
    }
84
85
    /**
86
     * @Given /^I want to view all coupons of (this promotion)$/
87
     * @Given /^I want to view 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(CouponInterface $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(\DateTime $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(\DateTime $date)
197
    {
198
        $this->createPage->setExpiresAt($date);
199
    }
200
201
    /**
202
     * @When I change expires date to :date
203
     */
204
    public function iChangeExpiresDateTo(\DateTime $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(CouponInterface $coupon, PromotionInterface $promotion)
240
    {
241
        $this->indexPage->open(['promotionId' => $promotion->getId()]);
242
        $this->indexPage->deleteResourceOnPage(['code' => $coupon->getCode()]);
243
    }
244
245
    /**
246
     * @Then /^there should be (\d+) coupon related to (this promotion)$/
247
     */
248
    public function thereShouldBeCouponRelatedTo($number, PromotionInterface $promotion)
249
    {
250
        $this->indexPage->open(['promotionId' => $promotion->getId()]);
251
252
        Assert::eq(
253
            $number,
254
            $this->indexPage->countItems(),
255
            'There should be %s coupons but is %s'
256
        );
257
    }
258
259
    /**
260
     * @Then /^there should be coupon with code "([^"]+)"$/
261
     */
262
    public function thereShouldBeCouponWithCode($code)
263
    {
264
        Assert::true(
265
            $this->indexPage->isSingleResourceOnPage(['code' => $code]),
266
            sprintf('There should be coupon with code %s but it is not.', $code)
267
        );
268
    }
269
270
    /**
271
     * @Then this coupon should be valid until :date
272
     */
273
    public function thisCouponShouldBeValidUntil(\DateTime $date)
274
    {
275
        Assert::true(
276
            $this->indexPage->isSingleResourceOnPage(['Expires at' => date('d-m-Y', $date->getTimestamp())]),
277
            sprintf('There should be coupon with expires date %s', date('d-m-Y', $date->getTimestamp()))
278
        );
279
    }
280
281
    /**
282
     * @Then /^this coupon should have (\d+) usage limit$/
283
     */
284
    public function thisCouponShouldHaveUsageLimit($limit)
285
    {
286
        Assert::true(
287
            $this->indexPage->isSingleResourceOnPage(['Usage limit' => $limit]),
288
            sprintf('There should be coupon with %s usage limit', $limit)
289
        );
290
    }
291
292
    /**
293
     * @Then /^this coupon should have (\d+) per customer usage limit$/
294
     */
295
    public function thisCouponShouldHavePerCustomerUsageLimit($limit)
296
    {
297
        Assert::true(
298
            $this->indexPage->isSingleResourceOnPage(['Per customer usage limit' => $limit]),
299
            sprintf('There should be coupon with %s per customer usage limit', $limit)
300
        );
301
    }
302
303
    /**
304
     * @Then the code field should be disabled
305
     */
306
    public function theCodeFieldShouldBeDisabled()
307
    {
308
        Assert::true(
309
            $this->updatePage->isCodeDisabled(),
310
            'Code field should be disabled'
311
        );
312
    }
313
314
    /**
315
     * @Then /^I should be notified that coupon with this code already exists$/
316
     */
317
    public function iShouldBeNotifiedThatCouponWithThisCodeAlreadyExists()
318
    {
319
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
320
321
        Assert::true(
322
            $currentPage->checkValidationMessageFor('code', 'This coupon already exists.'),
323
            'Unique code violation message should appear on page, but it does not.'
324
        );
325
    }
326
327
    /**
328
     * @Then I should be notified that :element is required
329
     */
330
    public function iShouldBeNotifiedThatIsRequired($element)
331
    {
332
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
333
334
        Assert::true(
335
            $currentPage->checkValidationMessageFor($element, sprintf('Please enter coupon %s.', $element)),
336
            sprintf('I should be notified that coupon %s should be required.', $element)
337
        );
338
    }
339
340
    /**
341
     * @Then I should be notified that generate amount is required
342
     */
343
    public function iShouldBeNotifiedThatGenerateAmountIsRequired()
344
    {
345
        Assert::true(
346
            $this->generatePage->checkAmountValidation('Please enter amount of coupons to generate.'),
347
            'Generate amount violation message should appear on page, but it does not.'
348
        );
349
    }
350
351
    /**
352
     * @Then I should be notified that generate code length is required
353
     */
354
    public function iShouldBeNotifiedThatCodeLengthIsRequired()
355
    {
356
        Assert::true(
357
            $this->generatePage->checkCodeLengthValidation('Please enter coupon code length.'),
358
            'Generate code length violation message should appear on page, but it does not.'
359
        );
360
    }
361
362
    /**
363
     * @Then /^there should still be only one coupon with code "([^"]+)" related to (this promotion)$/
364
     */
365
    public function thereShouldStillBeOnlyOneCouponWithCodeRelatedTo($code, PromotionInterface $promotion)
366
    {
367
        $this->indexPage->open(['promotionId' => $promotion->getId()]);
368
369
        Assert::true(
370
            $this->indexPage->isSingleResourceOnPage(['code' => $code]),
371
            sprintf('There is no coupon with code %s.', $code)
372
        );
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
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
381
382
        Assert::true(
383
            $currentPage->checkValidationMessageFor('usage_limit', 'Coupon usage limit must be at least 1.'),
384
            'Min usage limit violation message should appear on page, but it did not.'
385
        );
386
    }
387
388
    /**
389
     * @Then /^(this coupon) should no longer exist in the coupon registry$/
390
     */
391
    public function couponShouldNotExistInTheRegistry(CouponInterface $coupon)
392
    {
393
        Assert::false(
394
            $this->indexPage->isSingleResourceOnPage(['code' => $coupon->getCode()]),
395
            sprintf('Coupon with code %s should not exist.', $coupon->getCode())
396
        );
397
    }
398
399
    /**
400
     * @Then I should be notified that it has been successfully generated
401
     */
402
    public function iShouldBeNotifiedThatItHasBeenSuccessfullyGenerated()
403
    {
404
        $this->notificationChecker->checkNotification('Success Promotion coupons have been successfully generated.', NotificationType::success());
405
    }
406
407
    /**
408
     * @Then I should be notified that it is in use and cannot be deleted
409
     */
410
    public function iShouldBeNotifiedOfFailure()
411
    {
412
        $this->notificationChecker->checkNotification(
413
            "Error Cannot delete, the promotion coupon is in use.",
414
            NotificationType::failure()
415
        );
416
    }
417
418
    /**
419
     * @Then /^(this coupon) should still exist in the registry$/
420
     */
421
    public function couponShouldStillExistInTheRegistry(CouponInterface $coupon)
422
    {
423
        Assert::true(
424
            $this->indexPage->isSingleResourceOnPage(['code' => $coupon->getCode()]),
425
            sprintf('Coupon with code %s should exist.', $coupon->getCode())
426
        );
427
    }
428
429
    /**
430
     * @Then /^I should be notified that generating (\d+) coupons with code length equal to (\d+) is not possible$/
431
     */
432
    public function iShouldBeNotifiedThatGeneratingCouponsWithCodeLengthIsNotPossible($amount, $codeLength)
433
    {
434
        $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);
435
436
        Assert::true(
437
            $this->generatePage->checkGenerationValidation($message),
438
            'Generate violation message should appear on page, but it does not.'
439
        );
440
    }
441
}
442