Completed
Push — api ( cecea0...116a36 )
by Kamil
29:57 queued 30s
created

iBrowseProductOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
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\Api\Admin;
15
16
use Behat\Behat\Context\Context;
17
use Sylius\Behat\Client\ApiClientInterface;
18
use Sylius\Behat\Service\SharedStorageInterface;
19
use Sylius\Component\Product\Model\ProductOptionInterface;
20
use Webmozart\Assert\Assert;
21
22
final class ManagingProductOptionsContext implements Context
23
{
24
    /** @var ApiClientInterface */
25
    private $client;
26
27
    /** @var SharedStorageInterface */
28
    private $sharedStorage;
29
30
    public function __construct(ApiClientInterface $client, SharedStorageInterface $sharedStorage)
31
    {
32
        $this->client = $client;
33
        $this->sharedStorage = $sharedStorage;
34
    }
35
36
    /**
37
     * @When I browse product options
38
     */
39
    public function iBrowseProductOptions(): void
40
    {
41
        $this->client->index('product_options');
42
    }
43
44
    /**
45
     * @Given I want to create a new product option
46
     */
47
    public function iWantToCreateANewProductOption(): void
48
    {
49
        $this->client->buildCreateRequest('product_options');
50
    }
51
52
    /**
53
     * @When I want to modify the :productOption product option
54
     */
55
    public function iWantToModifyProductOption(ProductOptionInterface $productOption): void
56
    {
57
        $this->sharedStorage->set('product_option', $productOption);
58
        $this->client->buildUpdateRequest('product_options', $productOption->getCode());
59
    }
60
61
    /**
62
     * @When I name it :name in :language
63
     * @When I do not name it
64
     */
65
    public function iNameItInLanguage(?string $name = null, ?string $language = 'en_US'): void
66
    {
67
        $data = ['translations' => [$language => ['locale' => $language]]];
68
        if ($name !== null) {
69
            $data['translations'][$language]['name'] = $name;
70
        }
71
72
        $this->client->addCompoundRequestData($data);
73
    }
74
75
    /**
76
     * @When I rename it to :name in :language
77
     */
78
    public function iRenameItInLanguage(string $name, string $language): void
79
    {
80
        $this->client->updateRequestData(['translations' => [$language => ['name' => $name, 'locale' => $language]]]);
81
    }
82
83
    /**
84
     * @When I remove its name from :language translation
85
     */
86
    public function iRemoveItsNameFromTranslation(string $language): void
87
    {
88
        $this->client->updateRequestData(['translations' => [$language => ['name' => '', 'locale' => $language]]]);
89
    }
90
91
    /**
92
     * @When I specify its code as :code
93
     * @When I do not specify its code
94
     */
95
    public function iSpecifyItsCodeAs(?string $code = null): void
96
    {
97
        if ($code !== null) {
98
            $this->client->addRequestData('code', $code);
99
        }
100
    }
101
102
    /**
103
     * @When I add the :value option value identified by :code
104
     */
105
    public function iAddTheOptionValueWithCodeAndValue(string $value, string $code): void
106
    {
107
        $this->client->addCompoundRequestData([
108
            'values' => [
109
                ['code' => $code, 'translations' => [['value' => $value, 'locale' => 'en_US']]]
110
            ]
111
        ]);
112
    }
113
114
    /**
115
     * @When I do not add an option value
116
     */
117
    public function iDoNotAddAnOptionValue(): void
118
    {
119
        // Intentionally left blank to fulfill context expectation
120
    }
121
122
    /**
123
     * @When I add it
124
     * @When I try to add it
125
     */
126
    public function iAddIt(): void
127
    {
128
        $this->client->create();
129
    }
130
131
    /**
132
     * @When I save my changes
133
     * @When I try to save my changes
134
     */
135
    public function iSaveMyChanges(): void
136
    {
137
        $this->client->update();
138
    }
139
140
    /**
141
     * @Then I should see :count product options in the list
142
     */
143
    public function iShouldSeeProductOptionsInTheList(int $count): void
144
    {
145
        $itemsCount = $this->client->countCollectionItems();
146
147
        Assert::eq($count, $itemsCount, sprintf('Expected %d product options, but got %d', $count, $itemsCount));
148
    }
149
150
    /**
151
     * @Then the product option :productOption should be in the registry
152
     * @Then the product option :productOption should appear in the registry
153
     */
154
    public function theProductOptionShouldAppearInTheRegistry(ProductOptionInterface $productOption): void
155
    {
156
        $this->sharedStorage->set('product_option', $productOption);
157
158
        $this->client->index('product_options');
159
        Assert::true(
160
            $this->client->hasItemWithValue('name', $productOption->getName()),
161
            sprintf('Product option should have name "%s", but it does not.', $productOption->getName())
162
        );
163
    }
164
165
    /**
166
     * @Then the first product option in the list should have :field :value
167
     */
168
    public function theFirstProductOptionInTheListShouldHave(string $field, string $value): void
169
    {
170
        Assert::true(
171
            $this->client->hasItemOnPositionWithValue(0, $field, $value),
172
            sprintf('There should be product option with %s "%s" on position %d, but it does not.', $field, $value, 1)
173
        );
174
    }
175
176
    /**
177
     * @Then the last product option in the list should have :field :value
178
     */
179
    public function theLastProductOptionInTheListShouldHave(string $field, string $value): void
180
    {
181
        $count = $this->client->countCollectionItems();
182
183
        Assert::true(
184
            $this->client->hasItemOnPositionWithValue($count-1, $field, $value),
185
            sprintf('There should be product option with %s "%s" on position %d, but it does not.', $field, $value, $count-1)
186
        );
187
    }
188
189
    /**
190
     * @Then the product option with :element :value should not be added
191
     */
192
    public function theProductOptionWithElementValueShouldNotBeAdded(string $element, string $value): void
193
    {
194
        $this->client->index('product_options');
195
        Assert::false(
196
            $this->client->hasItemWithValue($element, $value),
197
            sprintf('Product option should not have %s "%s", but it does,', $element, $value)
198
        );
199
    }
200
201
    /**
202
     * @Then there should still be only one product option with :element :value
203
     */
204
    public function thereShouldStillBeOnlyOneProductOptionWith(string $element, string $value): void
205
    {
206
        $this->client->index('product_options');
207
        $itemsCount = $this->client->countCollectionItems();
208
209
        Assert::eq(1, $this->client->countCollectionItems(), sprintf('Expected 1 product options, but got %d', $itemsCount));
210
        Assert::true($this->client->hasItemWithValue($element, $value));
211
    }
212
213
    /**
214
     * @Then /^(this product option) name should be "([^"]+)"$/
215
     * @Then /^(this product option) should still be named "([^"]+)"$/
216
     */
217
    public function thisProductOptionNameShouldBe(ProductOptionInterface $productOption, string $name): void
218
    {
219
        $this->client->show('product_options', $productOption->getCode());
220
        Assert::true($this->client->responseHasValue('name', $name));
221
    }
222
223
    /**
224
     * @Then /^(product option "[^"]+") should have the "([^"]+)" option value$/
225
     * @Then /^(this product option) should have the "([^"]*)" option value$/
226
     */
227
    public function productOptionShouldHaveTheOptionValue(
228
        ProductOptionInterface $productOption,
229
        string $optionValueName
230
    ): void {
231
        $this->client->subResourceIndex('product_options', 'values', $productOption->getCode());
232
233
        Assert::true($this->client->hasItemWithTranslation('en_US', 'value', $optionValueName));
234
    }
235
236
    /**
237
     * @Then I should not be able to edit its code
238
     */
239
    public function iShouldNotBeAbleToEditItsCode(): void
240
    {
241
        $this->client->addRequestData('code', 'NEW_CODE');
242
        $this->client->update();
243
244
        Assert::false($this->client->responseHasValue('code', 'NEW_CODE'));
245
    }
246
247
    /**
248
     * @Then I should be notified that product option with this code already exists
249
     */
250
    public function iShouldBeNotifiedThatProductOptionWithThisCodeAlreadyExists(): void
251
    {
252
        Assert::false($this->client->isCreationSuccessful(), 'Product option has been created successfully, but it should not');
253
        Assert::same($this->client->getError(), 'code: The option with given code already exists.');
254
    }
255
256
    /**
257
     * @Then I should be notified that :element is required
258
     */
259
    public function iShouldBeNotifiedThatElementIsRequired(string $element): void
260
    {
261
        Assert::contains($this->client->getError(), sprintf('%s: Please enter option %s.', $element, $element));
262
    }
263
}
264