You are here

class PluginBasedExchangeRateProviderTest in Currency 8.3

@coversDefaultClass \Drupal\currency\PluginBasedExchangeRateProvider

@group Currency

Hierarchy

Expanded class hierarchy of PluginBasedExchangeRateProviderTest

File

tests/src/Unit/PluginBasedExchangeRateProviderTest.php, line 17

Namespace

Drupal\Tests\currency\Unit
View source
class PluginBasedExchangeRateProviderTest extends UnitTestCase {

  /**
   * The configuration factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $configFactory;

  /**
   * The currency exchanger plugin manager.
   *
   * @var \Drupal\Component\Plugin\PluginManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $currencyExchangeRateProviderManager;

  /**
   * The class under test.
   *
   * @var \Drupal\currency\PluginBasedExchangeRateProvider
   */
  protected $sut;

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    $this->configFactory = $this
      ->getMockBuilder(ConfigFactoryInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
    $this->currencyExchangeRateProviderManager = $this
      ->createMock(ExchangeRateProviderManagerInterface::class);
    $this->sut = new PluginBasedExchangeRateProvider($this->currencyExchangeRateProviderManager, $this->configFactory);
  }

  /**
   * @covers ::loadConfiguration
   */
  public function testLoadConfiguration() {
    $plugin_id_a = $this
      ->randomMachineName();
    $plugin_id_b = $this
      ->randomMachineName();
    $plugin_definitions = array(
      $plugin_id_a => array(),
      $plugin_id_b => array(),
    );
    $config_value = array(
      array(
        'plugin_id' => $plugin_id_b,
        'status' => TRUE,
      ),
    );
    $this->currencyExchangeRateProviderManager
      ->expects($this
      ->once())
      ->method('getDefinitions')
      ->willReturn($plugin_definitions);
    $config = $this
      ->getMockBuilder(Config::class)
      ->disableOriginalConstructor()
      ->getMock();
    $config
      ->expects($this
      ->once())
      ->method('get')
      ->with('plugins')
      ->willReturn($config_value);
    $this->configFactory
      ->expects($this
      ->once())
      ->method('get')
      ->with('currency.exchange_rate_provider')
      ->willReturn($config);
    $configuration = $this->sut
      ->loadConfiguration();
    $expected = array(
      $plugin_id_b => TRUE,
      $plugin_id_a => FALSE,
    );
    $this
      ->assertSame($expected, $configuration);
  }

  /**
   * @covers ::saveConfiguration
   */
  public function testSaveConfiguration() {
    $configuration = array(
      'currency_historical_rates' => TRUE,
      'currency_fixed_rates' => TRUE,
      'foo' => FALSE,
    );
    $configuration_data = array(
      array(
        'plugin_id' => 'currency_historical_rates',
        'status' => TRUE,
      ),
      array(
        'plugin_id' => 'currency_fixed_rates',
        'status' => TRUE,
      ),
      array(
        'plugin_id' => 'foo',
        'status' => FALSE,
      ),
    );
    $config = $this
      ->getMockBuilder(Config::class)
      ->disableOriginalConstructor()
      ->getMock();
    $config
      ->expects($this
      ->once())
      ->method('set')
      ->with('plugins', $configuration_data);
    $config
      ->expects($this
      ->once())
      ->method('save');
    $this->configFactory
      ->expects($this
      ->once())
      ->method('getEditable')
      ->with('currency.exchange_rate_provider')
      ->willReturn($config);
    $this->sut
      ->saveConfiguration($configuration);
  }

  /**
   * @covers ::load
   */
  public function testLoad() {
    $currency_code_from = 'EUR';
    $currency_code_to = 'NLG';
    $rate = new ExchangeRate($currency_code_from, $currency_code_to, '2.20371');
    $exchange_rate_provider_id_a = $this
      ->randomMachineName();
    $exchange_rate_provider_id_b = $this
      ->randomMachineName();
    $exchange_rate_provider_b = $this
      ->createMock('\\Commercie\\CurrencyExchange\\ExchangeRateProviderInterface');
    $exchange_rate_provider_b
      ->expects($this
      ->once())
      ->method('load')
      ->with($currency_code_from, $currency_code_to)
      ->willReturn($rate);
    $plugin_definitions = [
      $exchange_rate_provider_id_a => [
        'id' => $exchange_rate_provider_id_a,
      ],
      $exchange_rate_provider_id_b => [
        'id' => $exchange_rate_provider_id_b,
      ],
    ];
    $this->currencyExchangeRateProviderManager
      ->expects($this
      ->once())
      ->method('createInstance')
      ->with($exchange_rate_provider_id_b)
      ->willReturn($exchange_rate_provider_b);
    $this->currencyExchangeRateProviderManager
      ->expects($this
      ->once())
      ->method('getDefinitions')
      ->willReturn($plugin_definitions);
    $config_value = [
      [
        'plugin_id' => $exchange_rate_provider_id_a,
        'status' => FALSE,
      ],
      [
        'plugin_id' => $exchange_rate_provider_id_b,
        'status' => TRUE,
      ],
    ];
    $config = $this
      ->getMockBuilder('\\Drupal\\Core\\Config\\Config')
      ->disableOriginalConstructor()
      ->getMock();
    $config
      ->expects($this
      ->once())
      ->method('get')
      ->with('plugins')
      ->will($this
      ->returnValue($config_value));
    $this->configFactory
      ->expects($this
      ->once())
      ->method('get')
      ->with('currency.exchange_rate_provider')
      ->will($this
      ->returnValue($config));
    $this
      ->assertSame($rate, $this->sut
      ->load($currency_code_from, $currency_code_to));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
PluginBasedExchangeRateProviderTest::$configFactory protected property The configuration factory.
PluginBasedExchangeRateProviderTest::$currencyExchangeRateProviderManager protected property The currency exchanger plugin manager.
PluginBasedExchangeRateProviderTest::$sut protected property The class under test.
PluginBasedExchangeRateProviderTest::setUp public function Overrides UnitTestCase::setUp
PluginBasedExchangeRateProviderTest::testLoad public function @covers ::load
PluginBasedExchangeRateProviderTest::testLoadConfiguration public function @covers ::loadConfiguration
PluginBasedExchangeRateProviderTest::testSaveConfiguration public function @covers ::saveConfiguration
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.