Skip to content

Commit b7d4a85

Browse files
committed
test
1 parent b93121d commit b7d4a85

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Queue;
4+
5+
use Illuminate\Bus\Queueable;
6+
use Illuminate\Contracts\Queue\ShouldQueue;
7+
use Illuminate\Foundation\Bus\Dispatchable;
8+
use Illuminate\Foundation\Testing\RefreshDatabase;
9+
use Illuminate\Queue\InteractsWithQueue;
10+
use Illuminate\Queue\Middleware\RetryIf;
11+
use Illuminate\Support\Facades\Queue;
12+
use InvalidArgumentException;
13+
use Orchestra\Testbench\Attributes\WithConfig;
14+
use PHPUnit\Framework\Attributes\DataProvider;
15+
16+
#[WithConfig('queue.default', 'database')]
17+
class RetryIfMiddlewareTest extends QueueTestCase
18+
{
19+
use RefreshDatabase;
20+
21+
protected $driver = 'database';
22+
23+
public static function markFailedForRetryIfDataProvider(): array
24+
{
25+
return [
26+
'middleware fails on thrown exception' => [
27+
InvalidArgumentException::class,
28+
1,
29+
1,
30+
],
31+
'middleware retries if exception does not match' => [
32+
\LogicException::class,
33+
2,
34+
1,
35+
],
36+
];
37+
}
38+
39+
#[DataProvider('markFailedForRetryIfDataProvider')]
40+
public function test_retry_if_middleware(
41+
$throws,
42+
int $expectedExceptions,
43+
int $expectedFails
44+
) {
45+
RetryIfMiddlewareJob::dispatch($throws)->onQueue('default')->onConnection('database');
46+
47+
$failsCalled = $exceptionsOccurred = 0;
48+
Queue::exceptionOccurred(function () use (&$exceptionsOccurred) {
49+
$exceptionsOccurred++;
50+
});
51+
Queue::failing(function () use (&$failsCalled) {
52+
$failsCalled++;
53+
});
54+
55+
$this->runQueueWorkerCommand(['--stop-when-empty' => true, '--sleep' => 1], 2);
56+
57+
$this->assertEquals($expectedExceptions, $exceptionsOccurred);
58+
$this->assertEquals($expectedFails, $failsCalled);
59+
}
60+
}
61+
62+
class RetryIfMiddlewareJob implements ShouldQueue
63+
{
64+
use InteractsWithQueue;
65+
use Queueable;
66+
use Dispatchable;
67+
68+
public int $tries = 2;
69+
70+
public function __construct(private $throws)
71+
{
72+
}
73+
public function handle()
74+
{
75+
if ($this->throws === null) {
76+
return; // success
77+
}
78+
79+
throw new ($this->throws);
80+
}
81+
82+
public function middleware(): array
83+
{
84+
return [RetryIf::failForExceptions(InvalidArgumentException::class)];
85+
}
86+
}

0 commit comments

Comments
 (0)