View source
<?php
namespace Drupal\Tests\commerce_exchanger\Kernel;
use Drupal\commerce_exchanger\Entity\ExchangeRates;
use Drupal\commerce_exchanger\Exception\ExchangeRatesDataMismatchException;
use Drupal\commerce_price\Price;
use Drupal\Tests\commerce\Kernel\CommerceKernelTestBase;
class CommerceExchangerCalculatorTest extends CommerceKernelTestBase {
protected $priceHrk;
protected $priceUsd;
protected $exchanger;
protected $exchangerId;
public static $modules = [
'commerce_exchanger',
];
protected function setUp() {
parent::setUp();
$currency_importer = $this->container
->get('commerce_price.currency_importer');
$currency_importer
->import('HRK');
$this->priceHrk = new Price('100', 'HRK');
$this->priceUsd = new Price('100', 'USD');
$exchanger = ExchangeRates::create([
'label' => 'ECB Rate',
'id' => 'ecb_rates',
'plugin' => 'ecb',
'status' => TRUE,
]);
$exchanger
->save();
$this->exchanger = $exchanger;
$this->exchangerId = $exchanger
->getExchangerConfigName();
$this
->config($this->exchangerId)
->setData([
'rates' => [
'HRK' => [
'USD' => [
'value' => 0.15,
'sync' => 0,
],
],
'USD' => [
'HRK' => [
'value' => 6.85,
'sync' => 0,
],
],
],
])
->save();
}
public function testExchangerId() {
$this
->assertEqual($this->exchangerId, $this->container
->get('commerce_exchanger.calculate')
->getExchangerId());
}
public function testExchangerIdEmpty() {
$this->exchanger
->setStatus(FALSE);
$this->exchanger
->save();
$this
->assertEmpty($this->container
->get('commerce_exchanger.calculate')
->getExchangerId());
$this
->assertEmpty($this->container
->get('commerce_exchanger.calculate')
->getExchangeRates());
$this
->expectException(ExchangeRatesDataMismatchException::class);
$this->container
->get('commerce_exchanger.calculate')
->priceConversion($this->priceUsd, 'HRK');
$this
->expectException(ExchangeRatesDataMismatchException::class);
$this->container
->get('commerce_exchanger.calculate')
->priceConversion($this->priceHrk, 'USD');
}
public function testExchangeRates() {
$this
->assertArrayHasKey('HRK', $this->container
->get('commerce_exchanger.calculate')
->getExchangeRates());
$this
->assertArrayHasKey('USD', $this->container
->get('commerce_exchanger.calculate')
->getExchangeRates());
}
public function testExchangeRatesEmpty() {
$this->exchanger
->setStatus(FALSE);
$this->exchanger
->save();
$this
->assertEmpty($this->container
->get('commerce_exchanger.calculate')
->getExchangeRates());
}
public function testPriceConversion() {
$priceHrk = $this->container
->get('commerce_exchanger.calculate')
->priceConversion($this->priceHrk, 'USD');
$this
->assertEqual(100 * 0.15, $priceHrk
->getNumber());
$priceUsd = $this->container
->get('commerce_exchanger.calculate')
->priceConversion($this->priceUsd, 'HRK');
$this
->assertEqual(100 * 6.85, $priceUsd
->getNumber());
$price_equal = $this->container
->get('commerce_exchanger.calculate')
->priceConversion($this->priceUsd, 'USD');
$this
->assertEqual(100.0, $price_equal
->getNumber());
$this
->config($this->exchangerId)
->setData([
'rates' => [
'HRK' => [
'USD' => [
'value' => 0,
'sync' => 0,
],
],
'USD' => [
'HRK' => [
'value' => '0',
'sync' => 0,
],
],
],
])
->save();
$this
->expectException(ExchangeRatesDataMismatchException::class);
$this->container
->get('commerce_exchanger.calculate')
->priceConversion($this->priceUsd, 'HRK');
$this
->expectException(ExchangeRatesDataMismatchException::class);
$this->container
->get('commerce_exchanger.calculate')
->priceConversion($this->priceHrk, 'USD');
}
public function testPriceConversionEmpty() {
$this->exchanger
->setStatus(FALSE);
$this->exchanger
->save();
$this
->expectException(ExchangeRatesDataMismatchException::class);
$this->container
->get('commerce_exchanger.calculate')
->priceConversion($this->priceHrk, 'USD');
}
}