diff --git a/src/Illuminate/Queue/InteractsWithQueue.php b/src/Illuminate/Queue/InteractsWithQueue.php index a539f70c3967..6e7d3bd2c075 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, + 'Expected job to be manually failed with ['.$exception.'] but job failed with ['.get_class($this->job->failedWith).'].' + ); + + return $this; + } + + if (is_string($exception)) { + $exception = new ManuallyFailedException($exception); + } + + if ($exception instanceof Throwable) { + PHPUnit::assertInstanceOf( + get_class($exception), + $this->job->failedWith, + 'Expected job to be manually failed with ['.get_class($exception).'] but job failed with ['.get_class($this->job->failedWith).'].' + ); + + PHPUnit::assertEquals( + $exception->getCode(), + $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(), + 'Expected exceptoin message ['.$exception->getMessage().'] but job failed with exception message ['.$this->job->failedWith->getMessage().'].'); + } + + return $this; + } + /** * Assert that the job was not manually failed. *