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

ManagingTaxonsContext::iSetItsSlugToIn()   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\Taxon\CreateForParentPageInterface;
19
use Sylius\Behat\Page\Admin\Taxon\CreatePageInterface;
20
use Sylius\Behat\Page\Admin\Taxon\UpdatePageInterface;
21
use Sylius\Behat\Service\NotificationCheckerInterface;
22
use Sylius\Behat\Service\Resolver\CurrentPageResolverInterface;
23
use Sylius\Behat\Service\SharedStorageInterface;
24
use Sylius\Component\Core\Model\TaxonInterface;
25
use Webmozart\Assert\Assert;
26
27
final class ManagingTaxonsContext implements Context
28
{
29
    /** @var SharedStorageInterface */
30
    private $sharedStorage;
31
32
    /** @var CreatePageInterface */
33
    private $createPage;
34
35
    /** @var CreateForParentPageInterface */
36
    private $createForParentPage;
37
38
    /** @var UpdatePageInterface */
39
    private $updatePage;
40
41
    /** @var CurrentPageResolverInterface */
42
    private $currentPageResolver;
43
44
    /** @var NotificationCheckerInterface */
45
    private $notificationChecker;
46
47
    public function __construct(
48
        SharedStorageInterface $sharedStorage,
49
        CreatePageInterface $createPage,
50
        CreateForParentPageInterface $createForParentPage,
51
        UpdatePageInterface $updatePage,
52
        CurrentPageResolverInterface $currentPageResolver,
53
        NotificationCheckerInterface $notificationChecker
54
    ) {
55
        $this->sharedStorage = $sharedStorage;
56
        $this->createPage = $createPage;
57
        $this->createForParentPage = $createForParentPage;
58
        $this->updatePage = $updatePage;
59
        $this->currentPageResolver = $currentPageResolver;
60
        $this->notificationChecker = $notificationChecker;
61
    }
62
63
    /**
64
     * @Given I want to create a new taxon
65
     * @Given I want to see all taxons in store
66
     */
67
    public function iWantToCreateANewTaxon()
68
    {
69
        $this->createPage->open();
70
    }
71
72
    /**
73
     * @Given I want to create a new taxon for :taxon
74
     */
75
    public function iWantToCreateANewTaxonForParent(TaxonInterface $taxon)
76
    {
77
        $this->createForParentPage->open(['id' => $taxon->getId()]);
78
    }
79
80
    /**
81
     * @Given /^I want to modify the ("[^"]+" taxon)$/
82
     */
83
    public function iWantToModifyATaxon(TaxonInterface $taxon)
84
    {
85
        $this->sharedStorage->set('taxon', $taxon);
86
87
        $this->updatePage->open(['id' => $taxon->getId()]);
88
    }
89
90
    /**
91
     * @When I specify its code as :code
92
     * @When I do not specify its code
93
     */
94
    public function iSpecifyItsCodeAs(?string $code = null)
95
    {
96
        $this->createPage->specifyCode($code ?? '');
97
    }
98
99
    /**
100
     * @When I name it :name in :language
101
     * @When I rename it to :name in :language
102
     * @When I do not specify its name
103
     */
104
    public function iNameItIn(?string $name = null, $language = 'en_US')
105
    {
106
        $currentPage = $this->resolveCurrentPage();
107
108
        $currentPage->nameIt($name ?? '', $language);
109
    }
110
111
    /**
112
     * @When I set its slug to :slug
113
     * @When I do not specify its slug
114
     * @When I set its slug to :slug in :language
115
     */
116
    public function iSetItsSlugToIn(?string $slug = null, $language = 'en_US')
117
    {
118
        $currentPage = $this->resolveCurrentPage();
119
120
        $currentPage->specifySlug($slug ?? '', $language);
121
    }
122
123
    /**
124
     * @Then the slug field should not be editable
125
     * @Then the slug field should (also )not be editable in :language
126
     */
127
    public function theSlugFieldShouldNotBeEditable($language = 'en_US')
128
    {
129
        Assert::true($this->updatePage->isSlugReadonly($language));
130
    }
131
132
    /**
133
     * @When I enable slug modification
134
     * @When I enable slug modification in :language
135
     */
136
    public function iEnableSlugModification($language = 'en_US')
137
    {
138
        $this->updatePage->activateLanguageTab($language);
139
        $this->updatePage->enableSlugModification($language);
140
    }
141
142
    /**
143
     * @When I change its description to :description in :language
144
     */
145
    public function iChangeItsDescriptionToIn($description, $language)
146
    {
147
        $this->updatePage->describeItAs($description, $language);
148
    }
149
150
    /**
151
     * @When I describe it as :description in :language
152
     */
153
    public function iDescribeItAs($description, $language)
154
    {
155
        $this->createPage->describeItAs($description, $language);
156
    }
157
158
    /**
159
     * @Given /^I change its (parent taxon to "[^"]+")$/
160
     */
161
    public function iChangeItsParentTaxonTo(TaxonInterface $taxon)
162
    {
163
        $this->updatePage->chooseParent($taxon);
164
    }
165
166
    /**
167
     * @When I add it
168
     * @When I try to add it
169
     */
170
    public function iAddIt()
171
    {
172
        $this->createPage->create();
173
    }
174
175
    /**
176
     * @When I save my changes
177
     * @When I try to save my changes
178
     */
179
    public function iSaveMyChanges()
180
    {
181
        $this->updatePage->saveChanges();
182
    }
183
184
    /**
185
     * @Then /^the ("[^"]+" taxon) should appear in the registry$/
186
     */
187
    public function theTaxonShouldAppearInTheRegistry(TaxonInterface $taxon)
188
    {
189
        $this->updatePage->open(['id' => $taxon->getId()]);
190
        Assert::true($this->updatePage->hasResourceValues(['code' => $taxon->getCode()]));
191
    }
192
193
    /**
194
     * @Then this taxon :element should be :value
195
     */
196
    public function thisTaxonElementShouldBe($element, $value)
197
    {
198
        Assert::true($this->updatePage->hasResourceValues([$element => $value]));
199
    }
200
201
    /**
202
     * @Then this taxon should have slug :value in :language
203
     */
204
    public function thisTaxonElementShouldHaveSlugIn($value, $language = null)
205
    {
206
        if (null !== $language) {
207
            $this->updatePage->activateLanguageTab($language ?? '');
208
        }
209
210
        Assert::same($this->updatePage->getSlug($language ?? ''), $value);
211
    }
212
213
    /**
214
     * @Then the code field should be disabled
215
     */
216
    public function theCodeFieldShouldBeDisabled()
217
    {
218
        Assert::true($this->updatePage->isCodeDisabled());
219
    }
220
221
    /**
222
     * @Then /^the slug of the ("[^"]+" taxon) should(?:| still) be "([^"]+)"$/
223
     */
224
    public function productSlugShouldBe(TaxonInterface $taxon, $slug)
225
    {
226
        $this->updatePage->open(['id' => $taxon->getId()]);
227
228
        Assert::true($this->updatePage->hasResourceValues(['slug' => $slug]));
229
    }
230
231
    /**
232
     * @Then /^this taxon should (belongs to "[^"]+")$/
233
     */
234
    public function thisTaxonShouldBelongsTo(TaxonInterface $taxon)
235
    {
236
        Assert::true($this->updatePage->hasResourceValues(['parent' => $taxon->getCode()]));
237
    }
238
239
    /**
240
     * @Given it should not belong to any other taxon
241
     */
242
    public function itShouldNotBelongToAnyOtherTaxon()
243
    {
244
        Assert::isEmpty($this->updatePage->getParent());
245
    }
246
247
    /**
248
     * @Then I should be notified that taxon with this code already exists
249
     */
250
    public function iShouldBeNotifiedThatTaxonWithThisCodeAlreadyExists()
251
    {
252
        $currentPage = $this->resolveCurrentPage();
253
254
        Assert::same($currentPage->getValidationMessage('code'), 'Taxon with given code already exists.');
255
    }
256
257
    /**
258
     * @Then I should be notified that taxon slug must be unique
259
     */
260
    public function iShouldBeNotifiedThatTaxonSlugMustBeUnique()
261
    {
262
        $currentPage = $this->resolveCurrentPage();
263
264
        Assert::same($currentPage->getValidationMessage('slug'), 'Taxon slug must be unique.');
265
    }
266
267
    /**
268
     * @Then I should be notified that :element is required
269
     */
270
    public function iShouldBeNotifiedThatIsRequired($element)
271
    {
272
        $currentPage = $this->resolveCurrentPage();
273
274
        Assert::same($currentPage->getValidationMessage($element), sprintf('Please enter taxon %s.', $element));
275
    }
276
277
    /**
278
     * @Then /^there should(?:| still) be only one taxon with code "([^"]+)"$/
279
     */
280
    public function thereShouldStillBeOnlyOneTaxonWithCode($code)
281
    {
282
        Assert::true($this->updatePage->hasResourceValues(['code' => $code]));
283
    }
284
285
    /**
286
     * @Then /^taxon named "([^"]+)" should not be added$/
287
     * @Then the taxon named :name should no longer exist in the registry
288
     */
289
    public function taxonNamedShouldNotBeAdded($name)
290
    {
291
        Assert::same($this->createPage->countTaxonsByName($name), 0);
292
    }
293
294
    /**
295
     * @Then /^I should see (\d+) taxons on the list$/
296
     */
297
    public function iShouldSeeTaxonsInTheList($number)
298
    {
299
        Assert::same($this->createPage->countTaxons(), (int) $number);
300
    }
301
302
    /**
303
     * @Then I should see the taxon named :name in the list
304
     */
305
    public function iShouldSeeTheTaxonNamedInTheList($name)
306
    {
307
        Assert::same($this->createPage->countTaxonsByName($name), 1);
308
    }
309
310
    /**
311
     * @When I attach the :path image with :type type
312
     * @When I attach the :path image
313
     */
314
    public function iAttachImageWithType($path, $type = null)
315
    {
316
        $currentPage = $this->resolveCurrentPage();
317
318
        $currentPage->attachImage($path, $type);
319
    }
320
321
    /**
322
     * @Then /^(?:it|this taxon) should(?:| also) have an image with "([^"]*)" type$/
323
     */
324
    public function thisTaxonShouldHaveAnImageWithType($type)
325
    {
326
        Assert::true($this->updatePage->isImageWithTypeDisplayed($type));
327
    }
328
329
    /**
330
     * @Then /^(?:this taxon|it) should not have(?:| also) any images with "([^"]*)" type$/
331
     */
332
    public function thisTaxonShouldNotHaveAnImageWithType($code)
333
    {
334
        Assert::false($this->updatePage->isImageWithTypeDisplayed($code));
335
    }
336
337
    /**
338
     * @When /^I(?:| also) remove an image with "([^"]*)" type$/
339
     */
340
    public function iRemoveAnImageWithType($code)
341
    {
342
        $this->updatePage->removeImageWithType($code);
343
    }
344
345
    /**
346
     * @When I remove the first image
347
     */
348
    public function iRemoveTheFirstImage()
349
    {
350
        $this->updatePage->removeFirstImage();
351
    }
352
353
    /**
354
     * @Then /^(this taxon) should not have any images$/
355
     */
356
    public function thisTaxonShouldNotHaveAnyImages(TaxonInterface $taxon)
357
    {
358
        $this->iWantToModifyATaxon($taxon);
359
360
        Assert::same($this->updatePage->countImages(), 0);
361
    }
362
363
    /**
364
     * @When I change the image with the :type type to :path
365
     */
366
    public function iChangeItsImageToPathForTheType($path, $type)
367
    {
368
        $this->updatePage->changeImageWithType($type, $path);
369
    }
370
371
    /**
372
     * @When I change the first image type to :type
373
     */
374
    public function iChangeTheFirstImageTypeTo($type)
375
    {
376
        $this->updatePage->modifyFirstImageType($type);
377
    }
378
379
    /**
380
     * @Then /^(this taxon) should have only one image$/
381
     * @Then /^(this taxon) should(?:| still) have (\d+) images?$/
382
     */
383
    public function thereShouldStillBeOnlyOneImageInThisTaxon(TaxonInterface $taxon, $count = 1)
384
    {
385
        $this->iWantToModifyATaxon($taxon);
386
387
        Assert::same($this->updatePage->countImages(), (int) $count);
388
    }
389
390
    /**
391
     * @Then I should be notified that I cannot delete a menu taxon of any channel
392
     */
393
    public function iShouldBeNotifiedThatICannotDeleteAMenuTaxonOfAnyChannel(): void
394
    {
395
        $this->notificationChecker->checkNotification(
396
            'You cannot delete a menu taxon of any channel.',
397
            NotificationType::failure()
398
        );
399
    }
400
401
    /**
402
     * @return CreatePageInterface|CreateForParentPageInterface|UpdatePageInterface
403
     */
404
    private function resolveCurrentPage()
405
    {
406
        return $this->currentPageResolver->getCurrentPageWithForm([
407
            $this->createPage,
408
            $this->createForParentPage,
409
            $this->updatePage,
410
        ]);
411
    }
412
}
413