You are here

final class OtherEventTest in Hook Event Dispatcher 8

Class OtherEventTest.

@group hook_event_dispatcher

Testing all variables gives expected PHPMD warnings.

Plugin annotation

@SuppressWarnings(PHPMD . CouplingBetweenObjects);

Hierarchy

Expanded class hierarchy of OtherEventTest

File

tests/src/Unit/Preprocess/OtherEventTest.php, line 28

Namespace

Drupal\Tests\hook_event_dispatcher\Unit\Preprocess
View source
final class OtherEventTest 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 testBlockEvent() {
    $this
      ->createAndAssertEvent(BlockPreprocessEvent::class);
  }

  /**
   * Test a FieldPreprocessEvent.
   */
  public function testFieldEvent() {
    $this
      ->createAndAssertEvent(FieldPreprocessEvent::class);
  }

  /**
   * Test a FormPreprocessEvent.
   */
  public function testFormEvent() {
    $this
      ->createAndAssertEvent(FormPreprocessEvent::class);
  }

  /**
   * Test a HtmlPreprocessEvent.
   */
  public function testHtmlEvent() {
    $this
      ->createAndAssertEvent(HtmlPreprocessEvent::class);
  }

  /**
   * Test a ImagePreprocessEvent.
   */
  public function testImageEvent() {
    $this
      ->createAndAssertEvent(ImagePreprocessEvent::class);
  }

  /**
   * Test a PagePreprocessEvent.
   */
  public function testPageEvent() {
    $this
      ->createAndAssertEvent(PagePreprocessEvent::class);
  }

  /**
   * Test a ViewFieldPreprocessEvent.
   */
  public function testViewFieldEvent() {
    $this
      ->createAndAssertEvent(ViewFieldPreprocessEvent::class);
  }

  /**
   * Test a ViewPreprocessEvent.
   */
  public function testViewEvent() {
    $this
      ->createAndAssertEvent(ViewPreprocessEvent::class);
  }

  /**
   * Test a StatusMessagesPreprocessEvent.
   */
  public function testStatusMessagesEvent() {
    $this
      ->createAndAssertEvent(StatusMessagesPreprocessEvent::class);
  }

  /**
   * Test a unknown hook.
   */
  public function testNotMappingEvent() {
    $this->service
      ->createAndDispatchKnownEvents('NoneExistingHook', $this->variables);
    self::assertSame([], $this->dispatcher
      ->getEvents());
  }

  /**
   * Create and assert the given event class.
   *
   * @param string $class
   *   Event class name.
   */
  private function createAndAssertEvent($class) {

    /** @var \Drupal\hook_event_dispatcher\Event\Preprocess\AbstractPreprocessEvent $class */
    $this->service
      ->createAndDispatchKnownEvents($class::getHook(), $this->variables);
    self::assertSame($class::name(), $this->dispatcher
      ->getLastEventName());

    /** @var \Drupal\hook_event_dispatcher\Event\Preprocess\AbstractPreprocessEvent $event */
    $event = $this->dispatcher
      ->getLastEvent();
    self::assertInstanceOf($class, $event);
    self::assertInstanceOf(AbstractEventVariables::class, $event
      ->getVariables());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OtherEventTest::$dispatcher private property SpyEventDispatcher.
OtherEventTest::$service private property PreprocessEventService.
OtherEventTest::$variables private property Variables array.
OtherEventTest::createAndAssertEvent private function Create and assert the given event class.
OtherEventTest::setUp public function Overrides UnitTestCase::setUp
OtherEventTest::testBlockEvent public function Test a BlockPreprocessEvent.
OtherEventTest::testFieldEvent public function Test a FieldPreprocessEvent.
OtherEventTest::testFormEvent public function Test a FormPreprocessEvent.
OtherEventTest::testHtmlEvent public function Test a HtmlPreprocessEvent.
OtherEventTest::testImageEvent public function Test a ImagePreprocessEvent.
OtherEventTest::testNotMappingEvent public function Test a unknown hook.
OtherEventTest::testPageEvent public function Test a PagePreprocessEvent.
OtherEventTest::testStatusMessagesEvent public function Test a StatusMessagesPreprocessEvent.
OtherEventTest::testViewEvent public function Test a ViewPreprocessEvent.
OtherEventTest::testViewFieldEvent public function Test a ViewFieldPreprocessEvent.
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.