You are here

final class SpyEventDispatcher in Hook Event Dispatcher 8

Class SpyEventDispatcher.

Hierarchy

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

Expanded class hierarchy of SpyEventDispatcher

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

File

tests/src/Unit/Preprocess/Helpers/SpyEventDispatcher.php, line 16

Namespace

Drupal\Tests\hook_event_dispatcher\Unit\Preprocess\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($count) {
    $this->count = $count;
  }

  /**
   * Mocking an event dispatch, saving the event.
   *
   * {@inheritdoc}
   */
  public function dispatch($eventName, Event $event = NULL) {
    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() {
    end($this->events);
    return key($this->events);
  }

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

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

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

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

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

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

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

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

  /**
   * Mock.
   *
   * @inheritdoc
   */
  public function hasListeners($eventName = NULL) {
    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.