From 71d8fb2fdd89327e182dbcb066ea2d12c61790ae Mon Sep 17 00:00:00 2001 From: Jason McCreary Date: Mon, 11 Dec 2023 20:11:36 -0500 Subject: [PATCH] Ability to test chained job via closure --- .../Support/Testing/Fakes/BusFake.php | 12 ++++++++++++ tests/Support/SupportTestingBusFakeTest.php | 17 +++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/Illuminate/Support/Testing/Fakes/BusFake.php b/src/Illuminate/Support/Testing/Fakes/BusFake.php index 3d717eae6231..38a3f2c15708 100644 --- a/src/Illuminate/Support/Testing/Fakes/BusFake.php +++ b/src/Illuminate/Support/Testing/Fakes/BusFake.php @@ -421,6 +421,18 @@ protected function assertDispatchedWithChainOfObjects($command, $expectedChain, ! $chain[$index]($chainedBatch->toPendingBatch())) { return false; } + } elseif ($chain[$index] instanceof Closure) { + [$expectedType, $callback] = [$this->firstClosureParameterType($chain[$index]), $chain[$index]]; + + $chainedJob = unserialize($serializedChainedJob); + + if (! $chainedJob instanceof $expectedType) { + throw new RuntimeException('The chained job was expected to be of type '.$expectedType.', '.$chainedJob::class.' chained.'); + } + + if (! $callback($chainedJob)) { + return false; + } } elseif (is_string($chain[$index])) { if ($chain[$index] != get_class(unserialize($serializedChainedJob))) { return false; diff --git a/tests/Support/SupportTestingBusFakeTest.php b/tests/Support/SupportTestingBusFakeTest.php index b1d6d8bfd952..384eb930831a 100644 --- a/tests/Support/SupportTestingBusFakeTest.php +++ b/tests/Support/SupportTestingBusFakeTest.php @@ -534,6 +534,16 @@ public function testAssertChained() ChainedJobStub::class, ]); + $this->fake->chain([ + new ChainedJobStub(123), + new ChainedJobStub(456), + ])->dispatch(); + + $this->fake->assertChained([ + fn (ChainedJobStub $job) => $job->id === 123, + fn (ChainedJobStub $job) => $job->id === 456, + ]); + Container::setInstance(null); } @@ -777,6 +787,13 @@ class BusJobStub class ChainedJobStub { use Queueable; + + public $id; + + public function __construct($id = null) + { + $this->id = $id; + } } class OtherBusJobStub