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

iDeleteTheCustomerGroup()   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 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\Customer\Model\CustomerGroupInterface;
19
use Webmozart\Assert\Assert;
20
21
final class ManagingCustomerGroupsContext 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 customer group
33
     */
34
    public function iWantToCreateANewCustomerGroup(): void
35
    {
36
        $this->client->buildCreateRequest('customer_groups');
37
    }
38
39
    /**
40
     * @When I specify its code as :code
41
     * @When I do not specify its code
42
     */
43
    public function iSpecifyItsCodeAs(?string $code = null): void
44
    {
45
        if ($code !== null) {
46
            $this->client->addRequestData('code', $code);
47
        }
48
    }
49
50
    /**
51
     * @When I specify its name as :name
52
     */
53
    public function iSpecifyItsNameAs(string $name): void
54
    {
55
        $this->client->addRequestData('name', $name);
56
    }
57
58
    /**
59
     * @When I remove its name
60
     */
61
    public function iRemoveItsName(): void
62
    {
63
        $this->client->addRequestData('name', '');
64
    }
65
66
    /**
67
     * @When I add it
68
     * @When I try to add it
69
     */
70
    public function iAddIt()
71
    {
72
        $this->client->create();
73
    }
74
75
    /**
76
     * @When /^I want to edit (this customer group)$/
77
     */
78
    public function iWantToEditThisCustomerGroup(CustomerGroupInterface $customerGroup): void
79
    {
80
        $this->client->buildUpdateRequest('customer_groups', $customerGroup->getCode());
81
    }
82
83
    /**
84
     * @When I save my changes
85
     * @When I try to save my changes
86
     */
87
    public function iSaveMyChanges(): void
88
    {
89
        $this->client->update();
90
    }
91
92
    /**
93
     * @When I browse customer groups
94
     * @When I want to browse customer groups
95
     */
96
    public function iWantToBrowseCustomerGroups(): void
97
    {
98
        $this->client->index('customer_groups');
99
    }
100
101
    /**
102
     * @When I delete the :customerGroup customer group
103
     */
104
    public function iDeleteTheCustomerGroup(CustomerGroupInterface $customerGroup): void
105
    {
106
        $this->client->delete('customer_groups', $customerGroup->getCode());
107
    }
108
109
    /**
110
     * @Then the customer group :customerGroup should appear in the store
111
     */
112
    public function theCustomerGroupShouldAppearInTheStore(CustomerGroupInterface $customerGroup): void
113
    {
114
        $this->client->index('customer_groups');
115
        Assert::true(
116
            $this->client->hasItemWithValue('code', $customerGroup->getCode()),
117
            sprintf('Customer group with code %s does not exist', $customerGroup->getCode())
118
        );
119
    }
120
121
    /**
122
     * @Then this customer group with name :name should appear in the store
123
     * @Then I should see the customer group :name in the list
124
     */
125
    public function thisCustomerGroupWithNameShouldAppearInTheStore(string $name): void
126
    {
127
        $this->client->index('customer_groups');
128
        Assert::true(
129
            $this->client->hasItemWithValue('name', $name),
130
            sprintf('Customer group with name %s does not exist', $name)
131
        );
132
    }
133
134
    /**
135
     * @Then I should see a single customer group in the list
136
     * @Then I should see :amountOfCustomerGroups customer groups in the list
137
     */
138
    public function iShouldSeeCustomerGroupsInTheList(int $amountOfCustomerGroups = 1): void
139
    {
140
        $this->client->index('customer_groups');
141
        Assert::same($this->client->countCollectionItems(), $amountOfCustomerGroups);
142
    }
143
144
    /**
145
     * @Then /^(this customer group) should still be named "([^"]+)"$/
146
     */
147
    public function thisCustomerGroupShouldStillBeNamed(CustomerGroupInterface $customerGroup, string $name): void
148
    {
149
        $this->client->show('customer_groups', $customerGroup->getCode());
150
        Assert::true($this->client->responseHasValue('name', $name), 'Customer groups name is not ' . $name);
151
    }
152
153
    /**
154
     * @Then I should be notified that name is required
155
     */
156
    public function iShouldBeNotifiedThatNameIsRequired(): void
157
    {
158
        Assert::contains($this->client->getError(), 'name: Please enter a customer group name.');
159
    }
160
161
    /**
162
     * @Then I should be notified that customer group with this code already exists
163
     */
164
    public function iShouldBeNotifiedThatCustomerGroupWithThisCodeAlreadyExists(): void
165
    {
166
        Assert::contains($this->client->getError(), 'Customer group code has to be unique.');
167
    }
168
169
    /**
170
     * @Then I should be informed that this form contains errors
171
     */
172
    public function iShouldBeInformedThatThisFormContainsErrors(): void
173
    {
174
        Assert::notEmpty($this->client->getError());
175
    }
176
177
    /**
178
     * @Then I should not be able to edit its code
179
     */
180
    public function iShouldNotBeAbleToEditItsCode(): void
181
    {
182
        $this->client->addRequestData('code', 'NEW_CODE');
183
        $this->client->update();
184
185
        Assert::false($this->client->responseHasValue('code', 'NEW_CODE'), 'The code field with value NEW_CODE exist');
186
    }
187
188
    /**
189
     * @Then /^(this customer group) should no longer exist in the registry$/
190
     */
191
    public function thisCustomerGroupShouldNoLongerExistInTheRegistry(CustomerGroupInterface $customerGroup)
192
    {
193
        $code = $customerGroup->getCode();
194
        Assert::false($this->isItemOnIndex('code', $code), sprintf('Customer group with code %s exist', $code));
195
    }
196
197
    private function isItemOnIndex(string $property, string $value): bool
198
    {
199
        $this->client->index('customer_groups');
200
201
        return $this->client->hasItemWithValue($property, $value);
202
    }
203
}
204