Completed
Push — master ( ec5411...629401 )
by Kamil
31:49 queued 16:57
created

iShouldSeeTheImageAsMyAvatar()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 2
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 Sylius\Behat\Element\Admin\TopBarElementInterface;
18
use Sylius\Behat\NotificationType;
19
use Sylius\Behat\Page\Admin\Administrator\CreatePageInterface;
20
use Sylius\Behat\Page\Admin\Administrator\UpdatePageInterface;
21
use Sylius\Behat\Page\Admin\Crud\IndexPageInterface;
22
use Sylius\Behat\Service\NotificationCheckerInterface;
23
use Sylius\Behat\Service\SharedStorageInterface;
24
use Sylius\Component\Core\Model\AdminUserInterface;
25
use Sylius\Component\Resource\Repository\RepositoryInterface;
26
use Webmozart\Assert\Assert;
27
28
final class ManagingAdministratorsContext 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 TopBarElementInterface */
40
    private $topBarElement;
41
42
    /** @var NotificationCheckerInterface */
43
    private $notificationChecker;
44
45
    /** @var RepositoryInterface */
46
    private $adminUserRepository;
47
48
    /** @var SharedStorageInterface */
49
    private $sharedStorage;
50
51
    public function __construct(
52
        CreatePageInterface $createPage,
53
        IndexPageInterface $indexPage,
54
        UpdatePageInterface $updatePage,
55
        TopBarElementInterface $topBarElement,
56
        NotificationCheckerInterface $notificationChecker,
57
        RepositoryInterface $adminUserRepository,
58
        SharedStorageInterface $sharedStorage
59
    ) {
60
        $this->createPage = $createPage;
61
        $this->indexPage = $indexPage;
62
        $this->updatePage = $updatePage;
63
        $this->topBarElement = $topBarElement;
64
        $this->notificationChecker = $notificationChecker;
65
        $this->adminUserRepository = $adminUserRepository;
66
        $this->sharedStorage = $sharedStorage;
67
    }
68
69
    /**
70
     * @Given I want to create a new administrator
71
     */
72
    public function iWantToCreateANewAdministrator()
73
    {
74
        $this->createPage->open();
75
    }
76
77
    /**
78
     * @Given /^I am editing (my) details$/
79
     * @When /^I want to edit (this administrator)$/
80
     */
81
    public function iWantToEditThisAdministrator(AdminUserInterface $adminUser)
82
    {
83
        $this->updatePage->open(['id' => $adminUser->getId()]);
84
    }
85
86
    /**
87
     * @When I browse administrators
88
     * @When I want to browse administrators
89
     */
90
    public function iWantToBrowseAdministrators()
91
    {
92
        $this->indexPage->open();
93
    }
94
95
    /**
96
     * @When I specify its name as :username
97
     * @When I do not specify its name
98
     */
99
    public function iSpecifyItsNameAs($username = null)
100
    {
101
        $this->createPage->specifyUsername($username ?? '');
102
    }
103
104
    /**
105
     * @When I change its name to :username
106
     */
107
    public function iChangeItsNameTo($username)
108
    {
109
        $this->updatePage->changeUsername($username);
110
    }
111
112
    /**
113
     * @When I specify its email as :email
114
     * @When I do not specify its email
115
     */
116
    public function iSpecifyItsEmailAs($email = null)
117
    {
118
        $this->createPage->specifyEmail($email ?? '');
119
    }
120
121
    /**
122
     * @When I change its email to :email
123
     */
124
    public function iChangeItsEmailTo($email)
125
    {
126
        $this->updatePage->changeEmail($email);
127
    }
128
129
    /**
130
     * @When I specify its locale as :localeCode
131
     */
132
    public function iSpecifyItsLocaleAs($localeCode)
133
    {
134
        $this->createPage->specifyLocale($localeCode);
135
    }
136
137
    /**
138
     * @When I set my locale to :localeCode
139
     */
140
    public function iSetMyLocaleTo($localeCode)
141
    {
142
        $this->updatePage->changeLocale($localeCode);
143
        $this->updatePage->saveChanges();
144
    }
145
146
    /**
147
     * @When I specify its password as :password
148
     * @When I do not specify its password
149
     */
150
    public function iSpecifyItsPasswordAs($password = null)
151
    {
152
        $this->createPage->specifyPassword($password ?? '');
153
    }
154
155
    /**
156
     * @When I change its password to :password
157
     */
158
    public function iChangeItsPasswordTo($password)
159
    {
160
        $this->updatePage->changePassword($password);
161
    }
162
163
    /**
164
     * @When I enable it
165
     */
166
    public function iEnableIt()
167
    {
168
        $this->createPage->enable();
169
    }
170
171
    /**
172
     * @When I add it
173
     * @When I try to add it
174
     */
175
    public function iAddIt()
176
    {
177
        $this->createPage->create();
178
    }
179
180
    /**
181
     * @When I save my changes
182
     */
183
    public function iSaveMyChanges()
184
    {
185
        $this->updatePage->saveChanges();
186
    }
187
188
    /**
189
     * @When I delete administrator with email :email
190
     */
191
    public function iDeleteAdministratorWithEmail($email)
192
    {
193
        $this->indexPage->deleteResourceOnPage(['email' => $email]);
194
    }
195
196
    /**
197
     * @When I check (also) the :email administrator
198
     */
199
    public function iCheckTheAdministrator(string $email): void
200
    {
201
        $this->indexPage->checkResourceOnPage(['email' => $email]);
202
    }
203
204
    /**
205
     * @When I delete them
206
     */
207
    public function iDeleteThem(): void
208
    {
209
        $this->indexPage->bulkDelete();
210
    }
211
212
    /**
213
     * @When /^I (?:|upload|update) the "([^"]+)" image as (my) avatar$/
214
     */
215
    public function iUploadTheImageAsMyAvatar(string $avatar, AdminUserInterface $administrator): void
216
    {
217
        $path = $this->updateAvatar($avatar, $administrator);
218
219
        $this->sharedStorage->set($avatar, $path);
220
    }
221
222
    /**
223
     * @Then the administrator :email should appear in the store
224
     * @Then I should see the administrator :email in the list
225
     * @Then there should still be only one administrator with an email :email
226
     */
227
    public function theAdministratorShouldAppearInTheStore($email)
228
    {
229
        $this->indexPage->open();
230
231
        Assert::true($this->indexPage->isSingleResourceOnPage(['email' => $email]));
232
    }
233
234
    /**
235
     * @Then this administrator with name :username should appear in the store
236
     * @Then there should still be only one administrator with name :username
237
     */
238
    public function thisAdministratorWithNameShouldAppearInTheStore($username)
239
    {
240
        $this->indexPage->open();
241
242
        Assert::true($this->indexPage->isSingleResourceOnPage(['username' => $username]));
243
    }
244
245
    /**
246
     * @Then I should see a single administrator in the list
247
     * @Then /^there should be (\d+) administrators in the list$/
248
     */
249
    public function iShouldSeeAdministratorsInTheList(int $number = 1): void
250
    {
251
        Assert::same($this->indexPage->countItems(), (int) $number);
252
    }
253
254
    /**
255
     * @Then I should be notified that email must be unique
256
     */
257
    public function iShouldBeNotifiedThatEmailMustBeUnique()
258
    {
259
        Assert::same($this->createPage->getValidationMessage('email'), 'This email is already used.');
260
    }
261
262
    /**
263
     * @Then I should be notified that name must be unique
264
     */
265
    public function iShouldBeNotifiedThatNameMustBeUnique()
266
    {
267
        Assert::same($this->createPage->getValidationMessage('name'), 'This username is already used.');
268
    }
269
270
    /**
271
     * @Then I should be notified that the :elementName is required
272
     */
273
    public function iShouldBeNotifiedThatFirstNameIsRequired($elementName)
274
    {
275
        Assert::same($this->createPage->getValidationMessage($elementName), sprintf('Please enter your %s.', $elementName));
276
    }
277
278
    /**
279
     * @Then I should be notified that this email is not valid
280
     */
281
    public function iShouldBeNotifiedThatEmailIsNotValid()
282
    {
283
        Assert::same($this->createPage->getValidationMessage('email'), 'This email is invalid.');
284
    }
285
286
    /**
287
     * @Then this administrator should not be added
288
     */
289
    public function thisAdministratorShouldNotBeAdded()
290
    {
291
        $this->indexPage->open();
292
293
        Assert::same($this->indexPage->countItems(), 1);
294
    }
295
296
    /**
297
     * @Then there should not be :email administrator anymore
298
     */
299
    public function thereShouldBeNoAnymore($email)
300
    {
301
        Assert::false($this->indexPage->isSingleResourceOnPage(['email' => $email]));
302
    }
303
304
    /**
305
     * @Then I should be notified that it cannot be deleted
306
     */
307
    public function iShouldBeNotifiedThatItCannotBeDeleted()
308
    {
309
        $this->notificationChecker->checkNotification(
310
            'Cannot remove currently logged in user.',
311
            NotificationType::failure()
312
        );
313
    }
314
315
    /**
316
     * @Then /^I should see the "([^"]*)" image as (my) avatar$/
317
     */
318
    public function iShouldSeeTheImageAsMyAvatar(string $avatar, AdminUserInterface $administrator): void
319
    {
320
        /** @var AdminUserInterface $administrator */
321
        $administrator = $this->adminUserRepository->findOneBy(['id' => $administrator->getId()]);
322
323
        $this->updatePage->open(['id' => $administrator->getId()]);
324
325
        Assert::same($this->sharedStorage->get($avatar), $administrator->getAvatar()->getPath());
326
    }
327
328
    /**
329
     * @Then /^I should see the "([^"]*)" avatar image in the top bar next to my name$/
330
     */
331
    public function iShouldSeeTheAvatarImageInTheTopBarNextToMyName(string $avatar): void
332
    {
333
        Assert::true($this->topBarElement->hasAvatarInMainBar($avatar));
334
    }
335
336
    private function getAdministrator(AdminUserInterface $administrator): AdminUserInterface
337
    {
338
        /** @var AdminUserInterface $administrator */
339
        $administrator = $this->adminUserRepository->findOneBy(['id' => $administrator->getId()]);
340
341
        return $administrator;
342
    }
343
344
    private function getPath(AdminUserInterface $administrator): string
345
    {
346
        $administrator = $this->getAdministrator($administrator);
347
348
        $avatar = $administrator->getAvatar();
349
        if (null === $avatar) {
350
            return '';
351
        }
352
353
        return $avatar->getPath() ?? '';
354
    }
355
356
    private function updateAvatar(string $avatar, AdminUserInterface $administrator): string
357
    {
358
        $this->updatePage->attachAvatar($avatar);
359
        $this->updatePage->saveChanges();
360
361
        return $this->getPath($administrator);
362
    }
363
}
364