You are here

public function TokenEventTest::testTokenReplacementEvent in Hook Event Dispatcher 8

Test TokenReplacementEvent.

File

tests/src/Unit/Token/TokenEventTest.php, line 121

Class

TokenEventTest
Class TokenEventTest.

Namespace

Drupal\Tests\hook_event_dispatcher\Unit\Token

Code

public function testTokenReplacementEvent() {
  $replacement1 = 'Replacement value 1';
  $replacement2 = 'Replacement value 2';
  $this->manager
    ->setEventCallbacks([
    HookEventDispatcherInterface::TOKEN_REPLACEMENT => function (TokensReplacementEvent $event) use ($replacement1, $replacement2) {
      $event
        ->setReplacementValue('test_type', 'token1', $replacement1);
      $event
        ->setReplacementValue('test_type', 'token2', $replacement2);
    },
  ]);
  $type = 'test_type';
  $tokens = [
    'token1' => '[test_type:token1]',
    'token2' => '[test_type:token2]',
    'token3' => '[test_type:token3]',
  ];
  $data = [
    'test_data' => 'test!',
  ];
  $options = [
    'test_options' => 'Option value',
  ];
  $metaData = $this
    ->createMock(BubbleableMetadata::class);
  $result = hook_event_dispatcher_tokens($type, $tokens, $data, $options, $metaData);
  $expectedResult = [
    '[test_type:token1]' => $replacement1,
    '[test_type:token2]' => $replacement2,
  ];

  /** @var \Drupal\hook_event_dispatcher\Event\Token\TokensReplacementEvent $event */
  $event = $this->manager
    ->getRegisteredEvent(HookEventDispatcherInterface::TOKEN_REPLACEMENT);
  self::assertEquals($expectedResult, $result);
  self::assertEquals($type, $event
    ->getType());
  self::assertEquals($tokens, $event
    ->getTokens());
  self::assertEquals($data, $event
    ->getRawData());
  self::assertEquals($options, $event
    ->getOptions());
  self::assertEquals($metaData, $event
    ->getBubbleableMetadata());
  self::assertEquals('test!', $event
    ->getData('test_data'));
  self::assertNull($event
    ->getData('none_existing'));
  self::assertFalse($event
    ->forToken('none_existing', 'token1'));
  self::assertFalse($event
    ->forToken('test_type', 'none_existing'));
}