|
9 | 9 |
|
10 | 10 | use Codeception\Test\Unit; |
11 | 11 | use craft\commerce\errors\CurrencyException; |
| 12 | +use craft\commerce\events\PaymentCurrencyRateEvent; |
12 | 13 | use craft\commerce\Plugin; |
| 14 | +use craft\commerce\records\PaymentCurrency as PaymentCurrencyRecord; |
13 | 15 | use craft\commerce\services\PaymentCurrencies; |
14 | 16 | use craftcommercetests\fixtures\PaymentCurrenciesFixture; |
| 17 | +use Money\Currency; |
| 18 | +use Money\Money; |
15 | 19 | use UnitTester; |
| 20 | +use yii\base\Event; |
16 | 21 | use yii\base\InvalidConfigException; |
17 | 22 |
|
18 | 23 | /** |
@@ -154,4 +159,110 @@ public function testConvertException(): void |
154 | 159 | $this->expectException(CurrencyException::class); |
155 | 160 | $this->pc->convert(20, 'aaa'); |
156 | 161 | } |
| 162 | + |
| 163 | + /** |
| 164 | + * @group PaymentCurrencies |
| 165 | + */ |
| 166 | + public function testGetRateForReturnsRawRateWithoutHandler(): void |
| 167 | + { |
| 168 | + $eur = $this->pc->getPaymentCurrencyByIso('EUR'); |
| 169 | + self::assertSame(0.5, $this->pc->getRateFor($eur)); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * @group PaymentCurrencies |
| 174 | + */ |
| 175 | + public function testGetRateForReturnsEventRate(): void |
| 176 | + { |
| 177 | + $handler = static function(PaymentCurrencyRateEvent $event) { |
| 178 | + if ($event->paymentCurrency->iso === 'EUR') { |
| 179 | + $event->rate = 0.25; |
| 180 | + } |
| 181 | + }; |
| 182 | + Event::on(PaymentCurrencies::class, PaymentCurrencies::EVENT_DEFINE_PAYMENT_CURRENCY_RATE, $handler); |
| 183 | + |
| 184 | + try { |
| 185 | + $eur = $this->pc->getPaymentCurrencyByIso('EUR'); |
| 186 | + self::assertSame(0.25, $this->pc->getRateFor($eur)); |
| 187 | + |
| 188 | + $aud = $this->pc->getPaymentCurrencyByIso('AUD'); |
| 189 | + self::assertSame(1.3, $this->pc->getRateFor($aud), 'Untouched currencies fall through to the raw rate.'); |
| 190 | + } finally { |
| 191 | + Event::off(PaymentCurrencies::class, PaymentCurrencies::EVENT_DEFINE_PAYMENT_CURRENCY_RATE, $handler); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * @group PaymentCurrencies |
| 197 | + */ |
| 198 | + public function testConvertCurrencyUsesEventRate(): void |
| 199 | + { |
| 200 | + $handler = static function(PaymentCurrencyRateEvent $event) { |
| 201 | + if ($event->paymentCurrency->iso === 'EUR') { |
| 202 | + $event->rate = 0.25; |
| 203 | + } |
| 204 | + }; |
| 205 | + Event::on(PaymentCurrencies::class, PaymentCurrencies::EVENT_DEFINE_PAYMENT_CURRENCY_RATE, $handler); |
| 206 | + |
| 207 | + try { |
| 208 | + $converted = $this->pc->convertCurrency(40, $this->pc->getPrimaryPaymentCurrencyIso(), 'EUR'); |
| 209 | + self::assertSame(10.0, $converted); |
| 210 | + } finally { |
| 211 | + Event::off(PaymentCurrencies::class, PaymentCurrencies::EVENT_DEFINE_PAYMENT_CURRENCY_RATE, $handler); |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + /** |
| 216 | + * @group PaymentCurrencies |
| 217 | + */ |
| 218 | + public function testConvertAmountUsesEventRate(): void |
| 219 | + { |
| 220 | + $handler = static function(PaymentCurrencyRateEvent $event) { |
| 221 | + if ($event->paymentCurrency->iso === 'EUR') { |
| 222 | + $event->rate = 0.25; |
| 223 | + } |
| 224 | + }; |
| 225 | + Event::on(PaymentCurrencies::class, PaymentCurrencies::EVENT_DEFINE_PAYMENT_CURRENCY_RATE, $handler); |
| 226 | + |
| 227 | + try { |
| 228 | + $usd = new Money(4000, new Currency('USD')); |
| 229 | + $converted = $this->pc->convertAmount($usd, 'EUR'); |
| 230 | + self::assertSame('EUR', $converted->getCurrency()->getCode()); |
| 231 | + self::assertSame('1000', $converted->getAmount()); |
| 232 | + } finally { |
| 233 | + Event::off(PaymentCurrencies::class, PaymentCurrencies::EVENT_DEFINE_PAYMENT_CURRENCY_RATE, $handler); |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + /** |
| 238 | + * The event must not affect the rate that gets persisted when saving a |
| 239 | + * payment currency — saving isn't a conversion. |
| 240 | + * |
| 241 | + * @group PaymentCurrencies |
| 242 | + */ |
| 243 | + public function testSavePaymentCurrencyIgnoresEventRate(): void |
| 244 | + { |
| 245 | + $handler = static function(PaymentCurrencyRateEvent $event) { |
| 246 | + $event->rate = 999.0; |
| 247 | + }; |
| 248 | + Event::on(PaymentCurrencies::class, PaymentCurrencies::EVENT_DEFINE_PAYMENT_CURRENCY_RATE, $handler); |
| 249 | + |
| 250 | + try { |
| 251 | + $eur = $this->pc->getPaymentCurrencyByIso('EUR'); |
| 252 | + $originalRate = $eur->rate; |
| 253 | + $eur->rate = 0.75; |
| 254 | + |
| 255 | + self::assertTrue($this->pc->savePaymentCurrency($eur)); |
| 256 | + |
| 257 | + $record = PaymentCurrencyRecord::findOne(['id' => $eur->id]); |
| 258 | + self::assertNotNull($record); |
| 259 | + self::assertEquals(0.75, $record->rate, 'Raw admin-entered rate is persisted, not the event rate.'); |
| 260 | + |
| 261 | + // Restore for any tests that run after this one without isolation. |
| 262 | + $eur->rate = $originalRate; |
| 263 | + $this->pc->savePaymentCurrency($eur); |
| 264 | + } finally { |
| 265 | + Event::off(PaymentCurrencies::class, PaymentCurrencies::EVENT_DEFINE_PAYMENT_CURRENCY_RATE, $handler); |
| 266 | + } |
| 267 | + } |
157 | 268 | } |
0 commit comments