Completed
Push — master ( 4bb2b6...5501b3 )
by Kamil
22:50
created

ManagingZonesContext::iAddAZoneMember()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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