Completed
Push — master ( 9a4fa3...1bdebc )
by Kamil
31:53 queued 20:23
created

ManagingProductAttributesContext::iAddValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

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 3
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
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
     * @Then /^I should see (\d+) product attributes in the list$/
277
     */
278
    public function iShouldSeeCustomersInTheList($amountOfProductAttributes)
279
    {
280
        Assert::same($this->indexPage->countItems(), (int) $amountOfProductAttributes);
281
    }
282
283
    /**
284
     * @When /^I delete (this product attribute)$/
285
     */
286
    public function iDeleteThisProductAttribute(ProductAttributeInterface $productAttribute)
287
    {
288
        $this->indexPage->open();
289
        $this->indexPage->deleteResourceOnPage(['code' => $productAttribute->getCode(), 'name' => $productAttribute->getName()]);
290
    }
291
292
    /**
293
     * @Then /^(this product attribute) should no longer exist in the registry$/
294
     */
295
    public function thisProductAttributeShouldNoLongerExistInTheRegistry(ProductAttributeInterface $productAttribute)
296
    {
297
        Assert::false($this->indexPage->isSingleResourceOnPage(['code' => $productAttribute->getCode()]));
298
    }
299
300
    /**
301
     * @Then the first product attribute on the list should have name :name
302
     */
303
    public function theFirstProductAttributeOnTheListShouldHave($name)
304
    {
305
        $names = $this->indexPage->getColumnFields('name');
306
307
        Assert::same(reset($names), $name);
308
    }
309
310
    /**
311
     * @Then the last product attribute on the list should have name :name
312
     */
313
    public function theLastProductAttributeOnTheListShouldHave($name)
314
    {
315
        $names = $this->indexPage->getColumnFields('name');
316
317
        Assert::same(end($names), $name);
318
    }
319
320
    /**
321
     * @Then /^(this product attribute) should have value "([^"]*)"/
322
     */
323
    public function theSelectAttributeShouldHaveValue(ProductAttributeInterface $productAttribute, string $value): void
324
    {
325
        $this->iWantToEditThisAttribute($productAttribute);
326
327
        Assert::true($this->updatePage->hasAttributeValue($value));
328
    }
329
330
    /**
331
     * @param string $element
332
     * @param string $expectedMessage
333
     */
334
    private function assertFieldValidationMessage($element, $expectedMessage)
335
    {
336
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
337
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
338
339
        Assert::same($currentPage->getValidationMessage($element), $expectedMessage);
340
    }
341
}
342