You are here

function CurrencyLocalizeTest::testProcess in Currency 8.3

@covers ::process @covers ::processCallback

File

tests/src/Unit/Plugin/Filter/CurrencyLocalizeTest.php, line 128

Class

CurrencyLocalizeTest
@coversDefaultClass \Drupal\currency\Plugin\Filter\CurrencyLocalize

Namespace

Drupal\Tests\currency\Unit\Plugin\Filter

Code

function testProcess() {
  $cache_contexts = Cache::mergeContexts([
    'baz',
    'qux',
  ]);
  $cache_tags = Cache::mergeTags([
    'foo',
    'bar',
  ]);
  $map = [
    [
      '100',
      TRUE,
      LanguageInterface::TYPE_CONTENT,
      '€100.00',
    ],
    [
      '100.7654',
      TRUE,
      LanguageInterface::TYPE_CONTENT,
      '€100.77',
    ],
    [
      '1.99',
      TRUE,
      LanguageInterface::TYPE_CONTENT,
      '€1.99',
    ],
    [
      '2.99',
      TRUE,
      LanguageInterface::TYPE_CONTENT,
      '€2.99',
    ],
  ];
  $currency = $this
    ->createMock(CurrencyInterface::class);
  $currency
    ->expects($this
    ->any())
    ->method('formatAmount')
    ->willReturnMap($map);
  $currency
    ->expects($this
    ->atLeastOnce())
    ->method('getCacheContexts')
    ->willReturn($cache_contexts);
  $currency
    ->expects($this
    ->atLeastOnce())
    ->method('getCacheTags')
    ->willReturn($cache_tags);
  $this->currencyStorage
    ->expects($this
    ->any())
    ->method('load')
    ->with('EUR')
    ->willReturn($currency);
  $this->input
    ->expects($this
    ->any())
    ->method('parseAmount')
    ->will($this
    ->returnArgument(0));
  $langcode = $this
    ->randomMachineName(2);
  $tokens_valid = [
    '[currency-localize:EUR:100]' => '€100.00',
    '[currency-localize:EUR:100.7654]' => '€100.77',
    '[currency-localize:EUR:1.99]' => '€1.99',
    '[currency-localize:EUR:2.99]' => '€2.99',
  ];
  $tokens_invalid = [
    // Missing arguments.
    '[currency-localize]',
    '[currency-localize:]',
    '[currency-localize::]',
    '[currency-localize:EUR]',
    // Invalid currency code.
    '[currency-localize:123:456]',
    // Invalid currency code and missing argument.
    '[currency-localize:123]',
  ];
  foreach ($tokens_valid as $token => $replacement) {
    $result = $this->sut
      ->process($token, $langcode);
    $this
      ->assertInstanceOf(FilterProcessResult::class, $result);
    $this
      ->assertSame($replacement, $result
      ->getProcessedText());
    $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());
  }
}