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

ManagingShippingCategoriesContext::iNameIt()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 10
cc 2
nc 2
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\Api\Admin;
15
16
use Behat\Behat\Context\Context;
17
use Sylius\Behat\Client\ApiClientInterface;
18
use Sylius\Component\Shipping\Model\ShippingCategoryInterface;
19
use Webmozart\Assert\Assert;
20
21
final class ManagingShippingCategoriesContext implements Context
22
{
23
    /** @var ApiClientInterface */
24
    private $client;
25
26
    public function __construct(ApiClientInterface $client)
27
    {
28
        $this->client = $client;
29
    }
30
31
    /**
32
     * @When I want to create a new shipping category
33
     */
34
    public function iWantToCreateANewShippingCategory(): void
35
    {
36
        $this->client->buildCreateRequest('shipping_categories');
37
    }
38
39
    /**
40
     * @When I want to modify a shipping category :shippingCategory
41
     */
42
    public function iWantToModifyAShippingCategory(ShippingCategoryInterface $shippingCategory): void
43
    {
44
        $this->client->buildUpdateRequest('shipping_categories', $shippingCategory->getCode());
45
    }
46
47
    /**
48
     * @When I add it
49
     * @When I try to add it
50
     */
51
    public function iAddIt(): void
52
    {
53
        $this->client->create();
54
    }
55
56
    /**
57
     * @When I delete shipping category :shippingCategory
58
     */
59
    public function iDeleteShippingCategory(ShippingCategoryInterface $shippingCategory): void
60
    {
61
        $this->client->delete('shipping_categories', $shippingCategory->getCode());
62
    }
63
64
    /**
65
     * @When I browse shipping categories
66
     */
67
    public function iBrowseShippingCategories(): void
68
    {
69
        $this->client->index('shipping_categories');
70
    }
71
72
    /**
73
     * @When I do not specify its code
74
     * @When I specify its code as :code
75
     */
76
    public function iSpecifyItsCodeAs(?string $code = null): void
77
    {
78
        if ($code !== null) {
79
            $this->client->addRequestData('code', $code);
80
        }
81
    }
82
83
    /**
84
     * @When I name it :name
85
     * @When I do not specify its name
86
     * @When I rename it to :name
87
     */
88
    public function iNameIt(?string $name = null): void
89
    {
90
        if ($name !== null) {
91
            $this->client->addRequestData('name', $name);
92
        }
93
    }
94
95
    /**
96
     * @When I modify a shipping category :shippingCategory
97
     */
98
    public function iModifyAShippingCategory(ShippingCategoryInterface $shippingCategory): void
99
    {
100
        $this->client->buildUpdateRequest('shipping_categories', $shippingCategory->getCode());
101
    }
102
103
    /**
104
     * @When I specify its description as :description
105
     */
106
    public function iSpecifyItsDescriptionAs(string $description): void
107
    {
108
        $this->client->addRequestData('description', $description);
109
    }
110
111
    /**
112
     * @When I save my changes
113
     */
114
    public function iSaveMyChanges(): void
115
    {
116
        $this->client->update();
117
    }
118
119
    /**
120
     * @Then I should be notified that shipping category with this code already exists
121
     */
122
    public function iShouldBeNotifiedThatShippingCategoryWithThisCodeAlreadyExists(): void
123
    {
124
        Assert::same($this->client->getError(), 'code: The shipping category with given code already exists.');
125
    }
126
127
    /**
128
     * @Then I should be notified that :element is required
129
     */
130
    public function iShouldBeNotifiedThatElementIsRequired(string $element): void
131
    {
132
        Assert::same(
133
            $this->client->getError(), sprintf('%s: Please enter shipping category %s.', $element, $element)
134
        );
135
    }
136
137
    /**
138
     * @Then I should see a single shipping category in the list
139
     * @Then I should see :count shipping categories in the list
140
     */
141
    public function iShouldSeeShippingCategoriesInTheList(int $count = 1): void
142
    {
143
        $this->client->index('shipping_categories');
144
        Assert::same($this->client->countCollectionItems(), $count);
145
    }
146
147
    /**
148
     * @Then /^the (shipping category "([^"]+)") should be in the registry$/
149
     * @Then /^the (shipping category "([^"]+)") should appear in the registry$/
150
     */
151
    public function theShippingCategoryShouldAppearInTheRegistry(ShippingCategoryInterface $shippingCategory): void
152
    {
153
        $shippingCategoryName = $shippingCategory->getName();
154
        Assert::true(
155
            $this->isItemOnIndex('name', $shippingCategoryName),
156
            sprintf('Shipping category with name %s does not exists', $shippingCategoryName)
157
        );
158
    }
159
160
    /**
161
     * @Then shipping category with name :name should not be added
162
     */
163
    public function shippingCategoryWithNameShouldNotBeAdded(string $name): void
164
    {
165
        Assert::false(
166
            $this->isItemOnIndex('name', $name),
167
            sprintf('Shipping category with name %s exists', $name)
168
        );
169
    }
170
171
    /**
172
     * @Then /^(this shipping category) should no longer exist in the registry$/
173
     */
174
    public function thisShippingCategoryShouldNoLongerExistInTheRegistry(
175
        ShippingCategoryInterface $shippingCategory
176
    ): void {
177
        $shippingCategoryName = $shippingCategory->getName();
178
        Assert::false(
179
            $this->isItemOnIndex('name', $shippingCategoryName),
180
            sprintf('Shipping category with name %s exist', $shippingCategoryName)
181
        );
182
    }
183
184
    /**
185
     * @Then I should not be able to edit its code
186
     */
187
    public function iShouldNotBeAbleToEditItsCode(): void
188
    {
189
        $this->client->addRequestData('code', 'NEW_CODE');
190
        $this->client->update();
191
192
        Assert::false(
193
            $this->client->responseHasValue('code', 'NEW_CODE'),
194
            'The shipping category code should not be changed to "NEW_CODE", but it is'
195
        );
196
    }
197
198
    /**
199
     * @Then there should still be only one shipping category with code :code
200
     */
201
    public function thereShouldStillBeOnlyOneShippingCategoryWith(string $code): void
202
    {
203
        $this->client->index('shipping_categories');
204
        Assert::same(count($this->client->getCollectionItemsWithValue('code', $code)), 1);
205
    }
206
207
    /**
208
     * @Then this shipping category name should be :name
209
     */
210
    public function thisShippingCategoryNameShouldBe(string $name): void
211
    {
212
        Assert::true(
213
            $this->client->responseHasValue('name', $name),
214
            sprintf('Shipping category with name %s does not exists', $name)
215
        );
216
    }
217
218
    private function isItemOnIndex(string $property, string $value): bool
219
    {
220
        $this->client->index('shipping_categories');
221
222
        return $this->client->hasItemWithValue($property, $value);
223
    }
224
}
225