You are here

final class OtherEventTest in Hook Event Dispatcher 8.2

Same name and namespace in other branches
  1. 3.x modules/preprocess_event_dispatcher/tests/src/Unit/OtherEventTest.php \Drupal\Tests\preprocess_event_dispatcher\Unit\OtherEventTest

Class OtherEventTest.

@group preprocess_event_dispatcher

Testing all variables gives expected PHPMD warnings.

Plugin annotation

@SuppressWarnings(PHPMD . CouplingBetweenObjects);

Hierarchy

  • class \Drupal\Tests\preprocess_event_dispatcher\Unit\OtherEventTest extends \PHPUnit\Framework\TestCase

Expanded class hierarchy of OtherEventTest

File

modules/preprocess_event_dispatcher/tests/src/Unit/OtherEventTest.php, line 28

Namespace

Drupal\Tests\preprocess_event_dispatcher\Unit
View source
final class OtherEventTest extends TestCase {

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

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

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

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

  /**
   * Test a BlockPreprocessEvent.
   */
  public function testBlockEvent() : void {
    $this
      ->createAndAssertEvent(BlockPreprocessEvent::class);
  }

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

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

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

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

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

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

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

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

  /**
   * Test a unknown hook.
   */
  public function testNotMappingEvent() : void {
    $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(string $class) : void {

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

    /** @var \Drupal\preprocess_event_dispatcher\Event\AbstractPreprocessEvent $event */
    $event = $this->dispatcher
      ->getLastEvent();
    self::assertInstanceOf($class, $event);
    $variablesClass = str_replace([
      '\\Event\\',
      'PreprocessEvent',
    ], [
      '\\Variables\\',
      'EventVariables',
    ], $class);
    self::assertInstanceOf($variablesClass, $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
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.