You are here

final class EntityViewEventTest in Hook Event Dispatcher 8

Class EntityViewEventTest.

@package Drupal\Tests\hook_event_dispatcher\Unit\Entity

@group hook_event_dispatcher

Hierarchy

Expanded class hierarchy of EntityViewEventTest

File

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

Namespace

Drupal\Tests\hook_event_dispatcher\Unit\Entity
View source
final class EntityViewEventTest 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 EntityViewEvent by reference.
   */
  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());
  }

  /**
   * Test EntityViewEvent by set.
   */
  public function testEntityViewEventBySet() {
    $build = [
      'testBuild' => [
        'someBuild',
      ],
    ];
    $otherBuild = [
      'otherBuild' => [
        'lalala',
      ],
    ];
    $entity = $this
      ->createMock(EntityInterface::class);
    $display = $this
      ->createMock(EntityViewDisplayInterface::class);
    $viewMode = 'testViewMode';
    $this->manager
      ->setEventCallbacks([
      HookEventDispatcherInterface::ENTITY_VIEW => static function (EntityViewEvent $event) use ($otherBuild) {
        $event
          ->setBuild($otherBuild);
      },
    ]);
    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($otherBuild, $event
      ->getBuild());
    self::assertSame($entity, $event
      ->getEntity());
    self::assertSame($display, $event
      ->getDisplay());
    self::assertSame($viewMode, $event
      ->getViewMode());
  }

  /**
   * Test EntityViewAlterEvent.
   */
  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());
  }

  /**
   * Test EntityBuildDefaultsAlter.
   */
  public function testEntityBuildDefaultsAlter() {
    $build = $expectedBuild = [
      'testBuild' => [
        'someBuild',
      ],
    ];
    $entity = $this
      ->createMock(EntityInterface::class);
    $viewMode = 'entity_view_mode';
    $this->manager
      ->setEventCallbacks([
      HookEventDispatcherInterface::ENTITY_BUILD_DEFAULTS_ALTER => static function (EntityBuildDefaultsAlterEvent $event) {
        $event
          ->getBuild()['otherBuild'] = [
          'aBuild',
        ];
      },
    ]);
    $expectedBuild['otherBuild'] = [
      'aBuild',
    ];
    hook_event_dispatcher_entity_build_defaults_alter($build, $entity, $viewMode);

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

  /**
   * Test EntityBuildDefaultsAlter.
   */
  public function testEntityBuildDefaultsAlterWithNullViewMode() {
    $build = [
      'testBuild' => [
        'someBuild',
      ],
    ];
    $entity = $this
      ->createMock(EntityInterface::class);
    $viewMode = NULL;
    hook_event_dispatcher_entity_build_defaults_alter($build, $entity, $viewMode);

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

}

Members

Namesort descending Modifiers Type Description Overrides
EntityViewEventTest::$manager private property The manager.
EntityViewEventTest::setUp public function Overrides UnitTestCase::setUp
EntityViewEventTest::testEntityBuildDefaultsAlter public function Test EntityBuildDefaultsAlter.
EntityViewEventTest::testEntityBuildDefaultsAlterWithNullViewMode public function Test EntityBuildDefaultsAlter.
EntityViewEventTest::testEntityViewAlterEvent public function Test EntityViewAlterEvent.
EntityViewEventTest::testEntityViewEventByReference public function Test EntityViewEvent by reference.
EntityViewEventTest::testEntityViewEventBySet public function Test EntityViewEvent by set.
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.
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.