Completed
Push — master ( 7c0075...645752 )
by Kamil
18:52
created

iShouldBeNotifiedThatAnImageCodeIsRequired()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
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
namespace Sylius\Behat\Context\Ui\Admin;
13
14
use Behat\Behat\Context\Context;
15
use Sylius\Behat\Page\Admin\Taxon\CreateForParentPageInterface;
16
use Sylius\Behat\Page\Admin\Taxon\CreatePageInterface;
17
use Sylius\Behat\Page\Admin\Taxon\UpdatePageInterface;
18
use Sylius\Behat\Service\Resolver\CurrentPageResolverInterface;
19
use Sylius\Behat\Service\SharedStorageInterface;
20
use Sylius\Component\Core\Model\Taxon;
21
use Sylius\Component\Core\Model\TaxonInterface;
22
use Webmozart\Assert\Assert;
23
24
/**
25
 * @author Arkadiusz Krakowiak <[email protected]>
26
 */
27
final class ManagingTaxonsContext implements Context
28
{
29
    /**
30
     * @var SharedStorageInterface
31
     */
32
    private $sharedStorage;
33
34
    /**
35
     * @var CreatePageInterface
36
     */
37
    private $createPage;
38
39
    /**
40
     * @var CreateForParentPageInterface
41
     */
42
    private $createForParentPage;
43
44
    /**
45
     * @var UpdatePageInterface
46
     */
47
    private $updatePage;
48
49
    /**
50
     * @var CurrentPageResolverInterface
51
     */
52
    private $currentPageResolver;
53
54
    /**
55
     * @param SharedStorageInterface $sharedStorage
56
     * @param CreatePageInterface $createPage
57
     * @param CreateForParentPageInterface $createForParentPage
58
     * @param UpdatePageInterface $updatePage
59
     * @param CurrentPageResolverInterface $currentPageResolver
60
     */
61
    public function __construct(
62
        SharedStorageInterface $sharedStorage,
63
        CreatePageInterface $createPage,
64
        CreateForParentPageInterface $createForParentPage,
65
        UpdatePageInterface $updatePage,
66
        CurrentPageResolverInterface $currentPageResolver
67
    ) {
68
        $this->sharedStorage = $sharedStorage;
69
        $this->createPage = $createPage;
70
        $this->createForParentPage = $createForParentPage;
71
        $this->updatePage = $updatePage;
72
        $this->currentPageResolver = $currentPageResolver;
73
    }
74
75
    /**
76
     * @Given I want to create a new taxon
77
     * @Given I want to see all taxons in store
78
     */
79
    public function iWantToCreateANewTaxon()
80
    {
81
        $this->createPage->open();
82
    }
83
84
    /**
85
     * @Given I want to create a new taxon for :taxon
86
     */
87
    public function iWantToCreateANewTaxonForParent(TaxonInterface $taxon)
88
    {
89
        $this->createForParentPage->open(['id' => $taxon->getId()]);
90
    }
91
92
    /**
93
     * @Given /^I want to modify the ("[^"]+" taxon)$/
94
     */
95
    public function iWantToModifyATaxon(TaxonInterface $taxon)
96
    {
97
        $this->sharedStorage->set('taxon', $taxon);
98
99
        $this->updatePage->open(['id' => $taxon->getId()]);
100
    }
101
102
    /**
103
     * @When I specify its code as :code
104
     */
105
    public function iSpecifyItsCodeAs($code)
106
    {
107
        $this->createPage->specifyCode($code);
108
    }
109
110
    /**
111
     * @When I name it :name in :language
112
     */
113
    public function iNameItIn($name, $language)
114
    {
115
        $this->createPage->nameIt($name, $language);
116
    }
117
118
    /**
119
     * @When I rename it to :name in :language
120
     */
121
    public function iRenameItIn($name, $language)
122
    {
123
        $this->updatePage->nameIt($name, $language);
124
    }
125
126
    /**
127
     * @When I set its slug to :slug
128
     * @When I do not specify its slug
129
     * @When I set its slug to :slug in :language
130
     */
131
    public function iSetItsSlugToIn($slug = null, $language = 'en_US')
132
    {
133
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
134
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([
135
            $this->createPage,
136
            $this->createForParentPage,
137
            $this->updatePage,
138
        ]);
139
140
        $currentPage->specifySlug($slug, $language);
141
    }
142
143
    /**
144
     * @Then the slug field should not be editable
145
     * @Then the slug field should (also )not be editable in :language
146
     */
147
    public function theSlugFieldShouldNotBeEditable($language = 'en_US')
148
    {
149
        Assert::true(
150
            $this->updatePage->isSlugReadOnly($language),
151
            sprintf('Slug in "%s" should be immutable, but it does not.', $language)
152
        );
153
    }
154
155
    /**
156
     * @When I enable slug modification
157
     * @When I enable slug modification in :language
158
     */
159
    public function iEnableSlugModification($language = 'en_US')
160
    {
161
        $this->updatePage->activateLanguageTab($language);
162
        $this->updatePage->enableSlugModification($language);
163
    }
164
165
    /**
166
     * @When I change its description to :description in :language
167
     */
168
    public function iChangeItsDescriptionToIn($description, $language)
169
    {
170
        $this->updatePage->describeItAs($description, $language);
171
    }
172
173
    /**
174
     * @When I describe it as :description in :language
175
     */
176
    public function iDescribeItAs($description, $language)
177
    {
178
        $this->createPage->describeItAs($description, $language);
179
    }
180
181
    /**
182
     * @When /^I move up ("[^"]+" taxon)$/
183
     */
184
    public function iWantToMoveUpTaxon(TaxonInterface $taxon)
185
    {
186
        $this->createPage->moveUp($taxon);
187
    }
188
189
    /**
190
     * @Given /^I choose ("[^"]+" as a parent taxon)$/
191
     */
192
    public function iChooseAsAParentTaxon(TaxonInterface $taxon)
193
    {
194
        $this->createPage->chooseParent($taxon);
195
    }
196
197
    /**
198
     * @Given /^I change its (parent taxon to "[^"]+")$/
199
     */
200
    public function iChangeItsParentTaxonTo(TaxonInterface $taxon)
201
    {
202
        $this->updatePage->chooseParent($taxon);
203
    }
204
205
    /**
206
     * @When I do not specify its code
207
     */
208
    public function iDoNotSpecifyItsCode()
209
    {
210
        // Intentionally left blank to fulfill context expectation
211
    }
212
213
    /**
214
     * @When I do not specify its name
215
     */
216
    public function iDoNotSpecifyItsName()
217
    {
218
        // Intentionally left blank to fulfill context expectation
219
    }
220
221
    /**
222
     * @When I delete taxon named :name
223
     */
224
    public function iDeleteTaxonNamed($name)
225
    {
226
        $this->createPage->open();
227
        $this->createPage->deleteTaxonOnPageByName($name);
228
    }
229
230
    /**
231
     * @When I add it
232
     * @When I try to add it
233
     */
234
    public function iAddIt()
235
    {
236
        $this->createPage->create();
237
    }
238
239
    /**
240
     * @When I save my changes
241
     * @When I try to save my changes
242
     */
243
    public function iSaveMyChanges()
244
    {
245
        $this->updatePage->saveChanges();
246
    }
247
248
    /**
249
     * @Then /^the ("[^"]+" taxon) should appear in the registry$/
250
     */
251
    public function theTaxonShouldAppearInTheRegistry(TaxonInterface $taxon)
252
    {
253
        $this->updatePage->open(['id' => $taxon->getId()]);
254
        Assert::true(
255
            $this->updatePage->hasResourceValues(['code' => $taxon->getCode()]),
256
            sprintf('Taxon %s should be in the registry.', $taxon->getName())
257
        );
258
    }
259
260
    /**
261
     * @Then this taxon :element should be :value
262
     */
263
    public function thisTaxonElementShouldBe($element, $value)
264
    {
265
        Assert::true(
266
            $this->updatePage->hasResourceValues([$element => $value]),
267
            sprintf('Taxon with %s should have %s value.', $element, $value)
268
        );
269
    }
270
271
    /**
272
     * @Then this taxon should have slug :value in :language
273
     */
274
    public function thisTaxonElementShouldHaveSlugIn($value, $language = null)
275
    {
276
        if (null !== $language) {
277
            $this->updatePage->activateLanguageTab($language);
278
        }
279
280
        Assert::same(
281
            $this->updatePage->getSlug($language),
282
            $value,
283
            sprintf('Taxon should have slug "%s" but it has not.', $value)
284
        );
285
    }
286
287
    /**
288
     * @Then the code field should be disabled
289
     */
290
    public function theCodeFieldShouldBeDisabled()
291
    {
292
        Assert::true(
293
            $this->updatePage->isCodeDisabled(),
294
            'Code field should be disabled but it is not.'
295
        );
296
    }
297
298
    /**
299
     * @Then /^the slug of the ("[^"]+" taxon) should(?:| still) be "([^"]+)"$/
300
     */
301
    public function productSlugShouldBe(TaxonInterface $taxon, $slug)
302
    {
303
        $this->updatePage->open(['id' => $taxon->getId()]);
304
305
        Assert::true(
306
            $this->updatePage->hasResourceValues(['slug' => $slug]),
307
            sprintf('Taxon\'s slug should be %s.', $slug)
308
        );
309
    }
310
311
    /**
312
     * @Then /^this taxon should (belongs to "[^"]+")$/
313
     */
314
    public function thisTaxonShouldBelongsTo(TaxonInterface $taxon)
315
    {
316
        Assert::true(
317
            $this->updatePage->hasResourceValues(['parent' => $taxon->getId()]),
318
            sprintf('Current taxon should have %s parent taxon.', $taxon->getName())
319
        );
320
    }
321
322
    /**
323
     * @Given it should not belong to any other taxon
324
     */
325
    public function itShouldNotBelongToAnyOtherTaxon()
326
    {
327
        $parent = $this->updatePage->getParent();
328
329
        Assert::isEmpty(
330
            $parent,
331
            sprintf('Current taxon should not belong to any other, but it does belong to "%s"', $parent)
332
        );
333
    }
334
335
    /**
336
     * @Then I should be notified that taxon with this code already exists
337
     */
338
    public function iShouldBeNotifiedThatTaxonWithThisCodeAlreadyExists()
339
    {
340
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
341
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
342
343
        Assert::same($currentPage->getValidationMessage('code'), 'Taxon with given code already exists.');
344
    }
345
346
    /**
347
     * @Then I should be notified that taxon slug must be unique
348
     */
349
    public function iShouldBeNotifiedThatTaxonSlugMustBeUnique()
350
    {
351
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
352
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
353
354
        Assert::same($currentPage->getValidationMessage('slug'), 'Taxon slug must be unique.');
355
    }
356
357
    /**
358
     * @Then I should be notified that :element is required
359
     */
360
    public function iShouldBeNotifiedThatIsRequired($element)
361
    {
362
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
363
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
364
365
        Assert::same($currentPage->getValidationMessage($element), sprintf('Please enter taxon %s.', $element));
366
    }
367
368
    /**
369
     * @Then /^there should still be only one taxon with code "([^"]+)"$/
370
     */
371
    public function thereShouldStillBeOnlyOneTaxonWithCode($code)
372
    {
373
        Assert::true(
374
            $this->updatePage->hasResourceValues(['code' => $code]),
375
            sprintf('Taxon with code %s cannot be found.', $code)
376
        );
377
    }
378
379
    /**
380
     * @Then /^Taxon named "([^"]+)" should not be added$/
381
     * @Then the taxon named :name should no longer exist in the registry
382
     */
383
    public function taxonNamedShouldNotBeAdded($name)
384
    {
385
        Assert::eq(
386
            0,
387
            $this->createPage->countTaxonsByName($name),
388
            sprintf('Taxon %s should not exist.', $name)
389
        );
390
    }
391
392
    /**
393
     * @Then /^I should see (\d+) taxons on the list$/
394
     */
395
    public function iShouldSeeTaxonsInTheList($number)
396
    {
397
        $taxonsOnPage = $this->createPage->countTaxons();
398
399
        Assert::same(
400
            (int) $number,
401
            $taxonsOnPage,
402
            sprintf('On list should be %d taxons but get %d.', $number, $taxonsOnPage)
403
        );
404
    }
405
406
    /**
407
     * @Then I should see the taxon named :name in the list
408
     */
409
    public function iShouldSeeTheTaxonNamedInTheList($name)
410
    {
411
        Assert::eq(
412
            1,
413
            $this->createPage->countTaxonsByName($name),
414
            sprintf('Taxon %s does not exist or multiple taxons with this name exist.', $name)
415
        );
416
    }
417
418
    /**
419
     * @When I attach the :path image with a code :code
420
     */
421
    public function iAttachImageWithACode($path, $code)
422
    {
423
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
424
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
425
426
        $currentPage->attachImage($path, $code);
427
    }
428
429
    /**
430
     * @When I attach the :path image without a code
431
     */
432
    public function iAttachImageWithoutACode($path)
433
    {
434
        $this->updatePage->attachImage($path);
435
    }
436
437
    /**
438
     * @Then /^this taxon should have(?:| also) an image with a code "([^"]*)"$/
439
     */
440
    public function thisTaxonShouldHaveAnImageWithCode($code)
441
    {
442
        Assert::true(
443
            $this->updatePage->isImageWithCodeDisplayed($code),
444
            sprintf('Image with a code %s should have been displayed.', $code)
445
        );
446
    }
447
448
    /**
449
     * @Then /^this taxon should not have(?:| also) an image with a code "([^"]*)"$/
450
     */
451
    public function thisTaxonShouldNotHaveAnImageWithCode($code)
452
    {
453
        Assert::false(
454
            $this->updatePage->isImageWithCodeDisplayed($code),
455
            sprintf('Image with a code %s should not have been displayed.', $code)
456
        );
457
    }
458
459
    /**
460
     * @When /^I remove(?:| also) an image with a code "([^"]*)"$/
461
     */
462
    public function iRemoveAnImageWithACode($code)
463
    {
464
        $this->updatePage->removeImageWithCode($code);
465
    }
466
467
    /**
468
     * @When I remove the first image
469
     */
470
    public function iRemoveTheFirstImage()
471
    {
472
        $this->updatePage->removeFirstImage();
473
    }
474
475
    /**
476
     * @Then /^(this taxon) should not have any images$/
477
     */
478
    public function thisTaxonShouldNotHaveImages(TaxonInterface $taxon)
479
    {
480
        $this->iWantToModifyATaxon($taxon);
481
482
        Assert::eq(
483
            0,
484
            $this->updatePage->countImages(),
485
            'This taxon has %2$s, but it should not have.'
486
        );
487
    }
488
489
    /**
490
     * @When I change the image with the :code code to :path
491
     */
492
    public function iChangeItsImageToPathForTheCode($path, $code)
493
    {
494
        $this->updatePage->changeImageWithCode($code, $path);
495
    }
496
497
    /**
498
     * @Then I should be notified that the image with this code already exists
499
     */
500
    public function iShouldBeNotifiedThatTheImageWithThisCodeAlreadyExists()
501
    {
502
        Assert::same($this->updatePage->getValidationMessageForImage(), 'Image code must be unique within this taxon.');
503
    }
504
505
    /**
506
     * @Then I should be notified that an image code is required
507
     */
508
    public function iShouldBeNotifiedThatAnImageCodeIsRequired()
509
    {
510
        Assert::same(
511
            $this->updatePage->getValidationMessageForImage(),
512
            'Please enter an image code.'
513
        );
514
    }
515
516
    /**
517
     * @Then there should still be only one image in the :taxon taxon
518
     */
519
    public function thereShouldStillBeOnlyOneImageInThisTaxon(TaxonInterface $taxon)
520
    {
521
        $this->iWantToModifyATaxon($taxon);
522
523
        Assert::eq(
524
            1,
525
            $this->updatePage->countImages(),
526
            'This taxon has %2$s images, but it should have only one.'
527
        );
528
    }
529
530
    /**
531
     * @Then the image code field should be disabled
532
     */
533
    public function theImageCodeFieldShouldBeDisabled()
534
    {
535
        Assert::true(
536
            $this->updatePage->isImageCodeDisabled(),
537
            'Image code field should be disabled but it is not.'
538
        );
539
    }
540
541
    /**
542
     * @Then I should be notified that the :imageNumber image should have an unique code
543
     */
544
    public function iShouldBeNotifiedThatTheFirstImageShouldHaveAnUniqueCode($imageNumber)
545
    {
546
        preg_match_all('!\d+!', $imageNumber, $matches);
547
548
        Assert::same(
549
            $this->updatePage->getValidationMessageForImageAtPlace(((int) $matches[0][0]) - 1),
550
            'Image code must be unique within this taxon.'
551
        );
552
    }
553
554
    /**
555
     * @Then the first taxon on the list should be :taxon
556
     */
557
    public function theFirstTaxonOnTheListShouldBe(TaxonInterface $taxon)
558
    {
559
        $this->createPage->open();
560
        $this->createPage->waitForTaxonRelocation($taxon, 0);
561
562
        Assert::same(
563
            $this->createPage->getLeafNameFromPosition(0),
564
            $taxon->getName(),
565
            sprintf(
566
                'Expected %s as a first taxon, but got %s.',
567
                $taxon->getName(),
568
                $this->createPage->getLeafNameFromPosition(0)
569
            )
570
        );
571
    }
572
573
    /**
574
     * @Then they should have order like :firstTaxonName, :secondTaxonName, :thirdTaxonName and :fourthTaxonName
575
     */
576
    public function theyShouldHaveOrderLikeAnd(...$taxonsNames)
577
    {
578
        $leaves = $this->createPage->getLeaves();
579
580
        foreach ($leaves as $key => $leaf) {
581
            Assert::contains($taxonsNames[$key], $leaf->getText());
582
        }
583
    }
584
}
585