You are here

class HookEventDispatcherManagerSpy in Hook Event Dispatcher 8

Same name and namespace in other branches
  1. 8.2 tests/src/Unit/HookEventDispatcherManagerSpy.php \Drupal\Tests\hook_event_dispatcher\Unit\HookEventDispatcherManagerSpy
  2. 3.x tests/src/Unit/HookEventDispatcherManagerSpy.php \Drupal\Tests\hook_event_dispatcher\Unit\HookEventDispatcherManagerSpy

Class HookEventDispatcherManagerSpy.

@package Drupal\Tests\hook_event_dispatcher\Unit

Hierarchy

Expanded class hierarchy of HookEventDispatcherManagerSpy

32 files declare their use of HookEventDispatcherManagerSpy
BlockEventTest.php in tests/src/Unit/Block/BlockEventTest.php
CronEventTest.php in tests/src/Unit/Cron/CronEventTest.php
EntityAccessEventTest.php in tests/src/Unit/Entity/EntityAccessEventTest.php
EntityBaseFieldInfoAlterEventTest.php in tests/src/Unit/EntityType/EntityBaseFieldInfoAlterEventTest.php
EntityBundleFieldInfoAlterEventTest.php in tests/src/Unit/EntityType/EntityBundleFieldInfoAlterEventTest.php

... See full list

File

tests/src/Unit/HookEventDispatcherManagerSpy.php, line 14

Namespace

Drupal\Tests\hook_event_dispatcher\Unit
View source
class HookEventDispatcherManagerSpy implements HookEventDispatcherManagerInterface {

  /**
   * The maximum amount of events that should register.
   *
   * @var int
   */
  private $maxEventCount = 1;

  /**
   * Event callbacks.
   *
   * @var array
   */
  private $eventCallbacks = [];

  /**
   * The amount of event registered.
   *
   * @var int
   */
  private $eventCount = 0;

  /**
   * Registered events.
   *
   * @var \Drupal\hook_event_dispatcher\Event\EventInterface[]
   */
  private $registeredEvents = [];

  /**
   * {@inheritdoc}
   *
   * @throws \Drupal\Tests\hook_event_dispatcher\Unit\TooManyEventsException
   */
  public function register(EventInterface $event) {
    $this->eventCount++;
    if ($this->eventCount > $this->maxEventCount) {
      throw new TooManyEventsException("Got {$this->eventCount} events, but only {$this->maxEventCount} are allowed");
    }
    $type = $event
      ->getDispatcherType();
    $this->registeredEvents[$type] = $event;
    if (isset($this->eventCallbacks[$type])) {
      $this->eventCallbacks[$type]($event);
    }
  }

  /**
   * Set max event count.
   *
   * @param int $maxEventCount
   *   Event count.
   */
  public function setMaxEventCount($maxEventCount) {
    $this->maxEventCount = $maxEventCount;
  }

  /**
   * Set event callbacks.
   *
   * @param array $eventCallbacks
   *   Associative event callbacks array.
   */
  public function setEventCallbacks(array $eventCallbacks) {
    $this->eventCallbacks = $eventCallbacks;
  }

  /**
   * Getter.
   *
   * @param string $eventName
   *   Event name.
   *
   * @return \Drupal\hook_event_dispatcher\Event\EventInterface
   *   Registered event by name.
   */
  public function getRegisteredEvent($eventName) {
    if (!isset($this->registeredEvents[$eventName])) {
      throw new InvalidArgumentException("The event '{$eventName}' was not registered");
    }
    return $this->registeredEvents[$eventName];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
HookEventDispatcherManagerSpy::$eventCallbacks private property Event callbacks.
HookEventDispatcherManagerSpy::$eventCount private property The amount of event registered.
HookEventDispatcherManagerSpy::$maxEventCount private property The maximum amount of events that should register.
HookEventDispatcherManagerSpy::$registeredEvents private property Registered events.
HookEventDispatcherManagerSpy::getRegisteredEvent public function Getter.
HookEventDispatcherManagerSpy::register public function Overrides HookEventDispatcherManagerInterface::register
HookEventDispatcherManagerSpy::setEventCallbacks public function Set event callbacks.
HookEventDispatcherManagerSpy::setMaxEventCount public function Set max event count.