Completed
Push — master ( 65e4ab...130c38 )
by Kamil
13s
created

iShouldBeNotifiedThatRatioMustBeGreaterThanZero()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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