Completed
Push — remove-content-bundle ( 201341 )
by Kamil
34:43
created

iCannotChangeItsExchangeRate()   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 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\Currency\CreatePageInterface;
16
use Sylius\Behat\Page\Admin\Currency\IndexPageInterface;
17
use Sylius\Behat\Page\Admin\Currency\UpdatePageInterface;
18
use Sylius\Behat\Service\Resolver\CurrentPageResolverInterface;
19
use Sylius\Component\Currency\Model\CurrencyInterface;
20
use Webmozart\Assert\Assert;
21
22
/**
23
 * @author Anna Walasek <[email protected]>
24
 */
25
final class ManagingCurrenciesContext implements Context
26
{
27
    /**
28
     * @var IndexPageInterface
29
     */
30
    private $indexPage;
31
32
    /**
33
     * @var CreatePageInterface
34
     */
35
    private $createPage;
36
37
    /**
38
     * @var UpdatePageInterface
39
     */
40
    private $updatePage;
41
42
    /**
43
     * @var CurrentPageResolverInterface
44
     */
45
    private $currentPageResolver;
46
47
    /**
48
     * @param IndexPageInterface $indexPage
49
     * @param CreatePageInterface $createPage
50
     * @param UpdatePageInterface $updatePage
51
     * @param CurrentPageResolverInterface $currentPageResolver
52
     */
53
    public function __construct(
54
        IndexPageInterface $indexPage,
55
        CreatePageInterface $createPage,
56
        UpdatePageInterface $updatePage,
57
        CurrentPageResolverInterface $currentPageResolver
58
    ) {
59
        $this->createPage = $createPage;
60
        $this->indexPage = $indexPage;
61
        $this->updatePage = $updatePage;
62
        $this->currentPageResolver = $currentPageResolver;
63
    }
64
65
    /**
66
     * @Given I want to add a new currency
67
     */
68
    public function iWantToAddNewCurrency()
69
    {
70
        $this->createPage->open();
71
    }
72
73
    /**
74
     * @When I choose :currencyName
75
     */
76
    public function iChoose($currencyName)
77
    {
78
        $this->createPage->chooseName($currencyName);
79
    }
80
81
    /**
82
     * @When I add it
83
     * @When I try to add it
84
     */
85
    public function iAddIt()
86
    {
87
        $this->createPage->create();
88
    }
89
90
    /**
91
     * @When I specify its exchange rate as :exchangeRate
92
     */
93
    public function iSpecifyExchangeRate($exchangeRate)
94
    {
95
        $this->createPage->specifyExchangeRate($exchangeRate);
96
    }
97
98
    /**
99
     * @Then the currency :currency should appear in the store
100
     * @Then I should see the currency :currency in the list
101
     */
102
    public function currencyShouldAppearInTheStore(CurrencyInterface $currency)
103
    {
104
        $this->indexPage->open();
105
106
        Assert::true(
107
            $this->indexPage->isSingleResourceOnPage(['code' => $currency->getCode()]),
108
            sprintf('Currency %s should exist but it does not.', $currency->getCode())
109
        );
110
    }
111
112
    /**
113
     * @Given /^I want to edit (this currency)$/
114
     */
115
    public function iWantToEditThisCurrency(CurrencyInterface $currency)
116
    {
117
        $this->updatePage->open(['id' => $currency->getId()]);
118
    }
119
120
    /**
121
     * @When I enable it
122
     */
123
    public function iEnableIt()
124
    {
125
        $this->updatePage->enable();
126
    }
127
128
    /**
129
     * @When I disable it
130
     */
131
    public function iDisableIt()
132
    {
133
        $this->updatePage->disable();
134
    }
135
136
    /**
137
     * @Then I should not be able to disable this currency
138
     */
139
    public function iCannotDisableIt()
140
    {
141
        Assert::true(
142
            $this->updatePage->canBeDisabled(),
143
            'I should not be able to disable this currency.'
144
        );
145
    }
146
147
    /**
148
     * @Then I should not be able to change exchange rate of this currency
149
     */
150
    public function iCannotChangeItsExchangeRate()
151
    {
152
        Assert::true(
153
            $this->updatePage->canHaveExchangeRateChanged(),
154
            'I should not be able to change exchange rate of this currency.'
155
        );
156
    }
157
158
    /**
159
     * @When I save my changes
160
     * @When I try to save my changes
161
     */
162
    public function iSaveMyChanges()
163
    {
164
        $this->updatePage->saveChanges();
165
    }
166
167
    /**
168
     * @Then /^(this currency) should be disabled$/
169
     */
170
    public function thisCurrencyShouldBeDisabled(CurrencyInterface $currency)
171
    {
172
        $this->indexPage->open();
173
174
        Assert::true(
175
            $this->indexPage->isCurrencyDisabled($currency),
176
            sprintf('Currency %s should be disabled but it is not.', $currency->getCode())
177
        );
178
    }
179
180
    /**
181
     * @Then /^(this currency) should be enabled$/
182
     */
183
    public function thisCurrencyShouldBeEnabled(CurrencyInterface $currency)
184
    {
185
        $this->indexPage->open();
186
187
        Assert::true(
188
            $this->indexPage->isCurrencyEnabled($currency),
189
            sprintf('Currency %s should be enabled but it is not.', $currency->getCode())
190
        );
191
    }
192
193
    /**
194
     * @When I change exchange rate to :exchangeRate
195
     */
196
    public function iChangeExchangeRateTo($exchangeRate)
197
    {
198
       $this->updatePage->changeExchangeRate($exchangeRate);
199
    }
200
201
    /**
202
     * @Then this currency should have exchange rate :exchangeRate
203
     */
204
    public function thisCurrencyShouldHaveExchangeRate($exchangeRate)
205
    {
206
        Assert::eq(
207
            $exchangeRate,
208
            $this->updatePage->getExchangeRateValue(),
209
            sprintf(
210
                'Currency exchange rate should be equal %s, but was %s.',
211
                $exchangeRate,
212
                $this->updatePage->getExchangeRateValue()
213
            )
214
        );
215
    }
216
217
    /**
218
     * @Then the code field should be disabled
219
     */
220
    public function theCodeFiledShouldBeDisabled()
221
    {
222
        Assert::eq(
223
            'disabled',
224
            $this->updatePage->getCodeDisabledAttribute(),
225
            'Code field should be disabled but is not.'
226
        );
227
    }
228
229
    /**
230
     * @Then I should be notified that currency code must be unique
231
     */
232
    public function iShouldBeNotifiedThatCurrencyCodeMustBeUnique()
233
    {
234
        Assert::same($this->createPage->getValidationMessage('code'), 'Currency code must be unique.');
235
    }
236
237
    /**
238
     * @Then there should still be only one currency with :element :code
239
     */
240
    public function thereShouldStillBeOnlyOneCurrencyWithCode($element, $codeValue)
241
    {
242
        $this->indexPage->open();
243
244
        Assert::true(
245
            $this->indexPage->isSingleResourceOnPage([$element => $codeValue]),
246
            sprintf('Currency with %s %s cannot be found.', $element, $codeValue)
247
        );
248
    }
249
250
    /**
251
     * @Then I should be notified that exchange rate is required
252
     */
253
    public function iShouldBeNotifiedThatExchangeRateIsRequired()
254
    {
255
        /** @var CreatePageInterface|UpdatePageInterface $currentPage */
256
        $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
257
258
        Assert::same($currentPage->getValidationMessage('exchangeRate'), 'Please enter exchange rate.');
259
    }
260
261
    /**
262
     * @Then the currency :currencyName should not be added
263
     */
264
    public function theCurrencyShouldNotBeAdded($currencyName)
265
    {
266
        $this->indexPage->open();
267
268
        Assert::false(
269
            $this->indexPage->isSingleResourceOnPage(['name' => $currencyName]),
270
            sprintf('Currency with name %s was created, but it should not.', $currencyName)
271
        );
272
    }
273
274
    /**
275
     * @When I remove its exchange rate
276
     */
277
    public function iRemoveItsExchangeRate()
278
    {
279
        $this->updatePage->changeExchangeRate('');
280
    }
281
282
    /**
283
     * @Then /^(this currency) should still have exchange rate equal to ([0-9\.]+)$/
284
     */
285
    public function theCurrencyShouldStillHaveExchangeRateEquals(CurrencyInterface $currency, $exchangeRate)
286
    {
287
        $this->updatePage->open(['id' => $currency->getId()]);
288
289
        Assert::eq(
290
            $exchangeRate,
291
            $this->updatePage->getExchangeRateValue(),
292
            sprintf(
293
                'Currency exchange rate should be equal %s, but was %s.',
294
                $exchangeRate,
295
                $this->updatePage->getExchangeRateValue()
296
            )
297
        );
298
    }
299
300
    /**
301
     * @Given I want to browse currencies of the store
302
     */
303
    public function iWantToSeeAllCurrenciesInStore()
304
    {
305
        $this->indexPage->open();
306
    }
307
308
    /**
309
     * @Then /^I should see (\d+) currencies in the list$/
310
     */
311
    public function iShouldSeeCurrenciesInTheList($amountOfCurrencies)
312
    {
313
        Assert::same(
314
            (int) $amountOfCurrencies,
315
            $this->indexPage->countItems(),
316
            sprintf(
317
                'Amount of currencies should be equal %d, but was %d.',
318
                $amountOfCurrencies,
319
                $this->indexPage->countItems()
320
            )
321
        );
322
    }
323
}
324