You are here

final class EntityEventTest in Hook Event Dispatcher 8

Same name in this branch
  1. 8 tests/src/Unit/Entity/EntityEventTest.php \Drupal\Tests\hook_event_dispatcher\Unit\Entity\EntityEventTest
  2. 8 tests/src/Unit/Preprocess/EntityEventTest.php \Drupal\Tests\hook_event_dispatcher\Unit\Preprocess\EntityEventTest

Class EntityEventTest.

@group hook_event_dispatcher

Hierarchy

Expanded class hierarchy of EntityEventTest

File

tests/src/Unit/Preprocess/EntityEventTest.php, line 22

Namespace

Drupal\Tests\hook_event_dispatcher\Unit\Preprocess
View source
final class EntityEventTest extends UnitTestCase {

  /**
   * PreprocessEventService.
   *
   * @var \Drupal\hook_event_dispatcher\Service\PreprocessEventService
   *   PreprocessEventService.
   */
  private $service;

  /**
   * SpyEventDispatcher.
   *
   * @var \Drupal\Tests\hook_event_dispatcher\Unit\Preprocess\Helpers\SpyEventDispatcher
   *   SpyEventDispatcher
   */
  private $dispatcher;

  /**
   * Variables array.
   *
   * @var array
   *   Variables.
   */
  private $variables;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    $loader = YamlDefinitionsLoader::getInstance();
    $this->dispatcher = new SpyEventDispatcher();
    $this->service = new PreprocessEventService($this->dispatcher, $loader
      ->getMapper());
    $this->variables = [];
  }

  /**
   * Test a BlockPreprocessEvent.
   */
  public function testCommentEvent() {
    $this->variables = [
      'comment' => new EntityMock('comment', 'bundle', 'view_mode'),
      'view_mode' => 'view_mode',
    ];
    $this
      ->createAndAssertEntityEvent(CommentPreprocessEvent::class);
  }

  /**
   * Test a EckEntityPreprocessEvent.
   */
  public function testEckEntityEvent() {
    $this->variables = [
      'elements' => [
        '#view_mode' => 'view_mode',
      ],
      'eck_entity' => new EntityMock('eck_entity', 'bundle', 'view_mode'),
      'theme_hook_original' => 'eck_entity',
      'bundle' => 'bundle',
    ];
    $this
      ->createAndAssertEntityEvent(EckEntityPreprocessEvent::class);
  }

  /**
   * Test a NodePreprocessEvent.
   */
  public function testNodeEvent() {
    $this->variables = [
      'node' => new EntityMock('node', 'bundle', 'view_mode'),
      'theme_hook_original' => 'node',
      'view_mode' => 'view_mode',
    ];
    $this
      ->createAndAssertEntityEvent(NodePreprocessEvent::class);
  }

  /**
   * Test a ParagraphPreprocessEvent.
   */
  public function testParagraphEvent() {
    $this->variables = [
      'paragraph' => new EntityMock('paragraph', 'bundle', 'view_mode'),
      'theme_hook_original' => 'paragraph',
      'view_mode' => 'view_mode',
    ];
    $this
      ->createAndAssertEntityEvent(ParagraphPreprocessEvent::class);
  }

  /**
   * Test a TaxonomyTermPreprocessEvent.
   */
  public function testTaxonomyTermEvent() {
    $this->variables = [
      'term' => new EntityMock('taxonomy_term', 'bundle', 'view_mode'),
      'theme_hook_original' => 'taxonomy_term',
      'view_mode' => 'view_mode',
    ];
    $this
      ->createAndAssertEntityEvent(TaxonomyTermPreprocessEvent::class);
  }

  /**
   * Create and assert the given entity event class.
   *
   * @param string $class
   *   Event class name.
   */
  private function createAndAssertEntityEvent($class) {
    $this->dispatcher
      ->setExpectedEventCount(3);

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

    /** @var \Drupal\hook_event_dispatcher\Event\Preprocess\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::assertInstanceOf(AbstractEventVariables::class, $firstEvent
      ->getVariables());
    $secondEvent = \next($events);
    $secondName = \key($events);
    $bundle = $secondEvent
      ->getVariables()
      ->getEntityBundle();
    self::assertNotNull($bundle);
    self::assertInternalType('string', $bundle);
    $expectedName .= '.' . $bundle;
    self::assertSame($expectedName, $secondName);
    self::assertInstanceOf($class, $secondEvent);
    self::assertInstanceOf(AbstractEventVariables::class, $secondEvent
      ->getVariables());
    $thirdEvent = \next($events);
    $thirdName = \key($events);
    $viewMode = $thirdEvent
      ->getVariables()
      ->getViewMode();
    self::assertNotNull($viewMode);
    self::assertInternalType('string', $viewMode);
    $expectedName .= '.' . $viewMode;
    self::assertSame($expectedName, $thirdName);
    self::assertInstanceOf($class, $thirdEvent);
    self::assertInstanceOf(AbstractEventVariables::class, $thirdEvent
      ->getVariables());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityEventTest::$dispatcher private property SpyEventDispatcher.
EntityEventTest::$service private property PreprocessEventService.
EntityEventTest::$variables private property Variables array.
EntityEventTest::createAndAssertEntityEvent private function Create and assert the given entity event class.
EntityEventTest::setUp public function Overrides UnitTestCase::setUp
EntityEventTest::testCommentEvent public function Test a BlockPreprocessEvent.
EntityEventTest::testEckEntityEvent public function Test a EckEntityPreprocessEvent.
EntityEventTest::testNodeEvent public function Test a NodePreprocessEvent.
EntityEventTest::testParagraphEvent public function Test a ParagraphPreprocessEvent.
EntityEventTest::testTaxonomyTermEvent public function Test a TaxonomyTermPreprocessEvent.
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.