Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to test chained job via closure #49337

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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