Completed
Push — to-be-a-hat-or-not-to-be-kuhwa ( b72886...e84c48 )
by Kamil
19:11
created

theyShouldHaveOrderLikeAnd()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
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\TaxonInterface;
21
use Webmozart\Assert\Assert;
22
23
/**
24
 * @author Arkadiusz Krakowiak <[email protected]>
25
 */
26
final class ManagingTaxonsContext implements Context
27
{
28
    /**
29
     * @var SharedStorageInterface
30
     */
31
    private $sharedStorage;
32
33
    /**
34
     * @var CreatePageInterface
35
     */
36
    private $createPage;
37
38
    /**
39
     * @var CreateForParentPageInterface
40
     */
41
    private $createForParentPage;
42
43
    /**
44
     * @var UpdatePageInterface
45
     */
46
    private $updatePage;
47
48
    /**
49
     * @var CurrentPageResolverInterface
50
     */
51
    private $currentPageResolver;
52
53
    /**
54
     * @param SharedStorageInterface $sharedStorage
55
     * @param CreatePageInterface $createPage
56
     * @param CreateForParentPageInterface $createForParentPage
57
     * @param UpdatePageInterface $updatePage
58
     * @param CurrentPageResolverInterface $currentPageResolver
59
     */
60
    public function __construct(
61
        SharedStorageInterface $sharedStorage,
62
        CreatePageInterface $createPage,
63
        CreateForParentPageInterface $createForParentPage,
64
        UpdatePageInterface $updatePage,
65
        CurrentPageResolverInterface $currentPageResolver
66
    ) {
67
        $this->sharedStorage = $sharedStorage;
68
        $this->createPage = $createPage;
69
        $this->createForParentPage = $createForParentPage;
70
        $this->updatePage = $updatePage;
71
        $this->currentPageResolver = $currentPageResolver;
72
    }
73
74
    /**
75
     * @Given I want to create a new taxon
76
     * @Given I want to see all taxons in store
77
     */
78
    public function iWantToCreateANewTaxon()
79
    {
80
        $this->createPage->open();
81
    }
82
83
    /**
84
     * @Given I want to create a new taxon for :taxon
85
     */
86
    public function iWantToCreateANewTaxonForParent(TaxonInterface $taxon)
87
    {
88
        $this->createForParentPage->open(['id' => $taxon->getId()]);
89
    }
90
91
    /**
92
     * @Given /^I want to modify the ("[^"]+" taxon)$/
93
     */
94
    public function iWantToModifyATaxon(TaxonInterface $taxon)
95
    {
96
        $this->sharedStorage->set('taxon', $taxon);
97
98
        $this->updatePage->open(['id' => $taxon->getId()]);
99
    }
100
101
    /**
102
     * @When I specify its code as :code
103
     */
104
    public function iSpecifyItsCodeAs($code)
105
    {
106
        $this->createPage->specifyCode($code);
107
    }
108
109
    /**
110
     * @When I name it :name in :language
111
     */
112
    public function iNameItIn($name, $language)
113
    {
114
        $this->createPage->nameIt($name, $language);
115
    }
116
117
    /**
118
     * @When I rename it to :name in :language
119
     */
120
    public function iRenameItIn($name, $language)
121
    {
122
        $this->updatePage->nameIt($name, $language);
123
    }
124
125
    /**
126
     * @When I change its description to :description in :language
127
     */
128
    public function iChangeItsDescriptionToIn($description, $language)
129
    {
130
        $this->updatePage->describeItAs($description, $language);
131
    }
132
133
    /**
134
     * @When I specify its permalink as :permalink in :language
135
     */
136
    public function iSpecifyItsPermalinkAs($permalink, $language)
137
    {
138
        $this->createPage->specifyPermalink($permalink, $language);
139
    }
140
141
    /**
142
     * @When I change its permalink to :permalink in :language
143
     */
144
    public function iChangeItsPermalinkToIn($permalink, $language)
145
    {
146
        $this->updatePage->specifyPermalink($permalink, $language);
147
    }
148
149
    /**
150
     * @When I describe it as :description in :language
151
     */
152
    public function iDescribeItAs($description, $language)
153
    {
154
        $this->createPage->describeItAs($description, $language);
155
    }
156
157
    /**
158
     * @When /^I move up ("[^"]+" taxon)$/
159
     */
160
    public function iWantToMoveUpTaxon(TaxonInterface $taxon)
161
    {
162
        $this->createPage->moveUp($taxon);
163
    }
164
165
    /**
166
     * @When /^I move ("[^"]+" taxon) before ("[^"]+" taxon)$/
167
     */
168
    public function iMoveTaxonBeforeTaxon(TaxonInterface $taxonToMove, TaxonInterface $targetTaxon)
169
    {
170
        $this->createPage->insertBefore($taxonToMove, $targetTaxon);
171
    }
172
173
    /**
174
     * @Given /^I choose ("[^"]+" as a parent taxon)$/
175
     */
176
    public function iChooseAsAParentTaxon(TaxonInterface $taxon)
177
    {
178
        $this->createPage->chooseParent($taxon);
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 the code field should be disabled
257
     */
258
    public function theCodeFieldShouldBeDisabled()
259
    {
260
        Assert::true(
261
            $this->updatePage->isCodeDisabled(),
262
            'Code field should be disabled but it is not.'
263
        );
264
    }
265
266
    /**
267
     * @Then /^this taxon should (belongs to "[^"]+")$/
268
     */
269
    public function thisTaxonShouldBelongsTo(TaxonInterface $taxon)
270
    {
271
        Assert::true(
272
            $this->updatePage->hasResourceValues(['parent' => $taxon->getId()]),
273
            sprintf('Current taxon should have %s parent taxon.', $taxon->getName())
274
        );
275
    }
276
277
    /**
278
     * @Given it should not belong to any other taxon
279
     */
280
    public function itShouldNotBelongToAnyOtherTaxon()
281
    {
282
        $parent = $this->updatePage->getParent();
283
284
        Assert::isEmpty(
285
            $parent,
286
            sprintf('Current taxon should not belong to any other, but it does belong to "%s"', $parent)
287
        );
288
    }
289
290
    /**
291
     * @Then I should be notified that taxon with this code already exists
292
     */
293
    public function iShouldBeNotifiedThatTaxonWithThisCodeAlreadyExists()
294
    {
295
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
296
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
297
298
        Assert::same($currentPage->getValidationMessage('code'), 'Taxon with given code already exists.');
299
    }
300
301
    /**
302
     * @Then I should be notified that :element is required
303
     */
304
    public function iShouldBeNotifiedThatIsRequired($element)
305
    {
306
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
307
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
308
309
        Assert::same($currentPage->getValidationMessage($element), sprintf('Please enter taxon %s.', $element));
310
    }
311
312
    /**
313
     * @Then /^there should still be only one taxon with code "([^"]+)"$/
314
     */
315
    public function thereShouldStillBeOnlyOneTaxonWithCode($code)
316
    {
317
        Assert::true(
318
            $this->updatePage->hasResourceValues(['code' => $code]),
319
            sprintf('Taxon with code %s cannot be found.', $code)
320
        );
321
    }
322
323
    /**
324
     * @Then /^Taxon named "([^"]+)" should not be added$/
325
     * @Then the taxon named :name should no longer exist in the registry
326
     */
327
    public function taxonNamedShouldNotBeAdded($name)
328
    {
329
        Assert::eq(
330
            0,
331
            $this->createPage->countTaxonsByName($name),
332
            sprintf('Taxon %s should not exist.', $name)
333
        );
334
    }
335
336
    /**
337
     * @Then /^I should see (\d+) taxons on the list$/
338
     */
339
    public function iShouldSeeTaxonsInTheList($number)
340
    {
341
        $taxonsOnPage = $this->createPage->countTaxons();
342
343
        Assert::same(
344
            (int) $number,
345
            $taxonsOnPage,
346
            sprintf('On list should be %d taxons but get %d.', $number, $taxonsOnPage)
347
        );
348
    }
349
350
    /**
351
     * @Then I should see the taxon named :name in the list
352
     */
353
    public function iShouldSeeTheTaxonNamedInTheList($name)
354
    {
355
        Assert::eq(
356
            1,
357
            $this->createPage->countTaxonsByName($name),
358
            sprintf('Taxon %s does not exist or multiple taxons with this name exist.', $name)
359
        );
360
    }
361
362
    /**
363
     * @When I attach the :path image with a code :code
364
     */
365
    public function iAttachImageWithACode($path, $code)
366
    {
367
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
368
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
369
370
        $currentPage->attachImage($path, $code);
371
    }
372
373
    /**
374
     * @When I attach the :path image without a code
375
     */
376
    public function iAttachImageWithoutACode($path)
377
    {
378
        $this->updatePage->attachImage($path);
379
    }
380
381
    /**
382
     * @Then /^this taxon should have(?:| also) an image with a code "([^"]*)"$/
383
     */
384
    public function thisTaxonShouldHaveAnImageWithCode($code)
385
    {
386
        Assert::true(
387
            $this->updatePage->isImageWithCodeDisplayed($code),
388
            sprintf('Image with a code %s should have been displayed.', $code)
389
        );
390
    }
391
392
    /**
393
     * @Then /^this taxon should not have(?:| also) an image with a code "([^"]*)"$/
394
     */
395
    public function thisTaxonShouldNotHaveAnImageWithCode($code)
396
    {
397
        Assert::false(
398
            $this->updatePage->isImageWithCodeDisplayed($code),
399
            sprintf('Image with a code %s should not have been displayed.', $code)
400
        );
401
    }
402
403
    /**
404
     * @When /^I remove(?:| also) an image with a code "([^"]*)"$/
405
     */
406
    public function iRemoveAnImageWithACode($code)
407
    {
408
        $this->updatePage->removeImageWithCode($code);
409
    }
410
411
    /**
412
     * @When I remove the first image
413
     */
414
    public function iRemoveTheFirstImage()
415
    {
416
        $this->updatePage->removeFirstImage();
417
    }
418
419
    /**
420
     * @Then /^(this taxon) should not have any images$/
421
     */
422
    public function thisTaxonShouldNotHaveImages(TaxonInterface $taxon)
423
    {
424
        $this->iWantToModifyATaxon($taxon);
425
426
        Assert::eq(
427
            0,
428
            $this->updatePage->countImages(),
429
            'This taxon has %2$s, but it should not have.'
430
        );
431
    }
432
433
    /**
434
     * @When I change the image with the :code code to :path
435
     */
436
    public function iChangeItsImageToPathForTheCode($path, $code)
437
    {
438
        $this->updatePage->changeImageWithCode($code, $path);
439
    }
440
441
    /**
442
     * @Then I should be notified that the image with this code already exists
443
     */
444
    public function iShouldBeNotifiedThatTheImageWithThisCodeAlreadyExists()
445
    {
446
        Assert::same($this->updatePage->getValidationMessageForImage(), 'Image code must be unique within this taxon.');
447
    }
448
449
    /**
450
     * @Then I should be notified that an image code is required
451
     */
452
    public function iShouldBeNotifiedThatAnImageCodeIsRequired()
453
    {
454
        Assert::same(
455
            $this->updatePage->getValidationMessageForImage(),
456
            'Please enter an image code.'
457
        );
458
    }
459
460
    /**
461
     * @Then there should still be only one image in the :taxon taxon
462
     */
463
    public function thereShouldStillBeOnlyOneImageInThisTaxon(TaxonInterface $taxon)
464
    {
465
        $this->iWantToModifyATaxon($taxon);
466
467
        Assert::eq(
468
            1,
469
            $this->updatePage->countImages(),
470
            'This taxon has %2$s images, but it should have only one.'
471
        );
472
    }
473
474
    /**
475
     * @Then the image code field should be disabled
476
     */
477
    public function theImageCodeFieldShouldBeDisabled()
478
    {
479
        Assert::true(
480
            $this->updatePage->isImageCodeDisabled(),
481
            'Image code field should be disabled but it is not.'
482
        );
483
    }
484
485
    /**
486
     * @Then I should be notified that the :imageNumber image should have an unique code
487
     */
488
    public function iShouldBeNotifiedThatTheFirstImageShouldHaveAnUniqueCode($imageNumber)
489
    {
490
        preg_match_all('!\d+!', $imageNumber, $matches);
491
492
        Assert::same(
493
            $this->updatePage->getValidationMessageForImageAtPlace(((int) $matches[0][0]) - 1),
494
            'Image code must be unique within this taxon.'
495
        );
496
    }
497
498
    /**
499
     * @Then the first taxon on the list should be :taxon
500
     */
501
    public function theFirstTaxonOnTheListShouldBe(TaxonInterface $taxon)
502
    {
503
        Assert::same(
504
            $this->createPage->getFirstLeafName(),
505
            $taxon->getName(),
506
            sprintf(
507
                'Expected %s as a first taxon, but got %s.',
508
                $taxon->getName(),
509
                $this->createPage->getFirstLeafName()
510
            )
511
        );
512
    }
513
514
    /**
515
     * @Then they should have order like :firstTaxonName, :secondTaxonName, :thirdTaxonName and :fourthTaxonName
516
     */
517
    public function theyShouldHaveOrderLikeAnd(...$taxonsNames)
518
    {
519
        $leaves = $this->createPage->getLeaves();
520
521
        foreach ($leaves as $key => $leaf) {
522
            Assert::contains($taxonsNames[$key], $leaf->getText());
523
        }
524
    }
525
}
526