Completed
Push — sf-44 ( 026126...08883d )
by Kamil
79:34 queued 56:57
created

thereShouldStillBeOnlyOneChannelWithCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
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
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\UpdatePageInterface;
20
use Sylius\Behat\Page\Admin\Crud\IndexPageInterface;
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 specify menu taxon as :menuTaxon
107
     */
108
    public function iSpecifyMenuTaxonAs(string $menuTaxon): void
109
    {
110
        $this->createPage->specifyMenuTaxon($menuTaxon);
111
    }
112
113
    /**
114
     * @When I change its menu taxon to :menuTaxon
115
     */
116
    public function iChangeItsMenuTaxonTo(string $menuTaxon): void
117
    {
118
        $this->updatePage->changeMenuTaxon($menuTaxon);
119
    }
120
121
    /**
122
     * @When I allow to skip shipping step if only one shipping method is available
123
     */
124
    public function iAllowToSkipShippingStepIfOnlyOneShippingMethodIsAvailable()
125
    {
126
        $this->createPage->allowToSkipShippingStep();
127
    }
128
129
    /**
130
     * @When I allow to skip payment step if only one payment method is available
131
     */
132
    public function iAllowToSkipPaymentStepIfOnlyOnePaymentMethodIsAvailable()
133
    {
134
        $this->createPage->allowToSkipPaymentStep();
135
    }
136
137
    /**
138
     * @When I add it
139
     * @When I try to add it
140
     */
141
    public function iAddIt()
142
    {
143
        $this->createPage->create();
144
    }
145
146
    /**
147
     * @Then I should see the channel :channelName in the list
148
     * @Then the channel :channelName should appear in the registry
149
     * @Then the channel :channelName should be in the registry
150
     */
151
    public function theChannelShouldAppearInTheRegistry(string $channelName): void
152
    {
153
        $this->iWantToBrowseChannels();
154
155
        Assert::true($this->indexPage->isSingleResourceOnPage(['nameAndDescription' => $channelName]));
156
    }
157
158
    /**
159
     * @Then /^(this channel) should still be in the registry$/
160
     */
161
    public function thisChannelShouldAppearInTheRegistry(ChannelInterface $channel)
162
    {
163
        $this->theChannelShouldAppearInTheRegistry($channel->getName());
164
    }
165
166
    /**
167
     * @When I describe it as :description
168
     */
169
    public function iDescribeItAs($description)
170
    {
171
        $this->createPage->describeItAs($description);
172
    }
173
174
    /**
175
     * @When I set its hostname as :hostname
176
     */
177
    public function iSetItsHostnameAs($hostname)
178
    {
179
        $this->createPage->setHostname($hostname);
180
    }
181
182
    /**
183
     * @When I set its contact email as :contactEmail
184
     */
185
    public function iSetItsContactEmailAs($contactEmail)
186
    {
187
        $this->createPage->setContactEmail($contactEmail);
188
    }
189
190
    /**
191
     * @When I define its color as :color
192
     */
193
    public function iDefineItsColorAs($color)
194
    {
195
        $this->createPage->defineColor($color);
196
    }
197
198
    /**
199
     * @When I enable it
200
     */
201
    public function iEnableIt()
202
    {
203
        $this->updatePage->enable();
204
    }
205
206
    /**
207
     * @When I disable it
208
     */
209
    public function iDisableIt()
210
    {
211
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
212
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
213
214
        $currentPage->disable();
215
    }
216
217
    /**
218
     * @Then I should be notified that at least one channel has to be defined
219
     */
220
    public function iShouldBeNotifiedThatAtLeastOneChannelHasToBeDefinedIsRequired()
221
    {
222
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
223
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
224
225
        Assert::same($currentPage->getValidationMessage('enabled'), 'Must have at least one enabled entity');
226
    }
227
228
    /**
229
     * @Then channel with :element :value should not be added
230
     */
231
    public function channelWithShouldNotBeAdded($element, $value)
232
    {
233
        $this->iWantToBrowseChannels();
234
235
        Assert::false($this->indexPage->isSingleResourceOnPage([$element => $value]));
236
    }
237
238
    /**
239
     * @Then /^I should be notified that ([^"]+) is required$/
240
     */
241
    public function iShouldBeNotifiedThatIsRequired($element)
242
    {
243
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
244
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
245
246
        Assert::same(
247
            $currentPage->getValidationMessage(StringInflector::nameToCode($element)),
248
            sprintf('Please enter channel %s.', $element)
249
        );
250
    }
251
252
    /**
253
     * @Given I am modifying a channel :channel
254
     * @When I want to modify a channel :channel
255
     * @When /^I want to modify (this channel)$/
256
     */
257
    public function iWantToModifyChannel(ChannelInterface $channel): void
258
    {
259
        $this->updatePage->open(['id' => $channel->getId()]);
260
    }
261
262
    /**
263
     * @Then /^(this channel) name should be "([^"]+)"$/
264
     * @Then /^(this channel) should still be named "([^"]+)"$/
265
     */
266
    public function thisChannelNameShouldBe(ChannelInterface $channel, $channelName)
267
    {
268
        $this->iWantToBrowseChannels();
269
270
        Assert::true($this->indexPage->isSingleResourceOnPage([
271
            'code' => $channel->getCode(),
272
            'nameAndDescription' => $channelName,
273
        ]));
274
    }
275
276
    /**
277
     * @When I save my changes
278
     * @When I try to save my changes
279
     */
280
    public function iSaveMyChanges()
281
    {
282
        $this->updatePage->saveChanges();
283
    }
284
285
    /**
286
     * @When /^I define its type as (mobile|website|pos)$/
287
     */
288
    public function iDefineItsTypeAs(string $type): void
289
    {
290
        $this->createPage->setType($type);
291
    }
292
293
    /**
294
     * @Then I should be notified that channel with this code already exists
295
     */
296
    public function iShouldBeNotifiedThatChannelWithThisCodeAlreadyExists()
297
    {
298
        Assert::same($this->createPage->getValidationMessage('code'), 'Channel code has to be unique.');
299
    }
300
301
    /**
302
     * @Then there should still be only one channel with :element :value
303
     */
304
    public function thereShouldStillBeOnlyOneChannelWithCode($element, $value)
305
    {
306
        $this->iWantToBrowseChannels();
307
308
        Assert::true($this->indexPage->isSingleResourceOnPage([$element => $value]));
309
    }
310
311
    /**
312
     * @When I browse channels
313
     * @When I want to browse channels
314
     */
315
    public function iWantToBrowseChannels(): void
316
    {
317
        $this->indexPage->open();
318
    }
319
320
    /**
321
     * @When I check (also) the :channelName channel
322
     */
323
    public function iCheckTheChannel(string $channelName): void
324
    {
325
        $this->indexPage->checkResourceOnPage(['nameAndDescription' => $channelName]);
326
    }
327
328
    /**
329
     * @When I delete them
330
     */
331
    public function iDeleteThem(): void
332
    {
333
        $this->indexPage->bulkDelete();
334
    }
335
336
    /**
337
     * @Then I should see a single channel in the list
338
     * @Then I should see :numberOfChannels channels in the list
339
     */
340
    public function iShouldSeeChannelsInTheList(int $numberOfChannels = 1): void
341
    {
342
        Assert::same($this->indexPage->countItems(), $numberOfChannels);
343
    }
344
345
    /**
346
     * @Then the code field should be disabled
347
     */
348
    public function theCodeFieldShouldBeDisabled()
349
    {
350
        Assert::true($this->updatePage->isCodeDisabled());
351
    }
352
353
    /**
354
     * @Then /^(this channel) should be disabled$/
355
     */
356
    public function thisChannelShouldBeDisabled(ChannelInterface $channel)
357
    {
358
        $this->assertChannelState($channel, false);
359
    }
360
361
    /**
362
     * @Then /^(this channel) should be enabled$/
363
     * @Then channel with name :channel should still be enabled
364
     */
365
    public function thisChannelShouldBeEnabled(ChannelInterface $channel)
366
    {
367
        $this->assertChannelState($channel, true);
368
    }
369
370
    /**
371
     * @When I delete channel :channel
372
     */
373
    public function iDeleteChannel(ChannelInterface $channel)
374
    {
375
        $this->indexPage->open();
376
        $this->indexPage->deleteResourceOnPage(['nameAndDescription' => $channel->getName()]);
377
    }
378
379
    /**
380
     * @Then the :channelName channel should no longer exist in the registry
381
     */
382
    public function thisChannelShouldNoLongerExistInTheRegistry($channelName)
383
    {
384
        Assert::false($this->indexPage->isSingleResourceOnPage(['nameAndDescription' => $channelName]));
385
    }
386
387
    /**
388
     * @Then I should be notified that it cannot be deleted
389
     */
390
    public function iShouldBeNotifiedThatItCannotBeDeleted()
391
    {
392
        $this->notificationChecker->checkNotification(
393
            'The channel cannot be deleted. At least one enabled channel is required.',
394
            NotificationType::failure()
395
        );
396
    }
397
398
    /**
399
     * @When I make it available (only) in :locale
400
     */
401
    public function iMakeItAvailableIn(string $localeName): void
402
    {
403
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
404
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
405
406
        $currentPage->chooseLocale($localeName);
407
    }
408
409
    /**
410
     * @Then the channel :channel should be available in :locale
411
     */
412
    public function theChannelShouldBeAvailableIn(ChannelInterface $channel, $locale)
413
    {
414
        $this->updatePage->open(['id' => $channel->getId()]);
415
416
        Assert::true($this->updatePage->isLocaleChosen($locale));
417
    }
418
419
    /**
420
     * @When I allow for paying in :currencyCode
421
     */
422
    public function iAllowToPayingForThisChannel($currencyCode)
423
    {
424
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
425
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
426
427
        $currentPage->chooseCurrency($currencyCode);
428
    }
429
430
    /**
431
     * @Then paying in :currencyCode should be possible for the :channel channel
432
     */
433
    public function payingInEuroShouldBePossibleForTheChannel($currencyCode, ChannelInterface $channel)
434
    {
435
        $this->updatePage->open(['id' => $channel->getId()]);
436
437
        Assert::true($this->updatePage->isCurrencyChosen($currencyCode));
438
    }
439
440
    /**
441
     * @When I select the :taxZone as default tax zone
442
     */
443
    public function iSelectDefaultTaxZone($taxZone)
444
    {
445
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
446
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
447
448
        $currentPage->chooseDefaultTaxZone($taxZone);
449
    }
450
451
    /**
452
     * @Given I remove its default tax zone
453
     */
454
    public function iRemoveItsDefaultTaxZone()
455
    {
456
        $this->updatePage->chooseDefaultTaxZone('');
457
    }
458
459
    /**
460
     * @When I select the :taxCalculationStrategy as tax calculation strategy
461
     */
462
    public function iSelectTaxCalculationStrategy($taxCalculationStrategy)
463
    {
464
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
465
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
466
467
        $currentPage->chooseTaxCalculationStrategy($taxCalculationStrategy);
468
    }
469
470
    /**
471
     * @Then the default tax zone for the :channel channel should be :taxZone
472
     */
473
    public function theDefaultTaxZoneForTheChannelShouldBe(ChannelInterface $channel, $taxZone)
474
    {
475
        $this->updatePage->open(['id' => $channel->getId()]);
476
477
        Assert::true($this->updatePage->isDefaultTaxZoneChosen($taxZone));
478
    }
479
480
    /**
481
     * @When /^I change its type to (mobile|website|pos)$/
482
     */
483
    public function iChangeItsTypeTo(string $type): void
484
    {
485
        $this->updatePage->changeType($type);
486
    }
487
488
    /**
489
     * @Given channel :channel should not have default tax zone
490
     */
491
    public function channelShouldNotHaveDefaultTaxZone(ChannelInterface $channel)
492
    {
493
        $this->updatePage->open(['id' => $channel->getId()]);
494
495
        Assert::false($this->updatePage->isAnyDefaultTaxZoneChosen());
496
    }
497
498
    /**
499
     * @Then the tax calculation strategy for the :channel channel should be :taxCalculationStrategy
500
     */
501
    public function theTaxCalculationStrategyForTheChannelShouldBe(ChannelInterface $channel, $taxCalculationStrategy)
502
    {
503
        $this->updatePage->open(['id' => $channel->getId()]);
504
505
        Assert::true($this->updatePage->isTaxCalculationStrategyChosen($taxCalculationStrategy));
506
    }
507
508
    /**
509
     * @Then the base currency field should be disabled
510
     */
511
    public function theBaseCurrencyFieldShouldBeDisabled()
512
    {
513
        Assert::true($this->updatePage->isBaseCurrencyDisabled());
514
    }
515
516
    /**
517
     * @Then I should be notified that the default locale has to be enabled
518
     */
519
    public function iShouldBeNotifiedThatTheDefaultLocaleHasToBeEnabled(): void
520
    {
521
        Assert::same(
522
            $this->updatePage->getValidationMessage('default_locale'),
523
            'Default locale has to be enabled.'
524
        );
525
    }
526
527
    /**
528
     * @Then /^this channel type should be (mobile|website|pos)$/
529
     */
530
    public function thisChannelTypeShouldBe(string $type): void
531
    {
532
        Assert::same($this->updatePage->getType(), $type);
533
    }
534
535
    /**
536
     * @Given /^(this channel) menu taxon should be "([^"]+)"$/
537
     * @Given the channel :channel should have :menuTaxon as a menu taxon
538
     */
539
    public function thisChannelMenuTaxonShouldBe(ChannelInterface $channel, string $menuTaxon): void
540
    {
541
        if (!$this->updatePage->isOpen(['id' => $channel->getId()])) {
542
            $this->updatePage->open(['id' => $channel->getId()]);
543
        }
544
545
        Assert::same($this->updatePage->getMenuTaxon(), $menuTaxon);
546
    }
547
548
    /**
549
     * @param bool $state
550
     */
551
    private function assertChannelState(ChannelInterface $channel, $state)
552
    {
553
        $this->iWantToBrowseChannels();
554
555
        Assert::true($this->indexPage->isSingleResourceOnPage([
556
            'nameAndDescription' => $channel->getName(),
557
            'enabled' => $state ? 'Enabled' : 'Disabled',
558
        ]));
559
    }
560
}
561