Completed
Push — api ( cecea0...116a36 )
by Kamil
29:57 queued 30s
created

iShouldSeeThatTheSourceCurrencyIsDisabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\Page\Admin\ExchangeRate\CreatePageInterface;
18
use Sylius\Behat\Page\Admin\ExchangeRate\IndexPageInterface;
19
use Sylius\Behat\Page\Admin\ExchangeRate\UpdatePageInterface;
20
use Sylius\Component\Currency\Model\CurrencyInterface;
21
use Sylius\Component\Currency\Model\ExchangeRateInterface;
22
use Webmozart\Assert\Assert;
23
24
final class ManagingExchangeRatesContext implements Context
25
{
26
    /** @var CreatePageInterface */
27
    private $createPage;
28
29
    /** @var IndexPageInterface */
30
    private $indexPage;
31
32
    /** @var UpdatePageInterface */
33
    private $updatePage;
34
35
    public function __construct(
36
        CreatePageInterface $createPage,
37
        IndexPageInterface $indexPage,
38
        UpdatePageInterface $updatePage
39
    ) {
40
        $this->createPage = $createPage;
41
        $this->indexPage = $indexPage;
42
        $this->updatePage = $updatePage;
43
    }
44
45
    /**
46
     * @Given I want to add a new exchange rate
47
     */
48
    public function iWantToAddNewExchangeRate()
49
    {
50
        $this->createPage->open();
51
    }
52
53
    /**
54
     * @Given /^I want to edit (this exchange rate)$/
55
     * @When /^I am editing (this exchange rate)$/
56
     */
57
    public function iWantToEditThisExchangeRate(ExchangeRateInterface $exchangeRate)
58
    {
59
        $this->updatePage->open(['id' => $exchangeRate->getId()]);
60
    }
61
62
    /**
63
     * @Given I am browsing exchange rates of the store
64
     * @When I browse exchange rates
65
     * @When I browse exchange rates of the store
66
     */
67
    public function iWantToBrowseExchangeRatesOfTheStore()
68
    {
69
        $this->indexPage->open();
70
    }
71
72
    /**
73
     * @When /^I specify its ratio as (-?[0-9\.]+)$/
74
     * @When I don't specify its ratio
75
     */
76
    public function iSpecifyItsRatioAs($ratio = null)
77
    {
78
        $this->createPage->specifyRatio($ratio ?? '');
79
    }
80
81
    /**
82
     * @When I choose :currencyCode as the source currency
83
     */
84
    public function iChooseAsSourceCurrency($currencyCode)
85
    {
86
        $this->createPage->chooseSourceCurrency($currencyCode);
87
    }
88
89
    /**
90
     * @When I choose :currencyCode as the target currency
91
     */
92
    public function iChooseAsTargetCurrency($currencyCode)
93
    {
94
        $this->createPage->chooseTargetCurrency($currencyCode);
95
    }
96
97
    /**
98
     * @When I( try to) add it
99
     */
100
    public function iAddIt()
101
    {
102
        $this->createPage->create();
103
    }
104
105
    /**
106
     * @When I change ratio to :ratio
107
     */
108
    public function iChangeRatioTo($ratio)
109
    {
110
        $this->updatePage->changeRatio($ratio);
111
    }
112
113
    /**
114
     * @When I save my changes
115
     */
116
    public function iSaveMyChanges()
117
    {
118
        $this->updatePage->saveChanges();
119
    }
120
121
    /**
122
     * @When I delete the exchange rate between :sourceCurrencyName and :targetCurrencyName
123
     */
124
    public function iDeleteTheExchangeRateBetweenAnd($sourceCurrencyName, $targetCurrencyName)
125
    {
126
        $this->indexPage->open();
127
128
        $this->indexPage->deleteResourceOnPage([
129
            'sourceCurrency' => $sourceCurrencyName,
130
            'targetCurrency' => $targetCurrencyName,
131
        ]);
132
    }
133
134
    /**
135
     * @When I choose :currencyName as a currency filter
136
     */
137
    public function iChooseCurrencyAsACurrencyFilter($currencyName)
138
    {
139
        $this->indexPage->chooseCurrencyFilter($currencyName);
140
    }
141
142
    /**
143
     * @When I filter
144
     */
145
    public function iFilter()
146
    {
147
        $this->indexPage->filter();
148
    }
149
150
    /**
151
     * @When I check (also) the exchange rate between :sourceCurrencyName and :targetCurrencyName
152
     */
153
    public function iCheckTheExchangeRateBetweenAnd(string $sourceCurrencyName, string $targetCurrencyName): void
154
    {
155
        $this->indexPage->checkResourceOnPage([
156
            'sourceCurrency' => $sourceCurrencyName,
157
            'targetCurrency' => $targetCurrencyName,
158
        ]);
159
    }
160
161
    /**
162
     * @When I delete them
163
     */
164
    public function iDeleteThem(): void
165
    {
166
        $this->indexPage->bulkDelete();
167
    }
168
169
    /**
170
     * @Then I should see :count exchange rates on the list
171
     */
172
    public function iShouldSeeExchangeRatesOnTheList($count = 0)
173
    {
174
        $this->assertCountOfExchangeRatesOnTheList($count);
175
    }
176
177
    /**
178
     * @Then I should see a single exchange rate in the list
179
     * @Then I should( still) see one exchange rate on the list
180
     */
181
    public function iShouldSeeOneExchangeRateOnTheList()
182
    {
183
        $this->indexPage->open();
184
185
        $this->assertCountOfExchangeRatesOnTheList(1);
186
    }
187
188
    /**
189
     * @Then the exchange rate with ratio :ratio between :sourceCurrency and :targetCurrency should appear in the store
190
     */
191
    public function theExchangeRateBetweenAndShouldAppearInTheStore($ratio, CurrencyInterface $sourceCurrency, CurrencyInterface $targetCurrency)
192
    {
193
        $this->indexPage->open();
194
195
        $this->assertExchangeRateWithRatioIsOnTheList((float) $ratio, $sourceCurrency->getName(), $targetCurrency->getName());
196
    }
197
198
    /**
199
     * @Then I should see the exchange rate between :sourceCurrencyName and :targetCurrencyName in the list
200
     * @Then I should (also) see an exchange rate between :sourceCurrencyName and :targetCurrencyName on the list
201
     */
202
    public function iShouldSeeAnExchangeRateBetweenAndOnTheList(
203
        string $sourceCurrencyName,
204
        string $targetCurrencyName
205
    ): void {
206
        Assert::true($this->indexPage->isSingleResourceOnPage([
207
            'sourceCurrency' => $sourceCurrencyName,
208
            'targetCurrency' => $targetCurrencyName,
209
        ]));
210
    }
211
212
    /**
213
     * @Then it should have a ratio of :ratio
214
     */
215
    public function thisExchangeRateShouldHaveRatioOf($ratio)
216
    {
217
        Assert::eq($this->updatePage->getRatio(), $ratio);
218
    }
219
220
    /**
221
     * @Then /^(this exchange rate) should no longer be on the list$/
222
     */
223
    public function thisExchangeRateShouldNoLongerBeOnTheList(ExchangeRateInterface $exchangeRate)
224
    {
225
        $this->assertExchangeRateIsNotOnTheList(
226
            $exchangeRate->getSourceCurrency()->getName(),
227
            $exchangeRate->getTargetCurrency()->getName()
228
        );
229
    }
230
231
    /**
232
     * @Then the exchange rate between :sourceCurrencyName and :targetCurrencyName should not be added
233
     */
234
    public function theExchangeRateBetweenAndShouldNotBeAdded($sourceCurrencyName, $targetCurrencyName)
235
    {
236
        $this->indexPage->open();
237
238
        $this->assertExchangeRateIsNotOnTheList($sourceCurrencyName, $targetCurrencyName);
239
    }
240
241
    /**
242
     * @Then /^(this exchange rate) should have a ratio of ([0-9\.]+)$/
243
     */
244
    public function thisExchangeRateShouldHaveARatioOf(ExchangeRateInterface $exchangeRate, $ratio)
245
    {
246
        $sourceCurrencyName = $exchangeRate->getSourceCurrency()->getName();
247
        $targetCurrencyName = $exchangeRate->getTargetCurrency()->getName();
248
249
        $this->assertExchangeRateWithRatioIsOnTheList($ratio, $sourceCurrencyName, $targetCurrencyName);
250
    }
251
252
    /**
253
     * @Then I should not be able to edit its source currency
254
     */
255
    public function iShouldNotBeAbleToEditItsSourceCurrency(): void
256
    {
257
        Assert::true($this->updatePage->isSourceCurrencyDisabled());
258
    }
259
260
    /**
261
     * @Then I should not be able to edit its target currency
262
     */
263
    public function iShouldNotBeAbleToEditItsTargetCurrency(): void
264
    {
265
        Assert::true($this->updatePage->isTargetCurrencyDisabled());
266
    }
267
268
    /**
269
     * @Then /^I should be notified that ([^"]+) is required$/
270
     */
271
    public function iShouldBeNotifiedThatIsRequired($element)
272
    {
273
        Assert::same(
274
            $this->createPage->getValidationMessage($element),
275
            sprintf('Please enter exchange rate %s.', $element)
276
        );
277
    }
278
279
    /**
280
     * @Then I should be notified that the ratio must be greater than zero
281
     */
282
    public function iShouldBeNotifiedThatRatioMustBeGreaterThanZero()
283
    {
284
        Assert::same($this->createPage->getValidationMessage('ratio'), 'The ratio must be greater than 0.');
285
    }
286
287
    /**
288
     * @Then I should be notified that source and target currencies must differ
289
     */
290
    public function iShouldBeNotifiedThatSourceAndTargetCurrenciesMustDiffer()
291
    {
292
        $this->assertFormHasValidationMessage('The source and target currencies must differ.');
293
    }
294
295
    /**
296
     * @Then I should be notified that the currency pair must be unique
297
     */
298
    public function iShouldBeNotifiedThatTheCurrencyPairMustBeUnique()
299
    {
300
        $this->assertFormHasValidationMessage('The currency pair must be unique.');
301
    }
302
303
    /**
304
     * @param float $ratio
305
     * @param string $sourceCurrencyName
306
     * @param string $targetCurrencyName
307
     *
308
     * @throws \InvalidArgumentException
309
     */
310
    private function assertExchangeRateWithRatioIsOnTheList($ratio, $sourceCurrencyName, $targetCurrencyName)
311
    {
312
        Assert::true(
313
            $this->indexPage->isSingleResourceOnPage([
314
                'ratio' => (string) $ratio,
315
                'sourceCurrency' => $sourceCurrencyName,
316
                'targetCurrency' => $targetCurrencyName,
317
            ]),
318
            sprintf(
319
                'An exchange rate between %s and %s with a ratio of %s has not been found on the list.',
320
                $sourceCurrencyName,
321
                $targetCurrencyName,
322
                $ratio
323
            )
324
        );
325
    }
326
327
    /**
328
     * @param string $sourceCurrencyName
329
     * @param string $targetCurrencyName
330
     *
331
     * @throws \InvalidArgumentException
332
     */
333
    private function assertExchangeRateIsNotOnTheList($sourceCurrencyName, $targetCurrencyName)
334
    {
335
        Assert::false(
336
            $this->indexPage->isSingleResourceOnPage([
337
                'sourceCurrency' => $sourceCurrencyName,
338
                'targetCurrency' => $targetCurrencyName,
339
            ]),
340
            sprintf(
341
                'An exchange rate with source currency %s and target currency %s has been found on the list.',
342
                $sourceCurrencyName,
343
                $targetCurrencyName
344
            )
345
        );
346
    }
347
348
    /**
349
     * @param int $count
350
     *
351
     * @throws \InvalidArgumentException
352
     */
353
    private function assertCountOfExchangeRatesOnTheList($count)
354
    {
355
        Assert::same(
356
            $this->indexPage->countItems(),
357
            (int) $count,
358
            'Expected %2$d exchange rates to be on the list, but found %d instead.'
359
        );
360
    }
361
362
    /**
363
     * @param string $expectedMessage
364
     *
365
     * @throws \InvalidArgumentException
366
     */
367
    private function assertFormHasValidationMessage($expectedMessage)
368
    {
369
        Assert::true(
370
            $this->createPage->hasFormValidationError($expectedMessage),
371
            sprintf(
372
                'The validation message "%s" was not found on the page.',
373
                $expectedMessage
374
            )
375
        );
376
    }
377
}
378