Completed
Push — master ( 269470...f117d4 )
by Paweł
08:47
created

iArchiveTheShippingMethod()   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\ShippingMethod\IndexPageInterface;
17
use Sylius\Behat\Page\Admin\ShippingMethod\CreatePageInterface;
18
use Sylius\Behat\Page\Admin\ShippingMethod\UpdatePageInterface;
19
use Sylius\Behat\Service\NotificationCheckerInterface;
20
use Sylius\Behat\Service\Resolver\CurrentPageResolverInterface;
21
use Sylius\Component\Channel\Model\ChannelInterface;
22
use Sylius\Component\Core\Model\ShippingMethodInterface;
23
use Webmozart\Assert\Assert;
24
25
/**
26
 * @author Jan Góralski <[email protected]>
27
 * @author Łukasz Chruściel <[email protected]>
28
 */
29
final class ManagingShippingMethodsContext implements Context
30
{
31
    /**
32
     * @var IndexPageInterface
33
     */
34
    private $indexPage;
35
36
    /**
37
     * @var CreatePageInterface
38
     */
39
    private $createPage;
40
41
    /**
42
     * @var UpdatePageInterface
43
     */
44
    private $updatePage;
45
46
    /**
47
     * @var CurrentPageResolverInterface
48
     */
49
    private $currentPageResolver;
50
51
    /**
52
     * @var NotificationCheckerInterface
53
     */
54
    private $notificationChecker;
55
56
    /**
57
     * @param IndexPageInterface $indexPage
58
     * @param CreatePageInterface $createPage
59
     * @param UpdatePageInterface $updatePage
60
     * @param CurrentPageResolverInterface $currentPageResolver
61
     * @param NotificationCheckerInterface $notificationChecker
62
     */
63
    public function __construct(
64
        IndexPageInterface $indexPage,
65
        CreatePageInterface $createPage,
66
        UpdatePageInterface $updatePage,
67
        CurrentPageResolverInterface $currentPageResolver,
68
        NotificationCheckerInterface $notificationChecker
69
    ) {
70
        $this->indexPage = $indexPage;
71
        $this->createPage = $createPage;
72
        $this->updatePage = $updatePage;
73
        $this->currentPageResolver = $currentPageResolver;
74
        $this->notificationChecker = $notificationChecker;
75
    }
76
77
    /**
78
     * @When I want to create a new shipping method
79
     */
80
    public function iWantToCreateANewShippingMethod()
81
    {
82
        $this->createPage->open();
83
    }
84
85
    /**
86
     * @When I specify its code as :code
87
     * @When I do not specify its code
88
     */
89
    public function iSpecifyItsCodeAs($code = null)
90
    {
91
        $this->createPage->specifyCode($code);
92
    }
93
94
    /**
95
     * @When I specify its position as :position
96
     */
97
    public function iSpecifyItsPositionAs($position = null)
98
    {
99
        $this->createPage->specifyPosition($position);
100
    }
101
102
    /**
103
     * @When I name it :name in :language
104
     * @When I rename it to :name in :language
105
     */
106
    public function iNameItIn($name, $language)
107
    {
108
        $this->createPage->nameIt($name, $language);
109
    }
110
111
    /**
112
     * @When I describe it as :description in :language
113
     */
114
    public function iDescribeItAsIn($description, $language)
115
    {
116
        $this->createPage->describeIt($description, $language);
117
    }
118
119
    /**
120
     * @When I define it for the :zoneName zone
121
     */
122
    public function iDefineItForTheZone($zoneName)
123
    {
124
        $this->createPage->chooseZone($zoneName);
125
    }
126
127
    /**
128
     * @When I specify its amount as :amount for :channel channel
129
     */
130
    public function iSpecifyItsAmountForChannel($amount, ChannelInterface $channel)
131
    {
132
        $this->createPage->specifyAmountForChannel($channel->getCode(), $amount);
133
    }
134
135
    /**
136
     * @When I make it available in channel :channelName
137
     */
138
    public function iMakeItAvailableInChannel($channelName)
139
    {
140
        $this->createPage->checkChannel($channelName);
141
    }
142
143
    /**
144
     * @When I add it
145
     * @When I try to add it
146
     */
147
    public function iAddIt()
148
    {
149
        $this->createPage->create();
150
    }
151
152
    /**
153
     * @When I choose :calculatorName calculator
154
     * @When I do not specify amount for :calculatorName calculator
155
     */
156
    public function iChooseCalculator($calculatorName)
157
    {
158
        $this->createPage->chooseCalculator($calculatorName);
159
    }
160
161
    /**
162
     * @Then the shipping method :shipmentMethod should appear in the registry
163
     * @Then the shipping method :shipmentMethod should be in the registry
164
     */
165
    public function theShipmentMethodShouldAppearInTheRegistry($shipmentMethodName)
166
    {
167
        $this->iWantToBrowseShippingMethods();
168
169
        Assert::true(
170
            $this->indexPage->isSingleResourceOnPage(['name' => $shipmentMethodName]),
171
            sprintf('The shipping method with name %s has not been found.', $shipmentMethodName)
172
        );
173
    }
174
175
    /**
176
     * @Given /^(this shipping method) should still be in the registry$/
177
     */
178
    public function thisShippingMethodShouldStillBeInTheRegistry(ShippingMethodInterface $shippingMethod)
179
    {
180
        $this->theShipmentMethodShouldAppearInTheRegistry($shippingMethod->getName());
181
    }
182
183
    /**
184
     * @Then the shipping method :shippingMethod should be available in channel :channelName
185
     */
186
    public function theShippingMethodShouldBeAvailableInChannel(
187
        ShippingMethodInterface $shippingMethod,
188
        $channelName
189
    ) {
190
        $this->iWantToModifyAShippingMethod($shippingMethod);
191
192
        Assert::true(
193
            $this->updatePage->isAvailableInChannel($channelName),
194
            sprintf('Shipping method should be available in channel "%s" but it does not.', $channelName)
195
        );
196
    }
197
198
    /**
199
     * @Then I should be notified that shipping method with this code already exists
200
     */
201
    public function iShouldBeNotifiedThatShippingMethodWithThisCodeAlreadyExists()
202
    {
203
        Assert::same($this->createPage->getValidationMessage('code'), 'The shipping method with given code already exists.');
204
    }
205
206
    /**
207
     * @Then there should still be only one shipping method with :element :code
208
     */
209
    public function thereShouldStillBeOnlyOneShippingMethodWith($element, $code)
210
    {
211
        $this->iWantToBrowseShippingMethods();
212
213
        Assert::true(
214
            $this->indexPage->isSingleResourceOnPage([$element => $code]),
215
            sprintf('Shipping method with %s %s cannot be found.', $element, $code)
216
        );
217
    }
218
219
    /**
220
     * @Given I want to modify a shipping method :shippingMethod
221
     * @Given /^I want to modify (this shipping method)$/
222
     */
223
    public function iWantToModifyAShippingMethod(ShippingMethodInterface $shippingMethod)
224
    {
225
        $this->updatePage->open(['id' => $shippingMethod->getId()]);
226
    }
227
228
    /**
229
     * @Then the code field should be disabled
230
     */
231
    public function theCodeFieldShouldBeDisabled()
232
    {
233
        Assert::true(
234
            $this->updatePage->isCodeDisabled(),
235
            'Code should be immutable, but it does not.'
236
        );
237
    }
238
239
    /**
240
     * @Then /^(this shipping method) name should be "([^"]+)"$/
241
     * @Then /^(this shipping method) should still be named "([^"]+)"$/
242
     */
243
    public function thisShippingMethodNameShouldBe(ShippingMethodInterface $shippingMethod, $shippingMethodName)
244
    {
245
        $this->iWantToBrowseShippingMethods();
246
247
        Assert::true(
248
            $this->indexPage->isSingleResourceOnPage(
249
                [
250
                    'code' => $shippingMethod->getCode(),
251
                    'name' => $shippingMethodName,
252
                ]
253
            ),
254
            sprintf('Shipping method name %s has not been assigned properly.', $shippingMethodName)
255
        );
256
    }
257
258
    /**
259
     * @When I save my changes
260
     * @When I try to save my changes
261
     */
262
    public function iSaveMyChanges()
263
    {
264
        $this->updatePage->saveChanges();
265
    }
266
267
    /**
268
     * @Then I should be notified that :element is required
269
     */
270
    public function iShouldBeNotifiedThatIsRequired($element)
271
    {
272
        $this->assertFieldValidationMessage($element, sprintf('Please enter shipping method %s.', $element));
273
    }
274
275
    /**
276
     * @Then I should be notified that code needs to contain only specific symbols
277
     */
278
    public function iShouldBeNotifiedThatCodeShouldContain()
279
    {
280
        $this->assertFieldValidationMessage(
281
            'code',
282
            'Shipping method code can only be comprised of letters, numbers, dashes and underscores.'
283
        );
284
    }
285
286
    /**
287
     * @When I archive the :name shipping method
288
     */
289
    public function iArchiveTheShippingMethod($name)
290
    {
291
        $actions = $this->indexPage->getActionsForResource(['name' => $name]);
292
        $actions->pressButton('Archive');
293
    }
294
295
    /**
296
     * @When I restore the :name shipping method
297
     */
298
    public function iRestoreTheShippingMethod($name)
299
    {
300
        $actions = $this->indexPage->getActionsForResource(['name' => $name]);
301
        $actions->pressButton('Restore');
302
    }
303
304
    /**
305
     * @Then I should be viewing non archival shipping methods
306
     */
307
    public function iShouldBeViewingNonArchivalShippingMethods()
308
    {
309
        Assert::false($this->indexPage->isArchivalFilterEnabled());
310
    }
311
312
    /**
313
     * @Then I should see :count shipping methods on the list
314
     */
315
    public function thereShouldBeNoShippingMethodsOnTheList($count)
316
    {
317
        Assert::same(
318
            $this->indexPage->countItems(),
319
            (int) $count,
320
            'There should be %2$d shipping methods on the list, found %d instead.'
321
        );
322
    }
323
324
    /**
325
     * @Then the only shipping method on the list should be :name
326
     */
327
    public function theOnlyShippingMethodOnTheListShouldBe($name)
328
    {
329
        Assert::same(
330
            (int) $this->indexPage->countItems(),
331
            1,
332
            'There should be only one shipping method on the list, found %d instead.'
333
        );
334
        Assert::true(
335
            $this->indexPage->isSingleResourceOnPage(['name' => $name]),
336
            sprintf('Shipping method "%s" was not found on the list.', $name)
337
        );
338
    }
339
340
    /**
341
     * @Then shipping method with :element :name should not be added
342
     */
343
    public function shippingMethodWithElementValueShouldNotBeAdded($element, $name)
344
    {
345
        $this->iWantToBrowseShippingMethods();
346
347
        Assert::false(
348
            $this->indexPage->isSingleResourceOnPage([$element => $name]),
349
            sprintf('Shipping method with %s %s was created, but it should not.', $element, $name)
350
        );
351
    }
352
353
    /**
354
     * @When I do not name it
355
     */
356
    public function iDoNotNameIt()
357
    {
358
        // Intentionally left blank to fulfill context expectation
359
    }
360
361
    /**
362
     * @When I do not specify its zone
363
     */
364
    public function iDoNotSpecifyItsZone()
365
    {
366
        // Intentionally left blank to fulfill context expectation
367
    }
368
369
    /**
370
     * @When I remove its zone
371
     */
372
    public function iRemoveItsZone()
373
    {
374
        $this->updatePage->removeZone();
375
    }
376
377
    /**
378
     * @Then I should be notified that :element has to be selected
379
     */
380
    public function iShouldBeNotifiedThatElementHasToBeSelected($element)
381
    {
382
        $this->assertFieldValidationMessage($element, sprintf('Please select shipping method %s.', $element));
383
    }
384
385
    /**
386
     * @When I remove its name from :language translation
387
     */
388
    public function iRemoveItsNameFromTranslation($language)
389
    {
390
        $this->createPage->nameIt(null, $language);
391
    }
392
393
    /**
394
     * @Given I am browsing shipping methods
395
     * @When I want to browse shipping methods
396
     */
397
    public function iWantToBrowseShippingMethods()
398
    {
399
        $this->indexPage->open();
400
    }
401
402
    /**
403
     * @Given I am browsing archival shipping methods
404
     */
405
    public function iAmBrowsingArchivalShippingMethods()
406
    {
407
        $this->indexPage->open();
408
        $this->indexPage->chooseArchival('Yes');
409
        $this->indexPage->filter();
410
    }
411
412
    /**
413
     * @Given I filter archival shipping methods
414
     */
415
    public function iFilterArchivalShippingMethods()
416
    {
417
        $this->indexPage->chooseArchival('Yes');
418
        $this->indexPage->filter();
419
    }
420
421
    /**
422
     * @Then the first shipping method on the list should have :field :value
423
     */
424
    public function theFirstShippingMethodOnTheListShouldHave($field, $value)
425
    {
426
        $fields = $this->indexPage->getColumnFields($field);
427
        $actualValue = reset($fields);
428
429
        Assert::same(
430
            $actualValue,
431
            $value,
432
            sprintf('Expected first shipping method\'s %s to be "%s", but it is "%s".', $field, $value, $actualValue)
433
        );
434
    }
435
436
    /**
437
     * @Then the last shipping method on the list should have :field :value
438
     */
439
    public function theLastShippingMethodOnTheListShouldHave($field, $value)
440
    {
441
        $fields = $this->indexPage->getColumnFields($field);
442
        $actualValue = end($fields);
443
444
        Assert::same(
445
            $actualValue,
446
            $value,
447
            sprintf('Expected last shipping method\'s %s to be "%s", but it is "%s".', $field, $value, $actualValue)
448
        );
449
    }
450
451
    /**
452
     * @When I switch the way shipping methods are sorted by :field
453
     * @When I start sorting shipping methods by :field
454
     * @Given the shipping methods are already sorted by :field
455
     */
456
    public function iSortShippingMethodsBy($field)
457
    {
458
        $this->indexPage->sortBy($field);
459
    }
460
461
    /**
462
     * @Then I should see :numberOfShippingMethods shipping methods in the list
463
     */
464
    public function iShouldSeeShippingMethodsInTheList($numberOfShippingMethods)
465
    {
466
        $foundRows = $this->indexPage->countItems();
467
468
        Assert::true(
469
            ((int) $numberOfShippingMethods) === $foundRows,
470
            sprintf('%s rows with shipping methods should appear on page, %s rows has been found', $numberOfShippingMethods, $foundRows)
471
        );
472
    }
473
474
    /**
475
     * @When I enable it
476
     */
477
    public function iEnableIt()
478
    {
479
        $this->updatePage->enable();
480
    }
481
482
    /**
483
     * @When I disable it
484
     */
485
    public function iDisableIt()
486
    {
487
        $this->updatePage->disable();
488
    }
489
490
    /**
491
     * @Then /^(this shipping method) should be disabled$/
492
     */
493
    public function thisShippingMethodShouldBeDisabled(ShippingMethodInterface $shippingMethod)
494
    {
495
        $this->assertShippingMethodState($shippingMethod, false);
496
    }
497
498
    /**
499
     * @Then /^(this shipping method) should be enabled$/
500
     */
501
    public function thisShippingMethodShouldBeEnabled(ShippingMethodInterface $shippingMethod)
502
    {
503
        $this->assertShippingMethodState($shippingMethod, true);
504
    }
505
506
    /**
507
     * @When I delete shipping method :shippingMethod
508
     * @When I try to delete shipping method :shippingMethod
509
     */
510
    public function iDeleteShippingMethod(ShippingMethodInterface $shippingMethod)
511
    {
512
        $this->indexPage->open();
513
        $this->indexPage->deleteResourceOnPage(['name' => $shippingMethod->getName()]);
514
    }
515
516
    /**
517
     * @Then /^(this shipping method) should no longer exist in the registry$/
518
     */
519
    public function thisShippingMethodShouldNoLongerExistInTheRegistry(ShippingMethodInterface $shippingMethod)
520
    {
521
        Assert::false(
522
            $this->indexPage->isSingleResourceOnPage(['code' => $shippingMethod->getCode()]),
523
            sprintf('Shipping method with code %s exists but should not.', $shippingMethod->getCode())
524
        );
525
    }
526
527
    /**
528
     * @Then I should be notified that it is in use
529
     */
530
    public function iShouldBeNotifiedThatItIsInUse()
531
    {
532
        $this->notificationChecker->checkNotification('Cannot delete, the shipping method is in use.', NotificationType::failure());
533
    }
534
535
    /**
536
     * @Then I should be notified that amount for :channel channel should not be blank
537
     */
538
    public function iShouldBeNotifiedThatAmountForChannelShouldNotBeBlank(ChannelInterface $channel)
539
    {
540
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
541
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
542
543
        Assert::same(
544
            $currentPage->getValidationMessageForAmount($channel->getCode()),
545
            'This value should not be blank.'
546
        );
547
    }
548
549
    /**
550
     * @param string $element
551
     * @param string $expectedMessage
552
     */
553
    private function assertFieldValidationMessage($element, $expectedMessage)
554
    {
555
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
556
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
557
558
        Assert::same($currentPage->getValidationMessage($element), $expectedMessage);
559
    }
560
561
    /**
562
     * @param ShippingMethodInterface $shippingMethod
563
     * @param bool $state
564
     */
565
    private function assertShippingMethodState(ShippingMethodInterface $shippingMethod, $state)
566
    {
567
        $this->iWantToBrowseShippingMethods();
568
569
        Assert::true(
570
            $this->indexPage->isSingleResourceOnPage(
571
                [
572
                    'name' => $shippingMethod->getName(),
573
                    'enabled' => $state,
574
                ]
575
            ), sprintf('Shipping method with name %s and state %s has not been found.', $shippingMethod->getName(), $state));
576
    }
577
}
578