Completed
Push — symfony3-fqcn ( fc44dc...d46cdc )
by Kamil
34:08 queued 14:53
created

theLastProductAttributeOnTheListShouldHave()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
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\Crud\IndexPageInterface;
16
use Sylius\Behat\Page\Admin\ProductAttribute\CreatePageInterface;
17
use Sylius\Behat\Page\Admin\ProductAttribute\UpdatePageInterface;
18
use Sylius\Behat\Service\Resolver\CurrentPageResolverInterface;
19
use Sylius\Component\Product\Model\ProductAttributeInterface;
20
use Webmozart\Assert\Assert;
21
22
/**
23
 * @author Anna Walasek <[email protected]>
24
 */
25
final class ManagingProductAttributesContext implements Context
26
{
27
    /**
28
     * @var CreatePageInterface
29
     */
30
    private $createPage;
31
32
    /**
33
     * @var IndexPageInterface
34
     */
35
    private $indexPage;
36
37
    /**
38
     * @var UpdatePageInterface
39
     */
40
    private $updatePage;
41
42
    /**
43
     * @var CurrentPageResolverInterface
44
     */
45
    private $currentPageResolver;
46
47
    /**
48
     * @param CreatePageInterface $createPage
49
     * @param IndexPageInterface $indexPage
50
     * @param UpdatePageInterface $updatePage
51
     * @param CurrentPageResolverInterface $currentPageResolver
52
     */
53
    public function __construct(
54
        CreatePageInterface $createPage,
55
        IndexPageInterface $indexPage,
56
        UpdatePageInterface $updatePage,
57
        CurrentPageResolverInterface $currentPageResolver
58
    ) {
59
        $this->createPage = $createPage;
60
        $this->indexPage = $indexPage;
61
        $this->updatePage = $updatePage;
62
        $this->currentPageResolver = $currentPageResolver;
63
    }
64
65
    /**
66
     * @Given I want to create a new :type product attribute
67
     */
68
    public function iWantToCreateANewTextProductAttribute($type)
69
    {
70
        $this->createPage->open(['type' => $type]);
71
    }
72
73
    /**
74
     * @When I specify its code as :code
75
     * @When I do not specify its code
76
     */
77
    public function iSpecifyItsCodeAs($code = null)
78
    {
79
        $this->createPage->specifyCode($code);
80
    }
81
82
    /**
83
     * @When I name it :name in :language
84
     */
85
    public function iSpecifyItsNameAs($name, $language)
86
    {
87
        $this->createPage->nameIt($name, $language);
88
    }
89
90
    /**
91
     * @When I add it
92
     * @When I try to add it
93
     */
94
    public function iAddIt()
95
    {
96
        $this->createPage->create();
97
    }
98
99
    /**
100
     * @Then I should see the product attribute :name in the list
101
     */
102
    public function iShouldSeeTheProductAttributeInTheList($name)
103
    {
104
        $this->indexPage->open();
105
106
        Assert::true(
107
            $this->indexPage->isSingleResourceOnPage(['name' => $name]),
108
            sprintf('The product attribute with name %s should appear on page, but it does not.', $name)
109
        );
110
    }
111
112
    /**
113
     * @Then the :type attribute :name should appear in the store
114
     */
115
    public function theAttributeShouldAppearInTheStore($type, $name)
116
    {
117
        $this->indexPage->open();
118
119
        Assert::true(
120
            $this->indexPage->isSingleResourceWithSpecificElementOnPage(
121
                ['name' => $name],
122
                sprintf('td span.ui.label:contains("%s")', $type)
123
            ),
124
            sprintf('The product attribute with name %s and type %s should appear on page, but it does not.', $name, $type)
125
        );
126
    }
127
128
    /**
129
     * @When /^I want to edit (this product attribute)$/
130
     */
131
    public function iWantToEditThisAttribute(ProductAttributeInterface $productAttribute)
132
    {
133
        $this->updatePage->open(['id' => $productAttribute->getId()]);
134
    }
135
136
    /**
137
     * @When I change it name to :name in :language
138
     */
139
    public function iChangeItNameToIn($name, $language)
140
    {
141
        $this->updatePage->changeName($name, $language);
142
    }
143
144
    /**
145
     * @When I save my changes
146
     * @When I try to save my changes
147
     */
148
    public function iSaveMyChanges()
149
    {
150
        $this->updatePage->saveChanges();
151
    }
152
153
    /**
154
     * @Then the code field should be disabled
155
     */
156
    public function theCodeFieldShouldBeDisabled()
157
    {
158
        Assert::true(
159
            $this->updatePage->isCodeDisabled(),
160
            'Code field should be disabled, but it does not.'
161
        );
162
    }
163
164
    /**
165
     * @Then the type field should be disabled
166
     */
167
    public function theTypeFieldShouldBeDisabled()
168
    {
169
       $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
170
171
        Assert::true(
172
            $currentPage->isTypeDisabled(),
173
            'Type field should be disabled, but it does not.'
174
        );
175
    }
176
177
    /**
178
     * @Then I should be notified that product attribute with this code already exists
179
     */
180
    public function iShouldBeNotifiedThatProductAttributeWithThisCodeAlreadyExists()
181
    {
182
        Assert::same($this->updatePage->getValidationMessage('code'), 'This code is already in use.');
183
    }
184
185
    /**
186
     * @Given there should still be only one product attribute with code :code
187
     */
188
    public function thereShouldStillBeOnlyOneProductAttributeWithCode($code)
189
    {
190
        $this->indexPage->open();
191
192
        Assert::true(
193
            $this->indexPage->isSingleResourceOnPage(['code' => $code]),
194
            sprintf('There should be only one product attribute with code %s, but it does not.', $code)
195
        );
196
    }
197
198
    /**
199
     * @When I do not name it
200
     */
201
    public function iDoNotNameIt()
202
    {
203
        // Intentionally left blank to fulfill context expectation
204
    }
205
206
    /**
207
     * @Then I should be notified that :element is required
208
     */
209
    public function iShouldBeNotifiedThatIsRequired($element)
210
    {
211
        $this->assertFieldValidationMessage($element, sprintf('Please enter attribute %s.', $element));
212
    }
213
214
    /**
215
     * @Given the attribute with :elementName :elementValue should not appear in the store
216
     */
217
    public function theAttributeWithCodeShouldNotAppearInTheStore($elementName, $elementValue)
218
    {
219
        $this->indexPage->open();
220
221
        Assert::false(
222
            $this->indexPage->isSingleResourceOnPage([$elementName => $elementValue]),
223
            sprintf('There should not be product attribute with %s %s, but it is.', $elementName, $elementValue)
224
        );
225
    }
226
227
    /**
228
     * @When I remove its name from :language translation
229
     */
230
    public function iRemoveItsNameFromTranslation($language)
231
    {
232
        $this->updatePage->changeName('', $language);
233
    }
234
235
    /**
236
     * @When I want to see all product attributes in store
237
     */
238
    public function iWantToSeeAllProductAttributesInStore()
239
    {
240
        $this->indexPage->open();
241
    }
242
243
    /**
244
     * @Then /^I should see (\d+) product attributes in the list$/
245
     */
246
    public function iShouldSeeCustomersInTheList($amountOfProductAttributes)
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $amountOfProductAttributes exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
247
    {
248
        Assert::same(
249
            (int) $amountOfProductAttributes,
250
            $this->indexPage->countItems(),
251
            sprintf('Amount of product attributes should be equal %s, but is not.', $amountOfProductAttributes)
252
        );
253
    }
254
255
    /**
256
     * @When /^I delete (this product attribute)$/
257
     */
258
    public function iDeleteThisProductAttribute(ProductAttributeInterface $productAttribute)
259
    {
260
        $this->indexPage->open();
261
        $this->indexPage->deleteResourceOnPage(['code' => $productAttribute->getCode(), 'name' => $productAttribute->getName()]);
262
    }
263
264
    /**
265
     * @Then /^(this product attribute) should no longer exist in the registry$/
266
     */
267
    public function thisProductAttributeShouldNoLongerExistInTheRegistry(ProductAttributeInterface $productAttribute)
268
    {
269
        Assert::false(
270
            $this->indexPage->isSingleResourceOnPage(['code' => $productAttribute->getCode()]),
271
            sprintf('Product attribute %s should no exist in the registry, but it does.', $productAttribute->getName())
272
        );
273
    }
274
275
    /**
276
     * @Then the first product attribute on the list should have name :name
277
     */
278
    public function theFirstProductAttributeOnTheListShouldHave($name)
279
    {
280
        $fields = $this->indexPage->getColumnFields('name');
281
        $actualName = reset($fields);
282
283
        Assert::same(
284
            $actualName,
285
            $name,
286
            sprintf('Expected first product attribute\'s name to be "%s", but it is "%s".', $name, $actualName)
287
        );
288
    }
289
290
    /**
291
     * @Then the last product attribute on the list should have name :name
292
     */
293
    public function theLastProductAttributeOnTheListShouldHave($name)
294
    {
295
        $fields = $this->indexPage->getColumnFields('name');
296
        $actualName = end($fields);
297
298
        Assert::same(
299
            $actualName,
300
            $name,
301
            sprintf('Expected last product attribute\'s name to be "%s", but it is "%s".', $name, $actualName)
302
        );
303
    }
304
305
    /**
306
     * @param string $element
307
     * @param string $expectedMessage
308
     */
309
    private function assertFieldValidationMessage($element, $expectedMessage)
310
    {
311
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
312
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
313
314
        Assert::same($currentPage->getValidationMessage($element), $expectedMessage);
315
    }
316
}
317