Completed
Push — 1.0 ( 2fbcac...32464f )
by Kamil
116:20 queued 93:29
created

ManagingProductAttributesContext   B

Complexity

Total Complexity 38

Size/Duplication

Total Lines 402
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
wmc 38
lcom 2
cbo 7
dl 0
loc 402
rs 8.3999
c 0
b 0
f 0

38 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A iWantToCreateANewTextProductAttribute() 0 4 1
A iSpecifyItsCodeAs() 0 4 1
A iSpecifyItsNameAs() 0 4 1
A iAddIt() 0 4 1
A iAddValue() 0 7 1
A iChangeItsValueTo() 0 4 1
A iShouldSeeTheProductAttributeInTheList() 0 6 1
A theAttributeShouldAppearInTheStore() 0 9 1
A iWantToEditThisAttribute() 0 4 1
A iChangeItNameToIn() 0 4 1
A iSaveMyChanges() 0 4 1
A theCodeFieldShouldBeDisabled() 0 4 1
A theTypeFieldShouldBeDisabled() 0 6 1
A iShouldBeNotifiedThatProductAttributeWithThisCodeAlreadyExists() 0 4 1
A thereShouldStillBeOnlyOneProductAttributeWithCode() 0 6 1
A iDoNotNameIt() 0 4 1
A iShouldBeNotifiedThatIsRequired() 0 4 1
A theAttributeWithCodeShouldNotAppearInTheStore() 0 6 1
A iRemoveItsNameFromTranslation() 0 4 1
A iWantToSeeAllProductAttributesInStore() 0 4 1
A theAdministratorChangesTheValueTo() 0 15 1
A iSpecifyItsMinValueAs() 0 4 1
A iSpecifyItsMaxLengthAs() 0 4 1
A iCheckMultipleOption() 0 4 1
A iDoNotCheckMultipleOption() 0 4 1
A iShouldSeeCustomersInTheList() 0 4 1
A iDeleteThisProductAttribute() 0 5 1
A thisProductAttributeShouldNoLongerExistInTheRegistry() 0 4 1
A theFirstProductAttributeOnTheListShouldHave() 0 6 1
A theLastProductAttributeOnTheListShouldHave() 0 6 1
A theSelectAttributeShouldHaveValue() 0 6 1
A iShouldBeNotifiedThatMaxLengthMustBeGreaterOrEqualToTheMinLength() 0 6 1
A iShouldBeNotifiedThatMaxEntriesValueMustBeGreaterOrEqualToTheMinEntriesValue() 0 6 1
A iShouldBeNotifiedThatMinEntriesValueMustBeLowerOrEqualToTheNumberOfAddedChoices() 0 6 1
A iShouldBeNotifiedThatMultipleMustBeTrueIfMinOrMaxEntriesValuesAreSpecified() 0 6 1
A assertFieldValidationMessage() 0 7 1
A assertValidationMessage() 0 7 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
declare(strict_types=1);
13
14
namespace Sylius\Behat\Context\Ui\Admin;
15
16
use Behat\Behat\Context\Context;
17
use Sylius\Behat\Page\Admin\Crud\IndexPageInterface;
18
use Sylius\Behat\Page\Admin\ProductAttribute\CreatePageInterface;
19
use Sylius\Behat\Page\Admin\ProductAttribute\UpdatePageInterface;
20
use Sylius\Behat\Service\Resolver\CurrentPageResolverInterface;
21
use Sylius\Behat\Service\SharedSecurityServiceInterface;
22
use Sylius\Component\Core\Model\AdminUserInterface;
23
use Sylius\Component\Product\Model\ProductAttributeInterface;
24
use Webmozart\Assert\Assert;
25
26
/**
27
 * @author Anna Walasek <[email protected]>
28
 */
29
final class ManagingProductAttributesContext implements Context
30
{
31
    /**
32
     * @var CreatePageInterface
33
     */
34
    private $createPage;
35
36
    /**
37
     * @var IndexPageInterface
38
     */
39
    private $indexPage;
40
41
    /**
42
     * @var UpdatePageInterface
43
     */
44
    private $updatePage;
45
46
    /**
47
     * @var CurrentPageResolverInterface
48
     */
49
    private $currentPageResolver;
50
51
    /**
52
     * @var SharedSecurityServiceInterface
53
     */
54
    private $sharedSecurityService;
55
56
    /**
57
     * @param CreatePageInterface $createPage
58
     * @param IndexPageInterface $indexPage
59
     * @param UpdatePageInterface $updatePage
60
     * @param CurrentPageResolverInterface $currentPageResolver
61
     * @param SharedSecurityServiceInterface $sharedSecurityService
62
     */
63
    public function __construct(
64
        CreatePageInterface $createPage,
65
        IndexPageInterface $indexPage,
66
        UpdatePageInterface $updatePage,
67
        CurrentPageResolverInterface $currentPageResolver,
68
        SharedSecurityServiceInterface $sharedSecurityService
69
    ) {
70
        $this->createPage = $createPage;
71
        $this->indexPage = $indexPage;
72
        $this->updatePage = $updatePage;
73
        $this->currentPageResolver = $currentPageResolver;
74
        $this->sharedSecurityService = $sharedSecurityService;
75
    }
76
77
    /**
78
     * @When I want to create a new :type product attribute
79
     */
80
    public function iWantToCreateANewTextProductAttribute($type)
81
    {
82
        $this->createPage->open(['type' => $type]);
83
    }
84
85
    /**
86
     * @When I specify its code as :code
87
     * @When I do not specify its code
88
     */
89
    public function iSpecifyItsCodeAs($code = null)
90
    {
91
        $this->createPage->specifyCode($code);
92
    }
93
94
    /**
95
     * @When I name it :name in :language
96
     */
97
    public function iSpecifyItsNameAs($name, $language)
98
    {
99
        $this->createPage->nameIt($name, $language);
100
    }
101
102
    /**
103
     * @When I add it
104
     * @When I try to add it
105
     */
106
    public function iAddIt()
107
    {
108
        $this->createPage->create();
109
    }
110
111
    /**
112
     * @When I( also) add value :value
113
     */
114
    public function iAddValue(string $value): void
115
    {
116
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
117
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
118
119
        $currentPage->addAttributeValue($value);
120
    }
121
122
    /**
123
     * @When I change its value :oldValue to :newValue
124
     */
125
    public function iChangeItsValueTo(string $oldValue, string $newValue): void
126
    {
127
        $this->updatePage->changeAttributeValue($oldValue, $newValue);
128
    }
129
130
    /**
131
     * @Then I should see the product attribute :name in the list
132
     */
133
    public function iShouldSeeTheProductAttributeInTheList($name)
134
    {
135
        $this->indexPage->open();
136
137
        Assert::true($this->indexPage->isSingleResourceOnPage(['name' => $name]));
138
    }
139
140
    /**
141
     * @Then the :type attribute :name should appear in the store
142
     */
143
    public function theAttributeShouldAppearInTheStore($type, $name)
144
    {
145
        $this->indexPage->open();
146
147
        Assert::true($this->indexPage->isSingleResourceWithSpecificElementOnPage(
148
            ['name' => $name],
149
            sprintf('td span.ui.label:contains("%s")', $type)
150
        ));
151
    }
152
153
    /**
154
     * @When /^I want to edit (this product attribute)$/
155
     */
156
    public function iWantToEditThisAttribute(ProductAttributeInterface $productAttribute)
157
    {
158
        $this->updatePage->open(['id' => $productAttribute->getId()]);
159
    }
160
161
    /**
162
     * @When I change its name to :name in :language
163
     */
164
    public function iChangeItNameToIn($name, $language)
165
    {
166
        $this->updatePage->changeName($name, $language);
167
    }
168
169
    /**
170
     * @When I save my changes
171
     * @When I try to save my changes
172
     */
173
    public function iSaveMyChanges()
174
    {
175
        $this->updatePage->saveChanges();
176
    }
177
178
    /**
179
     * @Then the code field should be disabled
180
     */
181
    public function theCodeFieldShouldBeDisabled()
182
    {
183
        Assert::true($this->updatePage->isCodeDisabled());
184
    }
185
186
    /**
187
     * @Then the type field should be disabled
188
     */
189
    public function theTypeFieldShouldBeDisabled()
190
    {
191
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
192
193
        Assert::true($currentPage->isTypeDisabled());
194
    }
195
196
    /**
197
     * @Then I should be notified that product attribute with this code already exists
198
     */
199
    public function iShouldBeNotifiedThatProductAttributeWithThisCodeAlreadyExists()
200
    {
201
        Assert::same($this->updatePage->getValidationMessage('code'), 'This code is already in use.');
202
    }
203
204
    /**
205
     * @Then there should still be only one product attribute with code :code
206
     */
207
    public function thereShouldStillBeOnlyOneProductAttributeWithCode($code)
208
    {
209
        $this->indexPage->open();
210
211
        Assert::true($this->indexPage->isSingleResourceOnPage(['code' => $code]));
212
    }
213
214
    /**
215
     * @When I do not name it
216
     */
217
    public function iDoNotNameIt()
218
    {
219
        // Intentionally left blank to fulfill context expectation
220
    }
221
222
    /**
223
     * @Then I should be notified that :element is required
224
     */
225
    public function iShouldBeNotifiedThatIsRequired($element)
226
    {
227
        $this->assertFieldValidationMessage($element, sprintf('Please enter attribute %s.', $element));
228
    }
229
230
    /**
231
     * @Given the attribute with :elementName :elementValue should not appear in the store
232
     */
233
    public function theAttributeWithCodeShouldNotAppearInTheStore($elementName, $elementValue)
234
    {
235
        $this->indexPage->open();
236
237
        Assert::false($this->indexPage->isSingleResourceOnPage([$elementName => $elementValue]));
238
    }
239
240
    /**
241
     * @When I remove its name from :language translation
242
     */
243
    public function iRemoveItsNameFromTranslation($language)
244
    {
245
        $this->updatePage->changeName('', $language);
246
    }
247
248
    /**
249
     * @When I want to see all product attributes in store
250
     */
251
    public function iWantToSeeAllProductAttributesInStore()
252
    {
253
        $this->indexPage->open();
254
    }
255
256
    /**
257
     * @When /^(the administrator) changes (this product attribute)'s value "([^"]*)" to "([^"]*)"$/
258
     */
259
    public function theAdministratorChangesTheValueTo(
260
        AdminUserInterface $user,
261
        ProductAttributeInterface $productAttribute,
262
        string $oldValue,
263
        string $newValue
264
    ): void {
265
        $this->sharedSecurityService->performActionAsAdminUser(
266
            $user,
267
            function () use ($productAttribute, $oldValue, $newValue) {
268
                $this->iWantToEditThisAttribute($productAttribute);
269
                $this->iChangeItsValueTo($oldValue, $newValue);
270
                $this->iSaveMyChanges();
271
            }
272
        );
273
    }
274
275
    /**
276
     * @When I specify its min length as :min
277
     * @When I specify its min entries value as :min
278
     */
279
    public function iSpecifyItsMinValueAs(int $min): void
280
    {
281
        $this->createPage->specifyMinValue($min);
282
    }
283
284
    /**
285
     * @When I specify its max length as :max
286
     * @When I specify its max entries value as :max
287
     */
288
    public function iSpecifyItsMaxLengthAs(int $max): void
289
    {
290
        $this->createPage->specifyMaxValue($max);
291
    }
292
293
    /**
294
     * @When I check multiple option
295
     */
296
    public function iCheckMultipleOption(): void
297
    {
298
        $this->createPage->checkMultiple();
299
    }
300
301
    /**
302
     * @When I do not check multiple option
303
     */
304
    public function iDoNotCheckMultipleOption(): void
305
    {
306
        // Intentionally left blank to fulfill context expectation
307
    }
308
309
    /**
310
     * @Then /^I should see (\d+) product attributes in the list$/
311
     */
312
    public function iShouldSeeCustomersInTheList($amountOfProductAttributes)
313
    {
314
        Assert::same($this->indexPage->countItems(), (int) $amountOfProductAttributes);
315
    }
316
317
    /**
318
     * @When /^I delete (this product attribute)$/
319
     */
320
    public function iDeleteThisProductAttribute(ProductAttributeInterface $productAttribute)
321
    {
322
        $this->indexPage->open();
323
        $this->indexPage->deleteResourceOnPage(['code' => $productAttribute->getCode(), 'name' => $productAttribute->getName()]);
324
    }
325
326
    /**
327
     * @Then /^(this product attribute) should no longer exist in the registry$/
328
     */
329
    public function thisProductAttributeShouldNoLongerExistInTheRegistry(ProductAttributeInterface $productAttribute)
330
    {
331
        Assert::false($this->indexPage->isSingleResourceOnPage(['code' => $productAttribute->getCode()]));
332
    }
333
334
    /**
335
     * @Then the first product attribute on the list should have name :name
336
     */
337
    public function theFirstProductAttributeOnTheListShouldHave($name)
338
    {
339
        $names = $this->indexPage->getColumnFields('name');
340
341
        Assert::same(reset($names), $name);
342
    }
343
344
    /**
345
     * @Then the last product attribute on the list should have name :name
346
     */
347
    public function theLastProductAttributeOnTheListShouldHave($name)
348
    {
349
        $names = $this->indexPage->getColumnFields('name');
350
351
        Assert::same(end($names), $name);
352
    }
353
354
    /**
355
     * @Then /^(this product attribute) should have value "([^"]*)"/
356
     */
357
    public function theSelectAttributeShouldHaveValue(ProductAttributeInterface $productAttribute, string $value): void
358
    {
359
        $this->iWantToEditThisAttribute($productAttribute);
360
361
        Assert::true($this->updatePage->hasAttributeValue($value));
362
    }
363
364
    /**
365
     * @Then I should be notified that max length must be greater or equal to the min length
366
     */
367
    public function iShouldBeNotifiedThatMaxLengthMustBeGreaterOrEqualToTheMinLength(): void
368
    {
369
        $this->assertValidationMessage(
370
            'Configuration max length must be greater or equal to the min length.'
371
        );
372
    }
373
374
    /**
375
     * @Then I should be notified that max entries value must be greater or equal to the min entries value
376
     */
377
    public function iShouldBeNotifiedThatMaxEntriesValueMustBeGreaterOrEqualToTheMinEntriesValue(): void
378
    {
379
        $this->assertValidationMessage(
380
            'Configuration max entries value must be greater or equal to the min entries value.'
381
        );
382
    }
383
384
    /**
385
     * @Then I should be notified that min entries value must be lower or equal to the number of added choices
386
     */
387
    public function iShouldBeNotifiedThatMinEntriesValueMustBeLowerOrEqualToTheNumberOfAddedChoices(): void
388
    {
389
        $this->assertValidationMessage(
390
            'Configuration min entries value must be lower or equal to the number of added choices.'
391
        );
392
    }
393
394
    /**
395
     * @Then I should be notified that multiple must be true if min or max entries values are specified
396
     */
397
    public function iShouldBeNotifiedThatMultipleMustBeTrueIfMinOrMaxEntriesValuesAreSpecified(): void
398
    {
399
        $this->assertValidationMessage(
400
            'Configuration multiple must be true if min or max entries values are specified.'
401
        );
402
    }
403
404
    /**
405
     * @param string $element
406
     * @param string $expectedMessage
407
     *
408
     * @throws \InvalidArgumentException
409
     */
410
    private function assertFieldValidationMessage(string $element, string $expectedMessage): void
411
    {
412
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
413
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
414
415
        Assert::same($currentPage->getValidationMessage($element), $expectedMessage);
416
    }
417
418
    /**
419
     * @param string $expectedMessage
420
     *
421
     * @throws \InvalidArgumentException
422
     */
423
    private function assertValidationMessage(string $expectedMessage): void
424
    {
425
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
426
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
427
428
        Assert::same($currentPage->getValidationErrors(), $expectedMessage);
0 ignored issues
show
Bug introduced by
The method getValidationErrors does only exist in Sylius\Behat\Page\Admin\...ute\CreatePageInterface, but not in Sylius\Behat\Page\Admin\...ute\UpdatePageInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
429
    }
430
}
431