Completed
Push — 1.0-symfony-3.3.13 ( 577127...a3487e )
by Kamil
50:10 queued 26:38
created

iShouldSeeShippingMethodsInTheList()   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\ShippingMethod\CreatePageInterface;
19
use Sylius\Behat\Page\Admin\ShippingMethod\IndexPageInterface;
20
use Sylius\Behat\Page\Admin\ShippingMethod\UpdatePageInterface;
21
use Sylius\Behat\Service\NotificationCheckerInterface;
22
use Sylius\Behat\Service\Resolver\CurrentPageResolverInterface;
23
use Sylius\Component\Channel\Model\ChannelInterface;
24
use Sylius\Component\Core\Model\ShippingMethodInterface;
25
use Webmozart\Assert\Assert;
26
27
final class ManagingShippingMethodsContext implements Context
28
{
29
    /**
30
     * @var IndexPageInterface
31
     */
32
    private $indexPage;
33
34
    /**
35
     * @var CreatePageInterface
36
     */
37
    private $createPage;
38
39
    /**
40
     * @var UpdatePageInterface
41
     */
42
    private $updatePage;
43
44
    /**
45
     * @var CurrentPageResolverInterface
46
     */
47
    private $currentPageResolver;
48
49
    /**
50
     * @var NotificationCheckerInterface
51
     */
52
    private $notificationChecker;
53
54
    /**
55
     * @param IndexPageInterface $indexPage
56
     * @param CreatePageInterface $createPage
57
     * @param UpdatePageInterface $updatePage
58
     * @param CurrentPageResolverInterface $currentPageResolver
59
     * @param NotificationCheckerInterface $notificationChecker
60
     */
61
    public function __construct(
62
        IndexPageInterface $indexPage,
63
        CreatePageInterface $createPage,
64
        UpdatePageInterface $updatePage,
65
        CurrentPageResolverInterface $currentPageResolver,
66
        NotificationCheckerInterface $notificationChecker
67
    ) {
68
        $this->indexPage = $indexPage;
69
        $this->createPage = $createPage;
70
        $this->updatePage = $updatePage;
71
        $this->currentPageResolver = $currentPageResolver;
72
        $this->notificationChecker = $notificationChecker;
73
    }
74
75
    /**
76
     * @When I want to create a new shipping method
77
     */
78
    public function iWantToCreateANewShippingMethod()
79
    {
80
        $this->createPage->open();
81
    }
82
83
    /**
84
     * @When I specify its code as :code
85
     * @When I do not specify its code
86
     */
87
    public function iSpecifyItsCodeAs($code = null)
88
    {
89
        $this->createPage->specifyCode($code);
90
    }
91
92
    /**
93
     * @When I specify its position as :position
94
     */
95
    public function iSpecifyItsPositionAs($position = null)
96
    {
97
        $this->createPage->specifyPosition($position);
98
    }
99
100
    /**
101
     * @When I name it :name in :language
102
     * @When I rename it to :name in :language
103
     */
104
    public function iNameItIn($name, $language)
105
    {
106
        $this->createPage->nameIt($name, $language);
107
    }
108
109
    /**
110
     * @When I describe it as :description in :language
111
     */
112
    public function iDescribeItAsIn($description, $language)
113
    {
114
        $this->createPage->describeIt($description, $language);
115
    }
116
117
    /**
118
     * @When I define it for the :zoneName zone
119
     */
120
    public function iDefineItForTheZone($zoneName)
121
    {
122
        $this->createPage->chooseZone($zoneName);
123
    }
124
125
    /**
126
     * @When I specify its amount as :amount for :channel channel
127
     */
128
    public function iSpecifyItsAmountForChannel($amount, ChannelInterface $channel)
129
    {
130
        $this->createPage->specifyAmountForChannel($channel->getCode(), $amount);
131
    }
132
133
    /**
134
     * @When I make it available in channel :channelName
135
     */
136
    public function iMakeItAvailableInChannel($channelName)
137
    {
138
        $this->createPage->checkChannel($channelName);
139
    }
140
141
    /**
142
     * @When I add it
143
     * @When I try to add it
144
     */
145
    public function iAddIt()
146
    {
147
        $this->createPage->create();
148
    }
149
150
    /**
151
     * @When I choose :calculatorName calculator
152
     * @When I do not specify amount for :calculatorName calculator
153
     */
154
    public function iChooseCalculator($calculatorName)
155
    {
156
        $this->createPage->chooseCalculator($calculatorName);
157
    }
158
159
    /**
160
     * @Then the shipping method :shipmentMethod should appear in the registry
161
     * @Then the shipping method :shipmentMethod should be in the registry
162
     */
163
    public function theShipmentMethodShouldAppearInTheRegistry($shipmentMethodName)
164
    {
165
        $this->iWantToBrowseShippingMethods();
166
167
        Assert::true($this->indexPage->isSingleResourceOnPage(['name' => $shipmentMethodName]));
168
    }
169
170
    /**
171
     * @Given /^(this shipping method) should still be in the registry$/
172
     */
173
    public function thisShippingMethodShouldStillBeInTheRegistry(ShippingMethodInterface $shippingMethod)
174
    {
175
        $this->theShipmentMethodShouldAppearInTheRegistry($shippingMethod->getName());
176
    }
177
178
    /**
179
     * @Then the shipping method :shippingMethod should be available in channel :channelName
180
     */
181
    public function theShippingMethodShouldBeAvailableInChannel(
182
        ShippingMethodInterface $shippingMethod,
183
        $channelName
184
    ) {
185
        $this->iWantToModifyAShippingMethod($shippingMethod);
186
187
        Assert::true($this->updatePage->isAvailableInChannel($channelName));
188
    }
189
190
    /**
191
     * @Then I should be notified that shipping method with this code already exists
192
     */
193
    public function iShouldBeNotifiedThatShippingMethodWithThisCodeAlreadyExists()
194
    {
195
        Assert::same($this->createPage->getValidationMessage('code'), 'The shipping method with given code already exists.');
196
    }
197
198
    /**
199
     * @Then there should still be only one shipping method with :element :code
200
     */
201
    public function thereShouldStillBeOnlyOneShippingMethodWith($element, $code)
202
    {
203
        $this->iWantToBrowseShippingMethods();
204
205
        Assert::true($this->indexPage->isSingleResourceOnPage([$element => $code]));
206
    }
207
208
    /**
209
     * @Given I want to modify a shipping method :shippingMethod
210
     * @Given /^I want to modify (this shipping method)$/
211
     */
212
    public function iWantToModifyAShippingMethod(ShippingMethodInterface $shippingMethod)
213
    {
214
        $this->updatePage->open(['id' => $shippingMethod->getId()]);
215
    }
216
217
    /**
218
     * @Then the code field should be disabled
219
     */
220
    public function theCodeFieldShouldBeDisabled()
221
    {
222
        Assert::true($this->updatePage->isCodeDisabled());
223
    }
224
225
    /**
226
     * @Then /^(this shipping method) name should be "([^"]+)"$/
227
     * @Then /^(this shipping method) should still be named "([^"]+)"$/
228
     */
229
    public function thisShippingMethodNameShouldBe(ShippingMethodInterface $shippingMethod, $shippingMethodName)
230
    {
231
        $this->iWantToBrowseShippingMethods();
232
233
        Assert::true($this->indexPage->isSingleResourceOnPage([
234
            'code' => $shippingMethod->getCode(),
235
            'name' => $shippingMethodName,
236
        ]));
237
    }
238
239
    /**
240
     * @When I save my changes
241
     * @When I try to save my changes
242
     */
243
    public function iSaveMyChanges()
244
    {
245
        $this->updatePage->saveChanges();
246
    }
247
248
    /**
249
     * @Then I should be notified that :element is required
250
     */
251
    public function iShouldBeNotifiedThatIsRequired($element)
252
    {
253
        $this->assertFieldValidationMessage($element, sprintf('Please enter shipping method %s.', $element));
254
    }
255
256
    /**
257
     * @Then I should be notified that code needs to contain only specific symbols
258
     */
259
    public function iShouldBeNotifiedThatCodeShouldContain()
260
    {
261
        $this->assertFieldValidationMessage(
262
            'code',
263
            'Shipping method code can only be comprised of letters, numbers, dashes and underscores.'
264
        );
265
    }
266
267
    /**
268
     * @When I archive the :name shipping method
269
     */
270
    public function iArchiveTheShippingMethod($name)
271
    {
272
        $actions = $this->indexPage->getActionsForResource(['name' => $name]);
273
        $actions->pressButton('Archive');
274
    }
275
276
    /**
277
     * @When I restore the :name shipping method
278
     */
279
    public function iRestoreTheShippingMethod($name)
280
    {
281
        $actions = $this->indexPage->getActionsForResource(['name' => $name]);
282
        $actions->pressButton('Restore');
283
    }
284
285
    /**
286
     * @Then I should be viewing non archival shipping methods
287
     */
288
    public function iShouldBeViewingNonArchivalShippingMethods()
289
    {
290
        Assert::false($this->indexPage->isArchivalFilterEnabled());
291
    }
292
293
    /**
294
     * @Then I should see :count shipping methods on the list
295
     */
296
    public function thereShouldBeNoShippingMethodsOnTheList($count)
297
    {
298
        Assert::same($this->indexPage->countItems(), (int) $count);
299
    }
300
301
    /**
302
     * @Then the only shipping method on the list should be :name
303
     */
304
    public function theOnlyShippingMethodOnTheListShouldBe($name)
305
    {
306
        Assert::same((int) $this->indexPage->countItems(), 1);
307
        Assert::true($this->indexPage->isSingleResourceOnPage(['name' => $name]));
308
    }
309
310
    /**
311
     * @Then shipping method with :element :name should not be added
312
     */
313
    public function shippingMethodWithElementValueShouldNotBeAdded($element, $name)
314
    {
315
        $this->iWantToBrowseShippingMethods();
316
317
        Assert::false($this->indexPage->isSingleResourceOnPage([$element => $name]));
318
    }
319
320
    /**
321
     * @When I do not name it
322
     */
323
    public function iDoNotNameIt()
324
    {
325
        // Intentionally left blank to fulfill context expectation
326
    }
327
328
    /**
329
     * @When I do not specify its zone
330
     */
331
    public function iDoNotSpecifyItsZone()
332
    {
333
        // Intentionally left blank to fulfill context expectation
334
    }
335
336
    /**
337
     * @When I remove its zone
338
     */
339
    public function iRemoveItsZone()
340
    {
341
        $this->updatePage->removeZone();
342
    }
343
344
    /**
345
     * @Then I should be notified that :element has to be selected
346
     */
347
    public function iShouldBeNotifiedThatElementHasToBeSelected($element)
348
    {
349
        $this->assertFieldValidationMessage($element, sprintf('Please select shipping method %s.', $element));
350
    }
351
352
    /**
353
     * @When I remove its name from :language translation
354
     */
355
    public function iRemoveItsNameFromTranslation($language)
356
    {
357
        $this->createPage->nameIt(null, $language);
358
    }
359
360
    /**
361
     * @Given I am browsing shipping methods
362
     * @When I want to browse shipping methods
363
     */
364
    public function iWantToBrowseShippingMethods()
365
    {
366
        $this->indexPage->open();
367
    }
368
369
    /**
370
     * @Given I am browsing archival shipping methods
371
     */
372
    public function iAmBrowsingArchivalShippingMethods()
373
    {
374
        $this->indexPage->open();
375
        $this->indexPage->chooseArchival('Yes');
376
        $this->indexPage->filter();
377
    }
378
379
    /**
380
     * @Given I filter archival shipping methods
381
     */
382
    public function iFilterArchivalShippingMethods()
383
    {
384
        $this->indexPage->chooseArchival('Yes');
385
        $this->indexPage->filter();
386
    }
387
388
    /**
389
     * @Then the first shipping method on the list should have :field :value
390
     */
391
    public function theFirstShippingMethodOnTheListShouldHave($field, $value)
392
    {
393
        $fields = $this->indexPage->getColumnFields($field);
394
395
        Assert::same(reset($fields), $value);
396
    }
397
398
    /**
399
     * @Then the last shipping method on the list should have :field :value
400
     */
401
    public function theLastShippingMethodOnTheListShouldHave($field, $value)
402
    {
403
        $fields = $this->indexPage->getColumnFields($field);
404
405
        Assert::same(end($fields), $value);
406
    }
407
408
    /**
409
     * @When I switch the way shipping methods are sorted by :field
410
     * @When I start sorting shipping methods by :field
411
     * @Given the shipping methods are already sorted by :field
412
     */
413
    public function iSortShippingMethodsBy($field)
414
    {
415
        $this->indexPage->sortBy($field);
416
    }
417
418
    /**
419
     * @Then I should see :numberOfShippingMethods shipping methods in the list
420
     */
421
    public function iShouldSeeShippingMethodsInTheList($numberOfShippingMethods)
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $numberOfShippingMethods exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
422
    {
423
        Assert::same($this->indexPage->countItems(), (int) $numberOfShippingMethods);
424
    }
425
426
    /**
427
     * @When I enable it
428
     */
429
    public function iEnableIt()
430
    {
431
        $this->updatePage->enable();
432
    }
433
434
    /**
435
     * @When I disable it
436
     */
437
    public function iDisableIt()
438
    {
439
        $this->updatePage->disable();
440
    }
441
442
    /**
443
     * @Then /^(this shipping method) should be disabled$/
444
     */
445
    public function thisShippingMethodShouldBeDisabled(ShippingMethodInterface $shippingMethod)
446
    {
447
        $this->assertShippingMethodState($shippingMethod, false);
448
    }
449
450
    /**
451
     * @Then /^(this shipping method) should be enabled$/
452
     */
453
    public function thisShippingMethodShouldBeEnabled(ShippingMethodInterface $shippingMethod)
454
    {
455
        $this->assertShippingMethodState($shippingMethod, true);
456
    }
457
458
    /**
459
     * @When I delete shipping method :shippingMethod
460
     * @When I try to delete shipping method :shippingMethod
461
     */
462
    public function iDeleteShippingMethod(ShippingMethodInterface $shippingMethod)
463
    {
464
        $this->indexPage->open();
465
        $this->indexPage->deleteResourceOnPage(['name' => $shippingMethod->getName()]);
466
    }
467
468
    /**
469
     * @Then /^(this shipping method) should no longer exist in the registry$/
470
     */
471
    public function thisShippingMethodShouldNoLongerExistInTheRegistry(ShippingMethodInterface $shippingMethod)
472
    {
473
        Assert::false($this->indexPage->isSingleResourceOnPage(['code' => $shippingMethod->getCode()]));
474
    }
475
476
    /**
477
     * @Then I should be notified that it is in use
478
     */
479
    public function iShouldBeNotifiedThatItIsInUse()
480
    {
481
        $this->notificationChecker->checkNotification('Cannot delete, the shipping method is in use.', NotificationType::failure());
482
    }
483
484
    /**
485
     * @Then I should be notified that amount for :channel channel should not be blank
486
     */
487
    public function iShouldBeNotifiedThatAmountForChannelShouldNotBeBlank(ChannelInterface $channel)
488
    {
489
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
490
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
491
492
        Assert::same(
493
            $currentPage->getValidationMessageForAmount($channel->getCode()),
494
            'This value should not be blank.'
495
        );
496
    }
497
498
    /**
499
     * @param string $element
500
     * @param string $expectedMessage
501
     */
502
    private function assertFieldValidationMessage($element, $expectedMessage)
503
    {
504
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
505
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
506
507
        Assert::same($currentPage->getValidationMessage($element), $expectedMessage);
508
    }
509
510
    /**
511
     * @param ShippingMethodInterface $shippingMethod
512
     * @param bool $state
513
     */
514
    private function assertShippingMethodState(ShippingMethodInterface $shippingMethod, $state)
515
    {
516
        $this->iWantToBrowseShippingMethods();
517
518
        Assert::true($this->indexPage->isSingleResourceOnPage([
519
            'name' => $shippingMethod->getName(),
520
            'enabled' => $state ? 'Enabled' : 'Disabled',
521
        ]));
522
    }
523
}
524