BusFake.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. <?php
  2. namespace Illuminate\Support\Testing\Fakes;
  3. use Closure;
  4. use Illuminate\Bus\BatchRepository;
  5. use Illuminate\Bus\ChainedBatch;
  6. use Illuminate\Bus\PendingBatch;
  7. use Illuminate\Contracts\Bus\QueueingDispatcher;
  8. use Illuminate\Support\Arr;
  9. use Illuminate\Support\Collection;
  10. use Illuminate\Support\Traits\ReflectsClosures;
  11. use PHPUnit\Framework\Assert as PHPUnit;
  12. use RuntimeException;
  13. class BusFake implements Fake, QueueingDispatcher
  14. {
  15. use ReflectsClosures;
  16. /**
  17. * The original Bus dispatcher implementation.
  18. *
  19. * @var \Illuminate\Contracts\Bus\QueueingDispatcher
  20. */
  21. public $dispatcher;
  22. /**
  23. * The job types that should be intercepted instead of dispatched.
  24. *
  25. * @var array
  26. */
  27. protected $jobsToFake = [];
  28. /**
  29. * The job types that should be dispatched instead of faked.
  30. *
  31. * @var array
  32. */
  33. protected $jobsToDispatch = [];
  34. /**
  35. * The fake repository to track batched jobs.
  36. *
  37. * @var \Illuminate\Bus\BatchRepository
  38. */
  39. protected $batchRepository;
  40. /**
  41. * The commands that have been dispatched.
  42. *
  43. * @var array
  44. */
  45. protected $commands = [];
  46. /**
  47. * The commands that have been dispatched synchronously.
  48. *
  49. * @var array
  50. */
  51. protected $commandsSync = [];
  52. /**
  53. * The commands that have been dispatched after the response has been sent.
  54. *
  55. * @var array
  56. */
  57. protected $commandsAfterResponse = [];
  58. /**
  59. * The batches that have been dispatched.
  60. *
  61. * @var array
  62. */
  63. protected $batches = [];
  64. /**
  65. * Indicates if commands should be serialized and restored when pushed to the Bus.
  66. *
  67. * @var bool
  68. */
  69. protected bool $serializeAndRestore = false;
  70. /**
  71. * Create a new bus fake instance.
  72. *
  73. * @param \Illuminate\Contracts\Bus\QueueingDispatcher $dispatcher
  74. * @param array|string $jobsToFake
  75. * @param \Illuminate\Bus\BatchRepository|null $batchRepository
  76. * @return void
  77. */
  78. public function __construct(QueueingDispatcher $dispatcher, $jobsToFake = [], ?BatchRepository $batchRepository = null)
  79. {
  80. $this->dispatcher = $dispatcher;
  81. $this->jobsToFake = Arr::wrap($jobsToFake);
  82. $this->batchRepository = $batchRepository ?: new BatchRepositoryFake;
  83. }
  84. /**
  85. * Specify the jobs that should be dispatched instead of faked.
  86. *
  87. * @param array|string $jobsToDispatch
  88. * @return $this
  89. */
  90. public function except($jobsToDispatch)
  91. {
  92. $this->jobsToDispatch = array_merge($this->jobsToDispatch, Arr::wrap($jobsToDispatch));
  93. return $this;
  94. }
  95. /**
  96. * Assert if a job was dispatched based on a truth-test callback.
  97. *
  98. * @param string|\Closure $command
  99. * @param callable|int|null $callback
  100. * @return void
  101. */
  102. public function assertDispatched($command, $callback = null)
  103. {
  104. if ($command instanceof Closure) {
  105. [$command, $callback] = [$this->firstClosureParameterType($command), $command];
  106. }
  107. if (is_numeric($callback)) {
  108. return $this->assertDispatchedTimes($command, $callback);
  109. }
  110. PHPUnit::assertTrue(
  111. $this->dispatched($command, $callback)->count() > 0 ||
  112. $this->dispatchedAfterResponse($command, $callback)->count() > 0 ||
  113. $this->dispatchedSync($command, $callback)->count() > 0,
  114. "The expected [{$command}] job was not dispatched."
  115. );
  116. }
  117. /**
  118. * Assert if a job was pushed a number of times.
  119. *
  120. * @param string|\Closure $command
  121. * @param int $times
  122. * @return void
  123. */
  124. public function assertDispatchedTimes($command, $times = 1)
  125. {
  126. $callback = null;
  127. if ($command instanceof Closure) {
  128. [$command, $callback] = [$this->firstClosureParameterType($command), $command];
  129. }
  130. $count = $this->dispatched($command, $callback)->count() +
  131. $this->dispatchedAfterResponse($command, $callback)->count() +
  132. $this->dispatchedSync($command, $callback)->count();
  133. PHPUnit::assertSame(
  134. $times, $count,
  135. "The expected [{$command}] job was pushed {$count} times instead of {$times} times."
  136. );
  137. }
  138. /**
  139. * Determine if a job was dispatched based on a truth-test callback.
  140. *
  141. * @param string|\Closure $command
  142. * @param callable|null $callback
  143. * @return void
  144. */
  145. public function assertNotDispatched($command, $callback = null)
  146. {
  147. if ($command instanceof Closure) {
  148. [$command, $callback] = [$this->firstClosureParameterType($command), $command];
  149. }
  150. PHPUnit::assertTrue(
  151. $this->dispatched($command, $callback)->count() === 0 &&
  152. $this->dispatchedAfterResponse($command, $callback)->count() === 0 &&
  153. $this->dispatchedSync($command, $callback)->count() === 0,
  154. "The unexpected [{$command}] job was dispatched."
  155. );
  156. }
  157. /**
  158. * Assert that no jobs were dispatched.
  159. *
  160. * @return void
  161. */
  162. public function assertNothingDispatched()
  163. {
  164. PHPUnit::assertEmpty($this->commands, 'Jobs were dispatched unexpectedly.');
  165. }
  166. /**
  167. * Assert if a job was explicitly dispatched synchronously based on a truth-test callback.
  168. *
  169. * @param string|\Closure $command
  170. * @param callable|int|null $callback
  171. * @return void
  172. */
  173. public function assertDispatchedSync($command, $callback = null)
  174. {
  175. if ($command instanceof Closure) {
  176. [$command, $callback] = [$this->firstClosureParameterType($command), $command];
  177. }
  178. if (is_numeric($callback)) {
  179. return $this->assertDispatchedSyncTimes($command, $callback);
  180. }
  181. PHPUnit::assertTrue(
  182. $this->dispatchedSync($command, $callback)->count() > 0,
  183. "The expected [{$command}] job was not dispatched synchronously."
  184. );
  185. }
  186. /**
  187. * Assert if a job was pushed synchronously a number of times.
  188. *
  189. * @param string|\Closure $command
  190. * @param int $times
  191. * @return void
  192. */
  193. public function assertDispatchedSyncTimes($command, $times = 1)
  194. {
  195. $callback = null;
  196. if ($command instanceof Closure) {
  197. [$command, $callback] = [$this->firstClosureParameterType($command), $command];
  198. }
  199. $count = $this->dispatchedSync($command, $callback)->count();
  200. PHPUnit::assertSame(
  201. $times, $count,
  202. "The expected [{$command}] job was synchronously pushed {$count} times instead of {$times} times."
  203. );
  204. }
  205. /**
  206. * Determine if a job was dispatched based on a truth-test callback.
  207. *
  208. * @param string|\Closure $command
  209. * @param callable|null $callback
  210. * @return void
  211. */
  212. public function assertNotDispatchedSync($command, $callback = null)
  213. {
  214. if ($command instanceof Closure) {
  215. [$command, $callback] = [$this->firstClosureParameterType($command), $command];
  216. }
  217. PHPUnit::assertCount(
  218. 0, $this->dispatchedSync($command, $callback),
  219. "The unexpected [{$command}] job was dispatched synchronously."
  220. );
  221. }
  222. /**
  223. * Assert if a job was dispatched after the response was sent based on a truth-test callback.
  224. *
  225. * @param string|\Closure $command
  226. * @param callable|int|null $callback
  227. * @return void
  228. */
  229. public function assertDispatchedAfterResponse($command, $callback = null)
  230. {
  231. if ($command instanceof Closure) {
  232. [$command, $callback] = [$this->firstClosureParameterType($command), $command];
  233. }
  234. if (is_numeric($callback)) {
  235. return $this->assertDispatchedAfterResponseTimes($command, $callback);
  236. }
  237. PHPUnit::assertTrue(
  238. $this->dispatchedAfterResponse($command, $callback)->count() > 0,
  239. "The expected [{$command}] job was not dispatched after sending the response."
  240. );
  241. }
  242. /**
  243. * Assert if a job was pushed after the response was sent a number of times.
  244. *
  245. * @param string|\Closure $command
  246. * @param int $times
  247. * @return void
  248. */
  249. public function assertDispatchedAfterResponseTimes($command, $times = 1)
  250. {
  251. $callback = null;
  252. if ($command instanceof Closure) {
  253. [$command, $callback] = [$this->firstClosureParameterType($command), $command];
  254. }
  255. $count = $this->dispatchedAfterResponse($command, $callback)->count();
  256. PHPUnit::assertSame(
  257. $times, $count,
  258. "The expected [{$command}] job was pushed {$count} times instead of {$times} times."
  259. );
  260. }
  261. /**
  262. * Determine if a job was dispatched based on a truth-test callback.
  263. *
  264. * @param string|\Closure $command
  265. * @param callable|null $callback
  266. * @return void
  267. */
  268. public function assertNotDispatchedAfterResponse($command, $callback = null)
  269. {
  270. if ($command instanceof Closure) {
  271. [$command, $callback] = [$this->firstClosureParameterType($command), $command];
  272. }
  273. PHPUnit::assertCount(
  274. 0, $this->dispatchedAfterResponse($command, $callback),
  275. "The unexpected [{$command}] job was dispatched after sending the response."
  276. );
  277. }
  278. /**
  279. * Assert if a chain of jobs was dispatched.
  280. *
  281. * @param array $expectedChain
  282. * @return void
  283. */
  284. public function assertChained(array $expectedChain)
  285. {
  286. $command = $expectedChain[0];
  287. $expectedChain = array_slice($expectedChain, 1);
  288. $callback = null;
  289. if ($command instanceof Closure) {
  290. [$command, $callback] = [$this->firstClosureParameterType($command), $command];
  291. } elseif ($command instanceof ChainedBatchTruthTest) {
  292. $instance = $command;
  293. $command = ChainedBatch::class;
  294. $callback = fn ($job) => $instance($job->toPendingBatch());
  295. } elseif (! is_string($command)) {
  296. $instance = $command;
  297. $command = get_class($instance);
  298. $callback = function ($job) use ($instance) {
  299. return serialize($this->resetChainPropertiesToDefaults($job)) === serialize($instance);
  300. };
  301. }
  302. PHPUnit::assertTrue(
  303. $this->dispatched($command, $callback)->isNotEmpty(),
  304. "The expected [{$command}] job was not dispatched."
  305. );
  306. $this->assertDispatchedWithChainOfObjects($command, $expectedChain, $callback);
  307. }
  308. /**
  309. * Reset the chain properties to their default values on the job.
  310. *
  311. * @param mixed $job
  312. * @return mixed
  313. */
  314. protected function resetChainPropertiesToDefaults($job)
  315. {
  316. return tap(clone $job, function ($job) {
  317. $job->chainConnection = null;
  318. $job->chainQueue = null;
  319. $job->chainCatchCallbacks = null;
  320. $job->chained = [];
  321. });
  322. }
  323. /**
  324. * Assert if a job was dispatched with an empty chain based on a truth-test callback.
  325. *
  326. * @param string|\Closure $command
  327. * @param callable|null $callback
  328. * @return void
  329. */
  330. public function assertDispatchedWithoutChain($command, $callback = null)
  331. {
  332. if ($command instanceof Closure) {
  333. [$command, $callback] = [$this->firstClosureParameterType($command), $command];
  334. }
  335. PHPUnit::assertTrue(
  336. $this->dispatched($command, $callback)->isNotEmpty(),
  337. "The expected [{$command}] job was not dispatched."
  338. );
  339. $this->assertDispatchedWithChainOfObjects($command, [], $callback);
  340. }
  341. /**
  342. * Assert if a job was dispatched with chained jobs based on a truth-test callback.
  343. *
  344. * @param string $command
  345. * @param array $expectedChain
  346. * @param callable|null $callback
  347. * @return void
  348. */
  349. protected function assertDispatchedWithChainOfObjects($command, $expectedChain, $callback)
  350. {
  351. $chain = $expectedChain;
  352. PHPUnit::assertTrue(
  353. $this->dispatched($command, $callback)->filter(function ($job) use ($chain) {
  354. if (count($chain) !== count($job->chained)) {
  355. return false;
  356. }
  357. foreach ($job->chained as $index => $serializedChainedJob) {
  358. if ($chain[$index] instanceof ChainedBatchTruthTest) {
  359. $chainedBatch = unserialize($serializedChainedJob);
  360. if (! $chainedBatch instanceof ChainedBatch ||
  361. ! $chain[$index]($chainedBatch->toPendingBatch())) {
  362. return false;
  363. }
  364. } elseif ($chain[$index] instanceof Closure) {
  365. [$expectedType, $callback] = [$this->firstClosureParameterType($chain[$index]), $chain[$index]];
  366. $chainedJob = unserialize($serializedChainedJob);
  367. if (! $chainedJob instanceof $expectedType) {
  368. throw new RuntimeException('The chained job was expected to be of type '.$expectedType.', '.$chainedJob::class.' chained.');
  369. }
  370. if (! $callback($chainedJob)) {
  371. return false;
  372. }
  373. } elseif (is_string($chain[$index])) {
  374. if ($chain[$index] != get_class(unserialize($serializedChainedJob))) {
  375. return false;
  376. }
  377. } elseif (serialize($chain[$index]) != $serializedChainedJob) {
  378. return false;
  379. }
  380. }
  381. return true;
  382. })->isNotEmpty(),
  383. 'The expected chain was not dispatched.'
  384. );
  385. }
  386. /**
  387. * Create a new assertion about a chained batch.
  388. *
  389. * @param \Closure $callback
  390. * @return \Illuminate\Support\Testing\Fakes\ChainedBatchTruthTest
  391. */
  392. public function chainedBatch(Closure $callback)
  393. {
  394. return new ChainedBatchTruthTest($callback);
  395. }
  396. /**
  397. * Assert if a batch was dispatched based on a truth-test callback.
  398. *
  399. * @param callable $callback
  400. * @return void
  401. */
  402. public function assertBatched(callable $callback)
  403. {
  404. PHPUnit::assertTrue(
  405. $this->batched($callback)->count() > 0,
  406. 'The expected batch was not dispatched.'
  407. );
  408. }
  409. /**
  410. * Assert the number of batches that have been dispatched.
  411. *
  412. * @param int $count
  413. * @return void
  414. */
  415. public function assertBatchCount($count)
  416. {
  417. PHPUnit::assertCount(
  418. $count, $this->batches,
  419. );
  420. }
  421. /**
  422. * Assert that no batched jobs were dispatched.
  423. *
  424. * @return void
  425. */
  426. public function assertNothingBatched()
  427. {
  428. PHPUnit::assertEmpty($this->batches, 'Batched jobs were dispatched unexpectedly.');
  429. }
  430. /**
  431. * Get all of the jobs matching a truth-test callback.
  432. *
  433. * @param string $command
  434. * @param callable|null $callback
  435. * @return \Illuminate\Support\Collection
  436. */
  437. public function dispatched($command, $callback = null)
  438. {
  439. if (! $this->hasDispatched($command)) {
  440. return collect();
  441. }
  442. $callback = $callback ?: fn () => true;
  443. return collect($this->commands[$command])->filter(fn ($command) => $callback($command));
  444. }
  445. /**
  446. * Get all of the jobs dispatched synchronously matching a truth-test callback.
  447. *
  448. * @param string $command
  449. * @param callable|null $callback
  450. * @return \Illuminate\Support\Collection
  451. */
  452. public function dispatchedSync(string $command, $callback = null)
  453. {
  454. if (! $this->hasDispatchedSync($command)) {
  455. return collect();
  456. }
  457. $callback = $callback ?: fn () => true;
  458. return collect($this->commandsSync[$command])->filter(fn ($command) => $callback($command));
  459. }
  460. /**
  461. * Get all of the jobs dispatched after the response was sent matching a truth-test callback.
  462. *
  463. * @param string $command
  464. * @param callable|null $callback
  465. * @return \Illuminate\Support\Collection
  466. */
  467. public function dispatchedAfterResponse(string $command, $callback = null)
  468. {
  469. if (! $this->hasDispatchedAfterResponse($command)) {
  470. return collect();
  471. }
  472. $callback = $callback ?: fn () => true;
  473. return collect($this->commandsAfterResponse[$command])->filter(fn ($command) => $callback($command));
  474. }
  475. /**
  476. * Get all of the pending batches matching a truth-test callback.
  477. *
  478. * @param callable $callback
  479. * @return \Illuminate\Support\Collection
  480. */
  481. public function batched(callable $callback)
  482. {
  483. if (empty($this->batches)) {
  484. return collect();
  485. }
  486. return collect($this->batches)->filter(fn ($batch) => $callback($batch));
  487. }
  488. /**
  489. * Determine if there are any stored commands for a given class.
  490. *
  491. * @param string $command
  492. * @return bool
  493. */
  494. public function hasDispatched($command)
  495. {
  496. return isset($this->commands[$command]) && ! empty($this->commands[$command]);
  497. }
  498. /**
  499. * Determine if there are any stored commands for a given class.
  500. *
  501. * @param string $command
  502. * @return bool
  503. */
  504. public function hasDispatchedSync($command)
  505. {
  506. return isset($this->commandsSync[$command]) && ! empty($this->commandsSync[$command]);
  507. }
  508. /**
  509. * Determine if there are any stored commands for a given class.
  510. *
  511. * @param string $command
  512. * @return bool
  513. */
  514. public function hasDispatchedAfterResponse($command)
  515. {
  516. return isset($this->commandsAfterResponse[$command]) && ! empty($this->commandsAfterResponse[$command]);
  517. }
  518. /**
  519. * Dispatch a command to its appropriate handler.
  520. *
  521. * @param mixed $command
  522. * @return mixed
  523. */
  524. public function dispatch($command)
  525. {
  526. if ($this->shouldFakeJob($command)) {
  527. $this->commands[get_class($command)][] = $this->getCommandRepresentation($command);
  528. } else {
  529. return $this->dispatcher->dispatch($command);
  530. }
  531. }
  532. /**
  533. * Dispatch a command to its appropriate handler in the current process.
  534. *
  535. * Queueable jobs will be dispatched to the "sync" queue.
  536. *
  537. * @param mixed $command
  538. * @param mixed $handler
  539. * @return mixed
  540. */
  541. public function dispatchSync($command, $handler = null)
  542. {
  543. if ($this->shouldFakeJob($command)) {
  544. $this->commandsSync[get_class($command)][] = $this->getCommandRepresentation($command);
  545. } else {
  546. return $this->dispatcher->dispatchSync($command, $handler);
  547. }
  548. }
  549. /**
  550. * Dispatch a command to its appropriate handler in the current process.
  551. *
  552. * @param mixed $command
  553. * @param mixed $handler
  554. * @return mixed
  555. */
  556. public function dispatchNow($command, $handler = null)
  557. {
  558. if ($this->shouldFakeJob($command)) {
  559. $this->commands[get_class($command)][] = $this->getCommandRepresentation($command);
  560. } else {
  561. return $this->dispatcher->dispatchNow($command, $handler);
  562. }
  563. }
  564. /**
  565. * Dispatch a command to its appropriate handler behind a queue.
  566. *
  567. * @param mixed $command
  568. * @return mixed
  569. */
  570. public function dispatchToQueue($command)
  571. {
  572. if ($this->shouldFakeJob($command)) {
  573. $this->commands[get_class($command)][] = $this->getCommandRepresentation($command);
  574. } else {
  575. return $this->dispatcher->dispatchToQueue($command);
  576. }
  577. }
  578. /**
  579. * Dispatch a command to its appropriate handler.
  580. *
  581. * @param mixed $command
  582. * @return mixed
  583. */
  584. public function dispatchAfterResponse($command)
  585. {
  586. if ($this->shouldFakeJob($command)) {
  587. $this->commandsAfterResponse[get_class($command)][] = $this->getCommandRepresentation($command);
  588. } else {
  589. return $this->dispatcher->dispatch($command);
  590. }
  591. }
  592. /**
  593. * Create a new chain of queueable jobs.
  594. *
  595. * @param \Illuminate\Support\Collection|array $jobs
  596. * @return \Illuminate\Foundation\Bus\PendingChain
  597. */
  598. public function chain($jobs)
  599. {
  600. $jobs = Collection::wrap($jobs);
  601. $jobs = ChainedBatch::prepareNestedBatches($jobs);
  602. return new PendingChainFake($this, $jobs->shift(), $jobs->toArray());
  603. }
  604. /**
  605. * Attempt to find the batch with the given ID.
  606. *
  607. * @param string $batchId
  608. * @return \Illuminate\Bus\Batch|null
  609. */
  610. public function findBatch(string $batchId)
  611. {
  612. return $this->batchRepository->find($batchId);
  613. }
  614. /**
  615. * Create a new batch of queueable jobs.
  616. *
  617. * @param \Illuminate\Support\Collection|array $jobs
  618. * @return \Illuminate\Bus\PendingBatch
  619. */
  620. public function batch($jobs)
  621. {
  622. return new PendingBatchFake($this, Collection::wrap($jobs));
  623. }
  624. /**
  625. * Dispatch an empty job batch for testing.
  626. *
  627. * @param string $name
  628. * @return \Illuminate\Bus\Batch
  629. */
  630. public function dispatchFakeBatch($name = '')
  631. {
  632. return $this->batch([])->name($name)->dispatch();
  633. }
  634. /**
  635. * Record the fake pending batch dispatch.
  636. *
  637. * @param \Illuminate\Bus\PendingBatch $pendingBatch
  638. * @return \Illuminate\Bus\Batch
  639. */
  640. public function recordPendingBatch(PendingBatch $pendingBatch)
  641. {
  642. $this->batches[] = $pendingBatch;
  643. return $this->batchRepository->store($pendingBatch);
  644. }
  645. /**
  646. * Determine if a command should be faked or actually dispatched.
  647. *
  648. * @param mixed $command
  649. * @return bool
  650. */
  651. protected function shouldFakeJob($command)
  652. {
  653. if ($this->shouldDispatchCommand($command)) {
  654. return false;
  655. }
  656. if (empty($this->jobsToFake)) {
  657. return true;
  658. }
  659. return collect($this->jobsToFake)
  660. ->filter(function ($job) use ($command) {
  661. return $job instanceof Closure
  662. ? $job($command)
  663. : $job === get_class($command);
  664. })->isNotEmpty();
  665. }
  666. /**
  667. * Determine if a command should be dispatched or not.
  668. *
  669. * @param mixed $command
  670. * @return bool
  671. */
  672. protected function shouldDispatchCommand($command)
  673. {
  674. return collect($this->jobsToDispatch)
  675. ->filter(function ($job) use ($command) {
  676. return $job instanceof Closure
  677. ? $job($command)
  678. : $job === get_class($command);
  679. })->isNotEmpty();
  680. }
  681. /**
  682. * Specify if commands should be serialized and restored when being batched.
  683. *
  684. * @param bool $serializeAndRestore
  685. * @return $this
  686. */
  687. public function serializeAndRestore(bool $serializeAndRestore = true)
  688. {
  689. $this->serializeAndRestore = $serializeAndRestore;
  690. return $this;
  691. }
  692. /**
  693. * Serialize and unserialize the command to simulate the queueing process.
  694. *
  695. * @param mixed $command
  696. * @return mixed
  697. */
  698. protected function serializeAndRestoreCommand($command)
  699. {
  700. return unserialize(serialize($command));
  701. }
  702. /**
  703. * Return the command representation that should be stored.
  704. *
  705. * @param mixed $command
  706. * @return mixed
  707. */
  708. protected function getCommandRepresentation($command)
  709. {
  710. return $this->serializeAndRestore ? $this->serializeAndRestoreCommand($command) : $command;
  711. }
  712. /**
  713. * Set the pipes commands should be piped through before dispatching.
  714. *
  715. * @param array $pipes
  716. * @return $this
  717. */
  718. public function pipeThrough(array $pipes)
  719. {
  720. $this->dispatcher->pipeThrough($pipes);
  721. return $this;
  722. }
  723. /**
  724. * Determine if the given command has a handler.
  725. *
  726. * @param mixed $command
  727. * @return bool
  728. */
  729. public function hasCommandHandler($command)
  730. {
  731. return $this->dispatcher->hasCommandHandler($command);
  732. }
  733. /**
  734. * Retrieve the handler for a command.
  735. *
  736. * @param mixed $command
  737. * @return mixed
  738. */
  739. public function getCommandHandler($command)
  740. {
  741. return $this->dispatcher->getCommandHandler($command);
  742. }
  743. /**
  744. * Map a command to a handler.
  745. *
  746. * @param array $map
  747. * @return $this
  748. */
  749. public function map(array $map)
  750. {
  751. $this->dispatcher->map($map);
  752. return $this;
  753. }
  754. }