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

[11.x] assertChain and assertNoChain on job instance #50858

Merged
merged 5 commits into from
Apr 1, 2024
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
36 changes: 36 additions & 0 deletions src/Illuminate/Bus/Queueable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Illuminate\Queue\CallQueuedClosure;
use Illuminate\Support\Arr;
use PHPUnit\Framework\Assert as PHPUnit;
use RuntimeException;

trait Queueable
Expand Down Expand Up @@ -273,4 +274,39 @@ public function invokeChainCatchCallbacks($e)
$callback($e);
});
}

/**
* Assert that the job has the given chain of jobs attached to it.
*
* @param array $expectedChain
* @return void
*/
public function assertHasChain($expectedChain)
{
PHPUnit::assertTrue(
collect($expectedChain)->isNotEmpty(),
'The expected chain can not be empty.'
);

if (collect($expectedChain)->contains(fn ($job) => is_object($job))) {
$expectedChain = collect($expectedChain)->map(fn ($job) => serialize($job))->all();
} else {
$chain = collect($this->chained)->map(fn ($job) => get_class(unserialize($job)))->all();
}

PHPUnit::assertTrue(
$expectedChain === ($chain ?? $this->chained),
'The job does not have the expected chain.'
);
}

/**
* Assert that the job has no remaining chained jobs.
*
* @return void
*/
public function assertDoesntHaveChain()
{
PHPUnit::assertEmpty($this->chained, 'The job has chained jobs.');
}
}
63 changes: 63 additions & 0 deletions tests/Support/SupportTestingQueueFakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,69 @@ public function testItCanFakePushedJobsWithClassAndPayload()
$fake->assertPushed('JobStub', 1);
$fake->assertPushed('JobStub', fn ($job, $queue, $payload) => $payload === ['job' => 'payload']);
}

public function testAssertChainUsingClassesOrObjectsArray()
{
$job = new JobWithChainStub([
new JobStub,
]);

$job->assertHasChain([
JobStub::class,
]);

$job->assertHasChain([
new JobStub(),
]);
}

public function testAssertNoChain()
{
$job = new JobWithChainStub([]);

$job->assertDoesntHaveChain();
}

public function testAssertChainErrorHandling()
{
$job = new JobWithChainStub([
new JobStub,
]);

try {
$job->assertHasChain([]);
$this->fail();
} catch (ExpectationFailedException $e) {
$this->assertStringContainsString('The expected chain can not be empty.', $e->getMessage());
}

try {
$job->assertHasChain([
new JobStub,
new JobStub,
]);
$this->fail();
} catch (ExpectationFailedException $e) {
$this->assertStringContainsString('The job does not have the expected chain.', $e->getMessage());
}

try {
$job->assertHasChain([
JobStub::class,
JobStub::class,
]);
$this->fail();
} catch (ExpectationFailedException $e) {
$this->assertStringContainsString('The job does not have the expected chain.', $e->getMessage());
}

try {
$job->assertDoesntHaveChain();
$this->fail();
} catch (ExpectationFailedException $e) {
$this->assertStringContainsString('The job has chained jobs.', $e->getMessage());
}
}
}

class JobStub
Expand Down