You are here

private function EntityEventTest::createAndAssertEntityEvent in Hook Event Dispatcher 3.x

Same name and namespace in other branches
  1. 8.2 modules/preprocess_event_dispatcher/tests/src/Unit/EntityEventTest.php \Drupal\Tests\preprocess_event_dispatcher\Unit\EntityEventTest::createAndAssertEntityEvent()

Create and assert the given entity event class.

Parameters

string $class: Event class name.

array $variables: Variables.

5 calls to EntityEventTest::createAndAssertEntityEvent()
EntityEventTest::testCommentEvent in modules/preprocess_event_dispatcher/tests/src/Unit/EntityEventTest.php
Test a BlockPreprocessEvent.
EntityEventTest::testEckEntityEvent in modules/preprocess_event_dispatcher/tests/src/Unit/EntityEventTest.php
Test a EckEntityPreprocessEvent.
EntityEventTest::testNodeEvent in modules/preprocess_event_dispatcher/tests/src/Unit/EntityEventTest.php
Test a NodePreprocessEvent.
EntityEventTest::testParagraphEvent in modules/preprocess_event_dispatcher/tests/src/Unit/EntityEventTest.php
Test a ParagraphPreprocessEvent.
EntityEventTest::testTaxonomyTermEvent in modules/preprocess_event_dispatcher/tests/src/Unit/EntityEventTest.php
Test a TaxonomyTermPreprocessEvent.

File

modules/preprocess_event_dispatcher/tests/src/Unit/EntityEventTest.php, line 129

Class

EntityEventTest
Class EntityEventTest.

Namespace

Drupal\Tests\preprocess_event_dispatcher\Unit

Code

private function createAndAssertEntityEvent(string $class, array $variables) : void {
  $this->dispatcher
    ->setExpectedEventCount(3);

  /** @var \Drupal\preprocess_event_dispatcher\Event\AbstractPreprocessEntityEvent $class */
  $this->service
    ->createAndDispatchKnownEvents($class::getHook(), $variables);

  /** @var \Drupal\preprocess_event_dispatcher\Event\AbstractPreprocessEntityEvent[] $events */
  $events = $this->dispatcher
    ->getEvents();
  $expectedName = $class::DISPATCH_NAME_PREFIX . $class::getHook();
  $firstEvent = reset($events);
  $firstName = key($events);
  self::assertSame($expectedName, $firstName);
  self::assertInstanceOf($class, $firstEvent);
  self::assertNotNull($firstEvent
    ->getVariables());
  $secondEvent = next($events);
  $secondName = key($events);

  /** @var \Drupal\preprocess_event_dispatcher\Variables\AbstractEntityEventVariables $secondVariables */
  $secondVariables = $secondEvent
    ->getVariables();
  $bundle = $secondVariables
    ->getEntityBundle();
  $expectedName .= '.' . $bundle;
  self::assertSame($expectedName, $secondName);
  self::assertInstanceOf($class, $secondEvent);
  $thirdEvent = next($events);
  $thirdName = key($events);

  /** @var \Drupal\preprocess_event_dispatcher\Variables\AbstractEntityEventVariables $thirdVariables */
  $thirdVariables = $thirdEvent
    ->getVariables();
  $viewMode = $thirdVariables
    ->getViewMode();
  $expectedName .= '.' . $viewMode;
  self::assertSame($expectedName, $thirdName);
  self::assertInstanceOf($class, $thirdEvent);
}