PageRenderTime 22ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Support/Testing/Fakes/QueueFake.php

https://gitlab.com/jjpa2018/dashboard
PHP | 414 lines | 187 code | 42 blank | 185 comment | 8 complexity | 1cbae27cb9078592dc7125da42992f25 MD5 | raw file
  1. <?php
  2. namespace Illuminate\Support\Testing\Fakes;
  3. use BadMethodCallException;
  4. use Closure;
  5. use Illuminate\Contracts\Queue\Queue;
  6. use Illuminate\Queue\QueueManager;
  7. use Illuminate\Support\Traits\ReflectsClosures;
  8. use PHPUnit\Framework\Assert as PHPUnit;
  9. class QueueFake extends QueueManager implements Queue
  10. {
  11. use ReflectsClosures;
  12. /**
  13. * All of the jobs that have been pushed.
  14. *
  15. * @var array
  16. */
  17. protected $jobs = [];
  18. /**
  19. * Assert if a job was pushed based on a truth-test callback.
  20. *
  21. * @param string|\Closure $job
  22. * @param callable|int|null $callback
  23. * @return void
  24. */
  25. public function assertPushed($job, $callback = null)
  26. {
  27. if ($job instanceof Closure) {
  28. [$job, $callback] = [$this->firstClosureParameterType($job), $job];
  29. }
  30. if (is_numeric($callback)) {
  31. return $this->assertPushedTimes($job, $callback);
  32. }
  33. PHPUnit::assertTrue(
  34. $this->pushed($job, $callback)->count() > 0,
  35. "The expected [{$job}] job was not pushed."
  36. );
  37. }
  38. /**
  39. * Assert if a job was pushed a number of times.
  40. *
  41. * @param string $job
  42. * @param int $times
  43. * @return void
  44. */
  45. protected function assertPushedTimes($job, $times = 1)
  46. {
  47. $count = $this->pushed($job)->count();
  48. PHPUnit::assertSame(
  49. $times, $count,
  50. "The expected [{$job}] job was pushed {$count} times instead of {$times} times."
  51. );
  52. }
  53. /**
  54. * Assert if a job was pushed based on a truth-test callback.
  55. *
  56. * @param string $queue
  57. * @param string|\Closure $job
  58. * @param callable|null $callback
  59. * @return void
  60. */
  61. public function assertPushedOn($queue, $job, $callback = null)
  62. {
  63. if ($job instanceof Closure) {
  64. [$job, $callback] = [$this->firstClosureParameterType($job), $job];
  65. }
  66. $this->assertPushed($job, function ($job, $pushedQueue) use ($callback, $queue) {
  67. if ($pushedQueue !== $queue) {
  68. return false;
  69. }
  70. return $callback ? $callback(...func_get_args()) : true;
  71. });
  72. }
  73. /**
  74. * Assert if a job was pushed with chained jobs based on a truth-test callback.
  75. *
  76. * @param string $job
  77. * @param array $expectedChain
  78. * @param callable|null $callback
  79. * @return void
  80. */
  81. public function assertPushedWithChain($job, $expectedChain = [], $callback = null)
  82. {
  83. PHPUnit::assertTrue(
  84. $this->pushed($job, $callback)->isNotEmpty(),
  85. "The expected [{$job}] job was not pushed."
  86. );
  87. PHPUnit::assertTrue(
  88. collect($expectedChain)->isNotEmpty(),
  89. 'The expected chain can not be empty.'
  90. );
  91. $this->isChainOfObjects($expectedChain)
  92. ? $this->assertPushedWithChainOfObjects($job, $expectedChain, $callback)
  93. : $this->assertPushedWithChainOfClasses($job, $expectedChain, $callback);
  94. }
  95. /**
  96. * Assert if a job was pushed with an empty chain based on a truth-test callback.
  97. *
  98. * @param string $job
  99. * @param callable|null $callback
  100. * @return void
  101. */
  102. public function assertPushedWithoutChain($job, $callback = null)
  103. {
  104. PHPUnit::assertTrue(
  105. $this->pushed($job, $callback)->isNotEmpty(),
  106. "The expected [{$job}] job was not pushed."
  107. );
  108. $this->assertPushedWithChainOfClasses($job, [], $callback);
  109. }
  110. /**
  111. * Assert if a job was pushed with chained jobs based on a truth-test callback.
  112. *
  113. * @param string $job
  114. * @param array $expectedChain
  115. * @param callable|null $callback
  116. * @return void
  117. */
  118. protected function assertPushedWithChainOfObjects($job, $expectedChain, $callback)
  119. {
  120. $chain = collect($expectedChain)->map(function ($job) {
  121. return serialize($job);
  122. })->all();
  123. PHPUnit::assertTrue(
  124. $this->pushed($job, $callback)->filter(function ($job) use ($chain) {
  125. return $job->chained == $chain;
  126. })->isNotEmpty(),
  127. 'The expected chain was not pushed.'
  128. );
  129. }
  130. /**
  131. * Assert if a job was pushed with chained jobs based on a truth-test callback.
  132. *
  133. * @param string $job
  134. * @param array $expectedChain
  135. * @param callable|null $callback
  136. * @return void
  137. */
  138. protected function assertPushedWithChainOfClasses($job, $expectedChain, $callback)
  139. {
  140. $matching = $this->pushed($job, $callback)->map->chained->map(function ($chain) {
  141. return collect($chain)->map(function ($job) {
  142. return get_class(unserialize($job));
  143. });
  144. })->filter(function ($chain) use ($expectedChain) {
  145. return $chain->all() === $expectedChain;
  146. });
  147. PHPUnit::assertTrue(
  148. $matching->isNotEmpty(), 'The expected chain was not pushed.'
  149. );
  150. }
  151. /**
  152. * Determine if the given chain is entirely composed of objects.
  153. *
  154. * @param array $chain
  155. * @return bool
  156. */
  157. protected function isChainOfObjects($chain)
  158. {
  159. return ! collect($chain)->contains(function ($job) {
  160. return ! is_object($job);
  161. });
  162. }
  163. /**
  164. * Determine if a job was pushed based on a truth-test callback.
  165. *
  166. * @param string|\Closure $job
  167. * @param callable|null $callback
  168. * @return void
  169. */
  170. public function assertNotPushed($job, $callback = null)
  171. {
  172. if ($job instanceof Closure) {
  173. [$job, $callback] = [$this->firstClosureParameterType($job), $job];
  174. }
  175. PHPUnit::assertCount(
  176. 0, $this->pushed($job, $callback),
  177. "The unexpected [{$job}] job was pushed."
  178. );
  179. }
  180. /**
  181. * Assert that no jobs were pushed.
  182. *
  183. * @return void
  184. */
  185. public function assertNothingPushed()
  186. {
  187. PHPUnit::assertEmpty($this->jobs, 'Jobs were pushed unexpectedly.');
  188. }
  189. /**
  190. * Get all of the jobs matching a truth-test callback.
  191. *
  192. * @param string $job
  193. * @param callable|null $callback
  194. * @return \Illuminate\Support\Collection
  195. */
  196. public function pushed($job, $callback = null)
  197. {
  198. if (! $this->hasPushed($job)) {
  199. return collect();
  200. }
  201. $callback = $callback ?: function () {
  202. return true;
  203. };
  204. return collect($this->jobs[$job])->filter(function ($data) use ($callback) {
  205. return $callback($data['job'], $data['queue']);
  206. })->pluck('job');
  207. }
  208. /**
  209. * Determine if there are any stored jobs for a given class.
  210. *
  211. * @param string $job
  212. * @return bool
  213. */
  214. public function hasPushed($job)
  215. {
  216. return isset($this->jobs[$job]) && ! empty($this->jobs[$job]);
  217. }
  218. /**
  219. * Resolve a queue connection instance.
  220. *
  221. * @param mixed $value
  222. * @return \Illuminate\Contracts\Queue\Queue
  223. */
  224. public function connection($value = null)
  225. {
  226. return $this;
  227. }
  228. /**
  229. * Get the size of the queue.
  230. *
  231. * @param string|null $queue
  232. * @return int
  233. */
  234. public function size($queue = null)
  235. {
  236. return collect($this->jobs)->flatten(1)->filter(function ($job) use ($queue) {
  237. return $job['queue'] === $queue;
  238. })->count();
  239. }
  240. /**
  241. * Push a new job onto the queue.
  242. *
  243. * @param string $job
  244. * @param mixed $data
  245. * @param string|null $queue
  246. * @return mixed
  247. */
  248. public function push($job, $data = '', $queue = null)
  249. {
  250. $this->jobs[is_object($job) ? get_class($job) : $job][] = [
  251. 'job' => $job,
  252. 'queue' => $queue,
  253. ];
  254. }
  255. /**
  256. * Push a raw payload onto the queue.
  257. *
  258. * @param string $payload
  259. * @param string|null $queue
  260. * @param array $options
  261. * @return mixed
  262. */
  263. public function pushRaw($payload, $queue = null, array $options = [])
  264. {
  265. //
  266. }
  267. /**
  268. * Push a new job onto the queue after a delay.
  269. *
  270. * @param \DateTimeInterface|\DateInterval|int $delay
  271. * @param string $job
  272. * @param mixed $data
  273. * @param string|null $queue
  274. * @return mixed
  275. */
  276. public function later($delay, $job, $data = '', $queue = null)
  277. {
  278. return $this->push($job, $data, $queue);
  279. }
  280. /**
  281. * Push a new job onto the queue.
  282. *
  283. * @param string $queue
  284. * @param string $job
  285. * @param mixed $data
  286. * @return mixed
  287. */
  288. public function pushOn($queue, $job, $data = '')
  289. {
  290. return $this->push($job, $data, $queue);
  291. }
  292. /**
  293. * Push a new job onto the queue after a delay.
  294. *
  295. * @param string $queue
  296. * @param \DateTimeInterface|\DateInterval|int $delay
  297. * @param string $job
  298. * @param mixed $data
  299. * @return mixed
  300. */
  301. public function laterOn($queue, $delay, $job, $data = '')
  302. {
  303. return $this->push($job, $data, $queue);
  304. }
  305. /**
  306. * Pop the next job off of the queue.
  307. *
  308. * @param string|null $queue
  309. * @return \Illuminate\Contracts\Queue\Job|null
  310. */
  311. public function pop($queue = null)
  312. {
  313. //
  314. }
  315. /**
  316. * Push an array of jobs onto the queue.
  317. *
  318. * @param array $jobs
  319. * @param mixed $data
  320. * @param string|null $queue
  321. * @return mixed
  322. */
  323. public function bulk($jobs, $data = '', $queue = null)
  324. {
  325. foreach ($jobs as $job) {
  326. $this->push($job, $data, $queue);
  327. }
  328. }
  329. /**
  330. * Get the jobs that have been pushed.
  331. *
  332. * @return array
  333. */
  334. public function pushedJobs()
  335. {
  336. return $this->jobs;
  337. }
  338. /**
  339. * Get the connection name for the queue.
  340. *
  341. * @return string
  342. */
  343. public function getConnectionName()
  344. {
  345. //
  346. }
  347. /**
  348. * Set the connection name for the queue.
  349. *
  350. * @param string $name
  351. * @return $this
  352. */
  353. public function setConnectionName($name)
  354. {
  355. return $this;
  356. }
  357. /**
  358. * Override the QueueManager to prevent circular dependency.
  359. *
  360. * @param string $method
  361. * @param array $parameters
  362. * @return mixed
  363. *
  364. * @throws \BadMethodCallException
  365. */
  366. public function __call($method, $parameters)
  367. {
  368. throw new BadMethodCallException(sprintf(
  369. 'Call to undefined method %s::%s()', static::class, $method
  370. ));
  371. }
  372. }