123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- <?php
-
- namespace Illuminate\Support\Testing\Fakes;
-
- use Illuminate\Support\Arr;
- use PHPUnit\Framework\Assert as PHPUnit;
- use Illuminate\Contracts\Events\Dispatcher;
-
- class EventFake implements Dispatcher
- {
- /**
- * The original event dispatcher.
- *
- * @var \Illuminate\Contracts\Events\Dispatcher
- */
- protected $dispatcher;
-
- /**
- * The event types that should be intercepted instead of dispatched.
- *
- * @var array
- */
- protected $eventsToFake;
-
- /**
- * All of the events that have been intercepted keyed by type.
- *
- * @var array
- */
- protected $events = [];
-
- /**
- * Create a new event fake instance.
- *
- * @param \Illuminate\Contracts\Events\Dispatcher $dispatcher
- * @param array|string $eventsToFake
- * @return void
- */
- public function __construct(Dispatcher $dispatcher, $eventsToFake = [])
- {
- $this->dispatcher = $dispatcher;
-
- $this->eventsToFake = Arr::wrap($eventsToFake);
- }
-
- /**
- * Assert if an event was dispatched based on a truth-test callback.
- *
- * @param string $event
- * @param callable|int|null $callback
- * @return void
- */
- public function assertDispatched($event, $callback = null)
- {
- if (is_int($callback)) {
- return $this->assertDispatchedTimes($event, $callback);
- }
-
- PHPUnit::assertTrue(
- $this->dispatched($event, $callback)->count() > 0,
- "The expected [{$event}] event was not dispatched."
- );
- }
-
- /**
- * Assert if a event was dispatched a number of times.
- *
- * @param string $event
- * @param int $times
- * @return void
- */
- public function assertDispatchedTimes($event, $times = 1)
- {
- PHPUnit::assertTrue(
- ($count = $this->dispatched($event)->count()) === $times,
- "The expected [{$event}] event was dispatched {$count} times instead of {$times} times."
- );
- }
-
- /**
- * Determine if an event was dispatched based on a truth-test callback.
- *
- * @param string $event
- * @param callable|null $callback
- * @return void
- */
- public function assertNotDispatched($event, $callback = null)
- {
- PHPUnit::assertTrue(
- $this->dispatched($event, $callback)->count() === 0,
- "The unexpected [{$event}] event was dispatched."
- );
- }
-
- /**
- * Get all of the events matching a truth-test callback.
- *
- * @param string $event
- * @param callable|null $callback
- * @return \Illuminate\Support\Collection
- */
- public function dispatched($event, $callback = null)
- {
- if (! $this->hasDispatched($event)) {
- return collect();
- }
-
- $callback = $callback ?: function () {
- return true;
- };
-
- return collect($this->events[$event])->filter(function ($arguments) use ($callback) {
- return $callback(...$arguments);
- });
- }
-
- /**
- * Determine if the given event has been dispatched.
- *
- * @param string $event
- * @return bool
- */
- public function hasDispatched($event)
- {
- return isset($this->events[$event]) && ! empty($this->events[$event]);
- }
-
- /**
- * Register an event listener with the dispatcher.
- *
- * @param string|array $events
- * @param mixed $listener
- * @return void
- */
- public function listen($events, $listener)
- {
- //
- }
-
- /**
- * Determine if a given event has listeners.
- *
- * @param string $eventName
- * @return bool
- */
- public function hasListeners($eventName)
- {
- //
- }
-
- /**
- * Register an event and payload to be dispatched later.
- *
- * @param string $event
- * @param array $payload
- * @return void
- */
- public function push($event, $payload = [])
- {
- //
- }
-
- /**
- * Register an event subscriber with the dispatcher.
- *
- * @param object|string $subscriber
- * @return void
- */
- public function subscribe($subscriber)
- {
- //
- }
-
- /**
- * Flush a set of pushed events.
- *
- * @param string $event
- * @return void
- */
- public function flush($event)
- {
- //
- }
-
- /**
- * Fire an event and call the listeners.
- *
- * @param string|object $event
- * @param mixed $payload
- * @param bool $halt
- * @return array|null
- */
- public function fire($event, $payload = [], $halt = false)
- {
- return $this->dispatch($event, $payload, $halt);
- }
-
- /**
- * Fire an event and call the listeners.
- *
- * @param string|object $event
- * @param mixed $payload
- * @param bool $halt
- * @return array|null
- */
- public function dispatch($event, $payload = [], $halt = false)
- {
- $name = is_object($event) ? get_class($event) : (string) $event;
-
- if ($this->shouldFakeEvent($name)) {
- $this->events[$name][] = func_get_args();
- } else {
- $this->dispatcher->dispatch($event, $payload, $halt);
- }
- }
-
- /**
- * Determine if an event should be faked or actually dispatched.
- *
- * @param string $eventName
- * @return bool
- */
- protected function shouldFakeEvent($eventName)
- {
- return empty($this->eventsToFake) || in_array($eventName, $this->eventsToFake);
- }
-
- /**
- * Remove a set of listeners from the dispatcher.
- *
- * @param string $event
- * @return void
- */
- public function forget($event)
- {
- //
- }
-
- /**
- * Forget all of the queued listeners.
- *
- * @return void
- */
- public function forgetPushed()
- {
- //
- }
-
- /**
- * Dispatch an event and call the listeners.
- *
- * @param string|object $event
- * @param mixed $payload
- * @return void
- */
- public function until($event, $payload = [])
- {
- return $this->dispatch($event, $payload, true);
- }
- }
|