View source
<?php
namespace Drupal\Tests\currency\Unit\Plugin\Currency\ExchangeRateProvider;
use Drupal\Core\Config\Config;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\currency\Plugin\Currency\ExchangeRateProvider\FixedRates;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FixedRatesTest extends UnitTestCase {
protected $config;
protected $configFactory;
protected $sut;
public function setUp() : void {
$configuration = array();
$plugin_id = $this
->randomMachineName();
$plugin_definition = array();
$this->configFactory = $this
->createMock(ConfigFactoryInterface::class);
$this->sut = new FixedRates($configuration, $plugin_id, $plugin_definition, $this->configFactory);
}
function testCreate() {
$container = $this
->createMock(ContainerInterface::class);
$map = array(
array(
'config.factory',
ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
$this->configFactory,
),
);
$container
->expects($this
->any())
->method('get')
->willReturnMap($map);
$sut = FixedRates::create($container, array(), '', array());
$this
->assertInstanceOf(FixedRates::class, $sut);
}
public function testLoadConfiguration() {
list($rates) = $this
->prepareExchangeRates();
$this
->assertSame($rates, $this->sut
->loadAll());
}
public function testLoad() {
list($rates) = $this
->prepareExchangeRates();
$reverse_rate = '0.511291';
$this
->assertSame($rates['EUR']['NLG'], $this->sut
->load('EUR', 'NLG')
->getRate());
$this
->assertSame($rates['NLG']['EUR'], $this->sut
->load('NLG', 'EUR')
->getRate());
$this
->assertSame($rates['EUR']['DEM'], $this->sut
->load('EUR', 'DEM')
->getRate());
$this
->assertSame($reverse_rate, $this->sut
->load('DEM', 'EUR')
->getRate());
$this
->assertNull($this->sut
->load('NLG', 'UAH'));
}
public function testLoadMultiple() {
list($rates) = $this
->prepareExchangeRates();
$rates = array(
'EUR' => array(
'NLG' => $rates['EUR']['NLG'],
),
'NLG' => array(
'EUR' => $rates['NLG']['EUR'],
),
'ABC' => array(
'XXX' => NULL,
),
);
$returned_rates = $this->sut
->loadMultiple(array(
'EUR' => array(
'NLG',
),
'NLG' => array(
'EUR',
),
'ABC' => array(
'XXX',
),
));
$this
->assertSame($rates['EUR']['NLG'], $returned_rates['EUR']['NLG']
->getRate());
$this
->assertSame($rates['NLG']['EUR'], $returned_rates['NLG']['EUR']
->getRate());
$this
->assertNull($returned_rates['ABC']['XXX']);
}
public function testSave() {
$currency_code_from = $this
->randomMachineName(3);
$currency_code_to = $this
->randomMachineName(3);
$rate = mt_rand();
list($rates, $rates_data) = $this
->prepareExchangeRates();
$rates[$currency_code_from][$currency_code_to] = $rate;
$rates_data[] = array(
'currency_code_from' => $currency_code_from,
'currency_code_to' => $currency_code_to,
'rate' => $rate,
);
$this->config
->expects($this
->once())
->method('set')
->with('rates', $rates_data);
$this->config
->expects($this
->once())
->method('save');
$this->sut
->save($currency_code_from, $currency_code_to, $rate);
}
function testDelete() {
list($rates, $rates_data) = $this
->prepareExchangeRates();
unset($rates['EUR']['NLG']);
unset($rates_data[1]);
$this->config
->expects($this
->once())
->method('set')
->with('rates', $rates_data);
$this->config
->expects($this
->once())
->method('save');
$this->sut
->delete('EUR', 'NLG');
}
protected function prepareExchangeRates() {
$rates = array(
'EUR' => array(
'DEM' => '1.95583',
'NLG' => '2.20371',
),
'NLG' => array(
'EUR' => '0.453780216',
),
);
$rates_data = array();
foreach ($rates as $currency_code_from => $currency_code_from_rates) {
foreach ($currency_code_from_rates as $currency_code_to => $rate) {
$rates_data[] = array(
'currency_code_from' => $currency_code_from,
'currency_code_to' => $currency_code_to,
'rate' => $rate,
);
}
}
$this->config = $this
->getMockBuilder(Config::class)
->disableOriginalConstructor()
->getMock();
$this->config
->expects($this
->any())
->method('get')
->with('rates')
->willReturn($rates_data);
$this->configFactory
->expects($this
->any())
->method('get')
->with('currency.exchanger.fixed_rates')
->willReturn($this->config);
$this->configFactory
->expects($this
->any())
->method('getEditable')
->with('currency.exchanger.fixed_rates')
->willReturn($this->config);
return array(
$rates,
$rates_data,
);
}
}