You are here

public function EntityViewEventTest::testEntityViewEventByReference in Hook Event Dispatcher 8

Test EntityViewEvent by reference.

File

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

Class

EntityViewEventTest
Class EntityViewEventTest.

Namespace

Drupal\Tests\hook_event_dispatcher\Unit\Entity

Code

public function testEntityViewEventByReference() {
  $build = $expectedBuild = [
    'testBuild' => [
      'someBuild',
    ],
  ];
  $entity = $this
    ->createMock(EntityInterface::class);
  $display = $this
    ->createMock(EntityViewDisplayInterface::class);
  $viewMode = 'testViewMode';
  $this->manager
    ->setEventCallbacks([
    HookEventDispatcherInterface::ENTITY_VIEW => static function (EntityViewEvent $event) {
      $event
        ->getBuild()['otherBuild'] = [
        'aBuild',
      ];
    },
  ]);
  $expectedBuild['otherBuild'] = [
    'aBuild',
  ];
  hook_event_dispatcher_entity_view($build, $entity, $display, $viewMode);

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