Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

EventFake.php 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. namespace Illuminate\Support\Testing\Fakes;
  3. use Closure;
  4. use Illuminate\Support\Arr;
  5. use PHPUnit\Framework\Assert as PHPUnit;
  6. use Illuminate\Contracts\Events\Dispatcher;
  7. class EventFake implements Dispatcher
  8. {
  9. /**
  10. * The original event dispatcher.
  11. *
  12. * @var \Illuminate\Contracts\Events\Dispatcher
  13. */
  14. protected $dispatcher;
  15. /**
  16. * The event types that should be intercepted instead of dispatched.
  17. *
  18. * @var array
  19. */
  20. protected $eventsToFake;
  21. /**
  22. * All of the events that have been intercepted keyed by type.
  23. *
  24. * @var array
  25. */
  26. protected $events = [];
  27. /**
  28. * Create a new event fake instance.
  29. *
  30. * @param \Illuminate\Contracts\Events\Dispatcher $dispatcher
  31. * @param array|string $eventsToFake
  32. * @return void
  33. */
  34. public function __construct(Dispatcher $dispatcher, $eventsToFake = [])
  35. {
  36. $this->dispatcher = $dispatcher;
  37. $this->eventsToFake = Arr::wrap($eventsToFake);
  38. }
  39. /**
  40. * Assert if an event was dispatched based on a truth-test callback.
  41. *
  42. * @param string $event
  43. * @param callable|int|null $callback
  44. * @return void
  45. */
  46. public function assertDispatched($event, $callback = null)
  47. {
  48. if (is_int($callback)) {
  49. return $this->assertDispatchedTimes($event, $callback);
  50. }
  51. PHPUnit::assertTrue(
  52. $this->dispatched($event, $callback)->count() > 0,
  53. "The expected [{$event}] event was not dispatched."
  54. );
  55. }
  56. /**
  57. * Assert if a event was dispatched a number of times.
  58. *
  59. * @param string $event
  60. * @param int $times
  61. * @return void
  62. */
  63. public function assertDispatchedTimes($event, $times = 1)
  64. {
  65. PHPUnit::assertTrue(
  66. ($count = $this->dispatched($event)->count()) === $times,
  67. "The expected [{$event}] event was dispatched {$count} times instead of {$times} times."
  68. );
  69. }
  70. /**
  71. * Determine if an event was dispatched based on a truth-test callback.
  72. *
  73. * @param string $event
  74. * @param callable|null $callback
  75. * @return void
  76. */
  77. public function assertNotDispatched($event, $callback = null)
  78. {
  79. PHPUnit::assertTrue(
  80. $this->dispatched($event, $callback)->count() === 0,
  81. "The unexpected [{$event}] event was dispatched."
  82. );
  83. }
  84. /**
  85. * Get all of the events matching a truth-test callback.
  86. *
  87. * @param string $event
  88. * @param callable|null $callback
  89. * @return \Illuminate\Support\Collection
  90. */
  91. public function dispatched($event, $callback = null)
  92. {
  93. if (! $this->hasDispatched($event)) {
  94. return collect();
  95. }
  96. $callback = $callback ?: function () {
  97. return true;
  98. };
  99. return collect($this->events[$event])->filter(function ($arguments) use ($callback) {
  100. return $callback(...$arguments);
  101. });
  102. }
  103. /**
  104. * Determine if the given event has been dispatched.
  105. *
  106. * @param string $event
  107. * @return bool
  108. */
  109. public function hasDispatched($event)
  110. {
  111. return isset($this->events[$event]) && ! empty($this->events[$event]);
  112. }
  113. /**
  114. * Register an event listener with the dispatcher.
  115. *
  116. * @param string|array $events
  117. * @param mixed $listener
  118. * @return void
  119. */
  120. public function listen($events, $listener)
  121. {
  122. $this->dispatcher->listen($events, $listener);
  123. }
  124. /**
  125. * Determine if a given event has listeners.
  126. *
  127. * @param string $eventName
  128. * @return bool
  129. */
  130. public function hasListeners($eventName)
  131. {
  132. return $this->dispatcher->hasListeners($eventName);
  133. }
  134. /**
  135. * Register an event and payload to be dispatched later.
  136. *
  137. * @param string $event
  138. * @param array $payload
  139. * @return void
  140. */
  141. public function push($event, $payload = [])
  142. {
  143. //
  144. }
  145. /**
  146. * Register an event subscriber with the dispatcher.
  147. *
  148. * @param object|string $subscriber
  149. * @return void
  150. */
  151. public function subscribe($subscriber)
  152. {
  153. $this->dispatcher->subscribe($subscriber);
  154. }
  155. /**
  156. * Flush a set of pushed events.
  157. *
  158. * @param string $event
  159. * @return void
  160. */
  161. public function flush($event)
  162. {
  163. //
  164. }
  165. /**
  166. * Fire an event and call the listeners.
  167. *
  168. * @param string|object $event
  169. * @param mixed $payload
  170. * @param bool $halt
  171. * @return array|null
  172. */
  173. public function dispatch($event, $payload = [], $halt = false)
  174. {
  175. $name = is_object($event) ? get_class($event) : (string) $event;
  176. if ($this->shouldFakeEvent($name, $payload)) {
  177. $this->events[$name][] = func_get_args();
  178. } else {
  179. return $this->dispatcher->dispatch($event, $payload, $halt);
  180. }
  181. }
  182. /**
  183. * Determine if an event should be faked or actually dispatched.
  184. *
  185. * @param string $eventName
  186. * @param mixed $payload
  187. * @return bool
  188. */
  189. protected function shouldFakeEvent($eventName, $payload)
  190. {
  191. if (empty($this->eventsToFake)) {
  192. return true;
  193. }
  194. return collect($this->eventsToFake)
  195. ->filter(function ($event) use ($eventName, $payload) {
  196. return $event instanceof Closure
  197. ? $event($eventName, $payload)
  198. : $event === $eventName;
  199. })
  200. ->isNotEmpty();
  201. }
  202. /**
  203. * Remove a set of listeners from the dispatcher.
  204. *
  205. * @param string $event
  206. * @return void
  207. */
  208. public function forget($event)
  209. {
  210. //
  211. }
  212. /**
  213. * Forget all of the queued listeners.
  214. *
  215. * @return void
  216. */
  217. public function forgetPushed()
  218. {
  219. //
  220. }
  221. /**
  222. * Dispatch an event and call the listeners.
  223. *
  224. * @param string|object $event
  225. * @param mixed $payload
  226. * @return void
  227. */
  228. public function until($event, $payload = [])
  229. {
  230. return $this->dispatch($event, $payload, true);
  231. }
  232. }