Completed
Push — symfony3 ( 405d0c...88ded0 )
by Kamil
32:03 queued 12:32
created

theShippingMethodShouldBeAvailableInChannel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
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\Page\Admin\Crud\IndexPageInterface;
16
use Sylius\Behat\Page\Admin\ShippingMethod\CreatePageInterface;
17
use Sylius\Behat\Page\Admin\ShippingMethod\UpdatePageInterface;
18
use Sylius\Behat\Service\Resolver\CurrentPageResolverInterface;
19
use Sylius\Behat\Service\NotificationCheckerInterface;
20
use Sylius\Behat\NotificationType;
21
use Sylius\Component\Core\Model\ShippingMethodInterface;
22
use Sylius\Behat\Service\SharedStorageInterface;
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 SharedStorageInterface
33
     */
34
    private $sharedStorage;
35
36
    /**
37
     * @var IndexPageInterface
38
     */
39
    private $indexPage;
40
41
    /**
42
     * @var CreatePageInterface
43
     */
44
    private $createPage;
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 SharedStorageInterface $sharedStorage
63
     * @param IndexPageInterface $indexPage
64
     * @param CreatePageInterface $createPage
65
     * @param UpdatePageInterface $updatePage
66
     * @param CurrentPageResolverInterface $currentPageResolver
67
     * @param NotificationCheckerInterface $notificationChecker
68
     */
69
    public function __construct(
70
        SharedStorageInterface $sharedStorage,
71
        IndexPageInterface $indexPage,
72
        CreatePageInterface $createPage,
73
        UpdatePageInterface $updatePage,
74
        CurrentPageResolverInterface $currentPageResolver,
75
        NotificationCheckerInterface $notificationChecker
76
    ) {
77
        $this->sharedStorage = $sharedStorage;
78
        $this->indexPage = $indexPage;
79
        $this->createPage = $createPage;
80
        $this->updatePage = $updatePage;
81
        $this->currentPageResolver = $currentPageResolver;
82
        $this->notificationChecker = $notificationChecker;
83
    }
84
85
    /**
86
     * @Given I want to create a new shipping method
87
     */
88
    public function iWantToCreateANewShippingMethod()
89
    {
90
        $this->createPage->open();
91
    }
92
93
    /**
94
     * @When I specify its code as :code
95
     * @When I do not specify its code
96
     */
97
    public function iSpecifyItsCodeAs($code = null)
98
    {
99
        $this->createPage->specifyCode($code);
100
    }
101
102
    /**
103
     * @When I specify its position as :position
104
     * @When I do not specify its position
105
     */
106
    public function iSpecifyItsPositionAs($position = null)
107
    {
108
        $this->createPage->specifyPosition($position);
109
    }
110
111
    /**
112
     * @When I name it :name in :language
113
     * @When I rename it to :name in :language
114
     */
115
    public function iNameItIn($name, $language)
116
    {
117
        $this->createPage->nameIt($name, $language);
118
    }
119
120
    /**
121
     * @When I describe it as :description in :language
122
     */
123
    public function iDescribeItAsIn($description, $language)
124
    {
125
        $this->createPage->describeIt($description, $language);
126
    }
127
128
    /**
129
     * @When I define it for the :zoneName zone
130
     */
131
    public function iDefineItForTheZone($zoneName)
132
    {
133
        $this->createPage->chooseZone($zoneName);
134
    }
135
136
    /**
137
     * @When I specify its amount as :amount
138
     */
139
    public function iSpecifyItsAmountAs($amount)
140
    {
141
        $this->createPage->specifyAmount($amount);
142
    }
143
144
    /**
145
     * @When I make it available in channel :channel
146
     */
147
    public function iMakeItAvaialbelInChannel($channel)
148
    {
149
        $this->createPage->checkChannel($channel);
150
    }
151
152
    /**
153
     * @When I add it
154
     * @When I try to add it
155
     */
156
    public function iAddIt()
157
    {
158
        $this->createPage->create();
159
    }
160
161
    /**
162
     * @When I choose :calculatorName calculator
163
     * @When I do not specify amount for :calculatorName calculator
164
     */
165
    public function iChooseCalculator($calculatorName)
166
    {
167
        $this->createPage->chooseCalculator($calculatorName);
168
    }
169
170
    /**
171
     * @Then the shipping method :shipmentMethod should appear in the registry
172
     * @Then the shipping method :shipmentMethod should be in the registry
173
     */
174
    public function theShipmentMethodShouldAppearInTheRegistry($shipmentMethodName)
175
    {
176
        $this->iWantToBrowseShippingMethods();
177
178
        Assert::true(
179
            $this->indexPage->isSingleResourceOnPage(['name' => $shipmentMethodName]),
180
            sprintf('The shipping method with name %s has not been found.', $shipmentMethodName)
181
        );
182
    }
183
184
    /**
185
     * @Given /^(this shipping method) should still be in the registry$/
186
     */
187
    public function thisShippingMethodShouldStillBeInTheRegistry(ShippingMethodInterface $shippingMethod)
188
    {
189
        $this->theShipmentMethodShouldAppearInTheRegistry($shippingMethod->getName());
190
    }
191
192
    /**
193
     * @Then the shipping method :shippingMethod should be available in channel :channelName
194
     */
195
    public function theShippingMethodShouldBeAvailableInChannel(
196
        ShippingMethodInterface $shippingMethod,
197
        $channelName
198
    ) {
199
        $this->iWantToModifyAShippingMethod($shippingMethod);
200
201
        Assert::true(
202
            $this->updatePage->isAvailableInChannel($channelName),
203
            sprintf('Shipping method should be available in channel "%s" but it does not.', $channelName)
204
        );
205
    }
206
207
    /**
208
     * @Then I should be notified that shipping method with this code already exists
209
     */
210
    public function iShouldBeNotifiedThatShippingMethodWithThisCodeAlreadyExists()
211
    {
212
        Assert::same($this->createPage->getValidationMessage('code'), 'The shipping method with given code already exists.');
213
    }
214
215
    /**
216
     * @Then there should still be only one shipping method with :element :code
217
     */
218
    public function thereShouldStillBeOnlyOneShippingMethodWith($element, $code)
219
    {
220
        $this->iWantToBrowseShippingMethods();
221
222
        Assert::true(
223
            $this->indexPage->isSingleResourceOnPage([$element => $code]),
224
            sprintf('Shipping method with %s %s cannot be found.', $element, $code)
225
        );
226
    }
227
228
    /**
229
     * @Given I want to modify a shipping method :shippingMethod
230
     * @Given /^I want to modify (this shipping method)$/
231
     */
232
    public function iWantToModifyAShippingMethod(ShippingMethodInterface $shippingMethod)
233
    {
234
        $this->updatePage->open(['id' => $shippingMethod->getId()]);
235
    }
236
237
    /**
238
     * @Then the code field should be disabled
239
     */
240
    public function theCodeFieldShouldBeDisabled()
241
    {
242
        Assert::true(
243
            $this->updatePage->isCodeDisabled(),
244
            'Code should be immutable, but it does not.'
245
        );
246
    }
247
248
    /**
249
     * @Then /^(this shipping method) name should be "([^"]+)"$/
250
     * @Then /^(this shipping method) should still be named "([^"]+)"$/
251
     */
252
    public function thisShippingMethodNameShouldBe(ShippingMethodInterface $shippingMethod, $shippingMethodName)
253
    {
254
        $this->iWantToBrowseShippingMethods();
255
256
        Assert::true(
257
            $this->indexPage->isSingleResourceOnPage(
258
                [
259
                    'code' => $shippingMethod->getCode(),
260
                    'name' => $shippingMethodName,
261
                ]
262
            ),
263
            sprintf('Shipping method name %s has not been assigned properly.', $shippingMethodName)
264
        );
265
    }
266
267
    /**
268
     * @When I save my changes
269
     * @When I try to save my changes
270
     */
271
    public function iSaveMyChanges()
272
    {
273
        $this->updatePage->saveChanges();
274
    }
275
276
    /**
277
     * @Then I should be notified that :element is required
278
     */
279
    public function iShouldBeNotifiedThatIsRequired($element)
280
    {
281
        $this->assertFieldValidationMessage($element, sprintf('Please enter shipping method %s.', $element));
282
    }
283
284
    /**
285
     * @Then shipping method with :element :name should not be added
286
     */
287
    public function shippingMethodWithElementValueShouldNotBeAdded($element, $name)
288
    {
289
        $this->iWantToBrowseShippingMethods();
290
291
        Assert::false(
292
            $this->indexPage->isSingleResourceOnPage([$element => $name]),
293
            sprintf('Shipping method with %s %s was created, but it should not.', $element, $name)
294
        );
295
    }
296
297
    /**
298
     * @When I do not name it
299
     */
300
    public function iDoNotNameIt()
301
    {
302
        // Intentionally left blank to fulfill context expectation
303
    }
304
305
    /**
306
     * @When I do not specify its zone
307
     */
308
    public function iDoNotSpecifyItsZone()
309
    {
310
        // Intentionally left blank to fulfill context expectation
311
    }
312
313
    /**
314
     * @Then I should be notified that :element has to be selected
315
     */
316
    public function iShouldBeNotifiedThatElementHasToBeSelected($element)
317
    {
318
        $this->assertFieldValidationMessage($element, sprintf('Please select shipping method %s.', $element));
319
    }
320
321
    /**
322
     * @When I remove its name from :language translation
323
     */
324
    public function iRemoveItsNameFromTranslation($language)
325
    {
326
        $this->createPage->nameIt(null, $language);
327
    }
328
329
    /**
330
     * @Then I should be notified that :field should not be blank
331
     */
332
    public function iShouldBeNotifiedThatAmountShouldNotBeBlank($field)
333
    {
334
        $this->assertFieldValidationMessage($field, 'This value should not be blank.');
335
    }
336
    /**
337
     * @Given I am browsing shipping methods
338
     * @When I want to browse shipping methods
339
     */
340
    public function iWantToBrowseShippingMethods()
341
    {
342
        $this->indexPage->open();
343
    }
344
345
    /**
346
     * @Then the first shipping method on the list should have :field :value
347
     */
348
    public function theFirstShippingMethodOnTheListShouldHave($field, $value)
349
    {
350
        $fields = $this->indexPage->getColumnFields($field);
351
        $actualValue = reset($fields);
352
353
        Assert::same(
354
            $actualValue,
355
            $value,
356
            sprintf('Expected first shipping method\'s %s to be "%s", but it is "%s".', $field, $value, $actualValue)
357
        );
358
    }
359
360
    /**
361
     * @Then the last shipping method on the list should have :field :value
362
     */
363
    public function theLastShippingMethodOnTheListShouldHave($field, $value)
364
    {
365
        $fields = $this->indexPage->getColumnFields($field);
366
        $actualValue = end($fields);
367
368
        Assert::same(
369
            $actualValue,
370
            $value,
371
            sprintf('Expected last shipping method\'s %s to be "%s", but it is "%s".', $field, $value, $actualValue)
372
        );
373
    }
374
375
    /**
376
     * @When I switch the way shipping methods are sorted by :field
377
     * @When I start sorting shipping methods by :field
378
     * @Given the shipping methods are already sorted by :field
379
     */
380
    public function iSortShippingMethodsBy($field)
381
    {
382
        $this->indexPage->sortBy($field);
383
    }
384
385
    /**
386
     * @Then I should see :numberOfShippingMethods shipping methods in the list
387
     */
388
    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...
389
    {
390
        $foundRows = $this->indexPage->countItems();
391
392
        Assert::true(
393
            ((int) $numberOfShippingMethods) === $foundRows,
394
            sprintf('%s rows with shipping methods should appear on page, %s rows has been found', $numberOfShippingMethods, $foundRows)
395
        );
396
    }
397
398
    /**
399
     * @When I enable it
400
     */
401
    public function iEnableIt()
402
    {
403
        $this->updatePage->enable();
404
    }
405
406
    /**
407
     * @When I disable it
408
     */
409
    public function iDisableIt()
410
    {
411
        $this->updatePage->disable();
412
    }
413
414
    /**
415
     * @Then /^(this shipping method) should be disabled$/
416
     */
417
    public function thisShippingMethodShouldBeDisabled(ShippingMethodInterface $shippingMethod)
418
    {
419
        $this->assertShippingMethodState($shippingMethod, false);
420
    }
421
422
    /**
423
     * @Then /^(this shipping method) should be enabled$/
424
     */
425
    public function thisShippingMethodShouldBeEnabled(ShippingMethodInterface $shippingMethod)
426
    {
427
        $this->assertShippingMethodState($shippingMethod, true);
428
    }
429
430
    /**
431
     * @When I delete shipping method :shippingMethod
432
     * @When I try to delete shipping method :shippingMethod
433
     */
434
    public function iDeleteShippingMethod(ShippingMethodInterface $shippingMethod)
435
    {
436
        $this->indexPage->open();
437
        $this->indexPage->deleteResourceOnPage(['name' => $shippingMethod->getName()]);
438
    }
439
440
    /**
441
     * @Then /^(this shipping method) should no longer exist in the registry$/
442
     */
443
    public function thisShippingMethodShouldNoLongerExistInTheRegistry(ShippingMethodInterface $shippingMethod)
444
    {
445
        Assert::false(
446
            $this->indexPage->isSingleResourceOnPage(['code' => $shippingMethod->getCode()]),
447
            sprintf('Shipping method with code %s exists but should not.', $shippingMethod->getCode())
448
        );
449
    }
450
451
    /**
452
     * @Then I should be notified that it is in use
453
     */
454
    public function iShouldBeNotifiedThatItIsInUse()
455
    {
456
        $this->notificationChecker->checkNotification('Cannot delete, the shipping method is in use.', NotificationType::failure());
457
    }
458
459
    /**
460
     * @param string $element
461
     * @param string $expectedMessage
462
     */
463
    private function assertFieldValidationMessage($element, $expectedMessage)
464
    {
465
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
466
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
467
468
        Assert::same($currentPage->getValidationMessage($element), $expectedMessage);
469
    }
470
471
    /**
472
     * @param ShippingMethodInterface $shippingMethod
473
     * @param bool $state
474
     */
475
    private function assertShippingMethodState(ShippingMethodInterface $shippingMethod, $state)
476
    {
477
        $this->iWantToBrowseShippingMethods();
478
479
        Assert::true(
480
            $this->indexPage->isSingleResourceOnPage(
481
                [
482
                    'name' => $shippingMethod->getName(),
483
                    'enabled' => $state,
484
                ]
485
            ), sprintf('Shipping method with name %s and state %s has not been found.', $shippingMethod->getName(), $state));
486
    }
487
}
488