You are here

class CurrencyExchangeTest in Currency 8.3

@coversDefaultClass \Drupal\currency\Plugin\Filter\CurrencyExchange

@group Currency

Hierarchy

Expanded class hierarchy of CurrencyExchangeTest

File

tests/src/Unit/Plugin/Filter/CurrencyExchangeTest.php, line 22

Namespace

Drupal\Tests\currency\Unit\Plugin\Filter
View source
class CurrencyExchangeTest extends UnitTestCase {

  /**
   * The cache contexts manager.
   *
   * @var \Drupal\Core\Cache\Context\CacheContextsManager|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $cacheContextsManager;

  /**
   * The exchange rate provider.
   *
   * @var \Drupal\currency\Plugin\Currency\ExchangeRateProvider\ExchangeRateProviderInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $exchangeRateProvider;

  /**
   * The input parser.
   *
   * @var \Commercie\Currency\InputInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $input;

  /**
   * The plugin definiton.
   *
   * @var mixed[]
   */
  protected $pluginDefinition;

  /**
   * The string translator.
   *
   * @var \Drupal\Core\StringTranslation\TranslationInterface
   */
  protected $stringTranslation;

  /**
   * The class under test.
   *
   * @var \Drupal\currency\Plugin\Filter\CurrencyExchange|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $sut;

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    $configuration = [];
    $plugin_id = $this
      ->randomMachineName();
    $this->pluginDefinition = [
      'cache' => TRUE,
      'provider' => $this
        ->randomMachineName(),
    ];
    $this->cacheContextsManager = $this
      ->getMockBuilder(CacheContextsManager::class)
      ->disableOriginalConstructor()
      ->getMock();
    $this->cacheContextsManager
      ->expects($this
      ->any())
      ->method('assertValidTokens')
      ->willReturn(TRUE);
    $this->exchangeRateProvider = $this
      ->createMock(ExchangeRateProviderInterface::class);
    $this->input = $this
      ->createMock(InputInterface::class);
    $this->stringTranslation = $this
      ->getStringTranslationStub();
    $container = new Container();
    $container
      ->set('cache_contexts_manager', $this->cacheContextsManager);
    \Drupal::setContainer($container);
    $this->sut = new CurrencyExchange($configuration, $plugin_id, $this->pluginDefinition, $this->stringTranslation, $this->exchangeRateProvider, $this->input);
  }

  /**
   * @covers ::create
   * @covers ::__construct
   */
  function testCreate() {
    $container = $this
      ->createMock(ContainerInterface::class);
    $map = [
      [
        'currency.input',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->input,
      ],
      [
        'string_translation',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->stringTranslation,
      ],
      [
        'currency.exchange_rate_provider',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->exchangeRateProvider,
      ],
    ];
    $container
      ->expects($this
      ->any())
      ->method('get')
      ->willReturnMap($map);
    $sut = CurrencyExchange::create($container, [], '', $this->pluginDefinition);
    $this
      ->assertInstanceOf(CurrencyExchange::class, $sut);
  }

  /**
   * @covers ::process
   * @covers ::processCallback
   */
  public function testProcess() {
    $cache_contexts = Cache::mergeContexts([
      'baz',
      'qux',
    ]);
    $cache_tags = Cache::mergeTags([
      'foo',
      'bar',
    ]);
    $currency_code_from = 'EUR';
    $currency_code_to = 'NLG';
    $rate = '2.20371';
    $exchange_rate_provider_id = 'foo_bar';
    $exchange_rate = new ExchangeRate($currency_code_from, $currency_code_to, $rate, $exchange_rate_provider_id);
    $exchange_rate
      ->addCacheContexts($cache_contexts);
    $exchange_rate
      ->addCacheTags($cache_tags);
    $this->input
      ->expects($this
      ->any())
      ->method('parseAmount')
      ->will($this
      ->returnArgument(0));
    $this->exchangeRateProvider
      ->expects($this
      ->any())
      ->method('load')
      ->with($currency_code_from, $currency_code_to)
      ->willReturn($exchange_rate);
    $langcode = $this
      ->randomMachineName(2);
    $tokens_valid = [
      '[currency:EUR:NLG]' => '2.20371',
      '[currency:EUR:NLG:1]' => '2.20371',
      '[currency:EUR:NLG:2]' => '4.40742',
    ];
    $tokens_invalid = [
      // Missing arguments.
      '[currency]',
      '[currency:]',
      '[currency::]',
      '[currency:EUR]',
      // Invalid currency code.
      '[currency:EUR:123]',
      '[currency:123:EUR]',
      // Invalid currency code and missing argument.
      '[currency:123]',
    ];
    foreach ($tokens_valid as $token => $replacement) {
      $result = $this->sut
        ->process($token, $langcode);
      $this
        ->assertInstanceOf(FilterProcessResult::class, $result);
      $this
        ->assertSame(round($replacement, 4), round($result
        ->getProcessedText(), 4));
      $this
        ->assertSame($cache_contexts, $result
        ->getCacheContexts());
      $this
        ->assertSame($cache_tags, $result
        ->getCacheTags());
    }
    foreach ($tokens_invalid as $token) {
      $result = $this->sut
        ->process($token, $langcode);
      $this
        ->assertInstanceOf(FilterProcessResult::class, $result);
      $this
        ->assertSame($token, $result
        ->getProcessedText());
      $this
        ->assertEmpty($result
        ->getCacheContexts());
      $this
        ->assertEmpty($result
        ->getCacheTags());
    }
  }

  /**
   * @covers ::tips
   */
  public function testTips() {
    $tips = $this->sut
      ->tips();
    $this
      ->assertInstanceOf(TranslatableMarkup::class, $tips);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CurrencyExchangeTest::$cacheContextsManager protected property The cache contexts manager.
CurrencyExchangeTest::$exchangeRateProvider protected property The exchange rate provider.
CurrencyExchangeTest::$input protected property The input parser.
CurrencyExchangeTest::$pluginDefinition protected property The plugin definiton.
CurrencyExchangeTest::$stringTranslation protected property The string translator.
CurrencyExchangeTest::$sut protected property The class under test.
CurrencyExchangeTest::setUp public function Overrides UnitTestCase::setUp
CurrencyExchangeTest::testCreate function @covers ::create @covers ::__construct
CurrencyExchangeTest::testProcess public function @covers ::process @covers ::processCallback
CurrencyExchangeTest::testTips public function @covers ::tips
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.
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.