Completed
Push — unused-definitions ( d9908f )
by Kamil
18:33
created

ManagingTaxonsContext::theFirstTaxonOnTheListShouldBe()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
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\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
     * @Given /^I change its (parent taxon to "[^"]+")$/
183
     */
184
    public function iChangeItsParentTaxonTo(TaxonInterface $taxon)
185
    {
186
        $this->updatePage->chooseParent($taxon);
187
    }
188
189
    /**
190
     * @When I do not specify its code
191
     */
192
    public function iDoNotSpecifyItsCode()
193
    {
194
        // Intentionally left blank to fulfill context expectation
195
    }
196
197
    /**
198
     * @When I do not specify its name
199
     */
200
    public function iDoNotSpecifyItsName()
201
    {
202
        // Intentionally left blank to fulfill context expectation
203
    }
204
205
    /**
206
     * @When I delete taxon named :name
207
     */
208
    public function iDeleteTaxonNamed($name)
209
    {
210
        $this->createPage->open();
211
        $this->createPage->deleteTaxonOnPageByName($name);
212
    }
213
214
    /**
215
     * @When I add it
216
     * @When I try to add it
217
     */
218
    public function iAddIt()
219
    {
220
        $this->createPage->create();
221
    }
222
223
    /**
224
     * @When I save my changes
225
     * @When I try to save my changes
226
     */
227
    public function iSaveMyChanges()
228
    {
229
        $this->updatePage->saveChanges();
230
    }
231
232
    /**
233
     * @Then /^the ("[^"]+" taxon) should appear in the registry$/
234
     */
235
    public function theTaxonShouldAppearInTheRegistry(TaxonInterface $taxon)
236
    {
237
        $this->updatePage->open(['id' => $taxon->getId()]);
238
        Assert::true(
239
            $this->updatePage->hasResourceValues(['code' => $taxon->getCode()]),
240
            sprintf('Taxon %s should be in the registry.', $taxon->getName())
241
        );
242
    }
243
244
    /**
245
     * @Then this taxon :element should be :value
246
     */
247
    public function thisTaxonElementShouldBe($element, $value)
248
    {
249
        Assert::true(
250
            $this->updatePage->hasResourceValues([$element => $value]),
251
            sprintf('Taxon with %s should have %s value.', $element, $value)
252
        );
253
    }
254
255
    /**
256
     * @Then this taxon should have slug :value in :language
257
     */
258
    public function thisTaxonElementShouldHaveSlugIn($value, $language = null)
259
    {
260
        if (null !== $language) {
261
            $this->updatePage->activateLanguageTab($language);
262
        }
263
264
        Assert::same(
265
            $this->updatePage->getSlug($language),
266
            $value,
267
            sprintf('Taxon should have slug "%s" but it has not.', $value)
268
        );
269
    }
270
271
    /**
272
     * @Then the code field should be disabled
273
     */
274
    public function theCodeFieldShouldBeDisabled()
275
    {
276
        Assert::true(
277
            $this->updatePage->isCodeDisabled(),
278
            'Code field should be disabled but it is not.'
279
        );
280
    }
281
282
    /**
283
     * @Then /^the slug of the ("[^"]+" taxon) should(?:| still) be "([^"]+)"$/
284
     */
285
    public function productSlugShouldBe(TaxonInterface $taxon, $slug)
286
    {
287
        $this->updatePage->open(['id' => $taxon->getId()]);
288
289
        Assert::true(
290
            $this->updatePage->hasResourceValues(['slug' => $slug]),
291
            sprintf('Taxon\'s slug should be %s.', $slug)
292
        );
293
    }
294
295
    /**
296
     * @Then /^this taxon should (belongs to "[^"]+")$/
297
     */
298
    public function thisTaxonShouldBelongsTo(TaxonInterface $taxon)
299
    {
300
        Assert::true(
301
            $this->updatePage->hasResourceValues(['parent' => $taxon->getId()]),
302
            sprintf('Current taxon should have %s parent taxon.', $taxon->getName())
303
        );
304
    }
305
306
    /**
307
     * @Given it should not belong to any other taxon
308
     */
309
    public function itShouldNotBelongToAnyOtherTaxon()
310
    {
311
        $parent = $this->updatePage->getParent();
312
313
        Assert::isEmpty(
314
            $parent,
315
            sprintf('Current taxon should not belong to any other, but it does belong to "%s"', $parent)
316
        );
317
    }
318
319
    /**
320
     * @Then I should be notified that taxon with this code already exists
321
     */
322
    public function iShouldBeNotifiedThatTaxonWithThisCodeAlreadyExists()
323
    {
324
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
325
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
326
327
        Assert::same($currentPage->getValidationMessage('code'), 'Taxon with given code already exists.');
328
    }
329
330
    /**
331
     * @Then I should be notified that taxon slug must be unique
332
     */
333
    public function iShouldBeNotifiedThatTaxonSlugMustBeUnique()
334
    {
335
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
336
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
337
338
        Assert::same($currentPage->getValidationMessage('slug'), 'Taxon slug must be unique.');
339
    }
340
341
    /**
342
     * @Then I should be notified that :element is required
343
     */
344
    public function iShouldBeNotifiedThatIsRequired($element)
345
    {
346
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
347
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
348
349
        Assert::same($currentPage->getValidationMessage($element), sprintf('Please enter taxon %s.', $element));
350
    }
351
352
    /**
353
     * @Then /^there should still be only one taxon with code "([^"]+)"$/
354
     */
355
    public function thereShouldStillBeOnlyOneTaxonWithCode($code)
356
    {
357
        Assert::true(
358
            $this->updatePage->hasResourceValues(['code' => $code]),
359
            sprintf('Taxon with code %s cannot be found.', $code)
360
        );
361
    }
362
363
    /**
364
     * @Then /^Taxon named "([^"]+)" should not be added$/
365
     * @Then the taxon named :name should no longer exist in the registry
366
     */
367
    public function taxonNamedShouldNotBeAdded($name)
368
    {
369
        Assert::eq(
370
            0,
371
            $this->createPage->countTaxonsByName($name),
372
            sprintf('Taxon %s should not exist.', $name)
373
        );
374
    }
375
376
    /**
377
     * @Then /^I should see (\d+) taxons on the list$/
378
     */
379
    public function iShouldSeeTaxonsInTheList($number)
380
    {
381
        $taxonsOnPage = $this->createPage->countTaxons();
382
383
        Assert::same(
384
            (int) $number,
385
            $taxonsOnPage,
386
            sprintf('On list should be %d taxons but get %d.', $number, $taxonsOnPage)
387
        );
388
    }
389
390
    /**
391
     * @Then I should see the taxon named :name in the list
392
     */
393
    public function iShouldSeeTheTaxonNamedInTheList($name)
394
    {
395
        Assert::eq(
396
            1,
397
            $this->createPage->countTaxonsByName($name),
398
            sprintf('Taxon %s does not exist or multiple taxons with this name exist.', $name)
399
        );
400
    }
401
402
    /**
403
     * @When I attach the :path image with a code :code
404
     */
405
    public function iAttachImageWithACode($path, $code)
406
    {
407
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
408
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
409
410
        $currentPage->attachImage($path, $code);
411
    }
412
413
    /**
414
     * @When I attach the :path image without a code
415
     */
416
    public function iAttachImageWithoutACode($path)
417
    {
418
        $this->updatePage->attachImage($path);
419
    }
420
421
    /**
422
     * @Then /^this taxon should have(?:| also) an image with a code "([^"]*)"$/
423
     */
424
    public function thisTaxonShouldHaveAnImageWithCode($code)
425
    {
426
        Assert::true(
427
            $this->updatePage->isImageWithCodeDisplayed($code),
428
            sprintf('Image with a code %s should have been displayed.', $code)
429
        );
430
    }
431
432
    /**
433
     * @Then /^this taxon should not have(?:| also) an image with a code "([^"]*)"$/
434
     */
435
    public function thisTaxonShouldNotHaveAnImageWithCode($code)
436
    {
437
        Assert::false(
438
            $this->updatePage->isImageWithCodeDisplayed($code),
439
            sprintf('Image with a code %s should not have been displayed.', $code)
440
        );
441
    }
442
443
    /**
444
     * @When /^I remove(?:| also) an image with a code "([^"]*)"$/
445
     */
446
    public function iRemoveAnImageWithACode($code)
447
    {
448
        $this->updatePage->removeImageWithCode($code);
449
    }
450
451
    /**
452
     * @When I remove the first image
453
     */
454
    public function iRemoveTheFirstImage()
455
    {
456
        $this->updatePage->removeFirstImage();
457
    }
458
459
    /**
460
     * @Then /^(this taxon) should not have any images$/
461
     */
462
    public function thisTaxonShouldNotHaveImages(TaxonInterface $taxon)
463
    {
464
        $this->iWantToModifyATaxon($taxon);
465
466
        Assert::eq(
467
            0,
468
            $this->updatePage->countImages(),
469
            'This taxon has %2$s, but it should not have.'
470
        );
471
    }
472
473
    /**
474
     * @When I change the image with the :code code to :path
475
     */
476
    public function iChangeItsImageToPathForTheCode($path, $code)
477
    {
478
        $this->updatePage->changeImageWithCode($code, $path);
479
    }
480
481
    /**
482
     * @Then I should be notified that the image with this code already exists
483
     */
484
    public function iShouldBeNotifiedThatTheImageWithThisCodeAlreadyExists()
485
    {
486
        Assert::same($this->updatePage->getValidationMessageForImage(), 'Image code must be unique within this taxon.');
487
    }
488
489
    /**
490
     * @Then I should be notified that an image code is required
491
     */
492
    public function iShouldBeNotifiedThatAnImageCodeIsRequired()
493
    {
494
        Assert::same(
495
            $this->updatePage->getValidationMessageForImage(),
496
            'Please enter an image code.'
497
        );
498
    }
499
500
    /**
501
     * @Then there should still be only one image in the :taxon taxon
502
     */
503
    public function thereShouldStillBeOnlyOneImageInThisTaxon(TaxonInterface $taxon)
504
    {
505
        $this->iWantToModifyATaxon($taxon);
506
507
        Assert::eq(
508
            1,
509
            $this->updatePage->countImages(),
510
            'This taxon has %2$s images, but it should have only one.'
511
        );
512
    }
513
514
    /**
515
     * @Then the image code field should be disabled
516
     */
517
    public function theImageCodeFieldShouldBeDisabled()
518
    {
519
        Assert::true(
520
            $this->updatePage->isImageCodeDisabled(),
521
            'Image code field should be disabled but it is not.'
522
        );
523
    }
524
525
    /**
526
     * @Then I should be notified that the :imageNumber image should have an unique code
527
     */
528
    public function iShouldBeNotifiedThatTheFirstImageShouldHaveAnUniqueCode($imageNumber)
529
    {
530
        preg_match_all('!\d+!', $imageNumber, $matches);
531
532
        Assert::same(
533
            $this->updatePage->getValidationMessageForImageAtPlace(((int) $matches[0][0]) - 1),
534
            'Image code must be unique within this taxon.'
535
        );
536
    }
537
}
538