You are here

public function EntityViewEventTest::testEntityViewAlterEvent in Hook Event Dispatcher 8.2

Same name and namespace in other branches
  1. 3.x modules/core_event_dispatcher/tests/src/Unit/Entity/EntityViewEventTest.php \Drupal\Tests\core_event_dispatcher\Unit\Entity\EntityViewEventTest::testEntityViewAlterEvent()

Test EntityViewAlterEvent.

File

modules/core_event_dispatcher/tests/src/Unit/Entity/EntityViewEventTest.php, line 73

Class

EntityViewEventTest
Class EntityViewEventTest.

Namespace

Drupal\Tests\core_event_dispatcher\Unit\Entity

Code

public function testEntityViewAlterEvent() : void {
  $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',
  ];
  core_event_dispatcher_entity_view_alter($build, $entity, $display);

  /** @var \Drupal\core_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());
}