SpyEventDispatcher.php in Hook Event Dispatcher 8
File
tests/src/Unit/Preprocess/Helpers/SpyEventDispatcher.php
View source
<?php
namespace Drupal\Tests\hook_event_dispatcher\Unit\Preprocess\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($count) {
$this->count = $count;
}
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;
}
public function getLastEventName() {
end($this->events);
return key($this->events);
}
public function getLastEvent() {
return end($this->events);
}
public function getEvents() {
return $this->events;
}
public function addListener($eventName, $listener, $priority = 0) {
throw new BadMethodCallException('This spy does not support this call');
}
public function addSubscriber(EventSubscriberInterface $subscriber) {
throw new BadMethodCallException('This spy does not support this call');
}
public function removeListener($eventName, $listener) {
throw new BadMethodCallException('This spy does not support this call');
}
public function removeSubscriber(EventSubscriberInterface $subscriber) {
throw new BadMethodCallException('This spy does not support this call');
}
public function getListeners($eventName = NULL) {
throw new BadMethodCallException('This spy does not support this call');
}
public function getListenerPriority($eventName, $listener) {
throw new BadMethodCallException('This spy does not support this call');
}
public function hasListeners($eventName = NULL) {
throw new BadMethodCallException('This spy does not support this call');
}
}