Issues (3877)

Zed/Currency/Business/CurrencyFacadeTest.php (1 issue)

1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerTest\Zed\Currency\Business;
9
10
use Codeception\Test\Unit;
11
use Generated\Shared\DataBuilder\CurrencyBuilder;
12
use Generated\Shared\Transfer\CurrencyTransfer;
13
use Generated\Shared\Transfer\QuoteTransfer;
14
use Generated\Shared\Transfer\QuoteValidationResponseTransfer;
15
use Generated\Shared\Transfer\StoreTransfer;
16
use Spryker\Zed\Currency\Business\CurrencyFacade;
17
use Spryker\Zed\Currency\Business\CurrencyFacadeInterface;
18
19
/**
20
 * Auto-generated group annotations
21
 *
22
 * @group SprykerTest
23
 * @group Zed
24
 * @group Currency
25
 * @group Business
26
 * @group Facade
27
 * @group CurrencyFacadeTest
28
 * Add your own group annotations below this line
29
 */
30
class CurrencyFacadeTest extends Unit
31
{
32
    /**
33
     * @var string
34
     */
35
    protected const ERROR_MESSAGE_CURRENCY_DATA_IS_MISSING = 'quote.validation.error.currency_is_missing';
36
37
    /**
38
     * @var string
39
     */
40
    protected const ERROR_MESSAGE_CURRENCY_DATA_IS_INCORRECT = 'quote.validation.error.currency_is_incorrect';
41
42
    /**
43
     * @var string
44
     */
45
    protected const WRONG_ISO_CODE = 'WRONGCODE';
46
47
    /**
48
     * @var string
49
     */
50
    protected const STORE_NAME_DE = 'DE';
51
52
    /**
53
     * @var int
54
     */
55
    protected const STORE_ID_DE = 1;
56
57
    /**
58
     * @var string
59
     */
60
    protected const STORE_NAME_US = 'US';
61
62
    /**
63
     * @var string
64
     */
65
    protected const EUR_ISO_CODE = 'EUR';
66
67
    /**
68
     * @var string
69
     */
70
    protected const USD_ISO_CODE = 'USD';
71
72
    /**
73
     * @var string
74
     */
75
    protected const GBP_ISO_CODE = 'GBP';
76
77
    /**
78
     * @var \SprykerTest\Zed\Currency\CurrencyBusinessTester
79
     */
80
    protected $tester;
81
82
    /**
83
     * @return void
84
     */
85
    public function testGetByIdCurrencyShouldReturnCurrencyTransfer(): void
86
    {
87
        $idCurrency = $this->tester->haveCurrency();
88
        $currencyTransfer = $this->tester->getFacade()->getByIdCurrency($idCurrency);
89
90
        $this->assertNotNull($currencyTransfer);
91
    }
92
93
    /**
94
     * @return void
95
     */
96
    public function testCreateCurrencyShouldPersistGivenData(): void
97
    {
98
        $currencyTransfer = (new CurrencyBuilder())->build();
99
100
        $idCurrency = $this->tester->getFacade()->createCurrency($currencyTransfer);
101
102
        $this->assertNotNull($idCurrency);
103
    }
104
105
    /**
106
     * @return void
107
     */
108
    public function testGetByIdCurrencyShouldReturnCurrencyFromPersistence(): void
109
    {
110
        $currencyTransfer = $this->tester->getFacade()->getByIdCurrency(1);
111
112
        $this->assertInstanceOf(CurrencyTransfer::class, $currencyTransfer);
113
    }
114
115
    /**
116
     * @return void
117
     */
118
    public function testValidateCurrencyInQuoteWithEmptyCurrency(): void
119
    {
120
        $quoteTransfer = new QuoteTransfer();
121
        $quoteValidationResponseTransfer = $this->getQuoteValidationResponseTransfer($quoteTransfer);
122
123
        $errors = array_map(function ($quoteErrorTransfer) {
124
            return $quoteErrorTransfer->getMessage();
125
        }, (array)$quoteValidationResponseTransfer->getErrors());
126
127
        //Act
128
        $this->assertFalse($quoteValidationResponseTransfer->getIsSuccessful());
129
        $this->assertContains(static::ERROR_MESSAGE_CURRENCY_DATA_IS_MISSING, $errors);
130
    }
131
132
    /**
133
     * @return void
134
     */
135
    public function testValidateCurrencyInQuoteWithEmptyCurrencyIsoCode(): void
136
    {
137
        $currencyTransfer = new CurrencyTransfer();
138
        $quoteTransfer = (new QuoteTransfer())
139
            ->setCurrency($currencyTransfer);
140
        $quoteValidationResponseTransfer = $this->getQuoteValidationResponseTransfer($quoteTransfer);
141
142
        $errors = array_map(function ($quoteErrorTransfer) {
143
            return $quoteErrorTransfer->getMessage();
144
        }, (array)$quoteValidationResponseTransfer->getErrors());
145
146
        //Act
147
        $this->assertFalse($quoteValidationResponseTransfer->getIsSuccessful());
148
        $this->assertContains(static::ERROR_MESSAGE_CURRENCY_DATA_IS_MISSING, $errors);
149
    }
150
151
    /**
152
     * @return void
153
     */
154
    public function testValidateCurrencyInQuoteWithWrongCurrencyIsoCode(): void
155
    {
156
        // Arrange
157
        $storeTransfer = $this->tester->haveStore([StoreTransfer::NAME => static::STORE_NAME_DE]);
158
159
        $currencyTransfer = (new CurrencyTransfer())
160
            ->setCode(static::WRONG_ISO_CODE);
161
        $quoteTransfer = (new QuoteTransfer())
162
            ->setCurrency($currencyTransfer)
163
            ->setStore($storeTransfer);
164
165
        // Act
166
        $quoteValidationResponseTransfer = $this->getQuoteValidationResponseTransfer($quoteTransfer);
167
168
        // Assert
169
        $errors = array_map(function ($quoteErrorTransfer) {
170
            return $quoteErrorTransfer->getMessage();
171
        }, (array)$quoteValidationResponseTransfer->getErrors());
172
173
        $this->assertFalse($quoteValidationResponseTransfer->getIsSuccessful());
174
        $this->assertContains(static::ERROR_MESSAGE_CURRENCY_DATA_IS_INCORRECT, $errors);
175
    }
176
177
    /**
178
     * @return void
179
     */
180
    public function testValidateCurrencyInQuoteWithCorrectIsoCode(): void
181
    {
182
        // Arrange
183
        $storeTransfer = $this->tester->haveStore([StoreTransfer::NAME => static::STORE_NAME_DE]);
184
        $storeTransfer->addAvailableCurrencyIsoCode(static::EUR_ISO_CODE);
185
186
        $currencyTransfer = (new CurrencyTransfer())
187
            ->setCode(static::EUR_ISO_CODE);
188
        $storeTransfer = (new StoreTransfer())
189
            ->setName(static::STORE_NAME_DE);
190
        $quoteTransfer = (new QuoteTransfer())
191
            ->setCurrency($currencyTransfer)
192
            ->setStore($storeTransfer);
193
194
        // Act
195
        $quoteValidationResponseTransfer = $this->getQuoteValidationResponseTransfer($quoteTransfer);
196
197
        // Assert
198
        $this->assertTrue($quoteValidationResponseTransfer->getIsSuccessful());
199
        $this->assertEmpty($quoteValidationResponseTransfer->getErrors());
200
    }
201
202
    /**
203
     * @return void
204
     */
205
    public function testUpdateStoreCountriesWithAddingNewAndRemovingOldRelations(): void
206
    {
207
        // Arrange
208
        $storeTransfer = $this->tester->haveStore([
209
            StoreTransfer::NAME => static::STORE_NAME_DE,
210
        ]);
211
        $this->tester->deleteCurrencyStore($storeTransfer->getIdStoreOrFail());
212
213
        $idCurrencyEur = $this->tester->haveCurrency([CurrencyTransfer::CODE => static::EUR_ISO_CODE]);
214
        $idCurrencyGbp = $this->tester->haveCurrency([CurrencyTransfer::CODE => static::GBP_ISO_CODE]);
215
        $idCurrencyUsd = $this->tester->haveCurrency([CurrencyTransfer::CODE => static::USD_ISO_CODE]);
216
217
        $this->tester->haveCurrencyStore($storeTransfer->getIdStoreOrFail(), $idCurrencyEur);
218
        $this->tester->haveCurrencyStore($storeTransfer->getIdStoreOrFail(), $idCurrencyGbp);
219
220
        $storeTransfer->setDefaultCurrencyIsoCode(static::EUR_ISO_CODE);
221
        $storeTransfer->setAvailableCurrencyIsoCodes([static::EUR_ISO_CODE, static::USD_ISO_CODE]);
222
223
        // Act
224
        $storeResponseTransfer = $this->createCurrencyFacade()->updateStoreCurrencies($storeTransfer);
225
226
        // Assert
227
        $this->assertTrue($storeResponseTransfer->getIsSuccessful());
228
        $this->assertTrue($this->tester->currencyStoreExists($storeTransfer->getIdStoreOrFail(), $idCurrencyEur));
229
        $this->assertTrue($this->tester->currencyStoreExists($storeTransfer->getIdStoreOrFail(), $idCurrencyUsd));
230
        $this->assertFalse($this->tester->currencyStoreExists($storeTransfer->getIdStoreOrFail(), $idCurrencyGbp));
231
    }
232
233
    /**
234
     * @return void
235
     */
236
    public function testExpandStoreTransfersWithCurrenciesSuccessful(): void
237
    {
238
        // Arrange
239
        $storeTransferEu = $this->tester->haveStore([StoreTransfer::NAME => static::STORE_NAME_DE]);
240
241
        $this->tester->deleteCurrencyStore($storeTransferEu->getIdStore());
242
243
        $idCurrencyEur = $this->tester->haveCurrency([CurrencyTransfer::CODE => static::EUR_ISO_CODE]);
244
        $idCurrencyUsd = $this->tester->haveCurrency([CurrencyTransfer::CODE => static::USD_ISO_CODE]);
0 ignored issues
show
The assignment to $idCurrencyUsd is dead and can be removed.
Loading history...
245
246
        $this->tester->haveCurrencyStore($storeTransferEu->getIdStoreOrFail(), $idCurrencyEur);
247
248
        // Act
249
        $storeTransfers = $this->createCurrencyFacade()->expandStoreTransfersWithCurrencies([
250
            $storeTransferEu->getIdStoreOrFail() => $storeTransferEu,
251
        ]);
252
253
        // Assert
254
        $this->assertSame(
255
            [static::EUR_ISO_CODE],
256
            array_values($storeTransfers[$storeTransferEu->getIdStoreOrFail()]->getAvailableCurrencyIsoCodes()),
257
        );
258
    }
259
260
    /**
261
     * @return void
262
     */
263
    public function testExpandStoreTransfersWithCurrenciesSuccessfulWithoutCurrencyStoreRelations(): void
264
    {
265
        // Arrange
266
        $storeTransferEu = $this->tester->haveStore([StoreTransfer::NAME => static::STORE_NAME_DE]);
267
268
        $this->tester->deleteCurrencyStore($storeTransferEu->getIdStoreOrFail());
269
270
        // Act
271
        $storeTransfers = $this->createCurrencyFacade()->expandStoreTransfersWithCurrencies([
272
            $storeTransferEu->getIdStoreOrFail() => $storeTransferEu,
273
        ]);
274
275
        // Assert
276
        $this->assertSame(
277
            [],
278
            array_values($storeTransfers[$storeTransferEu->getIdStoreOrFail()]->getAvailableCurrencyIsoCodes()),
279
        );
280
    }
281
282
    /**
283
     * @return \Spryker\Zed\Currency\Business\CurrencyFacadeInterface
284
     */
285
    protected function createCurrencyFacade(): CurrencyFacadeInterface
286
    {
287
        return new CurrencyFacade();
288
    }
289
290
    /**
291
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
292
     *
293
     * @return \Generated\Shared\Transfer\QuoteValidationResponseTransfer
294
     */
295
    protected function getQuoteValidationResponseTransfer(QuoteTransfer $quoteTransfer): QuoteValidationResponseTransfer
296
    {
297
        return $this->tester->getFacade()->validateCurrencyInQuote($quoteTransfer);
298
    }
299
}
300