Skip to content

Commit

Permalink
[11.x] add assertFailedWith to InteractsWithQueue trait (laravel#53980)
Browse files Browse the repository at this point in the history
* [11.x] add assertFailedWith to InteractsWithQueue

* formatting

* formatting

* formatting

* Update InteractsWithQueue.php

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
teddy-francfort and taylorotwell authored Dec 27, 2024
1 parent 8636996 commit 0222c3e
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/Illuminate/Queue/InteractsWithQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit 0222c3e

Please sign in to comment.