Completed
Push — master ( ab6ee6...8da333 )
by Kamil
32:30 queued 09:03
created

ManagingZonesContext::iAddIt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
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\Ui\Admin;
15
16
use Behat\Behat\Context\Context;
17
use Behat\Mink\Exception\ElementNotFoundException;
18
use Sylius\Behat\NotificationType;
19
use Sylius\Behat\Page\Admin\Crud\IndexPageInterface;
20
use Sylius\Behat\Page\Admin\Zone\CreatePageInterface;
21
use Sylius\Behat\Page\Admin\Zone\UpdatePageInterface;
22
use Sylius\Behat\Service\NotificationCheckerInterface;
23
use Sylius\Behat\Service\Resolver\CurrentPageResolverInterface;
24
use Sylius\Component\Addressing\Model\ZoneInterface;
25
use Sylius\Component\Addressing\Model\ZoneMemberInterface;
26
use Webmozart\Assert\Assert;
27
28
final class ManagingZonesContext implements Context
29
{
30
    /** @var CreatePageInterface */
31
    private $createPage;
32
33
    /** @var IndexPageInterface */
34
    private $indexPage;
35
36
    /** @var UpdatePageInterface */
37
    private $updatePage;
38
39
    /** @var CurrentPageResolverInterface */
40
    private $currentPageResolver;
41
42
    /** @var NotificationCheckerInterface */
43
    private $notificationChecker;
44
45
    public function __construct(
46
        CreatePageInterface $createPage,
47
        IndexPageInterface $indexPage,
48
        UpdatePageInterface $updatePage,
49
        CurrentPageResolverInterface $currentPageResolver,
50
        NotificationCheckerInterface $notificationChecker
51
    ) {
52
        $this->createPage = $createPage;
53
        $this->indexPage = $indexPage;
54
        $this->updatePage = $updatePage;
55
        $this->currentPageResolver = $currentPageResolver;
56
        $this->notificationChecker = $notificationChecker;
57
    }
58
59
    /**
60
     * @When I want to create a new zone consisting of :memberType
61
     */
62
    public function iWantToCreateANewZoneWithMembers($memberType)
63
    {
64
        $this->createPage->open(['type' => $memberType]);
65
    }
66
67
    /**
68
     * @When I browse zones
69
     * @When I want to see all zones in store
70
     */
71
    public function iWantToSeeAllZonesInStore()
72
    {
73
        $this->indexPage->open();
74
    }
75
76
    /**
77
     * @When /^I want to modify the (zone named "[^"]+")$/
78
     */
79
    public function iWantToModifyAZoneNamed(ZoneInterface $zone)
80
    {
81
        $this->updatePage->open(['id' => $zone->getId()]);
82
    }
83
84
    /**
85
     * @When /^I delete (zone named "([^"]*)")$/
86
     */
87
    public function iDeleteZoneNamed(ZoneInterface $zone)
88
    {
89
        $this->indexPage->open();
90
        $this->indexPage->deleteResourceOnPage(['name' => $zone->getName(), 'code' => $zone->getCode()]);
91
    }
92
93
    /**
94
     * @When /^I remove (the "([^"]*)" (?:country|province|zone) member)$/
95
     */
96
    public function iRemoveTheMember(ZoneMemberInterface $zoneMember)
97
    {
98
        $this->updatePage->removeMember($zoneMember);
99
    }
100
101
    /**
102
     * @When I rename it to :name
103
     */
104
    public function iRenameItTo($name)
105
    {
106
        $this->updatePage->nameIt($name ?? '');
107
    }
108
109
    /**
110
     * @When I name it :name
111
     */
112
    public function iNameIt($name)
113
    {
114
        $this->createPage->nameIt($name ?? '');
115
    }
116
117
    /**
118
     * @When I specify its code as :code
119
     */
120
    public function iSpecifyItsCodeAs($code)
121
    {
122
        $this->createPage->specifyCode($code ?? '');
123
    }
124
125
    /**
126
     * @When I do not specify its code
127
     */
128
    public function iDoNotSpecifyItsCode()
129
    {
130
        // Intentionally left blank to fulfill context expectation
131
    }
132
133
    /**
134
     * @When I do not specify its name
135
     */
136
    public function iDoNotSpecifyItsName()
137
    {
138
        // Intentionally left blank to fulfill context expectation
139
    }
140
141
    /**
142
     * @When I do not add a country member
143
     */
144
    public function iDoNotAddACountryMember()
145
    {
146
        // Intentionally left blank to fulfill context expectation
147
    }
148
149
    /**
150
     * @When /^I add a(?: country| province| zone) "([^"]+)"$/
151
     */
152
    public function iAddAZoneMember($name)
153
    {
154
        $this->createPage->addMember();
155
        $this->createPage->chooseMember($name);
156
    }
157
158
    /**
159
     * @When I select its scope as :scope
160
     */
161
    public function iSelectItsScopeAs($scope)
162
    {
163
        $this->createPage->selectScope($scope);
164
    }
165
166
    /**
167
     * @When I add it
168
     * @When I try to add it
169
     */
170
    public function iAddIt()
171
    {
172
        $this->createPage->create();
173
    }
174
175
    /**
176
     * @When I save my changes
177
     */
178
    public function iSaveMyChanges()
179
    {
180
        $this->updatePage->saveChanges();
181
    }
182
183
    /**
184
     * @When I check (also) the :zoneName zone
185
     */
186
    public function iCheckTheZone(string $zoneName): void
187
    {
188
        $this->indexPage->checkResourceOnPage(['name' => $zoneName]);
189
    }
190
191
    /**
192
     * @When I delete them
193
     */
194
    public function iDeleteThem(): void
195
    {
196
        $this->indexPage->bulkDelete();
197
    }
198
199
    /**
200
     * @Then /^the (zone named "[^"]+") with (the "[^"]+" (?:country|province|zone) member) should appear in the registry$/
201
     */
202
    public function theZoneWithTheCountryShouldAppearInTheRegistry(ZoneInterface $zone, ZoneMemberInterface $zoneMember)
203
    {
204
        $this->assertZoneAndItsMember($zone, $zoneMember);
205
    }
206
207
    /**
208
     * @Given its scope should be :scope
209
     */
210
    public function itsScopeShouldBe($scope)
211
    {
212
        Assert::same($this->updatePage->getScope(), $scope);
213
    }
214
215
    /**
216
     * @Then /^(this zone) should have only (the "([^"]*)" (?:country|province|zone) member)$/
217
     */
218
    public function thisZoneShouldHaveOnlyTheProvinceMember(ZoneInterface $zone, ZoneMemberInterface $zoneMember)
219
    {
220
        $this->assertZoneAndItsMember($zone, $zoneMember);
221
222
        Assert::same($this->updatePage->countMembers(), 1);
223
    }
224
225
    /**
226
     * @Then /^(this zone) name should be "([^"]*)"/
227
     */
228
    public function thisZoneNameShouldBe(ZoneInterface $zone, $name)
229
    {
230
        Assert::true($this->updatePage->hasResourceValues(['code' => $zone->getCode(), 'name' => $name]));
231
    }
232
233
    /**
234
     * @Then I should not be able to edit its code
235
     */
236
    public function iShouldNotBeAbleToEditItsCode(): void
237
    {
238
        Assert::true($this->updatePage->isCodeDisabled());
239
    }
240
241
    /**
242
     * @Then /^I should be notified that zone with this code already exists$/
243
     */
244
    public function iShouldBeNotifiedThatZoneWithThisCodeAlreadyExists()
245
    {
246
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
247
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
248
249
        Assert::same($currentPage->getValidationMessage('code'), 'Zone code must be unique.');
250
    }
251
252
    /**
253
     * @Then /^there should still be only one zone with code "([^"]*)"$/
254
     */
255
    public function thereShouldStillBeOnlyOneZoneWithCode($code)
256
    {
257
        $this->indexPage->open();
258
259
        Assert::true($this->indexPage->isSingleResourceOnPage(['code' => $code]));
260
    }
261
262
    /**
263
     * @Then I should be notified that :element is required
264
     */
265
    public function iShouldBeNotifiedThatIsRequired($element)
266
    {
267
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
268
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
269
270
        Assert::same($currentPage->getValidationMessage($element), sprintf('Please enter zone %s.', $element));
271
    }
272
273
    /**
274
     * @Then zone with :element :value should not be added
275
     */
276
    public function zoneWithNameShouldNotBeAdded($element, $value)
277
    {
278
        $this->indexPage->open();
279
280
        Assert::false($this->indexPage->isSingleResourceOnPage([$element => $value]));
281
    }
282
283
    /**
284
     * @Then /^I should be notified that at least one zone member is required$/
285
     */
286
    public function iShouldBeNotifiedThatAtLeastOneZoneMemberIsRequired()
287
    {
288
        Assert::true($this->createPage->checkValidationMessageForMembers('Please add at least 1 zone member.'));
289
    }
290
291
    /**
292
     * @Then I should not be able to edit its type
293
     */
294
    public function iShouldNotBeAbleToEditItsType(): void
295
    {
296
        Assert::true($this->createPage->isTypeFieldDisabled());
297
    }
298
299
    /**
300
     * @Then it should be of :type type
301
     */
302
    public function itShouldBeOfType($type)
303
    {
304
        Assert::true($this->createPage->hasType($type));
305
    }
306
307
    /**
308
     * @Then the zone named :zoneName should no longer exist in the registry
309
     */
310
    public function thisZoneShouldNoLongerExistInTheRegistry($zoneName)
311
    {
312
        Assert::false($this->indexPage->isSingleResourceOnPage(['name' => $zoneName]));
313
    }
314
315
    /**
316
     * @Then I should see a single zone in the list
317
     * @Then I should see :amount zones in the list
318
     */
319
    public function iShouldSeeZonesInTheList(int $amount = 1): void
320
    {
321
        Assert::same($this->indexPage->countItems(), $amount);
322
    }
323
324
    /**
325
     * @Then /^I should(?:| still) see the (zone named "([^"]+)") in the list$/
326
     */
327
    public function iShouldSeeTheZoneNamedInTheList(ZoneInterface $zone)
328
    {
329
        Assert::true($this->indexPage->isSingleResourceOnPage(['code' => $zone->getCode(), 'name' => $zone->getName()]));
330
    }
331
332
    /**
333
     * @Then I should be notified that this zone cannot be deleted
334
     */
335
    public function iShouldBeNotifiedThatThisZoneCannotBeDeleted()
336
    {
337
        $this->notificationChecker->checkNotification('Error Cannot delete, the zone is in use.', NotificationType::failure());
338
    }
339
340
    /**
341
     * @throws \InvalidArgumentException
342
     */
343
    private function assertZoneAndItsMember(ZoneInterface $zone, ZoneMemberInterface $zoneMember)
344
    {
345
        Assert::true(
346
            $this->updatePage->hasResourceValues([
347
                'code' => $zone->getCode(),
348
                'name' => $zone->getName(),
349
            ]),
350
            sprintf('Zone %s is not valid', $zone->getName())
351
        );
352
353
        Assert::true($this->updatePage->hasMember($zoneMember));
354
    }
355
356
    /**
357
     * @Then /^I can not add a(?: country| province| zone) "([^"]+)"$/
358
     */
359
    public function iCanNotAddAZoneMember($name)
360
    {
361
        $member = null;
362
363
        try {
364
            $member = $this->createPage->chooseMember($name);
365
        } catch (ElementNotFoundException $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
366
        }
367
        Assert::isEmpty($member);
368
    }
369
}
370