SpyEventDispatcher.php in Hook Event Dispatcher 3.x
File
modules/preprocess_event_dispatcher/tests/src/Unit/Helpers/SpyEventDispatcher.php
View source
<?php
namespace Drupal\Tests\preprocess_event_dispatcher\Unit\Helpers;
use BadMethodCallException;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use function count;
use function end;
use function key;
final class SpyEventDispatcher implements EventDispatcherInterface {
private $events = [];
private $count = 1;
public function setExpectedEventCount(int $count) : void {
$this->count = $count;
}
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;
}
public function getLastEventName() : string {
end($this->events);
return key($this->events);
}
public function getLastEvent() : Event {
return end($this->events);
}
public function getEvents() : array {
return $this->events;
}
public function addListener($eventName, $listener, $priority = 0) : void {
throw new BadMethodCallException('This spy does not support this call');
}
public function addSubscriber(EventSubscriberInterface $subscriber) : void {
throw new BadMethodCallException('This spy does not support this call');
}
public function removeListener($eventName, $listener) : void {
throw new BadMethodCallException('This spy does not support this call');
}
public function removeSubscriber(EventSubscriberInterface $subscriber) : void {
throw new BadMethodCallException('This spy does not support this call');
}
public function getListeners($eventName = NULL) : array {
throw new BadMethodCallException('This spy does not support this call');
}
public function getListenerPriority($eventName, $listener) : ?int {
throw new BadMethodCallException('This spy does not support this call');
}
public function hasListeners($eventName = NULL) : bool {
throw new BadMethodCallException('This spy does not support this call');
}
}