EventFake.php 6.0KB

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