Completed
Push — symfony3 ( 405d0c...88ded0 )
by Kamil
32:03 queued 12:32
created

thePaymentMethodShouldBeAvailableInChannel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
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
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\PaymentMethod\CreatePageInterface;
17
use Sylius\Behat\Page\Admin\PaymentMethod\UpdatePageInterface;
18
use Sylius\Behat\Service\Resolver\CurrentPageResolverInterface;
19
use Sylius\Component\Core\Model\ChannelInterface;
20
use Sylius\Component\Payment\Model\PaymentMethodInterface;
21
use Webmozart\Assert\Assert;
22
23
/**
24
 * @author Arkadiusz Krakowiak <[email protected]>
25
 * @author Grzegorz Sadowski <[email protected]>
26
 */
27
final class ManagingPaymentMethodsContext implements Context
28
{
29
    /**
30
     * @var CreatePageInterface
31
     */
32
    private $createPage;
33
34
    /**
35
     * @var IndexPageInterface
36
     */
37
    private $indexPage;
38
39
    /**
40
     * @var UpdatePageInterface
41
     */
42
    private $updatePage;
43
44
    /**
45
     * @var CurrentPageResolverInterface
46
     */
47
    private $currentPageResolver;
48
49
    /**
50
     * @param CreatePageInterface $createPage
51
     * @param IndexPageInterface $indexPage
52
     * @param UpdatePageInterface $updatePage
53
     * @param CurrentPageResolverInterface $currentPageResolver
54
     */
55
    public function __construct(
56
        CreatePageInterface $createPage,
57
        IndexPageInterface $indexPage,
58
        UpdatePageInterface $updatePage,
59
        CurrentPageResolverInterface $currentPageResolver
60
    ) {
61
        $this->createPage = $createPage;
62
        $this->indexPage = $indexPage;
63
        $this->updatePage = $updatePage;
64
        $this->currentPageResolver = $currentPageResolver;
65
    }
66
67
    /**
68
     * @Given I want to modify the :paymentMethod payment method
69
     */
70
    public function iWantToModifyAPaymentMethod(PaymentMethodInterface $paymentMethod)
71
    {
72
        $this->updatePage->open(['id' => $paymentMethod->getId()]);
73
    }
74
75
    /**
76
     * @When I name it :name in :language
77
     * @When I rename it to :name in :language
78
     * @When I remove its name from :language translation
79
     */
80
    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...
81
    {
82
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
83
84
        $currentPage->nameIt($name, $language);
85
    }
86
87
    /**
88
     * @When I do not name it
89
     */
90
    public function iDoNotNameIt()
91
    {
92
        // Intentionally left blank to fulfill context expectation
93
    }
94
95
    /**
96
     * @When I enable it
97
     */
98
    public function iEnableIt()
99
    {
100
        $this->updatePage->enable();
101
    }
102
103
    /**
104
     * @When I disable it
105
     */
106
    public function iDisableIt()
107
    {
108
        $this->updatePage->disable();
109
    }
110
111
    /**
112
     * @When I save my changes
113
     * @When I try to save my changes
114
     */
115
    public function iSaveMyChanges()
116
    {
117
        $this->updatePage->saveChanges();
118
    }
119
120
    /**
121
     * @When I delete the :paymentMethod payment method
122
     */
123
    public function iDeletePaymentMethod(PaymentMethodInterface $paymentMethod)
124
    {
125
        $this->indexPage->open();
126
        $this->indexPage->deleteResourceOnPage(['code' => $paymentMethod->getCode(), 'name' => $paymentMethod->getName()]);
127
    }
128
129
    /**
130
     * @When I choose :gatewayName gateway
131
     */
132
    public function iChooseGateway($gatewayName)
133
    {
134
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
135
136
        $currentPage->chooseGateway($gatewayName);
137
    }
138
139
    /**
140
     * @Then this payment method :element should be :value
141
     */
142
    public function thisPaymentMethodElementShouldBe($element, $value)
143
    {
144
        Assert::true(
145
            $this->updatePage->hasResourceValues([$element => $value]),
146
            sprintf('Payment method %s should be %s', $element, $value)
147
        );
148
    }
149
150
    /**
151
     * @Given I want to create a new payment method
152
     */
153
    public function iWantToCreateANewPaymentMethod()
154
    {
155
        $this->createPage->open();
156
    }
157
158
    /**
159
     * @When I specify its code as :code
160
     * @When I do not specify its code
161
     */
162
    public function iSpecifyItsCodeAs($code = null)
163
    {
164
        $this->createPage->specifyCode($code);
165
    }
166
167
    /**
168
     * @When I describe it as :description in :language
169
     */
170
    public function iDescribeItAsIn($description, $language)
171
    {
172
        $this->createPage->describeIt($description, $language);
173
    }
174
175
    /**
176
     * @When make it available in channel :channel
177
     */
178
    public function iMakeItAvailableInChannel($channel)
179
    {
180
        $this->createPage->checkChannel($channel);
181
    }
182
183
    /**
184
     * @Given I set its instruction as :instructions in :language
185
     */
186
    public function iSetItsInstructionAsIn($instructions, $language)
187
    {
188
        $this->createPage->setInstructions($instructions, $language);
189
    }
190
191
    /**
192
     * @When I add it
193
     * @When I try to add it
194
     */
195
    public function iAddIt()
196
    {
197
        $this->createPage->create();
198
    }
199
200
    /**
201
     * @Then the payment method :paymentMethodName should appear in the registry
202
     * @Then the payment method :paymentMethodName should be in the registry
203
     */
204
    public function thePaymentMethodShouldAppearInTheRegistry($paymentMethodName)
205
    {
206
        $this->indexPage->open();
207
208
        Assert::true(
209
            $this->indexPage->isSingleResourceOnPage(['name' => $paymentMethodName]),
210
            sprintf('Payment method with name %s has not been found.', $paymentMethodName)
211
        );
212
    }
213
214
    /**
215
     * @Given I am browsing payment methods
216
     * @When I browse payment methods
217
     */
218
    public function iBrowsePaymentMethods()
219
    {
220
        $this->indexPage->open();
221
    }
222
223
    /**
224
     * @Then the first payment method on the list should have :field :value
225
     */
226
    public function theFirstPaymentMethodOnTheListShouldHave($field, $value)
227
    {
228
        $actualValue = $this->indexPage->getColumnFields($field)[0];
229
230
        Assert::same(
231
            $actualValue,
232
            $value,
233
            sprintf('Expected first payment method\'s %s to be "%s", but it is "%s".', $field, $value, $actualValue)
234
        );
235
    }
236
237
    /**
238
     * @When I switch the way payment methods are sorted by :field
239
     * @When I start sorting payment methods by :field
240
     * @Given the payment methods are already sorted by :field
241
     */
242
    public function iSortPaymentMethodsBy($field)
243
    {
244
        $this->indexPage->sortBy($field);
245
    }
246
247
    /**
248
     * @Then I should see :amount payment methods in the list
249
     */
250
    public function iShouldSeePaymentMethodsInTheList($amount)
251
    {
252
        $foundRows = $this->indexPage->countItems();
253
254
        Assert::same(
255
            (int) $amount,
256
            $foundRows,
257
            '%2$s rows with payment methods should appear on page, %s rows has been found'
258
        );
259
    }
260
261
    /**
262
     * @Then I should be notified that :element is required
263
     */
264
    public function iShouldBeNotifiedThatIsRequired($element)
265
    {
266
        $this->assertFieldValidationMessage($element, sprintf('Please enter payment method %s.', $element));
267
    }
268
269
    /**
270
     * @Then the payment method with :element :value should not be added
271
     */
272
    public function thePaymentMethodWithElementValueShouldNotBeAdded($element, $value)
273
    {
274
        $this->iBrowsePaymentMethods();
275
276
        Assert::false(
277
            $this->indexPage->isSingleResourceOnPage([$element => $value]),
278
            sprintf('Payment method with %s %s was created, but it should not.', $element, $value)
279
        );
280
    }
281
282
    /**
283
     * @Then /^(this payment method) should still be named "([^"]+)"$/
284
     */
285
    public function thisShippingMethodNameShouldBe(PaymentMethodInterface $paymentMethod, $paymentMethodName)
286
    {
287
        $this->iBrowsePaymentMethods();
288
289
        Assert::true(
290
            $this->indexPage->isSingleResourceOnPage([
291
                'code' => $paymentMethod->getCode(),
292
                'name' => $paymentMethodName,
293
            ]),
294
            sprintf('Payment method name %s has not been assigned properly.', $paymentMethodName)
295
        );
296
    }
297
298
    /**
299
     * @param string $element
300
     * @param string $expectedMessage
301
     */
302
    private function assertFieldValidationMessage($element, $expectedMessage)
303
    {
304
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
305
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
306
307
        Assert::same($currentPage->getValidationMessage($element), $expectedMessage);
308
    }
309
310
    /**
311
     * @Then the code field should be disabled
312
     */
313
    public function theCodeFieldShouldBeDisabled()
314
    {
315
        Assert::true(
316
            $this->updatePage->isCodeDisabled(),
317
            'Code field should be disabled'
318
        );
319
    }
320
321
    /**
322
     * @Then this payment method should be enabled
323
     */
324
    public function thisPaymentMethodShouldBeEnabled()
325
    {
326
        Assert::true(
327
            $this->updatePage->isPaymentMethodEnabled(),
328
            'Payment method should be enabled'
329
        );
330
    }
331
332
    /**
333
     * @Then this payment method should be disabled
334
     */
335
    public function thisPaymentMethodShouldBeDisabled()
336
    {
337
        Assert::false(
338
            $this->updatePage->isPaymentMethodEnabled(),
339
            'Payment method should be disabled'
340
        );
341
    }
342
343
    /**
344
     * @Given the payment method :paymentMethod should have instructions :instructions in :language
345
     */
346
    public function thePaymentMethodShouldHaveInstructionsIn(
347
        PaymentMethodInterface $paymentMethod,
348
        $instructions,
349
        $language
350
    ) {
351
        $this->iWantToModifyAPaymentMethod($paymentMethod);
352
353
        Assert::same(
354
            $this->updatePage->getPaymentMethodInstructions($language),
355
            $instructions
356
        );
357
    }
358
359
    /**
360
     * @Then the payment method :paymentMethod should be available in channel :channelName
361
     */
362
    public function thePaymentMethodShouldBeAvailableInChannel(
363
        PaymentMethodInterface $paymentMethod,
364
        $channelName
365
    ) {
366
        $this->iWantToModifyAPaymentMethod($paymentMethod);
367
368
        Assert::true(
369
            $this->updatePage->isAvailableInChannel($channelName),
370
            sprintf('Payment method should be available in channel "%s" but it does not.', $channelName)
371
        );
372
    }
373
374
    /**
375
     * @Then /^(this payment method) should no longer exist in the registry$/
376
     */
377
    public function thisPaymentMethodShouldNoLongerExistInTheRegistry(PaymentMethodInterface $paymentMethod)
378
    {
379
        Assert::false(
380
            $this->indexPage->isSingleResourceOnPage(['code' => $paymentMethod->getCode(), 'name' => $paymentMethod->getName()]),
381
            sprintf('Payment method %s should no longer exist in the registry', $paymentMethod->getName())
382
        );
383
    }
384
385
    /**
386
     * @Then I should be notified that payment method with this code already exists
387
     */
388
    public function iShouldBeNotifiedThatPaymentMethodWithThisCodeAlreadyExists()
389
    {
390
        Assert::same($this->createPage->getValidationMessage('code'), 'The payment method with given code already exists.');
391
    }
392
393
    /**
394
     * @Then there should still be only one payment method with :element :code
395
     */
396
    public function thereShouldStillBeOnlyOnePaymentMethodWith($element, $code)
397
    {
398
        $this->iBrowsePaymentMethods();
399
400
        Assert::true(
401
            $this->indexPage->isSingleResourceOnPage([$element => $code]),
402
            sprintf('Payment method with %s %s cannot be found.', $element, $code)
403
        );
404
    }
405
}
406