Completed
Push — 1.1-phpstan-level-2 ( 261c61 )
by Kamil
195:32 queued 171:07
created

ManagingPaymentMethodsContext::iChooseGateway()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
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 Sylius\Behat\NotificationType;
18
use Sylius\Behat\Page\Admin\Crud\IndexPageInterface;
19
use Sylius\Behat\Page\Admin\PaymentMethod\CreatePageInterface;
20
use Sylius\Behat\Page\Admin\PaymentMethod\UpdatePageInterface;
21
use Sylius\Behat\Service\NotificationCheckerInterface;
22
use Sylius\Behat\Service\Resolver\CurrentPageResolverInterface;
23
use Sylius\Component\Payment\Model\PaymentMethodInterface;
24
use Webmozart\Assert\Assert;
25
26
final class ManagingPaymentMethodsContext implements Context
27
{
28
    /**
29
     * @var CreatePageInterface
30
     */
31
    private $createPage;
32
33
    /**
34
     * @var IndexPageInterface
35
     */
36
    private $indexPage;
37
38
    /**
39
     * @var UpdatePageInterface
40
     */
41
    private $updatePage;
42
43
    /**
44
     * @var CurrentPageResolverInterface
45
     */
46
    private $currentPageResolver;
47
48
    /**
49
     * @var NotificationCheckerInterface
50
     */
51
    private $notificationChecker;
52
53
    /**
54
     * @var array
55
     */
56
    private $gatewayFactories;
57
58
    /**
59
     * @param CreatePageInterface $createPage
60
     * @param IndexPageInterface $indexPage
61
     * @param UpdatePageInterface $updatePage
62
     * @param CurrentPageResolverInterface $currentPageResolver
63
     * @param NotificationCheckerInterface $notificationChecker
64
     * @param array $gatewayFactories
65
     */
66
    public function __construct(
67
        CreatePageInterface $createPage,
68
        IndexPageInterface $indexPage,
69
        UpdatePageInterface $updatePage,
70
        CurrentPageResolverInterface $currentPageResolver,
71
        NotificationCheckerInterface $notificationChecker,
72
        array $gatewayFactories
73
    ) {
74
        $this->createPage = $createPage;
75
        $this->indexPage = $indexPage;
76
        $this->updatePage = $updatePage;
77
        $this->currentPageResolver = $currentPageResolver;
78
        $this->notificationChecker = $notificationChecker;
79
        $this->gatewayFactories = $gatewayFactories;
80
    }
81
82
    /**
83
     * @Given I want to modify the :paymentMethod payment method
84
     */
85
    public function iWantToModifyAPaymentMethod(PaymentMethodInterface $paymentMethod)
86
    {
87
        $this->updatePage->open(['id' => $paymentMethod->getId()]);
88
    }
89
90
    /**
91
     * @When I name it :name in :language
92
     * @When I rename it to :name in :language
93
     * @When I remove its name from :language translation
94
     */
95
    public function iNameItIn($name = null, $language)
0 ignored issues
show
Coding Style introduced by
Parameters which have default values should be placed at the end.

If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway:

// $a must always be passed; it's default value is never used.
function someFunction($a = 5, $b) { }
Loading history...
96
    {
97
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
98
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
99
100
        $currentPage->nameIt($name, $language);
101
    }
102
103
    /**
104
     * @When I do not name it
105
     */
106
    public function iDoNotNameIt()
107
    {
108
        // Intentionally left blank to fulfill context expectation
109
    }
110
111
    /**
112
     * @When I enable it
113
     */
114
    public function iEnableIt()
115
    {
116
        $this->updatePage->enable();
117
    }
118
119
    /**
120
     * @When I disable it
121
     */
122
    public function iDisableIt()
123
    {
124
        $this->updatePage->disable();
125
    }
126
127
    /**
128
     * @When I save my changes
129
     * @When I try to save my changes
130
     */
131
    public function iSaveMyChanges()
132
    {
133
        $this->updatePage->saveChanges();
134
    }
135
136
    /**
137
     * @When I delete the :paymentMethod payment method
138
     * @When I try to delete the :paymentMethod payment method
139
     */
140
    public function iDeletePaymentMethod(PaymentMethodInterface $paymentMethod)
141
    {
142
        $this->indexPage->open();
143
        $this->indexPage->deleteResourceOnPage(['code' => $paymentMethod->getCode(), 'name' => $paymentMethod->getName()]);
144
    }
145
146
    /**
147
     * @Then I should be notified that it is in use
148
     */
149
    public function iShouldBeNotifiedThatItIsInUse()
150
    {
151
        $this->notificationChecker->checkNotification('Cannot delete, the payment method is in use.', NotificationType::failure());
152
    }
153
154
    /**
155
     * @Then this payment method :element should be :value
156
     */
157
    public function thisPaymentMethodElementShouldBe($element, $value)
158
    {
159
        Assert::true($this->updatePage->hasResourceValues([$element => $value]));
160
    }
161
162
    /**
163
     * @When I want to create a new offline payment method
164
     * @When I want to create a new payment method with :factory gateway factory
165
     */
166
    public function iWantToCreateANewPaymentMethod($factory = 'Offline')
167
    {
168
        $this->createPage->open(['factory' => array_search($factory, $this->gatewayFactories, true)]);
169
    }
170
171
    /**
172
     * @When I specify its code as :code
173
     * @When I do not specify its code
174
     */
175
    public function iSpecifyItsCodeAs($code = null)
176
    {
177
        $this->createPage->specifyCode($code);
178
    }
179
180
    /**
181
     * @When I describe it as :description in :language
182
     */
183
    public function iDescribeItAsIn($description, $language)
184
    {
185
        $this->createPage->describeIt($description, $language);
186
    }
187
188
    /**
189
     * @When make it available in channel :channel
190
     */
191
    public function iMakeItAvailableInChannel($channel)
192
    {
193
        $this->createPage->checkChannel($channel);
194
    }
195
196
    /**
197
     * @Given I set its instruction as :instructions in :language
198
     */
199
    public function iSetItsInstructionAsIn($instructions, $language)
200
    {
201
        $this->createPage->setInstructions($instructions, $language);
202
    }
203
204
    /**
205
     * @When I add it
206
     * @When I try to add it
207
     */
208
    public function iAddIt()
209
    {
210
        $this->createPage->create();
211
    }
212
213
    /**
214
     * @When I check (also) the :paymentMethodName payment method
215
     */
216
    public function iCheckThePaymentMethod(string $paymentMethodName): void
217
    {
218
        $this->indexPage->checkResourceOnPage(['name' => $paymentMethodName]);
219
    }
220
221
    /**
222
     * @When I delete them
223
     */
224
    public function iDeleteThem(): void
225
    {
226
        $this->indexPage->bulkDelete();
227
    }
228
229
    /**
230
     * @Then the payment method :paymentMethodName should appear in the registry
231
     * @Then the payment method :paymentMethodName should be in the registry
232
     * @Then I should see the payment method :paymentMethodName in the list
233
     */
234
    public function thePaymentMethodShouldAppearInTheRegistry(string $paymentMethodName): void
235
    {
236
        $this->indexPage->open();
237
238
        Assert::true($this->indexPage->isSingleResourceOnPage(['name' => $paymentMethodName]));
239
    }
240
241
    /**
242
     * @Given /^(this payment method) should still be in the registry$/
243
     */
244
    public function thisPaymentMethodShouldStillBeInTheRegistry(PaymentMethodInterface $paymentMethod)
245
    {
246
        $this->thePaymentMethodShouldAppearInTheRegistry($paymentMethod->getName());
247
    }
248
249
    /**
250
     * @Given I am browsing payment methods
251
     * @When I browse payment methods
252
     */
253
    public function iBrowsePaymentMethods()
254
    {
255
        $this->indexPage->open();
256
    }
257
258
    /**
259
     * @Then the first payment method on the list should have :field :value
260
     */
261
    public function theFirstPaymentMethodOnTheListShouldHave($field, $value)
262
    {
263
        Assert::same($this->indexPage->getColumnFields($field)[0], $value);
264
    }
265
266
    /**
267
     * @Then the last payment method on the list should have :field :value
268
     */
269
    public function theLastPaymentMethodOnTheListShouldHave($field, $value)
270
    {
271
        $values = $this->indexPage->getColumnFields($field);
272
273
        Assert::same(end($values), $value);
274
    }
275
276
    /**
277
     * @When I switch the way payment methods are sorted by :field
278
     * @When I start sorting payment methods by :field
279
     * @Given the payment methods are already sorted by :field
280
     */
281
    public function iSortPaymentMethodsBy($field)
282
    {
283
        $this->indexPage->sortBy($field);
284
    }
285
286
    /**
287
     * @Then I should see a single payment method in the list
288
     * @Then I should see :amount payment methods in the list
289
     */
290
    public function iShouldSeePaymentMethodsInTheList(int $amount = 1): void
291
    {
292
        Assert::same($this->indexPage->countItems(), (int) $amount);
293
    }
294
295
    /**
296
     * @Then I should be notified that :element is required
297
     */
298
    public function iShouldBeNotifiedThatIsRequired($element)
299
    {
300
        $this->assertFieldValidationMessage($element, sprintf('Please enter payment method %s.', $element));
301
    }
302
303
    /**
304
     * @Then I should be notified that I have to specify paypal :element
305
     */
306
    public function iShouldBeNotifiedThatIHaveToSpecifyPaypal($element)
307
    {
308
        Assert::same(
309
            $this->createPage->getValidationMessage('paypal_' . $element),
310
            sprintf('Please enter paypal %s.', $element)
311
        );
312
    }
313
314
    /**
315
     * @Then I should be notified that gateway name should contain only letters and underscores
316
     */
317
    public function iShouldBeNotifiedThatGatewayNameShouldContainOnlyLettersAndUnderscores()
318
    {
319
        Assert::same(
320
            $this->createPage->getValidationMessage('gateway_name'),
321
            'Gateway name should contain only letters and underscores.'
322
        );
323
    }
324
325
    /**
326
     * @Then the payment method with :element :value should not be added
327
     */
328
    public function thePaymentMethodWithElementValueShouldNotBeAdded($element, $value)
329
    {
330
        $this->iBrowsePaymentMethods();
331
332
        Assert::false($this->indexPage->isSingleResourceOnPage([$element => $value]));
333
    }
334
335
    /**
336
     * @Then /^(this payment method) should still be named "([^"]+)"$/
337
     */
338
    public function thisShippingMethodNameShouldBe(PaymentMethodInterface $paymentMethod, $paymentMethodName)
339
    {
340
        $this->iBrowsePaymentMethods();
341
342
        Assert::true($this->indexPage->isSingleResourceOnPage([
343
            'code' => $paymentMethod->getCode(),
344
            'name' => $paymentMethodName,
345
        ]));
346
    }
347
348
    /**
349
     * @param string $element
350
     * @param string $expectedMessage
351
     */
352
    private function assertFieldValidationMessage($element, $expectedMessage)
353
    {
354
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
355
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
356
357
        Assert::same($currentPage->getValidationMessage($element), $expectedMessage);
358
    }
359
360
    /**
361
     * @Then the code field should be disabled
362
     */
363
    public function theCodeFieldShouldBeDisabled()
364
    {
365
        Assert::true($this->updatePage->isCodeDisabled());
366
    }
367
368
    /**
369
     * @Then the factory name field should be disabled
370
     */
371
    public function theFactoryNameFieldShouldBeDisabled()
372
    {
373
        Assert::true($this->updatePage->isFactoryNameFieldDisabled());
374
    }
375
376
    /**
377
     * @Then this payment method should be enabled
378
     */
379
    public function thisPaymentMethodShouldBeEnabled()
380
    {
381
        Assert::true($this->updatePage->isPaymentMethodEnabled());
382
    }
383
384
    /**
385
     * @Then this payment method should be disabled
386
     */
387
    public function thisPaymentMethodShouldBeDisabled()
388
    {
389
        Assert::false($this->updatePage->isPaymentMethodEnabled());
390
    }
391
392
    /**
393
     * @Given the payment method :paymentMethod should have instructions :instructions in :language
394
     */
395
    public function thePaymentMethodShouldHaveInstructionsIn(
396
        PaymentMethodInterface $paymentMethod,
397
        $instructions,
398
        $language
399
    ) {
400
        $this->iWantToModifyAPaymentMethod($paymentMethod);
401
402
        Assert::same($this->updatePage->getPaymentMethodInstructions($language), $instructions);
403
    }
404
405
    /**
406
     * @Then the payment method :paymentMethod should be available in channel :channelName
407
     */
408
    public function thePaymentMethodShouldBeAvailableInChannel(
409
        PaymentMethodInterface $paymentMethod,
410
        $channelName
411
    ) {
412
        $this->iWantToModifyAPaymentMethod($paymentMethod);
413
414
        Assert::true($this->updatePage->isAvailableInChannel($channelName));
415
    }
416
417
    /**
418
     * @Then /^(this payment method) should no longer exist in the registry$/
419
     */
420
    public function thisPaymentMethodShouldNoLongerExistInTheRegistry(PaymentMethodInterface $paymentMethod)
421
    {
422
        Assert::false($this->indexPage->isSingleResourceOnPage([
423
            'code' => $paymentMethod->getCode(),
424
            'name' => $paymentMethod->getName(),
425
        ]));
426
    }
427
428
    /**
429
     * @Then I should be notified that payment method with this code already exists
430
     */
431
    public function iShouldBeNotifiedThatPaymentMethodWithThisCodeAlreadyExists()
432
    {
433
        Assert::same($this->createPage->getValidationMessage('code'), 'The payment method with given code already exists.');
434
    }
435
436
    /**
437
     * @Then there should still be only one payment method with :element :code
438
     */
439
    public function thereShouldStillBeOnlyOnePaymentMethodWith($element, $code)
440
    {
441
        $this->iBrowsePaymentMethods();
442
443
        Assert::true($this->indexPage->isSingleResourceOnPage([$element => $code]));
444
    }
445
446
    /**
447
     * @When I configure it with test paypal credentials
448
     */
449
    public function iConfigureItWithTestPaypalCredentials()
450
    {
451
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
452
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
453
454
        $currentPage->setPaypalGatewayUsername('TEST');
455
        $currentPage->setPaypalGatewayPassword('TEST');
456
        $currentPage->setPaypalGatewaySignature('TEST');
457
    }
458
459
    /**
460
     * @When I configure it for username :username with :signature signature
461
     */
462
    public function iConfigureItForUsernameWithSignature($username, $signature)
463
    {
464
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
465
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
466
467
        $currentPage->setPaypalGatewayUsername($username);
468
        $currentPage->setPaypalGatewaySignature($signature);
469
    }
470
471
    /**
472
     * @When I do not specify configuration password
473
     */
474
    public function iDoNotSpecifyConfigurationPassword()
475
    {
476
        // Intentionally left blank to fulfill context expectation
477
    }
478
479
    /**
480
     * @When I configure it with test stripe gateway data
481
     */
482
    public function iConfigureItWithTestStripeGatewayData()
483
    {
484
        $this->createPage->setStripeSecretKey('TEST');
485
        $this->createPage->setStripePublishableKey('TEST');
486
    }
487
}
488