Completed
Push — 1.0-symfony-3.3.13 ( 577127...a3487e )
by Kamil
50:10 queued 26:38
created

iShouldSeeTheZoneNamedInTheList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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\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
    /**
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
        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 /^the code field should be disabled$/
235
     */
236
    public function theCodeFieldShouldBeDisabled()
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 the type field should be disabled
293
     */
294
    public function theTypeFieldShouldBeDisabled()
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 (\d+) zones in the list$/
317
     */
318
    public function iShouldSeeZonesInTheList($number)
319
    {
320
        Assert::same($this->indexPage->countItems(), (int) $number);
321
    }
322
323
    /**
324
     * @Then /^I should(?:| still) see the (zone named "([^"]+)") in the list$/
325
     */
326
    public function iShouldSeeTheZoneNamedInTheList(ZoneInterface $zone)
327
    {
328
        Assert::true($this->indexPage->isSingleResourceOnPage(['code' => $zone->getCode(), 'name' => $zone->getName()]));
329
    }
330
331
    /**
332
     * @Then I should be notified that this zone cannot be deleted
333
     */
334
    public function iShouldBeNotifiedThatThisZoneCannotBeDeleted()
335
    {
336
        $this->notificationChecker->checkNotification('Error Cannot delete, the zone is in use.', NotificationType::failure());
337
    }
338
339
    /**
340
     * @param ZoneInterface $zone
341
     * @param ZoneMemberInterface $zoneMember
342
     *
343
     * @throws \InvalidArgumentException
344
     */
345
    private function assertZoneAndItsMember(ZoneInterface $zone, ZoneMemberInterface $zoneMember)
346
    {
347
        Assert::true(
348
            $this->updatePage->hasResourceValues([
349
                'code' => $zone->getCode(),
350
                'name' => $zone->getName(),
351
            ]),
352
            sprintf('Zone %s is not valid', $zone->getName())
353
        );
354
355
        Assert::true($this->updatePage->hasMember($zoneMember));
356
    }
357
358
    /**
359
     * @Then /^I can not add a(?: country| province| zone) "([^"]+)"$/
360
     */
361
    public function iCanNotAddAZoneMember($name)
362
    {
363
        $member = null;
364
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
        }
369
        Assert::isEmpty($member);
370
    }
371
}
372