Completed
Push — master ( 6f962e...549eec )
by Kamil
21:17
created

iSpecifyItsCodeAs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
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
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\CurrentPageResolverInterface;
19
use Sylius\Behat\Service\NotificationCheckerInterface;
20
use Sylius\Behat\NotificationType;
21
use Sylius\Component\Core\Model\ShippingMethodInterface;
22
use Sylius\Component\Core\Test\Services\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 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 define it for the :zoneName zone
113
     */
114
    public function iDefineItForTheZone($zoneName)
115
    {
116
        $this->createPage->chooseZone($zoneName);
117
    }
118
119
    /**
120
     * @When I specify its amount as :amount
121
     */
122
    public function iSpecifyItsAmountAs($amount)
123
    {
124
        $this->createPage->specifyAmount($amount);
125
    }
126
127
    /**
128
     * @When I add it
129
     * @When I try to add it
130
     */
131
    public function iAddIt()
132
    {
133
        $this->createPage->create();
134
    }
135
136
    /**
137
     * @When I choose :calculatorName calculator
138
     * @When I do not specify amount for :calculatorName calculator
139
     */
140
    public function iChooseCalculator($calculatorName)
141
    {
142
        $this->createPage->chooseCalculator($calculatorName);
143
    }
144
145
    /**
146
     * @Then the shipment method :shipmentMethod should appear in the registry
147
     * @Then the shipment method :shipmentMethod should be in the registry
148
     */
149
    public function theShipmentMethodShouldAppearInTheRegistry($shipmentMethodName)
150
    {
151
        $this->iWantToBrowseShippingMethods();
152
153
        Assert::true(
154
            $this->indexPage->isSingleResourceOnPage(['name' => $shipmentMethodName]),
155
            sprintf('The shipping method with name %s has not been found.', $shipmentMethodName)
156
        );
157
    }
158
159
    /**
160
     * @Given /^(this shipping method) should still be in the registry$/
161
     */
162
    public function thisShippingMethodShouldStillBeInTheRegistry(ShippingMethodInterface $shippingMethod)
163
    {
164
        $this->theShipmentMethodShouldAppearInTheRegistry($shippingMethod->getName());
165
    }
166
167
    /**
168
     * @Then I should be notified that shipping method with this code already exists
169
     */
170
    public function iShouldBeNotifiedThatShippingMethodWithThisCodeAlreadyExists()
171
    {
172
        Assert::true(
173
            $this->createPage->checkValidationMessageFor('code', 'The shipping method with given code already exists.'),
174
            'Unique code violation message should appear on page, but it does not.'
175
        );
176
    }
177
178
    /**
179
     * @Then there should still be only one shipping method with :element :code
180
     */
181
    public function thereShouldStillBeOnlyOneShippingMethodWith($element, $code)
182
    {
183
        $this->iWantToBrowseShippingMethods();
184
185
        Assert::true(
186
            $this->indexPage->isSingleResourceOnPage([$element => $code]),
187
            sprintf('Shipping method with %s %s cannot be founded.', $element, $code)
188
        );
189
    }
190
191
    /**
192
     * @Given I want to modify a shipping method :shippingMethod
193
     * @Given /^I want to modify (this shipping method)$/
194
     */
195
    public function iWantToModifyAShippingMethod(ShippingMethodInterface $shippingMethod)
196
    {
197
        $this->updatePage->open(['id' => $shippingMethod->getId()]);
198
    }
199
200
    /**
201
     * @Then the code field should be disabled
202
     */
203
    public function theCodeFieldShouldBeDisabled()
204
    {
205
        Assert::true(
206
            $this->updatePage->isCodeDisabled(),
207
            'Code should be immutable, but it does not.'
208
        );
209
    }
210
211
    /**
212
     * @Then /^(this shipping method) name should be "([^"]+)"$/
213
     * @Then /^(this shipping method) should still be named "([^"]+)"$/
214
     */
215
    public function thisShippingMethodNameShouldBe(ShippingMethodInterface $shippingMethod, $shippingMethodName)
216
    {
217
        $this->iWantToBrowseShippingMethods();
218
219
        Assert::true(
220
            $this->indexPage->isSingleResourceOnPage(
221
                [
222
                    'code' => $shippingMethod->getCode(),
223
                    'name' => $shippingMethodName,
224
                ]
225
            ),
226
            sprintf('Shipping method name %s has not been assigned properly.', $shippingMethodName)
227
        );
228
    }
229
230
    /**
231
     * @When I save my changes
232
     * @When I try to save my changes
233
     */
234
    public function iSaveMyChanges()
235
    {
236
        $this->updatePage->saveChanges();
237
    }
238
239
    /**
240
     * @Then I should be notified that :element is required
241
     */
242
    public function iShouldBeNotifiedThatIsRequired($element)
243
    {
244
        $this->assertFieldValidationMessage($element, sprintf('Please enter shipping method %s.', $element));
245
    }
246
247
    /**
248
     * @Then shipping method with :element :name should not be added
249
     */
250
    public function shippingMethodWithElementValueShouldNotBeAdded($element, $name)
251
    {
252
        $this->iWantToBrowseShippingMethods();
253
254
        Assert::false(
255
            $this->indexPage->isSingleResourceOnPage([$element => $name]),
256
            sprintf('Shipping method with %s %s was created, but it should not.', $element, $name)
257
        );
258
    }
259
260
    /**
261
     * @When I do not name it
262
     */
263
    public function iDoNotNameIt()
264
    {
265
        // Intentionally left blank to fulfill context expectation
266
    }
267
268
    /**
269
     * @When I do not specify its zone
270
     */
271
    public function iDoNotSpecifyItsZone()
272
    {
273
        // Intentionally left blank to fulfill context expectation
274
    }
275
276
    /**
277
     * @Then I should be notified that :element has to be selected
278
     */
279
    public function iShouldBeNotifiedThatElementHasToBeSelected($element)
280
    {
281
        $this->assertFieldValidationMessage($element, sprintf('Please select shipping method %s.', $element));
282
    }
283
284
    /**
285
     * @When I remove its name from :language translation
286
     */
287
    public function iRemoveItsNameFromTranslation($language)
288
    {
289
        $this->createPage->nameIt(null, $language);
290
    }
291
292
    /**
293
     * @Then I should be notified that :field should not be blank
294
     */
295
    public function iShouldBeNotifiedThatAmountShouldNotBeBlank($field)
296
    {
297
        $this->assertFieldValidationMessage($field, 'This value should not be blank.');
298
    }
299
    /**
300
     * @When I want to browse shipping methods
301
     */
302
    public function iWantToBrowseShippingMethods()
303
    {
304
        $this->indexPage->open();
305
    }
306
307
    /**
308
     * @Then I should see :numberOfShippingMethods shipping methods in the list
309
     */
310
    public function iShouldSeeShippingMethodsInTheList($numberOfShippingMethods)
311
    {
312
        $foundRows = $this->indexPage->countItems();
313
314
        Assert::true(
315
            ((int) $numberOfShippingMethods) === $foundRows,
316
            sprintf('%s rows with shipping methods should appear on page, %s rows has been found', $numberOfShippingMethods, $foundRows)
317
        );
318
    }
319
320
    /**
321
     * @When I enable it
322
     */
323
    public function iEnableIt()
324
    {
325
        $this->updatePage->enable();
326
    }
327
328
    /**
329
     * @When I disable it
330
     */
331
    public function iDisableIt()
332
    {
333
        $this->updatePage->disable();
334
    }
335
336
    /**
337
     * @Then /^(this shipping method) should be disabled$/
338
     */
339
    public function thisShippingMethodShouldBeDisabled(ShippingMethodInterface $shippingMethod)
340
    {
341
        $this->assertShippingMethodState($shippingMethod, false);
342
    }
343
344
    /**
345
     * @Then /^(this shipping method) should be enabled$/
346
     */
347
    public function thisShippingMethodShouldBeEnabled(ShippingMethodInterface $shippingMethod)
348
    {
349
        $this->assertShippingMethodState($shippingMethod, true);
350
    }
351
352
    /**
353
     * @When I delete shipping method :shippingMethod
354
     * @When I try to delete shipping method :shippingMethod
355
     */
356
    public function iDeleteShippingMethod(ShippingMethodInterface $shippingMethod)
357
    {
358
        $this->indexPage->open();
359
        $this->indexPage->deleteResourceOnPage(['name' => $shippingMethod->getName()]);
360
    }
361
362
    /**
363
     * @Then /^(this shipping method) should no longer exist in the registry$/
364
     */
365
    public function thisShippingMethodShouldNoLongerExistInTheRegistry(ShippingMethodInterface $shippingMethod)
366
    {
367
        Assert::false(
368
            $this->indexPage->isSingleResourceOnPage(['code' => $shippingMethod->getCode()]),
369
            sprintf('Shipping method with code %s exists but should not.', $shippingMethod->getCode())
370
        );
371
    }
372
373
    /**
374
     * @Then I should be notified that it is in use
375
     */
376
    public function iShouldBeNotifiedThatItIsInUse()
377
    {
378
        $this->notificationChecker->checkNotification('Cannot delete, the shipping method is in use.', NotificationType::failure());
379
    }
380
381
    /**
382
     * @param string $element
383
     * @param string $expectedMessage
384
     */
385
    private function assertFieldValidationMessage($element, $expectedMessage)
386
    {
387
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm($this->createPage, $this->updatePage);
388
389
        Assert::true(
390
            $currentPage->checkValidationMessageFor($element, $expectedMessage),
391
            sprintf('Shipping method %s should be required.', $element)
392
        );
393
    }
394
395
    /**
396
     * @param ShippingMethodInterface $shippingMethod
397
     * @param bool $state
398
     */
399
    private function assertShippingMethodState(ShippingMethodInterface $shippingMethod, $state)
400
    {
401
        $this->iWantToBrowseShippingMethods();
402
403
        Assert::true(
404
            $this->indexPage->isSingleResourceOnPage(
405
                [
406
                    'name' => $shippingMethod->getName(),
407
                    'enabled' => $state,
408
                ]
409
            ), sprintf('Shipping method with name %s and state %s has not been found.', $shippingMethod->getName(), $state));
410
    }
411
}
412