Completed
Push — remove-content-bundle ( 201341 )
by Kamil
34:43
created

theSlugFieldShouldNotBeEditable()   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
     */
129
    public function iSetItsSlugTo($slug)
130
    {
131
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
132
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([
133
            $this->createPage,
134
            $this->createForParentPage,
135
            $this->updatePage,
136
        ]);
137
138
        $currentPage->specifySlug($slug);
139
    }
140
141
    /**
142
     * @Then the slug field should not be editable
143
     */
144
    public function theSlugFieldShouldNotBeEditable()
145
    {
146
        Assert::true(
147
            $this->updatePage->isSlugReadOnly(),
148
            'Slug should be immutable, but it does not.'
149
        );
150
    }
151
152
    /**
153
     * @When I enable slug modification
154
     */
155
    public function iEnableSlugModification()
156
    {
157
        $this->updatePage->enableSlugModification();
158
    }
159
160
    /**
161
     * @When I change its description to :description in :language
162
     */
163
    public function iChangeItsDescriptionToIn($description, $language)
164
    {
165
        $this->updatePage->describeItAs($description, $language);
166
    }
167
168
    /**
169
     * @When I describe it as :description in :language
170
     */
171
    public function iDescribeItAs($description, $language)
172
    {
173
        $this->createPage->describeItAs($description, $language);
174
    }
175
176
    /**
177
     * @When /^I move up ("[^"]+" taxon)$/
178
     */
179
    public function iWantToMoveUpTaxon(TaxonInterface $taxon)
180
    {
181
        $this->createPage->moveUp($taxon);
182
    }
183
184
    /**
185
     * @When /^I move ("[^"]+" taxon) before ("[^"]+" taxon)$/
186
     */
187
    public function iMoveTaxonBeforeTaxon(TaxonInterface $taxonToMove, TaxonInterface $targetTaxon)
188
    {
189
        $this->createPage->insertBefore($taxonToMove, $targetTaxon);
190
    }
191
192
    /**
193
     * @Given /^I choose ("[^"]+" as a parent taxon)$/
194
     */
195
    public function iChooseAsAParentTaxon(TaxonInterface $taxon)
196
    {
197
        $this->createPage->chooseParent($taxon);
198
    }
199
200
    /**
201
     * @Given /^I change its (parent taxon to "[^"]+")$/
202
     */
203
    public function iChangeItsParentTaxonTo(TaxonInterface $taxon)
204
    {
205
        $this->updatePage->chooseParent($taxon);
206
    }
207
208
    /**
209
     * @When I do not specify its code
210
     */
211
    public function iDoNotSpecifyItsCode()
212
    {
213
        // Intentionally left blank to fulfill context expectation
214
    }
215
216
    /**
217
     * @When I do not specify its name
218
     */
219
    public function iDoNotSpecifyItsName()
220
    {
221
        // Intentionally left blank to fulfill context expectation
222
    }
223
224
    /**
225
     * @When I delete taxon named :name
226
     */
227
    public function iDeleteTaxonNamed($name)
228
    {
229
        $this->createPage->open();
230
        $this->createPage->deleteTaxonOnPageByName($name);
231
    }
232
233
    /**
234
     * @When I add it
235
     * @When I try to add it
236
     */
237
    public function iAddIt()
238
    {
239
        $this->createPage->create();
240
    }
241
242
    /**
243
     * @When I save my changes
244
     * @When I try to save my changes
245
     */
246
    public function iSaveMyChanges()
247
    {
248
        $this->updatePage->saveChanges();
249
    }
250
251
    /**
252
     * @Then /^the ("[^"]+" taxon) should appear in the registry$/
253
     */
254
    public function theTaxonShouldAppearInTheRegistry(TaxonInterface $taxon)
255
    {
256
        $this->updatePage->open(['id' => $taxon->getId()]);
257
        Assert::true(
258
            $this->updatePage->hasResourceValues(['code' => $taxon->getCode()]),
259
            sprintf('Taxon %s should be in the registry.', $taxon->getName())
260
        );
261
    }
262
263
    /**
264
     * @Then this taxon :element should be :value
265
     */
266
    public function thisTaxonElementShouldBe($element, $value)
267
    {
268
        Assert::true(
269
            $this->updatePage->hasResourceValues([$element => $value]),
270
            sprintf('Taxon with %s should have %s value.', $element, $value)
271
        );
272
    }
273
274
    /**
275
     * @Then the code field should be disabled
276
     */
277
    public function theCodeFieldShouldBeDisabled()
278
    {
279
        Assert::true(
280
            $this->updatePage->isCodeDisabled(),
281
            'Code field should be disabled but it is not.'
282
        );
283
    }
284
285
    /**
286
     * @Then /^the slug of the ("[^"]+" taxon) should(?:| still) be "([^"]+)"$/
287
     */
288
    public function productSlugShouldBe(TaxonInterface $taxon, $slug)
289
    {
290
        $this->updatePage->open(['id' => $taxon->getId()]);
291
292
        Assert::true(
293
            $this->updatePage->hasResourceValues(['slug' => $slug]),
294
            sprintf('Taxon\'s slug should be %s.', $slug)
295
        );
296
    }
297
298
    /**
299
     * @Then /^this taxon should (belongs to "[^"]+")$/
300
     */
301
    public function thisTaxonShouldBelongsTo(TaxonInterface $taxon)
302
    {
303
        Assert::true(
304
            $this->updatePage->hasResourceValues(['parent' => $taxon->getId()]),
305
            sprintf('Current taxon should have %s parent taxon.', $taxon->getName())
306
        );
307
    }
308
309
    /**
310
     * @Given it should not belong to any other taxon
311
     */
312
    public function itShouldNotBelongToAnyOtherTaxon()
313
    {
314
        $parent = $this->updatePage->getParent();
315
316
        Assert::isEmpty(
317
            $parent,
318
            sprintf('Current taxon should not belong to any other, but it does belong to "%s"', $parent)
319
        );
320
    }
321
322
    /**
323
     * @Then I should be notified that taxon with this code already exists
324
     */
325
    public function iShouldBeNotifiedThatTaxonWithThisCodeAlreadyExists()
326
    {
327
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
328
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
329
330
        Assert::same($currentPage->getValidationMessage('code'), 'Taxon with given code already exists.');
331
    }
332
333
    /**
334
     * @Then I should be notified that :element is required
335
     */
336
    public function iShouldBeNotifiedThatIsRequired($element)
337
    {
338
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
339
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
340
341
        Assert::same($currentPage->getValidationMessage($element), sprintf('Please enter taxon %s.', $element));
342
    }
343
344
    /**
345
     * @Then /^there should still be only one taxon with code "([^"]+)"$/
346
     */
347
    public function thereShouldStillBeOnlyOneTaxonWithCode($code)
348
    {
349
        Assert::true(
350
            $this->updatePage->hasResourceValues(['code' => $code]),
351
            sprintf('Taxon with code %s cannot be found.', $code)
352
        );
353
    }
354
355
    /**
356
     * @Then /^Taxon named "([^"]+)" should not be added$/
357
     * @Then the taxon named :name should no longer exist in the registry
358
     */
359
    public function taxonNamedShouldNotBeAdded($name)
360
    {
361
        Assert::eq(
362
            0,
363
            $this->createPage->countTaxonsByName($name),
364
            sprintf('Taxon %s should not exist.', $name)
365
        );
366
    }
367
368
    /**
369
     * @Then /^I should see (\d+) taxons on the list$/
370
     */
371
    public function iShouldSeeTaxonsInTheList($number)
372
    {
373
        $taxonsOnPage = $this->createPage->countTaxons();
374
375
        Assert::same(
376
            (int) $number,
377
            $taxonsOnPage,
378
            sprintf('On list should be %d taxons but get %d.', $number, $taxonsOnPage)
379
        );
380
    }
381
382
    /**
383
     * @Then I should see the taxon named :name in the list
384
     */
385
    public function iShouldSeeTheTaxonNamedInTheList($name)
386
    {
387
        Assert::eq(
388
            1,
389
            $this->createPage->countTaxonsByName($name),
390
            sprintf('Taxon %s does not exist or multiple taxons with this name exist.', $name)
391
        );
392
    }
393
394
    /**
395
     * @When I attach the :path image with a code :code
396
     */
397
    public function iAttachImageWithACode($path, $code)
398
    {
399
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
400
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
401
402
        $currentPage->attachImage($path, $code);
403
    }
404
405
    /**
406
     * @When I attach the :path image without a code
407
     */
408
    public function iAttachImageWithoutACode($path)
409
    {
410
        $this->updatePage->attachImage($path);
411
    }
412
413
    /**
414
     * @Then /^this taxon should have(?:| also) an image with a code "([^"]*)"$/
415
     */
416
    public function thisTaxonShouldHaveAnImageWithCode($code)
417
    {
418
        Assert::true(
419
            $this->updatePage->isImageWithCodeDisplayed($code),
420
            sprintf('Image with a code %s should have been displayed.', $code)
421
        );
422
    }
423
424
    /**
425
     * @Then /^this taxon should not have(?:| also) an image with a code "([^"]*)"$/
426
     */
427
    public function thisTaxonShouldNotHaveAnImageWithCode($code)
428
    {
429
        Assert::false(
430
            $this->updatePage->isImageWithCodeDisplayed($code),
431
            sprintf('Image with a code %s should not have been displayed.', $code)
432
        );
433
    }
434
435
    /**
436
     * @When /^I remove(?:| also) an image with a code "([^"]*)"$/
437
     */
438
    public function iRemoveAnImageWithACode($code)
439
    {
440
        $this->updatePage->removeImageWithCode($code);
441
    }
442
443
    /**
444
     * @When I remove the first image
445
     */
446
    public function iRemoveTheFirstImage()
447
    {
448
        $this->updatePage->removeFirstImage();
449
    }
450
451
    /**
452
     * @Then /^(this taxon) should not have any images$/
453
     */
454
    public function thisTaxonShouldNotHaveImages(TaxonInterface $taxon)
455
    {
456
        $this->iWantToModifyATaxon($taxon);
457
458
        Assert::eq(
459
            0,
460
            $this->updatePage->countImages(),
461
            'This taxon has %2$s, but it should not have.'
462
        );
463
    }
464
465
    /**
466
     * @When I change the image with the :code code to :path
467
     */
468
    public function iChangeItsImageToPathForTheCode($path, $code)
469
    {
470
        $this->updatePage->changeImageWithCode($code, $path);
471
    }
472
473
    /**
474
     * @Then I should be notified that the image with this code already exists
475
     */
476
    public function iShouldBeNotifiedThatTheImageWithThisCodeAlreadyExists()
477
    {
478
        Assert::same($this->updatePage->getValidationMessageForImage(), 'Image code must be unique within this taxon.');
479
    }
480
481
    /**
482
     * @Then I should be notified that an image code is required
483
     */
484
    public function iShouldBeNotifiedThatAnImageCodeIsRequired()
485
    {
486
        Assert::same(
487
            $this->updatePage->getValidationMessageForImage(),
488
            'Please enter an image code.'
489
        );
490
    }
491
492
    /**
493
     * @Then there should still be only one image in the :taxon taxon
494
     */
495
    public function thereShouldStillBeOnlyOneImageInThisTaxon(TaxonInterface $taxon)
496
    {
497
        $this->iWantToModifyATaxon($taxon);
498
499
        Assert::eq(
500
            1,
501
            $this->updatePage->countImages(),
502
            'This taxon has %2$s images, but it should have only one.'
503
        );
504
    }
505
506
    /**
507
     * @Then the image code field should be disabled
508
     */
509
    public function theImageCodeFieldShouldBeDisabled()
510
    {
511
        Assert::true(
512
            $this->updatePage->isImageCodeDisabled(),
513
            'Image code field should be disabled but it is not.'
514
        );
515
    }
516
517
    /**
518
     * @Then I should be notified that the :imageNumber image should have an unique code
519
     */
520
    public function iShouldBeNotifiedThatTheFirstImageShouldHaveAnUniqueCode($imageNumber)
521
    {
522
        preg_match_all('!\d+!', $imageNumber, $matches);
523
524
        Assert::same(
525
            $this->updatePage->getValidationMessageForImageAtPlace(((int) $matches[0][0]) - 1),
526
            'Image code must be unique within this taxon.'
527
        );
528
    }
529
530
    /**
531
     * @Then the first taxon on the list should be :taxon
532
     */
533
    public function theFirstTaxonOnTheListShouldBe(TaxonInterface $taxon)
534
    {
535
        Assert::same(
536
            $this->createPage->getFirstLeafName(),
537
            $taxon->getName(),
538
            sprintf(
539
                'Expected %s as a first taxon, but got %s.',
540
                $taxon->getName(),
541
                $this->createPage->getFirstLeafName()
542
            )
543
        );
544
    }
545
546
    /**
547
     * @Then they should have order like :firstTaxonName, :secondTaxonName, :thirdTaxonName and :fourthTaxonName
548
     */
549
    public function theyShouldHaveOrderLikeAnd(...$taxonsNames)
550
    {
551
        $leaves = $this->createPage->getLeaves();
552
553
        foreach ($leaves as $key => $leaf) {
554
            Assert::contains($taxonsNames[$key], $leaf->getText());
555
        }
556
    }
557
}
558