Skip to content

Commit 5654bdf

Browse files
Ability to test chained job via closure (#49337)
1 parent b41612c commit 5654bdf

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/Illuminate/Support/Testing/Fakes/BusFake.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,18 @@ protected function assertDispatchedWithChainOfObjects($command, $expectedChain,
421421
! $chain[$index]($chainedBatch->toPendingBatch())) {
422422
return false;
423423
}
424+
} elseif ($chain[$index] instanceof Closure) {
425+
[$expectedType, $callback] = [$this->firstClosureParameterType($chain[$index]), $chain[$index]];
426+
427+
$chainedJob = unserialize($serializedChainedJob);
428+
429+
if (! $chainedJob instanceof $expectedType) {
430+
throw new RuntimeException('The chained job was expected to be of type '.$expectedType.', '.$chainedJob::class.' chained.');
431+
}
432+
433+
if (! $callback($chainedJob)) {
434+
return false;
435+
}
424436
} elseif (is_string($chain[$index])) {
425437
if ($chain[$index] != get_class(unserialize($serializedChainedJob))) {
426438
return false;

tests/Support/SupportTestingBusFakeTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,16 @@ public function testAssertChained()
534534
ChainedJobStub::class,
535535
]);
536536

537+
$this->fake->chain([
538+
new ChainedJobStub(123),
539+
new ChainedJobStub(456),
540+
])->dispatch();
541+
542+
$this->fake->assertChained([
543+
fn (ChainedJobStub $job) => $job->id === 123,
544+
fn (ChainedJobStub $job) => $job->id === 456,
545+
]);
546+
537547
Container::setInstance(null);
538548
}
539549

@@ -777,6 +787,13 @@ class BusJobStub
777787
class ChainedJobStub
778788
{
779789
use Queueable;
790+
791+
public $id;
792+
793+
public function __construct($id = null)
794+
{
795+
$this->id = $id;
796+
}
780797
}
781798

782799
class OtherBusJobStub

0 commit comments

Comments
 (0)