Completed
Push — master ( 30cb53...645fea )
by Paweł
36:15 queued 26:35
created

ManagingZonesContext::itsScopeShouldBe()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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