You are here

public function EntityViewEventTest::testEntityViewAlterEvent in Hook Event Dispatcher 8

Test EntityViewAlterEvent.

File

tests/src/Unit/Entity/EntityViewEventTest.php, line 102

Class

EntityViewEventTest
Class EntityViewEventTest.

Namespace

Drupal\Tests\hook_event_dispatcher\Unit\Entity

Code

public function testEntityViewAlterEvent() {
  $build = $expectedBuild = [
    'testBuild' => [
      'someBuild',
    ],
  ];
  $entity = $this
    ->createMock(EntityInterface::class);
  $display = $this
    ->createMock(EntityViewDisplayInterface::class);
  $this->manager
    ->setEventCallbacks([
    HookEventDispatcherInterface::ENTITY_VIEW_ALTER => static function (EntityViewAlterEvent $event) {
      $event
        ->getBuild()['otherBuild'] = [
        'aBuild',
      ];
    },
  ]);
  $expectedBuild['otherBuild'] = [
    'aBuild',
  ];
  hook_event_dispatcher_entity_view_alter($build, $entity, $display);

  /** @var \Drupal\hook_event_dispatcher\Event\Entity\EntityViewAlterEvent $event */
  $event = $this->manager
    ->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_VIEW_ALTER);
  self::assertSame($build, $event
    ->getBuild());
  self::assertSame($expectedBuild, $event
    ->getBuild());
  self::assertSame($entity, $event
    ->getEntity());
  self::assertSame($display, $event
    ->getDisplay());
}