Skip to content

Commit

Permalink
Ability to test chained job via closure (#49337)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmccreary authored Dec 12, 2023
1 parent b41612c commit 5654bdf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Illuminate/Support/Testing/Fakes/BusFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions tests/Support/SupportTestingBusFakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -777,6 +787,13 @@ class BusJobStub
class ChainedJobStub
{
use Queueable;

public $id;

public function __construct($id = null)
{
$this->id = $id;
}
}

class OtherBusJobStub
Expand Down

0 comments on commit 5654bdf

Please sign in to comment.