Completed
Push — master ( f906b5...0ffad1 )
by Kamil
17s
created

ManagingCurrenciesContext::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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