You are here

class TokenEventTest in Hook Event Dispatcher 8

Class TokenEventTest.

@package Drupal\Tests\hook_event_dispatcher\Unit\Form

@group hook_event_dispatcher

Hierarchy

Expanded class hierarchy of TokenEventTest

File

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

Namespace

Drupal\Tests\hook_event_dispatcher\Unit\Token
View source
class TokenEventTest extends UnitTestCase {

  /**
   * The manager.
   *
   * @var \Drupal\Tests\hook_event_dispatcher\Unit\HookEventDispatcherManagerSpy
   */
  private $manager;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    $builder = new ContainerBuilder();
    $this->manager = new HookEventDispatcherManagerSpy();
    $builder
      ->set('hook_event_dispatcher.manager', $this->manager);
    $builder
      ->compile();
    \Drupal::setContainer($builder);
  }

  /**
   * Test TokenInfoEvent.
   */
  public function testTokenInfoEvent() {
    $types = [
      TokenType::create('test_type', 'Test type')
        ->setDescription('Test type desc'),
      TokenType::create('other_type', 'Other type')
        ->setDescription('Other type!')
        ->setNeedsData('test_data'),
    ];
    $tokens = [
      Token::create('test_type', 'test_token1', 'Test name 1')
        ->setDescription('Test description 1'),
      Token::create('test_type', 'test_token2', 'Test name 2')
        ->setDescription('Test description 2'),
      Token::create('other_type', 'test_token3', 'Test name 3'),
      Token::create('dynamic_type', 'test_token4', 'Test name 4')
        ->setDynamic(TRUE),
    ];
    $this->manager
      ->setEventCallbacks([
      HookEventDispatcherInterface::TOKEN_INFO => function (TokensInfoEvent $event) use ($types, $tokens) {
        foreach ($types as $type) {
          $event
            ->addTokenType($type);
        }
        foreach ($tokens as $token) {
          $event
            ->addToken($token);
        }
      },
    ]);
    $result = hook_event_dispatcher_token_info();
    $expectedTypes = [
      'test_type' => [
        'name' => 'Test type',
        'description' => 'Test type desc',
        'needs-data' => NULL,
      ],
      'other_type' => [
        'name' => 'Other type',
        'description' => 'Other type!',
        'needs-data' => 'test_data',
      ],
    ];
    $expectedTokens = [
      'test_type' => [
        'test_token1' => [
          'name' => 'Test name 1',
          'description' => 'Test description 1',
          'dynamic' => FALSE,
        ],
        'test_token2' => [
          'name' => 'Test name 2',
          'description' => 'Test description 2',
          'dynamic' => FALSE,
        ],
      ],
      'other_type' => [
        'test_token3' => [
          'name' => 'Test name 3',
          'description' => NULL,
          'dynamic' => FALSE,
        ],
      ],
      'dynamic_type' => [
        'test_token4' => [
          'name' => 'Test name 4',
          'description' => NULL,
          'dynamic' => TRUE,
        ],
      ],
    ];

    /** @var \Drupal\hook_event_dispatcher\Event\Token\TokensInfoEvent $event */
    $event = $this->manager
      ->getRegisteredEvent(HookEventDispatcherInterface::TOKEN_INFO);
    self::assertEquals($expectedTypes, $result['types']);
    self::assertEquals($expectedTokens, $result['tokens']);
    self::assertEquals($expectedTypes, $event
      ->getTokenTypes());
    self::assertEquals($expectedTokens, $event
      ->getTokens());
  }

  /**
   * Test TokenReplacementEvent.
   */
  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'));
  }

  /**
   * Test TokenReplacementEvent invalid type exception.
   */
  public function testTokenReplacementEventInvalidTypeException() {
    $metaData = $this
      ->createMock(BubbleableMetadata::class);
    $event = new TokensReplacementEvent('', [], [], [], $metaData);
    $this
      ->setExpectedException(\UnexpectedValueException::class);
    $event
      ->setReplacementValue(NULL, '', '');
  }

  /**
   * Test TokenReplacementEvent invalid token exception.
   */
  public function testTokenReplacementEventInvalidTokenException() {
    $metaData = $this
      ->createMock(BubbleableMetadata::class);
    $event = new TokensReplacementEvent('', [], [], [], $metaData);
    $this
      ->setExpectedException(\UnexpectedValueException::class);
    $event
      ->setReplacementValue('', NULL, '');
  }

  /**
   * Test TokenReplacementEvent wrong replacement exception.
   */
  public function testTokenReplacementEventWrongReplacementException() {
    $metaData = $this
      ->createMock(BubbleableMetadata::class);
    $event = new TokensReplacementEvent('', [], [], [], $metaData);
    $this
      ->setExpectedException(\UnexpectedValueException::class);
    $event
      ->setReplacementValue('', '', '');
  }

  /**
   * Test TokenReplacementEvent invalid replacement Exception.
   */
  public function testTokenReplacementEventInvalidReplacementException() {
    $metaData = $this
      ->createMock(BubbleableMetadata::class);
    $event = new TokensReplacementEvent('test', [
      'token' => '[test:token]',
    ], [], [], $metaData);
    $this
      ->setExpectedException(\UnexpectedValueException::class);
    $event
      ->setReplacementValue('test', 'token', NULL);
  }

}

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.
TokenEventTest::$manager private property The manager.
TokenEventTest::setUp public function Overrides UnitTestCase::setUp
TokenEventTest::testTokenInfoEvent public function Test TokenInfoEvent.
TokenEventTest::testTokenReplacementEvent public function Test TokenReplacementEvent.
TokenEventTest::testTokenReplacementEventInvalidReplacementException public function Test TokenReplacementEvent invalid replacement Exception.
TokenEventTest::testTokenReplacementEventInvalidTokenException public function Test TokenReplacementEvent invalid token exception.
TokenEventTest::testTokenReplacementEventInvalidTypeException public function Test TokenReplacementEvent invalid type exception.
TokenEventTest::testTokenReplacementEventWrongReplacementException public function Test TokenReplacementEvent wrong replacement exception.
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.