Completed
Push — locale-in-url ( f95194...93251b )
by Kamil
21:02
created

ManagingExchangeRatesContext::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
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\CreatePageInterface;
16
use Sylius\Behat\Page\Admin\ExchangeRate\IndexPageInterface;
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
     */
163
    public function iShouldSeeExchangeRatesOnTheList($count = 0)
164
    {
165
        $this->assertCountOfExchangeRatesOnTheList($count);
166
    }
167
168
    /**
169
     * @Then I should( still) see one exchange rate on the list
170
     */
171
    public function iShouldSeeOneExchangeRateOnTheList()
172
    {
173
        $this->indexPage->open();
174
175
        $this->assertCountOfExchangeRatesOnTheList(1);
176
    }
177
178
    /**
179
     * @Then the exchange rate with ratio :ratio between :sourceCurrency and :targetCurrency should appear in the store
180
     */
181
    public function theExchangeRateBetweenAndShouldAppearInTheStore($ratio, $sourceCurrency, $targetCurrency)
182
    {
183
        $this->indexPage->open();
184
185
        $this->assertExchangeRateWithRatioIsOnTheList($ratio, $sourceCurrency, $targetCurrency);
186
    }
187
188
    /**
189
     * @Then I should (also) see an exchange rate between :sourceCurrencyName and :targetCurrencyName on the list
190
     */
191
    public function iShouldSeeAnExchangeRateBetweenAndOnTheList($sourceCurrencyName, $targetCurrencyName)
192
    {
193
        Assert::true($this->indexPage->isSingleResourceOnPage([
194
            'sourceCurrency' => $sourceCurrencyName,
195
            'targetCurrency' => $targetCurrencyName,
196
        ]));
197
    }
198
199
    /**
200
     * @Then it should have a ratio of :ratio
201
     */
202
    public function thisExchangeRateShouldHaveRatioOf($ratio)
203
    {
204
        Assert::eq($this->updatePage->getRatio(), $ratio);
205
    }
206
207
    /**
208
     * @Then /^(this exchange rate) should no longer be on the list$/
209
     */
210
    public function thisExchangeRateShouldNoLongerBeOnTheList(ExchangeRateInterface $exchangeRate)
211
    {
212
        $this->assertExchangeRateIsNotOnTheList(
213
            $exchangeRate->getSourceCurrency()->getName(),
214
            $exchangeRate->getTargetCurrency()->getName()
215
        );
216
    }
217
218
    /**
219
     * @Then the exchange rate between :sourceCurrencyName and :targetCurrencyName should not be added
220
     */
221
    public function theExchangeRateBetweenAndShouldNotBeAdded($sourceCurrencyName, $targetCurrencyName)
222
    {
223
        $this->indexPage->open();
224
225
        $this->assertExchangeRateIsNotOnTheList($sourceCurrencyName, $targetCurrencyName);
226
    }
227
228
    /**
229
     * @Then /^(this exchange rate) should have a ratio of ([0-9\.]+)$/
230
     */
231
    public function thisExchangeRateShouldHaveARatioOf(ExchangeRateInterface $exchangeRate, $ratio)
232
    {
233
        $sourceCurrencyName = $exchangeRate->getSourceCurrency()->getName();
234
        $targetCurrencyName = $exchangeRate->getTargetCurrency()->getName();
235
236
        $this->assertExchangeRateWithRatioIsOnTheList($ratio, $sourceCurrencyName, $targetCurrencyName);
237
    }
238
239
    /**
240
     * @Then I should see that the source currency is disabled
241
     */
242
    public function iShouldSeeThatTheSourceCurrencyIsDisabled()
243
    {
244
        Assert::true($this->updatePage->isSourceCurrencyDisabled());
245
    }
246
247
    /**
248
     * @Then I should see that the target currency is disabled
249
     */
250
    public function iShouldSeeThatTheTargetCurrencyIsDisabled()
251
    {
252
        Assert::true($this->updatePage->isTargetCurrencyDisabled());
253
    }
254
255
    /**
256
     * @Then /^I should be notified that ([^"]+) is required$/
257
     */
258
    public function iShouldBeNotifiedThatIsRequired($element)
259
    {
260
        Assert::same(
261
            $this->createPage->getValidationMessage($element),
262
            sprintf('Please enter exchange rate %s.', $element)
263
        );
264
    }
265
266
    /**
267
     * @Then I should be notified that the ratio must be greater than zero
268
     */
269
    public function iShouldBeNotifiedThatRatioMustBeGreaterThanZero()
270
    {
271
        Assert::same($this->createPage->getValidationMessage('ratio'), 'The ratio must be greater than 0.');
272
    }
273
274
    /**
275
     * @Then I should be notified that source and target currencies must differ
276
     */
277
    public function iShouldBeNotifiedThatSourceAndTargetCurrenciesMustDiffer()
278
    {
279
        $this->assertFormHasValidationMessage('The source and target currencies must differ.');
280
    }
281
282
    /**
283
     * @Then I should be notified that the currency pair must be unique
284
     */
285
    public function iShouldBeNotifiedThatTheCurrencyPairMustBeUnique()
286
    {
287
        $this->assertFormHasValidationMessage('The currency pair must be unique.');
288
    }
289
290
    /**
291
     * @param float $ratio
292
     * @param string $sourceCurrencyName
293
     * @param string $targetCurrencyName
294
     *
295
     * @throws \InvalidArgumentException
296
     */
297
    private function assertExchangeRateWithRatioIsOnTheList($ratio, $sourceCurrencyName, $targetCurrencyName)
298
    {
299
        Assert::true(
300
            $this->indexPage->isSingleResourceOnPage([
301
                'ratio' => $ratio,
302
                'sourceCurrency' => $sourceCurrencyName,
303
                'targetCurrency' => $targetCurrencyName,
304
            ]),
305
            sprintf(
306
                'An exchange rate between %s and %s with a ratio of %s has not been found on the list.',
307
                $sourceCurrencyName,
308
                $targetCurrencyName,
309
                $ratio
310
            )
311
        );
312
    }
313
314
    /**
315
     * @param string $sourceCurrencyName
316
     * @param string $targetCurrencyName
317
     *
318
     * @throws \InvalidArgumentException
319
     */
320
    private function assertExchangeRateIsNotOnTheList($sourceCurrencyName, $targetCurrencyName)
321
    {
322
        Assert::false(
323
            $this->indexPage->isSingleResourceOnPage([
324
                'sourceCurrency' => $sourceCurrencyName,
325
                'targetCurrency' => $targetCurrencyName,
326
            ]),
327
            sprintf(
328
                'An exchange rate with source currency %s and target currency %s has been found on the list.',
329
                $sourceCurrencyName,
330
                $targetCurrencyName
331
            )
332
        );
333
    }
334
335
    /**
336
     * @param int $count
337
     *
338
     * @throws \InvalidArgumentException
339
     */
340
    private function assertCountOfExchangeRatesOnTheList($count)
341
    {
342
        Assert::same(
343
            $this->indexPage->countItems(),
344
            (int) $count,
345
            'Expected %2$d exchange rates to be on the list, but found %d instead.'
346
        );
347
    }
348
349
    /**
350
     * @param string $expectedMessage
351
     *
352
     * @throws \InvalidArgumentException
353
     */
354
    private function assertFormHasValidationMessage($expectedMessage)
355
    {
356
        Assert::true(
357
            $this->createPage->hasFormValidationError($expectedMessage),
358
            sprintf(
359
                'The validation message "%s" was not found on the page.',
360
                $expectedMessage
361
            )
362
        );
363
    }
364
}
365