Completed
Push — master ( bfd93b...a37554 )
by Kamil
15s
created

iDeletePaymentMethod()   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
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
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\NotificationType;
16
use Sylius\Behat\Page\Admin\Crud\IndexPageInterface;
17
use Sylius\Behat\Page\Admin\PaymentMethod\CreatePageInterface;
18
use Sylius\Behat\Page\Admin\PaymentMethod\UpdatePageInterface;
19
use Sylius\Behat\Service\NotificationCheckerInterface;
20
use Sylius\Behat\Service\Resolver\CurrentPageResolverInterface;
21
use Sylius\Component\Payment\Model\PaymentMethodInterface;
22
use Webmozart\Assert\Assert;
23
24
/**
25
 * @author Arkadiusz Krakowiak <[email protected]>
26
 * @author Grzegorz Sadowski <[email protected]>
27
 */
28
final class ManagingPaymentMethodsContext 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
     * @var array
57
     */
58
    private $gatewayFactories;
59
60
    /**
61
     * @param CreatePageInterface $createPage
62
     * @param IndexPageInterface $indexPage
63
     * @param UpdatePageInterface $updatePage
64
     * @param CurrentPageResolverInterface $currentPageResolver
65
     * @param NotificationCheckerInterface $notificationChecker
66
     * @param array $gatewayFactories
67
     */
68
    public function __construct(
69
        CreatePageInterface $createPage,
70
        IndexPageInterface $indexPage,
71
        UpdatePageInterface $updatePage,
72
        CurrentPageResolverInterface $currentPageResolver,
73
        NotificationCheckerInterface $notificationChecker,
74
        array $gatewayFactories
75
    ) {
76
        $this->createPage = $createPage;
77
        $this->indexPage = $indexPage;
78
        $this->updatePage = $updatePage;
79
        $this->currentPageResolver = $currentPageResolver;
80
        $this->notificationChecker = $notificationChecker;
81
        $this->gatewayFactories = $gatewayFactories;
82
    }
83
84
    /**
85
     * @Given I want to modify the :paymentMethod payment method
86
     */
87
    public function iWantToModifyAPaymentMethod(PaymentMethodInterface $paymentMethod)
88
    {
89
        $this->updatePage->open(['id' => $paymentMethod->getId()]);
90
    }
91
92
    /**
93
     * @When I name it :name in :language
94
     * @When I rename it to :name in :language
95
     * @When I remove its name from :language translation
96
     */
97
    public function iNameItIn($name = null, $language)
98
    {
99
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
100
101
        $currentPage->nameIt($name, $language);
102
    }
103
104
    /**
105
     * @When I do not name it
106
     */
107
    public function iDoNotNameIt()
108
    {
109
        // Intentionally left blank to fulfill context expectation
110
    }
111
112
    /**
113
     * @When I enable it
114
     */
115
    public function iEnableIt()
116
    {
117
        $this->updatePage->enable();
118
    }
119
120
    /**
121
     * @When I disable it
122
     */
123
    public function iDisableIt()
124
    {
125
        $this->updatePage->disable();
126
    }
127
128
    /**
129
     * @When I save my changes
130
     * @When I try to save my changes
131
     */
132
    public function iSaveMyChanges()
133
    {
134
        $this->updatePage->saveChanges();
135
    }
136
137
    /**
138
     * @When I delete the :paymentMethod payment method
139
     * @When I try to delete the :paymentMethod payment method
140
     */
141
    public function iDeletePaymentMethod(PaymentMethodInterface $paymentMethod)
142
    {
143
        $this->indexPage->open();
144
        $this->indexPage->deleteResourceOnPage(['code' => $paymentMethod->getCode(), 'name' => $paymentMethod->getName()]);
145
    }
146
147
    /**
148
     * @Then I should be notified that it is in use
149
     */
150
    public function iShouldBeNotifiedThatItIsInUse()
151
    {
152
        $this->notificationChecker->checkNotification('Cannot delete, the payment method is in use.', NotificationType::failure());
153
    }
154
155
    /**
156
     * @When I choose :gatewayName gateway
157
     */
158
    public function iChooseGateway($gatewayName)
159
    {
160
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
161
162
        $currentPage->chooseGateway($gatewayName);
0 ignored issues
show
Bug introduced by
The method chooseGateway() does not seem to exist on object<Sylius\Behat\Page\SymfonyPageInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

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