From 7ef92879decc3cceba0b62afdf352fa63b69fd15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=CC=88nther=20Debrauwer?= Date: Sat, 30 Mar 2024 18:38:02 +0100 Subject: [PATCH 1/5] assertChain and assertNoChain --- src/Illuminate/Bus/Queueable.php | 40 ++++++++++++ tests/Support/SupportTestingQueueFakeTest.php | 63 +++++++++++++++++++ 2 files changed, 103 insertions(+) diff --git a/src/Illuminate/Bus/Queueable.php b/src/Illuminate/Bus/Queueable.php index 3d3bbb9b290e..875becff6a8a 100644 --- a/src/Illuminate/Bus/Queueable.php +++ b/src/Illuminate/Bus/Queueable.php @@ -6,6 +6,7 @@ use Illuminate\Queue\CallQueuedClosure; use Illuminate\Support\Arr; use RuntimeException; +use PHPUnit\Framework\Assert as PHPUnit; trait Queueable { @@ -273,4 +274,43 @@ public function invokeChainCatchCallbacks($e) $callback($e); }); } + + /** + * Assert that the job has the chained jobs. + * + * @param array $expectedChain + * @return void + */ + public function assertChain($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 chained jobs. + * + * @param array $expectedChain + * @return void + */ + public function assertNoChain() + { + PHPUnit::assertEmpty( + $this->chained, + 'The job has chained jobs.' + ); + } } diff --git a/tests/Support/SupportTestingQueueFakeTest.php b/tests/Support/SupportTestingQueueFakeTest.php index dd632426d543..1ca1293f9bdb 100644 --- a/tests/Support/SupportTestingQueueFakeTest.php +++ b/tests/Support/SupportTestingQueueFakeTest.php @@ -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->assertChain([ + JobStub::class, + ]); + + $job->assertChain([ + new JobStub(), + ]); + } + + public function testAssertNoChain() + { + $job = new JobWithChainStub([]); + + $job->assertNoChain(); + } + + public function testAssertChainErrorHandling() + { + $job = new JobWithChainStub([ + new JobStub, + ]); + + try { + $job->assertChain([]); + $this->fail(); + } catch (ExpectationFailedException $e) { + $this->assertStringContainsString('The expected chain can not be empty.', $e->getMessage()); + } + + try { + $job->assertChain([ + new JobStub, + new JobStub, + ]); + $this->fail(); + } catch (ExpectationFailedException $e) { + $this->assertStringContainsString('The job does not have the expected chain.', $e->getMessage()); + } + + try { + $job->assertChain([ + JobStub::class, + JobStub::class, + ]); + $this->fail(); + } catch (ExpectationFailedException $e) { + $this->assertStringContainsString('The job does not have the expected chain.', $e->getMessage()); + } + + try { + $job->assertNoChain(); + $this->fail(); + } catch (ExpectationFailedException $e) { + $this->assertStringContainsString('The job has chained jobs.', $e->getMessage()); + } + } } class JobStub From 9b09a8de0633e9f220f33c817645eb17b7f99618 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=CC=88nther=20Debrauwer?= Date: Sat, 30 Mar 2024 18:48:18 +0100 Subject: [PATCH 2/5] Fix linting issues --- src/Illuminate/Bus/Queueable.php | 2 +- tests/Support/SupportTestingQueueFakeTest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Bus/Queueable.php b/src/Illuminate/Bus/Queueable.php index 875becff6a8a..cecad227c1b6 100644 --- a/src/Illuminate/Bus/Queueable.php +++ b/src/Illuminate/Bus/Queueable.php @@ -5,8 +5,8 @@ use Closure; use Illuminate\Queue\CallQueuedClosure; use Illuminate\Support\Arr; -use RuntimeException; use PHPUnit\Framework\Assert as PHPUnit; +use RuntimeException; trait Queueable { diff --git a/tests/Support/SupportTestingQueueFakeTest.php b/tests/Support/SupportTestingQueueFakeTest.php index 1ca1293f9bdb..528829991f41 100644 --- a/tests/Support/SupportTestingQueueFakeTest.php +++ b/tests/Support/SupportTestingQueueFakeTest.php @@ -420,9 +420,9 @@ public function testItCanFakePushedJobsWithClassAndPayload() public function testAssertChainUsingClassesOrObjectsArray() { - ($job = new JobWithChainStub([ + $job = new JobWithChainStub([ new JobStub, - ])); + ]); $job->assertChain([ JobStub::class, From dc8bacae372f69a2a55cb57395e472404e766b28 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 1 Apr 2024 10:20:49 -0500 Subject: [PATCH 3/5] formatting --- src/Illuminate/Bus/Queueable.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Bus/Queueable.php b/src/Illuminate/Bus/Queueable.php index cecad227c1b6..33be3c9c67eb 100644 --- a/src/Illuminate/Bus/Queueable.php +++ b/src/Illuminate/Bus/Queueable.php @@ -276,7 +276,7 @@ public function invokeChainCatchCallbacks($e) } /** - * Assert that the job has the chained jobs. + * Assert that the job has the given chain of jobs attached to it. * * @param array $expectedChain * @return void @@ -301,9 +301,8 @@ public function assertChain($expectedChain) } /** - * Assert that the job has no chained jobs. + * Assert that the job has no remaining chained jobs. * - * @param array $expectedChain * @return void */ public function assertNoChain() From bca901888a2ef7152fb80ffc3b72d35bc5c7e49d Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 1 Apr 2024 15:42:13 -0500 Subject: [PATCH 4/5] formatting --- src/Illuminate/Bus/Queueable.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/Illuminate/Bus/Queueable.php b/src/Illuminate/Bus/Queueable.php index 33be3c9c67eb..867d464eb65d 100644 --- a/src/Illuminate/Bus/Queueable.php +++ b/src/Illuminate/Bus/Queueable.php @@ -281,7 +281,7 @@ public function invokeChainCatchCallbacks($e) * @param array $expectedChain * @return void */ - public function assertChain($expectedChain) + public function assertHasChain($expectedChain) { PHPUnit::assertTrue( collect($expectedChain)->isNotEmpty(), @@ -305,11 +305,8 @@ public function assertChain($expectedChain) * * @return void */ - public function assertNoChain() + public function assertDoesntHaveChain() { - PHPUnit::assertEmpty( - $this->chained, - 'The job has chained jobs.' - ); + PHPUnit::assertEmpty($this->chained, 'The job has chained jobs.'); } } From 5c9a1018d6e4f2002114b61e1356e37036672262 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 1 Apr 2024 15:43:19 -0500 Subject: [PATCH 5/5] fix test --- tests/Support/SupportTestingQueueFakeTest.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/Support/SupportTestingQueueFakeTest.php b/tests/Support/SupportTestingQueueFakeTest.php index 528829991f41..3122cbe6fe69 100644 --- a/tests/Support/SupportTestingQueueFakeTest.php +++ b/tests/Support/SupportTestingQueueFakeTest.php @@ -424,11 +424,11 @@ public function testAssertChainUsingClassesOrObjectsArray() new JobStub, ]); - $job->assertChain([ + $job->assertHasChain([ JobStub::class, ]); - $job->assertChain([ + $job->assertHasChain([ new JobStub(), ]); } @@ -437,7 +437,7 @@ public function testAssertNoChain() { $job = new JobWithChainStub([]); - $job->assertNoChain(); + $job->assertDoesntHaveChain(); } public function testAssertChainErrorHandling() @@ -447,14 +447,14 @@ public function testAssertChainErrorHandling() ]); try { - $job->assertChain([]); + $job->assertHasChain([]); $this->fail(); } catch (ExpectationFailedException $e) { $this->assertStringContainsString('The expected chain can not be empty.', $e->getMessage()); } try { - $job->assertChain([ + $job->assertHasChain([ new JobStub, new JobStub, ]); @@ -464,7 +464,7 @@ public function testAssertChainErrorHandling() } try { - $job->assertChain([ + $job->assertHasChain([ JobStub::class, JobStub::class, ]); @@ -474,7 +474,7 @@ public function testAssertChainErrorHandling() } try { - $job->assertNoChain(); + $job->assertDoesntHaveChain(); $this->fail(); } catch (ExpectationFailedException $e) { $this->assertStringContainsString('The job has chained jobs.', $e->getMessage());