HookEventDispatcherManagerSpy.php in Hook Event Dispatcher 8.2
File
tests/src/Unit/HookEventDispatcherManagerSpy.php
View source
<?php
namespace Drupal\Tests\hook_event_dispatcher\Unit;
use Drupal\hook_event_dispatcher\Event\EventInterface;
use Drupal\hook_event_dispatcher\Manager\HookEventDispatcherManagerInterface;
use InvalidArgumentException;
use Symfony\Component\EventDispatcher\Event;
class HookEventDispatcherManagerSpy implements HookEventDispatcherManagerInterface {
private $maxEventCount = 1;
private $eventCallbacks = [];
private $eventCount = 0;
private $registeredEvents = [];
public function register(EventInterface $event) : 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);
}
return $event;
}
public function setMaxEventCount($maxEventCount) : void {
$this->maxEventCount = $maxEventCount;
}
public function setEventCallbacks(array $eventCallbacks) : void {
$this->eventCallbacks = $eventCallbacks;
}
public function getRegisteredEvent($eventName) : EventInterface {
if (!isset($this->registeredEvents[$eventName])) {
throw new InvalidArgumentException("The event '{$eventName}' was not registered");
}
return $this->registeredEvents[$eventName];
}
}