Completed
Push — symfony3-fqcn ( 04e8c2...ea8d6b )
by Kamil
18:56
created

thisExchangeRateShouldNoLongerBeOnTheList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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