Skip to content

Commit

Permalink
[11.x] assertChain and assertNoChain on job instance (#50858)
Browse files Browse the repository at this point in the history
* assertChain and assertNoChain

* Fix linting issues

* formatting

* formatting

* fix test

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
gdebrauwer and taylorotwell authored Apr 1, 2024
1 parent 9983d7b commit f389d79
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
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

0 comments on commit f389d79

Please sign in to comment.