Completed
Push — master ( 87cbf2...406549 )
by Paweł
149:47 queued 134:45
created

theLastShippingMethodOnTheListShouldHave()   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 add it
146
     * @When I try to add it
147
     */
148
    public function iAddIt()
149
    {
150
        $this->createPage->create();
151
    }
152
153
    /**
154
     * @When I choose :calculatorName calculator
155
     * @When I do not specify amount for :calculatorName calculator
156
     */
157
    public function iChooseCalculator($calculatorName)
158
    {
159
        $this->createPage->chooseCalculator($calculatorName);
160
    }
161
162
    /**
163
     * @Then the shipment method :shipmentMethod should appear in the registry
164
     * @Then the shipment method :shipmentMethod should be in the registry
165
     */
166
    public function theShipmentMethodShouldAppearInTheRegistry($shipmentMethodName)
167
    {
168
        $this->iWantToBrowseShippingMethods();
169
170
        Assert::true(
171
            $this->indexPage->isSingleResourceOnPage(['name' => $shipmentMethodName]),
172
            sprintf('The shipping method with name %s has not been found.', $shipmentMethodName)
173
        );
174
    }
175
176
    /**
177
     * @Given /^(this shipping method) should still be in the registry$/
178
     */
179
    public function thisShippingMethodShouldStillBeInTheRegistry(ShippingMethodInterface $shippingMethod)
180
    {
181
        $this->theShipmentMethodShouldAppearInTheRegistry($shippingMethod->getName());
182
    }
183
184
    /**
185
     * @Then I should be notified that shipping method with this code already exists
186
     */
187
    public function iShouldBeNotifiedThatShippingMethodWithThisCodeAlreadyExists()
188
    {
189
        Assert::same($this->createPage->getValidationMessage('code'), 'The shipping method with given code already exists.');
190
    }
191
192
    /**
193
     * @Then there should still be only one shipping method with :element :code
194
     */
195
    public function thereShouldStillBeOnlyOneShippingMethodWith($element, $code)
196
    {
197
        $this->iWantToBrowseShippingMethods();
198
199
        Assert::true(
200
            $this->indexPage->isSingleResourceOnPage([$element => $code]),
201
            sprintf('Shipping method with %s %s cannot be found.', $element, $code)
202
        );
203
    }
204
205
    /**
206
     * @Given I want to modify a shipping method :shippingMethod
207
     * @Given /^I want to modify (this shipping method)$/
208
     */
209
    public function iWantToModifyAShippingMethod(ShippingMethodInterface $shippingMethod)
210
    {
211
        $this->updatePage->open(['id' => $shippingMethod->getId()]);
212
    }
213
214
    /**
215
     * @Then the code field should be disabled
216
     */
217
    public function theCodeFieldShouldBeDisabled()
218
    {
219
        Assert::true(
220
            $this->updatePage->isCodeDisabled(),
221
            'Code should be immutable, but it does not.'
222
        );
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(
234
            $this->indexPage->isSingleResourceOnPage(
235
                [
236
                    'code' => $shippingMethod->getCode(),
237
                    'name' => $shippingMethodName,
238
                ]
239
            ),
240
            sprintf('Shipping method name %s has not been assigned properly.', $shippingMethodName)
241
        );
242
    }
243
244
    /**
245
     * @When I save my changes
246
     * @When I try to save my changes
247
     */
248
    public function iSaveMyChanges()
249
    {
250
        $this->updatePage->saveChanges();
251
    }
252
253
    /**
254
     * @Then I should be notified that :element is required
255
     */
256
    public function iShouldBeNotifiedThatIsRequired($element)
257
    {
258
        $this->assertFieldValidationMessage($element, sprintf('Please enter shipping method %s.', $element));
259
    }
260
261
    /**
262
     * @Then shipping method with :element :name should not be added
263
     */
264
    public function shippingMethodWithElementValueShouldNotBeAdded($element, $name)
265
    {
266
        $this->iWantToBrowseShippingMethods();
267
268
        Assert::false(
269
            $this->indexPage->isSingleResourceOnPage([$element => $name]),
270
            sprintf('Shipping method with %s %s was created, but it should not.', $element, $name)
271
        );
272
    }
273
274
    /**
275
     * @When I do not name it
276
     */
277
    public function iDoNotNameIt()
278
    {
279
        // Intentionally left blank to fulfill context expectation
280
    }
281
282
    /**
283
     * @When I do not specify its zone
284
     */
285
    public function iDoNotSpecifyItsZone()
286
    {
287
        // Intentionally left blank to fulfill context expectation
288
    }
289
290
    /**
291
     * @Then I should be notified that :element has to be selected
292
     */
293
    public function iShouldBeNotifiedThatElementHasToBeSelected($element)
294
    {
295
        $this->assertFieldValidationMessage($element, sprintf('Please select shipping method %s.', $element));
296
    }
297
298
    /**
299
     * @When I remove its name from :language translation
300
     */
301
    public function iRemoveItsNameFromTranslation($language)
302
    {
303
        $this->createPage->nameIt(null, $language);
304
    }
305
306
    /**
307
     * @Then I should be notified that :field should not be blank
308
     */
309
    public function iShouldBeNotifiedThatAmountShouldNotBeBlank($field)
310
    {
311
        $this->assertFieldValidationMessage($field, 'This value should not be blank.');
312
    }
313
    /**
314
     * @Given I am browsing shipping methods
315
     * @When I want to browse shipping methods
316
     */
317
    public function iWantToBrowseShippingMethods()
318
    {
319
        $this->indexPage->open();
320
    }
321
322
    /**
323
     * @Then the first shipping method on the list should have :field :value
324
     */
325
    public function theFirstShippingMethodOnTheListShouldHave($field, $value)
326
    {
327
        $fields = $this->indexPage->getColumnFields($field);
328
        $actualValue = reset($fields);
329
330
        Assert::same(
331
            $actualValue,
332
            $value,
333
            sprintf('Expected first shipping method\'s %s to be "%s", but it is "%s".', $field, $value, $actualValue)
334
        );
335
    }
336
337
    /**
338
     * @Then the last shipping method on the list should have :field :value
339
     */
340
    public function theLastShippingMethodOnTheListShouldHave($field, $value)
341
    {
342
        $fields = $this->indexPage->getColumnFields($field);
343
        $actualValue = end($fields);
344
345
        Assert::same(
346
            $actualValue,
347
            $value,
348
            sprintf('Expected last shipping method\'s %s to be "%s", but it is "%s".', $field, $value, $actualValue)
349
        );
350
    }
351
352
    /**
353
     * @When I switch the way shipping methods are sorted by :field
354
     * @When I start sorting shipping methods by :field
355
     * @Given the shipping methods are already sorted by :field
356
     */
357
    public function iSortShippingMethodsBy($field)
358
    {
359
        $this->indexPage->sortBy($field);
360
    }
361
362
    /**
363
     * @Then I should see :numberOfShippingMethods shipping methods in the list
364
     */
365
    public function iShouldSeeShippingMethodsInTheList($numberOfShippingMethods)
366
    {
367
        $foundRows = $this->indexPage->countItems();
368
369
        Assert::true(
370
            ((int) $numberOfShippingMethods) === $foundRows,
371
            sprintf('%s rows with shipping methods should appear on page, %s rows has been found', $numberOfShippingMethods, $foundRows)
372
        );
373
    }
374
375
    /**
376
     * @When I enable it
377
     */
378
    public function iEnableIt()
379
    {
380
        $this->updatePage->enable();
381
    }
382
383
    /**
384
     * @When I disable it
385
     */
386
    public function iDisableIt()
387
    {
388
        $this->updatePage->disable();
389
    }
390
391
    /**
392
     * @Then /^(this shipping method) should be disabled$/
393
     */
394
    public function thisShippingMethodShouldBeDisabled(ShippingMethodInterface $shippingMethod)
395
    {
396
        $this->assertShippingMethodState($shippingMethod, false);
397
    }
398
399
    /**
400
     * @Then /^(this shipping method) should be enabled$/
401
     */
402
    public function thisShippingMethodShouldBeEnabled(ShippingMethodInterface $shippingMethod)
403
    {
404
        $this->assertShippingMethodState($shippingMethod, true);
405
    }
406
407
    /**
408
     * @When I delete shipping method :shippingMethod
409
     * @When I try to delete shipping method :shippingMethod
410
     */
411
    public function iDeleteShippingMethod(ShippingMethodInterface $shippingMethod)
412
    {
413
        $this->indexPage->open();
414
        $this->indexPage->deleteResourceOnPage(['name' => $shippingMethod->getName()]);
415
    }
416
417
    /**
418
     * @Then /^(this shipping method) should no longer exist in the registry$/
419
     */
420
    public function thisShippingMethodShouldNoLongerExistInTheRegistry(ShippingMethodInterface $shippingMethod)
421
    {
422
        Assert::false(
423
            $this->indexPage->isSingleResourceOnPage(['code' => $shippingMethod->getCode()]),
424
            sprintf('Shipping method with code %s exists but should not.', $shippingMethod->getCode())
425
        );
426
    }
427
428
    /**
429
     * @Then I should be notified that it is in use
430
     */
431
    public function iShouldBeNotifiedThatItIsInUse()
432
    {
433
        $this->notificationChecker->checkNotification('Cannot delete, the shipping method is in use.', NotificationType::failure());
434
    }
435
436
    /**
437
     * @param string $element
438
     * @param string $expectedMessage
439
     */
440
    private function assertFieldValidationMessage($element, $expectedMessage)
441
    {
442
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
443
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
444
445
        Assert::same($currentPage->getValidationMessage($element), $expectedMessage);
446
    }
447
448
    /**
449
     * @param ShippingMethodInterface $shippingMethod
450
     * @param bool $state
451
     */
452
    private function assertShippingMethodState(ShippingMethodInterface $shippingMethod, $state)
453
    {
454
        $this->iWantToBrowseShippingMethods();
455
456
        Assert::true(
457
            $this->indexPage->isSingleResourceOnPage(
458
                [
459
                    'name' => $shippingMethod->getName(),
460
                    'enabled' => $state,
461
                ]
462
            ), sprintf('Shipping method with name %s and state %s has not been found.', $shippingMethod->getName(), $state));
463
    }
464
}
465