BusFake.php 25 KB

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