Skip to content

Commit 0222c3e

Browse files
[11.x] add assertFailedWith to InteractsWithQueue trait (#53980)
* [11.x] add assertFailedWith to InteractsWithQueue * formatting * formatting * formatting * Update InteractsWithQueue.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 8636996 commit 0222c3e

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/Illuminate/Queue/InteractsWithQueue.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,52 @@ public function assertFailed()
145145
return $this;
146146
}
147147

148+
/**
149+
* Assert that the job was manually failed with a specific exception.
150+
*
151+
* @param \Throwable|string $exception
152+
* @return $this
153+
*/
154+
public function assertFailedWith($exception)
155+
{
156+
$this->assertFailed();
157+
158+
if (is_string($exception) && class_exists($exception)) {
159+
PHPUnit::assertInstanceOf(
160+
$exception,
161+
$this->job->failedWith,
162+
'Expected job to be manually failed with ['.$exception.'] but job failed with ['.get_class($this->job->failedWith).'].'
163+
);
164+
165+
return $this;
166+
}
167+
168+
if (is_string($exception)) {
169+
$exception = new ManuallyFailedException($exception);
170+
}
171+
172+
if ($exception instanceof Throwable) {
173+
PHPUnit::assertInstanceOf(
174+
get_class($exception),
175+
$this->job->failedWith,
176+
'Expected job to be manually failed with ['.get_class($exception).'] but job failed with ['.get_class($this->job->failedWith).'].'
177+
);
178+
179+
PHPUnit::assertEquals(
180+
$exception->getCode(),
181+
$this->job->failedWith->getCode(),
182+
'Expected exception code ['.$exception->getCode().'] but job failed with exception code ['.$this->job->failedWith->getCode().'].'
183+
);
184+
185+
PHPUnit::assertEquals(
186+
$exception->getMessage(),
187+
$this->job->failedWith->getMessage(),
188+
'Expected exceptoin message ['.$exception->getMessage().'] but job failed with exception message ['.$this->job->failedWith->getMessage().'].');
189+
}
190+
191+
return $this;
192+
}
193+
148194
/**
149195
* Assert that the job was not manually failed.
150196
*

0 commit comments

Comments
 (0)