QueueFake.php 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. * Assert if a job was pushed with chained jobs based on a truth-test callback.
  64. *
  65. * @param string $job
  66. * @param array $expectedChain
  67. * @param callable|null $callback
  68. * @return void
  69. */
  70. public function assertPushedWithChain($job, $expectedChain = [], $callback = null)
  71. {
  72. PHPUnit::assertTrue(
  73. $this->pushed($job, $callback)->isNotEmpty(),
  74. "The expected [{$job}] job was not pushed."
  75. );
  76. PHPUnit::assertTrue(
  77. collect($expectedChain)->isNotEmpty(),
  78. 'The expected chain can not be empty.'
  79. );
  80. $this->isChainOfObjects($expectedChain)
  81. ? $this->assertPushedWithChainOfObjects($job, $expectedChain, $callback)
  82. : $this->assertPushedWithChainOfClasses($job, $expectedChain, $callback);
  83. }
  84. /**
  85. * Assert if a job was pushed with chained jobs based on a truth-test callback.
  86. *
  87. * @param string $job
  88. * @param array $expectedChain
  89. * @param callable|null $callback
  90. * @return void
  91. */
  92. protected function assertPushedWithChainOfObjects($job, $expectedChain, $callback)
  93. {
  94. $chain = collect($expectedChain)->map(function ($job) {
  95. return serialize($job);
  96. })->all();
  97. PHPUnit::assertTrue(
  98. $this->pushed($job, $callback)->filter(function ($job) use ($chain) {
  99. return $job->chained == $chain;
  100. })->isNotEmpty(),
  101. 'The expected chain was not pushed.'
  102. );
  103. }
  104. /**
  105. * Assert if a job was pushed with chained jobs based on a truth-test callback.
  106. *
  107. * @param string $job
  108. * @param array $expectedChain
  109. * @param callable|null $callback
  110. * @return void
  111. */
  112. protected function assertPushedWithChainOfClasses($job, $expectedChain, $callback)
  113. {
  114. $matching = $this->pushed($job, $callback)->map->chained->map(function ($chain) {
  115. return collect($chain)->map(function ($job) {
  116. return get_class(unserialize($job));
  117. });
  118. })->filter(function ($chain) use ($expectedChain) {
  119. return $chain->all() === $expectedChain;
  120. });
  121. PHPUnit::assertTrue(
  122. $matching->isNotEmpty(), 'The expected chain was not pushed.'
  123. );
  124. }
  125. /**
  126. * Determine if the given chain is entirely composed of objects.
  127. *
  128. * @param array $chain
  129. * @return bool
  130. */
  131. protected function isChainOfObjects($chain)
  132. {
  133. return collect($chain)->count() == collect($chain)
  134. ->filter(function ($job) {
  135. return is_object($job);
  136. })->count();
  137. }
  138. /**
  139. * Determine if a job was pushed based on a truth-test callback.
  140. *
  141. * @param string $job
  142. * @param callable|null $callback
  143. * @return void
  144. */
  145. public function assertNotPushed($job, $callback = null)
  146. {
  147. PHPUnit::assertTrue(
  148. $this->pushed($job, $callback)->count() === 0,
  149. "The unexpected [{$job}] job was pushed."
  150. );
  151. }
  152. /**
  153. * Assert that no jobs were pushed.
  154. *
  155. * @return void
  156. */
  157. public function assertNothingPushed()
  158. {
  159. PHPUnit::assertEmpty($this->jobs, 'Jobs were pushed unexpectedly.');
  160. }
  161. /**
  162. * Get all of the jobs matching a truth-test callback.
  163. *
  164. * @param string $job
  165. * @param callable|null $callback
  166. * @return \Illuminate\Support\Collection
  167. */
  168. public function pushed($job, $callback = null)
  169. {
  170. if (! $this->hasPushed($job)) {
  171. return collect();
  172. }
  173. $callback = $callback ?: function () {
  174. return true;
  175. };
  176. return collect($this->jobs[$job])->filter(function ($data) use ($callback) {
  177. return $callback($data['job'], $data['queue']);
  178. })->pluck('job');
  179. }
  180. /**
  181. * Determine if there are any stored jobs for a given class.
  182. *
  183. * @param string $job
  184. * @return bool
  185. */
  186. public function hasPushed($job)
  187. {
  188. return isset($this->jobs[$job]) && ! empty($this->jobs[$job]);
  189. }
  190. /**
  191. * Resolve a queue connection instance.
  192. *
  193. * @param mixed $value
  194. * @return \Illuminate\Contracts\Queue\Queue
  195. */
  196. public function connection($value = null)
  197. {
  198. return $this;
  199. }
  200. /**
  201. * Get the size of the queue.
  202. *
  203. * @param string $queue
  204. * @return int
  205. */
  206. public function size($queue = null)
  207. {
  208. return count($this->jobs);
  209. }
  210. /**
  211. * Push a new job onto the queue.
  212. *
  213. * @param string $job
  214. * @param mixed $data
  215. * @param string $queue
  216. * @return mixed
  217. */
  218. public function push($job, $data = '', $queue = null)
  219. {
  220. $this->jobs[is_object($job) ? get_class($job) : $job][] = [
  221. 'job' => $job,
  222. 'queue' => $queue,
  223. ];
  224. }
  225. /**
  226. * Push a raw payload onto the queue.
  227. *
  228. * @param string $payload
  229. * @param string $queue
  230. * @param array $options
  231. * @return mixed
  232. */
  233. public function pushRaw($payload, $queue = null, array $options = [])
  234. {
  235. //
  236. }
  237. /**
  238. * Push a new job onto the queue after a delay.
  239. *
  240. * @param \DateTime|int $delay
  241. * @param string $job
  242. * @param mixed $data
  243. * @param string $queue
  244. * @return mixed
  245. */
  246. public function later($delay, $job, $data = '', $queue = null)
  247. {
  248. return $this->push($job, $data, $queue);
  249. }
  250. /**
  251. * Push a new job onto the queue.
  252. *
  253. * @param string $queue
  254. * @param string $job
  255. * @param mixed $data
  256. * @return mixed
  257. */
  258. public function pushOn($queue, $job, $data = '')
  259. {
  260. return $this->push($job, $data, $queue);
  261. }
  262. /**
  263. * Push a new job onto the queue after a delay.
  264. *
  265. * @param string $queue
  266. * @param \DateTime|int $delay
  267. * @param string $job
  268. * @param mixed $data
  269. * @return mixed
  270. */
  271. public function laterOn($queue, $delay, $job, $data = '')
  272. {
  273. return $this->push($job, $data, $queue);
  274. }
  275. /**
  276. * Pop the next job off of the queue.
  277. *
  278. * @param string $queue
  279. * @return \Illuminate\Contracts\Queue\Job|null
  280. */
  281. public function pop($queue = null)
  282. {
  283. //
  284. }
  285. /**
  286. * Push an array of jobs onto the queue.
  287. *
  288. * @param array $jobs
  289. * @param mixed $data
  290. * @param string $queue
  291. * @return mixed
  292. */
  293. public function bulk($jobs, $data = '', $queue = null)
  294. {
  295. foreach ($jobs as $job) {
  296. $this->push($job, $data, $queue);
  297. }
  298. }
  299. /**
  300. * Get the connection name for the queue.
  301. *
  302. * @return string
  303. */
  304. public function getConnectionName()
  305. {
  306. //
  307. }
  308. /**
  309. * Set the connection name for the queue.
  310. *
  311. * @param string $name
  312. * @return $this
  313. */
  314. public function setConnectionName($name)
  315. {
  316. return $this;
  317. }
  318. }