QueueFake.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. namespace Illuminate\Support\Testing\Fakes;
  3. // use Illuminate\Queue\QueueManager;
  4. use Illuminate\Contracts\Queue\Queue;
  5. use PHPUnit\Framework\Assert as PHPUnit;
  6. // class QueueFake extends QueueManager implements Queue
  7. class QueueFake implements Queue
  8. {
  9. /**
  10. * All of the jobs that have been pushed.
  11. *
  12. * @var array
  13. */
  14. protected $jobs = [];
  15. /**
  16. * Assert if a job was pushed based on a truth-test callback.
  17. *
  18. * @param string $job
  19. * @param callable|int|null $callback
  20. * @return void
  21. */
  22. public function assertPushed($job, $callback = null)
  23. {
  24. if (is_numeric($callback)) {
  25. return $this->assertPushedTimes($job, $callback);
  26. }
  27. PHPUnit::assertTrue(
  28. $this->pushed($job, $callback)->count() > 0,
  29. "The expected [{$job}] job was not pushed."
  30. );
  31. }
  32. /**
  33. * Assert if a job was pushed a number of times.
  34. *
  35. * @param string $job
  36. * @param int $times
  37. * @return void
  38. */
  39. protected function assertPushedTimes($job, $times = 1)
  40. {
  41. PHPUnit::assertTrue(
  42. ($count = $this->pushed($job)->count()) === $times,
  43. "The expected [{$job}] job was pushed {$count} times instead of {$times} times."
  44. );
  45. }
  46. /**
  47. * Assert if a job was pushed based on a truth-test callback.
  48. *
  49. * @param string $queue
  50. * @param string $job
  51. * @param callable|null $callback
  52. * @return void
  53. */
  54. public function assertPushedOn($queue, $job, $callback = null)
  55. {
  56. return $this->assertPushed($job, function ($job, $pushedQueue) use ($callback, $queue) {
  57. if ($pushedQueue !== $queue) {
  58. return false;
  59. }
  60. return $callback ? $callback(...func_get_args()) : true;
  61. });
  62. }
  63. /**
  64. * Determine if a job was pushed based on a truth-test callback.
  65. *
  66. * @param string $job
  67. * @param callable|null $callback
  68. * @return void
  69. */
  70. public function assertNotPushed($job, $callback = null)
  71. {
  72. PHPUnit::assertTrue(
  73. $this->pushed($job, $callback)->count() === 0,
  74. "The unexpected [{$job}] job was pushed."
  75. );
  76. }
  77. /**
  78. * Assert that no jobs were pushed.
  79. *
  80. * @return void
  81. */
  82. public function assertNothingPushed()
  83. {
  84. PHPUnit::assertEmpty($this->jobs, 'Jobs were pushed unexpectedly.');
  85. }
  86. /**
  87. * Get all of the jobs matching a truth-test callback.
  88. *
  89. * @param string $job
  90. * @param callable|null $callback
  91. * @return \Illuminate\Support\Collection
  92. */
  93. public function pushed($job, $callback = null)
  94. {
  95. if (! $this->hasPushed($job)) {
  96. return collect();
  97. }
  98. $callback = $callback ?: function () {
  99. return true;
  100. };
  101. return collect($this->jobs[$job])->filter(function ($data) use ($callback) {
  102. return $callback($data['job'], $data['queue']);
  103. })->pluck('job');
  104. }
  105. /**
  106. * Determine if there are any stored jobs for a given class.
  107. *
  108. * @param string $job
  109. * @return bool
  110. */
  111. public function hasPushed($job)
  112. {
  113. return isset($this->jobs[$job]) && ! empty($this->jobs[$job]);
  114. }
  115. /**
  116. * Resolve a queue connection instance.
  117. *
  118. * @param mixed $value
  119. * @return \Illuminate\Contracts\Queue\Queue
  120. */
  121. public function connection($value = null)
  122. {
  123. return $this;
  124. }
  125. /**
  126. * Get the size of the queue.
  127. *
  128. * @param string $queue
  129. * @return int
  130. */
  131. public function size($queue = null)
  132. {
  133. return 0;
  134. }
  135. /**
  136. * Push a new job onto the queue.
  137. *
  138. * @param string $job
  139. * @param mixed $data
  140. * @param string $queue
  141. * @return mixed
  142. */
  143. public function push($job, $data = '', $queue = null)
  144. {
  145. $this->jobs[is_object($job) ? get_class($job) : $job][] = [
  146. 'job' => $job,
  147. 'queue' => $queue,
  148. ];
  149. }
  150. /**
  151. * Push a raw payload onto the queue.
  152. *
  153. * @param string $payload
  154. * @param string $queue
  155. * @param array $options
  156. * @return mixed
  157. */
  158. public function pushRaw($payload, $queue = null, array $options = [])
  159. {
  160. //
  161. }
  162. /**
  163. * Push a new job onto the queue after a delay.
  164. *
  165. * @param \DateTime|int $delay
  166. * @param string $job
  167. * @param mixed $data
  168. * @param string $queue
  169. * @return mixed
  170. */
  171. public function later($delay, $job, $data = '', $queue = null)
  172. {
  173. return $this->push($job, $data, $queue);
  174. }
  175. /**
  176. * Push a new job onto the queue.
  177. *
  178. * @param string $queue
  179. * @param string $job
  180. * @param mixed $data
  181. * @return mixed
  182. */
  183. public function pushOn($queue, $job, $data = '')
  184. {
  185. return $this->push($job, $data, $queue);
  186. }
  187. /**
  188. * Push a new job onto the queue after a delay.
  189. *
  190. * @param string $queue
  191. * @param \DateTime|int $delay
  192. * @param string $job
  193. * @param mixed $data
  194. * @return mixed
  195. */
  196. public function laterOn($queue, $delay, $job, $data = '')
  197. {
  198. return $this->push($job, $data, $queue);
  199. }
  200. /**
  201. * Pop the next job off of the queue.
  202. *
  203. * @param string $queue
  204. * @return \Illuminate\Contracts\Queue\Job|null
  205. */
  206. public function pop($queue = null)
  207. {
  208. //
  209. }
  210. /**
  211. * Push an array of jobs onto the queue.
  212. *
  213. * @param array $jobs
  214. * @param mixed $data
  215. * @param string $queue
  216. * @return mixed
  217. */
  218. public function bulk($jobs, $data = '', $queue = null)
  219. {
  220. foreach ($jobs as $job) {
  221. $this->push($job, $data, $queue);
  222. }
  223. }
  224. /**
  225. * Get the connection name for the queue.
  226. *
  227. * @return string
  228. */
  229. public function getConnectionName()
  230. {
  231. //
  232. }
  233. /**
  234. * Set the connection name for the queue.
  235. *
  236. * @param string $name
  237. * @return $this
  238. */
  239. public function setConnectionName($name)
  240. {
  241. return $this;
  242. }
  243. }