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

QueueFake.php 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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)->contains(function ($job) {
  134. return ! is_object($job);
  135. });
  136. }
  137. /**
  138. * Determine if a job was pushed based on a truth-test callback.
  139. *
  140. * @param string $job
  141. * @param callable|null $callback
  142. * @return void
  143. */
  144. public function assertNotPushed($job, $callback = null)
  145. {
  146. PHPUnit::assertTrue(
  147. $this->pushed($job, $callback)->count() === 0,
  148. "The unexpected [{$job}] job was pushed."
  149. );
  150. }
  151. /**
  152. * Assert that no jobs were pushed.
  153. *
  154. * @return void
  155. */
  156. public function assertNothingPushed()
  157. {
  158. PHPUnit::assertEmpty($this->jobs, 'Jobs were pushed unexpectedly.');
  159. }
  160. /**
  161. * Get all of the jobs matching a truth-test callback.
  162. *
  163. * @param string $job
  164. * @param callable|null $callback
  165. * @return \Illuminate\Support\Collection
  166. */
  167. public function pushed($job, $callback = null)
  168. {
  169. if (! $this->hasPushed($job)) {
  170. return collect();
  171. }
  172. $callback = $callback ?: function () {
  173. return true;
  174. };
  175. return collect($this->jobs[$job])->filter(function ($data) use ($callback) {
  176. return $callback($data['job'], $data['queue']);
  177. })->pluck('job');
  178. }
  179. /**
  180. * Determine if there are any stored jobs for a given class.
  181. *
  182. * @param string $job
  183. * @return bool
  184. */
  185. public function hasPushed($job)
  186. {
  187. return isset($this->jobs[$job]) && ! empty($this->jobs[$job]);
  188. }
  189. /**
  190. * Resolve a queue connection instance.
  191. *
  192. * @param mixed $value
  193. * @return \Illuminate\Contracts\Queue\Queue
  194. */
  195. public function connection($value = null)
  196. {
  197. return $this;
  198. }
  199. /**
  200. * Get the size of the queue.
  201. *
  202. * @param string $queue
  203. * @return int
  204. */
  205. public function size($queue = null)
  206. {
  207. return count($this->jobs);
  208. }
  209. /**
  210. * Push a new job onto the queue.
  211. *
  212. * @param string $job
  213. * @param mixed $data
  214. * @param string $queue
  215. * @return mixed
  216. */
  217. public function push($job, $data = '', $queue = null)
  218. {
  219. $this->jobs[is_object($job) ? get_class($job) : $job][] = [
  220. 'job' => $job,
  221. 'queue' => $queue,
  222. ];
  223. }
  224. /**
  225. * Push a raw payload onto the queue.
  226. *
  227. * @param string $payload
  228. * @param string $queue
  229. * @param array $options
  230. * @return mixed
  231. */
  232. public function pushRaw($payload, $queue = null, array $options = [])
  233. {
  234. //
  235. }
  236. /**
  237. * Push a new job onto the queue after a delay.
  238. *
  239. * @param \DateTimeInterface|\DateInterval|int $delay
  240. * @param string $job
  241. * @param mixed $data
  242. * @param string $queue
  243. * @return mixed
  244. */
  245. public function later($delay, $job, $data = '', $queue = null)
  246. {
  247. return $this->push($job, $data, $queue);
  248. }
  249. /**
  250. * Push a new job onto the queue.
  251. *
  252. * @param string $queue
  253. * @param string $job
  254. * @param mixed $data
  255. * @return mixed
  256. */
  257. public function pushOn($queue, $job, $data = '')
  258. {
  259. return $this->push($job, $data, $queue);
  260. }
  261. /**
  262. * Push a new job onto the queue after a delay.
  263. *
  264. * @param string $queue
  265. * @param \DateTimeInterface|\DateInterval|int $delay
  266. * @param string $job
  267. * @param mixed $data
  268. * @return mixed
  269. */
  270. public function laterOn($queue, $delay, $job, $data = '')
  271. {
  272. return $this->push($job, $data, $queue);
  273. }
  274. /**
  275. * Pop the next job off of the queue.
  276. *
  277. * @param string $queue
  278. * @return \Illuminate\Contracts\Queue\Job|null
  279. */
  280. public function pop($queue = null)
  281. {
  282. //
  283. }
  284. /**
  285. * Push an array of jobs onto the queue.
  286. *
  287. * @param array $jobs
  288. * @param mixed $data
  289. * @param string $queue
  290. * @return mixed
  291. */
  292. public function bulk($jobs, $data = '', $queue = null)
  293. {
  294. foreach ($jobs as $job) {
  295. $this->push($job, $data, $queue);
  296. }
  297. }
  298. /**
  299. * Get the jobs that have been pushed.
  300. *
  301. * @return array
  302. */
  303. public function pushedJobs()
  304. {
  305. return $this->jobs;
  306. }
  307. /**
  308. * Get the connection name for the queue.
  309. *
  310. * @return string
  311. */
  312. public function getConnectionName()
  313. {
  314. //
  315. }
  316. /**
  317. * Set the connection name for the queue.
  318. *
  319. * @param string $name
  320. * @return $this
  321. */
  322. public function setConnectionName($name)
  323. {
  324. return $this;
  325. }
  326. }