QueueFake.php 6.1 KB

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