Completed
Push — 1.5-symfony-insights ( c13c26 )
by Kamil
05:55
created

iShouldBeNotifiedThatTheDefaultLocaleHasToBeEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\Channel\CreatePageInterface;
19
use Sylius\Behat\Page\Admin\Channel\IndexPageInterface;
20
use Sylius\Behat\Page\Admin\Channel\UpdatePageInterface;
21
use Sylius\Behat\Service\NotificationCheckerInterface;
22
use Sylius\Behat\Service\Resolver\CurrentPageResolverInterface;
23
use Sylius\Component\Core\Formatter\StringInflector;
24
use Sylius\Component\Core\Model\ChannelInterface;
25
use Sylius\Component\Currency\Model\CurrencyInterface;
26
use Webmozart\Assert\Assert;
27
28
final class ManagingChannelsContext implements Context
29
{
30
    /** @var IndexPageInterface */
31
    private $indexPage;
32
33
    /** @var CreatePageInterface */
34
    private $createPage;
35
36
    /** @var UpdatePageInterface */
37
    private $updatePage;
38
39
    /** @var CurrentPageResolverInterface */
40
    private $currentPageResolver;
41
42
    /** @var NotificationCheckerInterface */
43
    private $notificationChecker;
44
45
    public function __construct(
46
        IndexPageInterface $indexPage,
47
        CreatePageInterface $createPage,
48
        UpdatePageInterface $updatePage,
49
        CurrentPageResolverInterface $currentPageResolver,
50
        NotificationCheckerInterface $notificationChecker
51
    ) {
52
        $this->indexPage = $indexPage;
53
        $this->createPage = $createPage;
54
        $this->updatePage = $updatePage;
55
        $this->currentPageResolver = $currentPageResolver;
56
        $this->notificationChecker = $notificationChecker;
57
    }
58
59
    /**
60
     * @Given I want to create a new channel
61
     */
62
    public function iWantToCreateANewChannel()
63
    {
64
        $this->createPage->open();
65
    }
66
67
    /**
68
     * @When I specify its code as :code
69
     * @When I do not specify its code
70
     */
71
    public function iSpecifyItsCodeAs($code = null)
72
    {
73
        $this->createPage->specifyCode($code ?? '');
74
    }
75
76
    /**
77
     * @When I name it :name
78
     * @When I rename it to :name
79
     * @When I do not name it
80
     * @When I remove its name
81
     */
82
    public function iNameIt($name = null)
83
    {
84
        $this->createPage->nameIt($name ?? '');
85
    }
86
87
    /**
88
     * @When I choose :currency as the base currency
89
     * @When I do not choose base currency
90
     */
91
    public function iChooseAsABaseCurrency(?CurrencyInterface $currency = null)
92
    {
93
        $this->createPage->chooseBaseCurrency($currency ? $currency->getName() : null);
94
    }
95
96
    /**
97
     * @When I choose :locale as a default locale
98
     * @When I do not choose default locale
99
     */
100
    public function iChooseAsADefaultLocale($locale = null)
101
    {
102
        $this->createPage->chooseDefaultLocale($locale);
103
    }
104
105
    /**
106
     * @When I allow to skip shipping step if only one shipping method is available
107
     */
108
    public function iAllowToSkipShippingStepIfOnlyOneShippingMethodIsAvailable()
109
    {
110
        $this->createPage->allowToSkipShippingStep();
111
    }
112
113
    /**
114
     * @When I allow to skip payment step if only one payment method is available
115
     */
116
    public function iAllowToSkipPaymentStepIfOnlyOnePaymentMethodIsAvailable()
117
    {
118
        $this->createPage->allowToSkipPaymentStep();
119
    }
120
121
    /**
122
     * @When I add it
123
     * @When I try to add it
124
     */
125
    public function iAddIt()
126
    {
127
        $this->createPage->create();
128
    }
129
130
    /**
131
     * @Then I should see the channel :channelName in the list
132
     * @Then the channel :channelName should appear in the registry
133
     * @Then the channel :channelName should be in the registry
134
     */
135
    public function theChannelShouldAppearInTheRegistry(string $channelName): void
136
    {
137
        $this->iWantToBrowseChannels();
138
139
        Assert::true($this->indexPage->isSingleResourceOnPage(['nameAndDescription' => $channelName]));
140
    }
141
142
    /**
143
     * @Then /^(this channel) should still be in the registry$/
144
     */
145
    public function thisChannelShouldAppearInTheRegistry(ChannelInterface $channel)
146
    {
147
        $this->theChannelShouldAppearInTheRegistry($channel->getName());
148
    }
149
150
    /**
151
     * @When I describe it as :description
152
     */
153
    public function iDescribeItAs($description)
154
    {
155
        $this->createPage->describeItAs($description);
156
    }
157
158
    /**
159
     * @When I set its hostname as :hostname
160
     */
161
    public function iSetItsHostnameAs($hostname)
162
    {
163
        $this->createPage->setHostname($hostname);
164
    }
165
166
    /**
167
     * @When I set its contact email as :contactEmail
168
     */
169
    public function iSetItsContactEmailAs($contactEmail)
170
    {
171
        $this->createPage->setContactEmail($contactEmail);
172
    }
173
174
    /**
175
     * @When I define its color as :color
176
     */
177
    public function iDefineItsColorAs($color)
178
    {
179
        $this->createPage->defineColor($color);
180
    }
181
182
    /**
183
     * @When I enable it
184
     */
185
    public function iEnableIt()
186
    {
187
        $this->updatePage->enable();
188
    }
189
190
    /**
191
     * @When I disable it
192
     */
193
    public function iDisableIt()
194
    {
195
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
196
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
197
198
        $currentPage->disable();
199
    }
200
201
    /**
202
     * @Then I should be notified that at least one channel has to be defined
203
     */
204
    public function iShouldBeNotifiedThatAtLeastOneChannelHasToBeDefinedIsRequired()
205
    {
206
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
207
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
208
209
        Assert::same($currentPage->getValidationMessage('enabled'), 'Must have at least one enabled entity');
210
    }
211
212
    /**
213
     * @Then channel with :element :value should not be added
214
     */
215
    public function channelWithShouldNotBeAdded($element, $value)
216
    {
217
        $this->iWantToBrowseChannels();
218
219
        Assert::false($this->indexPage->isSingleResourceOnPage([$element => $value]));
220
    }
221
222
    /**
223
     * @Then /^I should be notified that ([^"]+) is required$/
224
     */
225
    public function iShouldBeNotifiedThatIsRequired($element)
226
    {
227
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
228
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
229
230
        Assert::same(
231
            $currentPage->getValidationMessage(StringInflector::nameToCode($element)),
232
            sprintf('Please enter channel %s.', $element)
233
        );
234
    }
235
236
    /**
237
     * @Given I am modifying a channel :channel
238
     * @When I want to modify a channel :channel
239
     * @When /^I want to modify (this channel)$/
240
     */
241
    public function iWantToModifyChannel(ChannelInterface $channel): void
242
    {
243
        $this->updatePage->open(['id' => $channel->getId()]);
244
    }
245
246
    /**
247
     * @Then /^(this channel) name should be "([^"]+)"$/
248
     * @Then /^(this channel) should still be named "([^"]+)"$/
249
     */
250
    public function thisChannelNameShouldBe(ChannelInterface $channel, $channelName)
251
    {
252
        $this->iWantToBrowseChannels();
253
254
        Assert::true($this->indexPage->isSingleResourceOnPage([
255
            'code' => $channel->getCode(),
256
            'nameAndDescription' => $channelName,
257
        ]));
258
    }
259
260
    /**
261
     * @When I save my changes
262
     * @When I try to save my changes
263
     */
264
    public function iSaveMyChanges()
265
    {
266
        $this->updatePage->saveChanges();
267
    }
268
269
    /**
270
     * @Then I should be notified that channel with this code already exists
271
     */
272
    public function iShouldBeNotifiedThatChannelWithThisCodeAlreadyExists()
273
    {
274
        Assert::same($this->createPage->getValidationMessage('code'), 'Channel code has to be unique.');
275
    }
276
277
    /**
278
     * @Then there should still be only one channel with :element :value
279
     */
280
    public function thereShouldStillBeOnlyOneChannelWithCode($element, $value)
281
    {
282
        $this->iWantToBrowseChannels();
283
284
        Assert::true($this->indexPage->isSingleResourceOnPage([$element => $value]));
285
    }
286
287
    /**
288
     * @When I browse channels
289
     * @When I want to browse channels
290
     */
291
    public function iWantToBrowseChannels(): void
292
    {
293
        $this->indexPage->open();
294
    }
295
296
    /**
297
     * @When I check (also) the :channelName channel
298
     */
299
    public function iCheckTheChannel(string $channelName): void
300
    {
301
        $this->indexPage->checkResourceOnPage(['nameAndDescription' => $channelName]);
302
    }
303
304
    /**
305
     * @When I delete them
306
     */
307
    public function iDeleteThem(): void
308
    {
309
        $this->indexPage->bulkDelete();
310
    }
311
312
    /**
313
     * @Then I should see a single channel in the list
314
     * @Then I should see :numberOfChannels channels in the list
315
     */
316
    public function iShouldSeeChannelsInTheList(int $numberOfChannels = 1): void
317
    {
318
        Assert::same($this->indexPage->countItems(), $numberOfChannels);
319
    }
320
321
    /**
322
     * @Then the code field should be disabled
323
     */
324
    public function theCodeFieldShouldBeDisabled()
325
    {
326
        Assert::true($this->updatePage->isCodeDisabled());
327
    }
328
329
    /**
330
     * @Then /^(this channel) should be disabled$/
331
     */
332
    public function thisChannelShouldBeDisabled(ChannelInterface $channel)
333
    {
334
        $this->assertChannelState($channel, false);
335
    }
336
337
    /**
338
     * @Then /^(this channel) should be enabled$/
339
     * @Then channel with name :channel should still be enabled
340
     */
341
    public function thisChannelShouldBeEnabled(ChannelInterface $channel)
342
    {
343
        $this->assertChannelState($channel, true);
344
    }
345
346
    /**
347
     * @When I delete channel :channel
348
     */
349
    public function iDeleteChannel(ChannelInterface $channel)
350
    {
351
        $this->indexPage->open();
352
        $this->indexPage->deleteResourceOnPage(['nameAndDescription' => $channel->getName()]);
353
    }
354
355
    /**
356
     * @Then the :channelName channel should no longer exist in the registry
357
     */
358
    public function thisChannelShouldNoLongerExistInTheRegistry($channelName)
359
    {
360
        Assert::false($this->indexPage->isSingleResourceOnPage(['nameAndDescription' => $channelName]));
361
    }
362
363
    /**
364
     * @Then I should be notified that it cannot be deleted
365
     */
366
    public function iShouldBeNotifiedThatItCannotBeDeleted()
367
    {
368
        $this->notificationChecker->checkNotification(
369
            'The channel cannot be deleted. At least one enabled channel is required.',
370
            NotificationType::failure()
371
        );
372
    }
373
374
    /**
375
     * @When I make it available (only) in :locale
376
     */
377
    public function iMakeItAvailableIn(string $localeName): void
378
    {
379
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
380
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
381
382
        $currentPage->chooseLocale($localeName);
383
    }
384
385
    /**
386
     * @Then the channel :channel should be available in :locale
387
     */
388
    public function theChannelShouldBeAvailableIn(ChannelInterface $channel, $locale)
389
    {
390
        $this->updatePage->open(['id' => $channel->getId()]);
391
392
        Assert::true($this->updatePage->isLocaleChosen($locale));
393
    }
394
395
    /**
396
     * @When I allow for paying in :currencyCode
397
     */
398
    public function iAllowToPayingForThisChannel($currencyCode)
399
    {
400
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
401
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
402
403
        $currentPage->chooseCurrency($currencyCode);
404
    }
405
406
    /**
407
     * @Then paying in :currencyCode should be possible for the :channel channel
408
     */
409
    public function payingInEuroShouldBePossibleForTheChannel($currencyCode, ChannelInterface $channel)
410
    {
411
        $this->updatePage->open(['id' => $channel->getId()]);
412
413
        Assert::true($this->updatePage->isCurrencyChosen($currencyCode));
414
    }
415
416
    /**
417
     * @When I select the :taxZone as default tax zone
418
     */
419
    public function iSelectDefaultTaxZone($taxZone)
420
    {
421
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
422
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
423
424
        $currentPage->chooseDefaultTaxZone($taxZone);
425
    }
426
427
    /**
428
     * @Given I remove its default tax zone
429
     */
430
    public function iRemoveItsDefaultTaxZone()
431
    {
432
        $this->updatePage->chooseDefaultTaxZone('');
433
    }
434
435
    /**
436
     * @When I select the :taxCalculationStrategy as tax calculation strategy
437
     */
438
    public function iSelectTaxCalculationStrategy($taxCalculationStrategy)
439
    {
440
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
441
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
442
443
        $currentPage->chooseTaxCalculationStrategy($taxCalculationStrategy);
444
    }
445
446
    /**
447
     * @Then the default tax zone for the :channel channel should be :taxZone
448
     */
449
    public function theDefaultTaxZoneForTheChannelShouldBe(ChannelInterface $channel, $taxZone)
450
    {
451
        $this->updatePage->open(['id' => $channel->getId()]);
452
453
        Assert::true($this->updatePage->isDefaultTaxZoneChosen($taxZone));
454
    }
455
456
    /**
457
     * @Given channel :channel should not have default tax zone
458
     */
459
    public function channelShouldNotHaveDefaultTaxZone(ChannelInterface $channel)
460
    {
461
        $this->updatePage->open(['id' => $channel->getId()]);
462
463
        Assert::false($this->updatePage->isAnyDefaultTaxZoneChosen());
464
    }
465
466
    /**
467
     * @Then the tax calculation strategy for the :channel channel should be :taxCalculationStrategy
468
     */
469
    public function theTaxCalculationStrategyForTheChannelShouldBe(ChannelInterface $channel, $taxCalculationStrategy)
470
    {
471
        $this->updatePage->open(['id' => $channel->getId()]);
472
473
        Assert::true($this->updatePage->isTaxCalculationStrategyChosen($taxCalculationStrategy));
474
    }
475
476
    /**
477
     * @Then the base currency field should be disabled
478
     */
479
    public function theBaseCurrencyFieldShouldBeDisabled()
480
    {
481
        Assert::true($this->updatePage->isBaseCurrencyDisabled());
482
    }
483
484
    /**
485
     * @Then I should be notified that the default locale has to be enabled
486
     */
487
    public function iShouldBeNotifiedThatTheDefaultLocaleHasToBeEnabled(): void
488
    {
489
        Assert::same(
490
            $this->updatePage->getValidationMessage('default_locale'),
491
            'Default locale has to be enabled.'
492
        );
493
    }
494
495
    /**
496
     * @param bool $state
497
     */
498
    private function assertChannelState(ChannelInterface $channel, $state)
499
    {
500
        $this->iWantToBrowseChannels();
501
502
        Assert::true($this->indexPage->isSingleResourceOnPage([
503
            'nameAndDescription' => $channel->getName(),
504
            'enabled' => $state ? 'Enabled' : 'Disabled',
505
        ]));
506
    }
507
}
508