From 0f2b34ed19fa17397a6954f6c0bdd2648fcd1333 Mon Sep 17 00:00:00 2001 From: Teddy FRANCFORT Date: Thu, 19 Dec 2024 17:41:34 +0100 Subject: [PATCH 1/5] [11.x] add assertFailedWith to InteractsWithQueue --- src/Illuminate/Queue/InteractsWithQueue.php | 46 +++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/Illuminate/Queue/InteractsWithQueue.php b/src/Illuminate/Queue/InteractsWithQueue.php index a539f70c3967..1fc9612ad310 100644 --- a/src/Illuminate/Queue/InteractsWithQueue.php +++ b/src/Illuminate/Queue/InteractsWithQueue.php @@ -145,6 +145,52 @@ public function assertFailed() return $this; } + /** + * Assert that the job was manually failed with a specific exception. + * + * @param \Throwable|string $exception + * + * @return $this + */ + public function assertFailedWith($exception) + { + $this->assertFailed(); + + if (is_string($exception) && class_exists($exception)) { + PHPUnit::assertInstanceOf( + $exception, + $this->job->failedWith, + 'Job was expected to be manually failed with '. $exception . ', but was not.' + ); + return $this; + } + + if (is_string($exception)) { + $exception = new ManuallyFailedException($exception); + } + + if ($exception instanceof Throwable) { + PHPUnit::assertInstanceOf( + get_class($exception), + $this->job->failedWith, + 'Job was expected to be manually failed with '. get_class($exception) . ', but was not.' + ); + + PHPUnit::assertEquals( + $exception->getCode(), + $this->job->failedWith->getCode(), + 'Exception code does not match. Code should be ' . $exception->getCode() . ' but is ' . $this->job->failedWith->getCode() . '.' + ); + PHPUnit::assertEquals( + $exception->getMessage(), + $this->job->failedWith->getMessage(), + 'Exception message does not match. Message should be ' . $exception->getMessage() . ' but is ' . $this->job->failedWith->getMessage() . '.' + ); + } + + return $this; + } + /** * Assert that the job was not manually failed. * From 617fdc9a90112b1a0be69d2283b79a5638099ffb Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 27 Dec 2024 15:24:10 -0600 Subject: [PATCH 2/5] formatting --- src/Illuminate/Queue/InteractsWithQueue.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Illuminate/Queue/InteractsWithQueue.php b/src/Illuminate/Queue/InteractsWithQueue.php index 1fc9612ad310..4a457ae41a3a 100644 --- a/src/Illuminate/Queue/InteractsWithQueue.php +++ b/src/Illuminate/Queue/InteractsWithQueue.php @@ -149,7 +149,6 @@ public function assertFailed() * Assert that the job was manually failed with a specific exception. * * @param \Throwable|string $exception - * * @return $this */ public function assertFailedWith($exception) @@ -160,8 +159,9 @@ public function assertFailedWith($exception) PHPUnit::assertInstanceOf( $exception, $this->job->failedWith, - 'Job was expected to be manually failed with '. $exception . ', but was not.' + 'Job was expected to be manually failed with ['.$exception.'], but was not.' ); + return $this; } @@ -173,19 +173,19 @@ public function assertFailedWith($exception) PHPUnit::assertInstanceOf( get_class($exception), $this->job->failedWith, - 'Job was expected to be manually failed with '. get_class($exception) . ', but was not.' + 'Job was expected to be manually failed with ['.get_class($exception).'], but was not.' ); PHPUnit::assertEquals( $exception->getCode(), $this->job->failedWith->getCode(), - 'Exception code does not match. Code should be ' . $exception->getCode() . ' but is ' . $this->job->failedWith->getCode() . '.' + 'Expected exception code ['.$exception->getCode().'] but job failed with exception code ['.$this->job->failedWith->getCode().'].' ); + PHPUnit::assertEquals( $exception->getMessage(), $this->job->failedWith->getMessage(), - 'Exception message does not match. Message should be ' . $exception->getMessage() . ' but is ' . $this->job->failedWith->getMessage() . '.' - ); + 'Expected exceptoin message ['.$exception->getMessage().'] but job failed with exception message ['.$this->job->failedWith->getMessage().'].'); } return $this; From 67c1875484aad1a4e4a773fa03bd0391788516f7 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 27 Dec 2024 15:25:58 -0600 Subject: [PATCH 3/5] formatting --- src/Illuminate/Queue/InteractsWithQueue.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Queue/InteractsWithQueue.php b/src/Illuminate/Queue/InteractsWithQueue.php index 4a457ae41a3a..b69cfc84f009 100644 --- a/src/Illuminate/Queue/InteractsWithQueue.php +++ b/src/Illuminate/Queue/InteractsWithQueue.php @@ -159,7 +159,7 @@ public function assertFailedWith($exception) PHPUnit::assertInstanceOf( $exception, $this->job->failedWith, - 'Job was expected to be manually failed with ['.$exception.'], but was not.' + 'Expected job to be manually failed with ['.$exception.'], but was failed with ['.get_class($this->job->failedWith).'].' ); return $this; From 25bc5038e503f43c61d3963d474b65af6c1fd48b Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 27 Dec 2024 15:27:34 -0600 Subject: [PATCH 4/5] formatting --- src/Illuminate/Queue/InteractsWithQueue.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Queue/InteractsWithQueue.php b/src/Illuminate/Queue/InteractsWithQueue.php index b69cfc84f009..bc0a79b16f0a 100644 --- a/src/Illuminate/Queue/InteractsWithQueue.php +++ b/src/Illuminate/Queue/InteractsWithQueue.php @@ -173,7 +173,7 @@ public function assertFailedWith($exception) PHPUnit::assertInstanceOf( get_class($exception), $this->job->failedWith, - 'Job was expected to be manually failed with ['.get_class($exception).'], but was not.' + 'Expected job to be manually failed with ['.get_class($exception).'], but was failed with ['.get_class($this->job->failedWith).'].' ); PHPUnit::assertEquals( From 627c94b5d87d52ce403f7ed127fd4d4e9bb22fea Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 27 Dec 2024 15:29:09 -0600 Subject: [PATCH 5/5] Update InteractsWithQueue.php --- src/Illuminate/Queue/InteractsWithQueue.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Queue/InteractsWithQueue.php b/src/Illuminate/Queue/InteractsWithQueue.php index bc0a79b16f0a..6e7d3bd2c075 100644 --- a/src/Illuminate/Queue/InteractsWithQueue.php +++ b/src/Illuminate/Queue/InteractsWithQueue.php @@ -159,7 +159,7 @@ public function assertFailedWith($exception) PHPUnit::assertInstanceOf( $exception, $this->job->failedWith, - 'Expected job to be manually failed with ['.$exception.'], but was failed with ['.get_class($this->job->failedWith).'].' + 'Expected job to be manually failed with ['.$exception.'] but job failed with ['.get_class($this->job->failedWith).'].' ); return $this; @@ -173,7 +173,7 @@ public function assertFailedWith($exception) PHPUnit::assertInstanceOf( get_class($exception), $this->job->failedWith, - 'Expected job to be manually failed with ['.get_class($exception).'], but was failed with ['.get_class($this->job->failedWith).'].' + 'Expected job to be manually failed with ['.get_class($exception).'] but job failed with ['.get_class($this->job->failedWith).'].' ); PHPUnit::assertEquals(