You are here

final class SpyEventDispatcher in Hook Event Dispatcher 8.2

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

Class SpyEventDispatcher.

Hierarchy

  • class \Drupal\Tests\preprocess_event_dispatcher\Unit\Helpers\SpyEventDispatcher implements \Symfony\Component\EventDispatcher\EventDispatcherInterface

Expanded class hierarchy of SpyEventDispatcher

3 files declare their use of SpyEventDispatcher
EntityEventTest.php in modules/preprocess_event_dispatcher/tests/src/Unit/EntityEventTest.php
OtherEventTest.php in modules/preprocess_event_dispatcher/tests/src/Unit/OtherEventTest.php
PreprocessEventPassTest.php in modules/preprocess_event_dispatcher/tests/src/Unit/PreprocessEventPassTest.php

File

modules/preprocess_event_dispatcher/tests/src/Unit/Helpers/SpyEventDispatcher.php, line 16

Namespace

Drupal\Tests\preprocess_event_dispatcher\Unit\Helpers
View source
final class SpyEventDispatcher implements EventDispatcherInterface {

  /**
   * Events keyed by event name.
   *
   * @var \Symfony\Component\EventDispatcher\Event[]
   */
  private $events = [];

  /**
   * Event count.
   *
   * @var int
   */
  private $count = 1;

  /**
   * Set the expected event count.
   *
   * @param int $count
   *   Event count.
   */
  public function setExpectedEventCount(int $count) : void {
    $this->count = $count;
  }

  /**
   * Mocking an event dispatch, saving the event.
   *
   * {@inheritdoc}
   */
  public function dispatch($eventName, Event $event = NULL) : void {
    if (count($this->events) === $this->count) {
      throw new BadMethodCallException("SpyEventDispatcher got called more then {$this->count} time(s)");
    }
    $this->events[$eventName] = $event;
  }

  /**
   * Get the last event name.
   *
   * @return string
   *   Last event name.
   */
  public function getLastEventName() : string {
    end($this->events);
    return key($this->events);
  }

  /**
   * Get the last event.
   *
   * @return \Symfony\Component\EventDispatcher\Event
   *   Last event.
   */
  public function getLastEvent() : Event {
    return end($this->events);
  }

  /**
   * Get the events keyed by event name.
   *
   * @return \Symfony\Component\EventDispatcher\Event[]
   *   Events keyed by event name.
   */
  public function getEvents() : array {
    return $this->events;
  }

  /**
   * Mock.
   *
   * @inheritdoc
   */
  public function addListener($eventName, $listener, $priority = 0) : void {
    throw new BadMethodCallException('This spy does not support this call');
  }

  /**
   * Mock.
   *
   * @inheritdoc
   */
  public function addSubscriber(EventSubscriberInterface $subscriber) : void {
    throw new BadMethodCallException('This spy does not support this call');
  }

  /**
   * Mock.
   *
   * @inheritdoc
   */
  public function removeListener($eventName, $listener) : void {
    throw new BadMethodCallException('This spy does not support this call');
  }

  /**
   * Mock.
   *
   * @inheritdoc
   */
  public function removeSubscriber(EventSubscriberInterface $subscriber) : void {
    throw new BadMethodCallException('This spy does not support this call');
  }

  /**
   * Mock.
   *
   * @inheritdoc
   */
  public function getListeners($eventName = NULL) : array {
    throw new BadMethodCallException('This spy does not support this call');
  }

  /**
   * Mock.
   *
   * @inheritdoc
   */
  public function getListenerPriority($eventName, $listener) : ?int {
    throw new BadMethodCallException('This spy does not support this call');
  }

  /**
   * Mock.
   *
   * @inheritdoc
   */
  public function hasListeners($eventName = NULL) : bool {
    throw new BadMethodCallException('This spy does not support this call');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SpyEventDispatcher::$count private property Event count.
SpyEventDispatcher::$events private property Events keyed by event name.
SpyEventDispatcher::addListener public function Mock.
SpyEventDispatcher::addSubscriber public function Mock.
SpyEventDispatcher::dispatch public function Mocking an event dispatch, saving the event.
SpyEventDispatcher::getEvents public function Get the events keyed by event name.
SpyEventDispatcher::getLastEvent public function Get the last event.
SpyEventDispatcher::getLastEventName public function Get the last event name.
SpyEventDispatcher::getListenerPriority public function Mock.
SpyEventDispatcher::getListeners public function Mock.
SpyEventDispatcher::hasListeners public function Mock.
SpyEventDispatcher::removeListener public function Mock.
SpyEventDispatcher::removeSubscriber public function Mock.
SpyEventDispatcher::setExpectedEventCount public function Set the expected event count.