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

ManagingTaxCategoriesContext   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 193
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 193
c 0
b 0
f 0
wmc 23
lcom 1
cbo 3
rs 10

21 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A iWantToCreateNewTaxCategory() 0 4 1
A iWantToModifyTaxCategory() 0 4 1
A iDeleteTaxCategory() 0 4 1
A iSpecifyItsCodeAs() 0 6 2
A iNameIt() 0 6 2
A iRemoveItsName() 0 4 1
A iAddIt() 0 4 1
A iDescribeItAs() 0 4 1
A iSaveMyChanges() 0 4 1
A iWantToBrowseTaxCategories() 0 4 1
A thisTaxCategoryShouldNoLongerExistInTheRegistry() 0 8 1
A theTaxCategoryShouldAppearInTheRegistry() 0 7 1
A iShouldNotBeAbleToEditItsCode() 0 7 1
A thisTaxCategoryNameShouldBe() 0 5 1
A iShouldBeNotifiedThatTaxCategoryWithThisCodeAlreadyExists() 0 4 1
A thereShouldStillBeOnlyOneTaxCategoryWith() 0 5 1
A iShouldBeNotifiedThatIsRequired() 0 4 1
A taxCategoryWithNamedElementShouldNotBeAdded() 0 4 1
A iShouldSeeSingleTaxCategoryInTheList() 0 4 1
A isItemOnIndex() 0 6 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\Taxation\Model\TaxCategoryInterface;
19
use Webmozart\Assert\Assert;
20
21
final class ManagingTaxCategoriesContext 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 tax category
33
     */
34
    public function iWantToCreateNewTaxCategory(): void
35
    {
36
        $this->client->buildCreateRequest('tax_categories');
37
    }
38
39
    /**
40
     * @When I want to modify a tax category :taxCategory
41
     * @When /^I want to modify (this tax category)$/
42
     */
43
    public function iWantToModifyTaxCategory(TaxCategoryInterface $taxCategory): void
44
    {
45
        $this->client->buildUpdateRequest('tax_categories', $taxCategory->getCode());
46
    }
47
48
    /**
49
     * @When I delete tax category :taxCategory
50
     */
51
    public function iDeleteTaxCategory(TaxCategoryInterface $taxCategory): void
52
    {
53
        $this->client->delete('tax_categories', $taxCategory->getCode());
54
    }
55
56
    /**
57
     * @When I specify its code as :code
58
     * @When I do not specify its code
59
     */
60
    public function iSpecifyItsCodeAs(?string $code = null): void
61
    {
62
        if ($code !== null) {
63
            $this->client->addRequestData('code', $code);
64
        }
65
    }
66
67
    /**
68
     * @When I name it :name
69
     * @When I rename it to :name
70
     * @When I do not name it
71
     */
72
    public function iNameIt(?string $name = null): void
73
    {
74
        if ($name !== null) {
75
            $this->client->addRequestData('name', $name);
76
        }
77
    }
78
79
    /**
80
     * @When I remove its name
81
     */
82
    public function iRemoveItsName(): void
83
    {
84
        $this->client->addRequestData('name', '');
85
    }
86
87
    /**
88
     * @When I add it
89
     * @When I try to add it
90
     */
91
    public function iAddIt(): void
92
    {
93
        $this->client->create();
94
    }
95
96
    /**
97
     * @When I describe it as :description
98
     */
99
    public function iDescribeItAs(string $description): void
100
    {
101
        $this->client->addRequestData('description', $description);
102
    }
103
104
    /**
105
     * @When I save my changes
106
     * @When I try to save my changes
107
     */
108
    public function iSaveMyChanges(): void
109
    {
110
        $this->client->update();
111
    }
112
113
    /**
114
     * @When I browse tax categories
115
     */
116
    public function iWantToBrowseTaxCategories(): void
117
    {
118
        $this->client->index('tax_categories');
119
    }
120
121
    /**
122
     * @Then /^(this tax category) should no longer exist in the registry$/
123
     */
124
    public function thisTaxCategoryShouldNoLongerExistInTheRegistry(TaxCategoryInterface $taxCategory): void
125
    {
126
        $code = $taxCategory->getCode();
127
        Assert::false(
128
            $this->isItemOnIndex('code', $code),
129
            sprintf('Tax category with code %s exist', $code)
130
        );
131
    }
132
133
    /**
134
     * @Then I should see the tax category :taxCategoryName in the list
135
     * @Then the tax category :taxCategoryName should appear in the registry
136
     */
137
    public function theTaxCategoryShouldAppearInTheRegistry(string $taxCategoryName): void
138
    {
139
        Assert::true(
140
            $this->isItemOnIndex('name', $taxCategoryName),
141
            sprintf('Tax category with name %s does not exist', $taxCategoryName)
142
        );
143
    }
144
145
    /**
146
     * @Then I should not be able to edit its code
147
     */
148
    public function iShouldNotBeAbleToEditItsCode(): void
149
    {
150
        $this->client->addRequestData('code', 'NEW_CODE');
151
        $this->client->update();
152
153
        Assert::false($this->client->responseHasValue('code', 'NEW_CODE'), 'The code field with value NEW_CODE exist');
154
    }
155
156
    /**
157
     * @Then /^(this tax category) name should be "([^"]+)"$/
158
     * @Then /^(this tax category) should still be named "([^"]+)"$/
159
     */
160
    public function thisTaxCategoryNameShouldBe(TaxCategoryInterface $taxCategory, string $taxCategoryName): void
161
    {
162
        $this->client->show('tax_categories', $taxCategory->getCode());
163
        Assert::true($this->client->responseHasValue('name', $taxCategoryName), sprintf('Tax category name is not %s', $taxCategoryName));
164
    }
165
166
    /**
167
     * @Then I should be notified that tax category with this code already exists
168
     */
169
    public function iShouldBeNotifiedThatTaxCategoryWithThisCodeAlreadyExists(): void
170
    {
171
        Assert::same($this->client->getError(), 'code: The tax category with given code already exists.');
172
    }
173
174
    /**
175
     * @Then there should still be only one tax category with :element :value
176
     */
177
    public function thereShouldStillBeOnlyOneTaxCategoryWith(string $element, string $value): void
178
    {
179
        $this->client->index('tax_categories');
180
        Assert::same(count($this->client->getCollectionItemsWithValue($element, $value)), 1);
181
    }
182
183
    /**
184
     * @Then I should be notified that :element is required
185
     */
186
    public function iShouldBeNotifiedThatIsRequired(string $element): void
187
    {
188
        Assert::contains($this->client->getError(), sprintf('%s: Please enter tax category %s.', $element, $element));
189
    }
190
191
    /**
192
     * @Then tax category with :element :name should not be added
193
     */
194
    public function taxCategoryWithNamedElementShouldNotBeAdded(string $element, string $name): void
195
    {
196
        Assert::false($this->isItemOnIndex($element, $name), sprintf('Tax category with %s %s does not exist', $element, $name));
197
    }
198
199
    /**
200
     * @Then I should see a single tax category in the list
201
     */
202
    public function iShouldSeeSingleTaxCategoryInTheList(): void
203
    {
204
        Assert::same($this->client->countCollectionItems(), 1);
205
    }
206
207
    private function isItemOnIndex(string $property, string $value): bool
208
    {
209
        $this->client->index('tax_categories');
210
211
        return $this->client->hasItemWithValue($property, $value);
212
    }
213
}
214