|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Illuminate\Tests\Queue; |
| 4 | + |
| 5 | +use Illuminate\Bus\Dispatcher; |
| 6 | +use Illuminate\Bus\Queueable; |
| 7 | +use Illuminate\Contracts\Queue\ShouldQueue; |
| 8 | +use Illuminate\Foundation\Bus\Dispatchable; |
| 9 | +use Illuminate\Queue\CallQueuedHandler; |
| 10 | +use Illuminate\Queue\InteractsWithQueue; |
| 11 | +use Illuminate\Queue\Jobs\FakeJob; |
| 12 | +use Illuminate\Queue\Middleware\FailOnException; |
| 13 | +use InvalidArgumentException; |
| 14 | +use LogicException; |
| 15 | +use Orchestra\Testbench\TestCase; |
| 16 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 17 | +use PHPUnit\Framework\Attributes\TestWith; |
| 18 | +use Throwable; |
| 19 | + |
| 20 | +class FailOnExceptionMiddlewareTest extends TestCase |
| 21 | +{ |
| 22 | + protected function setUp(): void |
| 23 | + { |
| 24 | + parent::setUp(); |
| 25 | + FailOnExceptionMiddlewareTestJob::$_middleware = []; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * @return array<string, array{class-string<\Throwable>, FailOnException, bool}> |
| 30 | + */ |
| 31 | + public static function testMiddlewareDataProvider(): array |
| 32 | + { |
| 33 | + return [ |
| 34 | + 'exception is in list' => [ |
| 35 | + InvalidArgumentException::class, |
| 36 | + new FailOnException([InvalidArgumentException::class]), |
| 37 | + true, |
| 38 | + ], |
| 39 | + 'exception is not in list' => [ |
| 40 | + LogicException::class, |
| 41 | + new FailOnException([InvalidArgumentException::class]), |
| 42 | + false, |
| 43 | + ], |
| 44 | + ]; |
| 45 | + } |
| 46 | + |
| 47 | + #[DataProvider('testMiddlewareDataProvider')] |
| 48 | + public function test_middleware( |
| 49 | + string $thrown, |
| 50 | + FailOnException $middleware, |
| 51 | + bool $expectedToFail |
| 52 | + ): void { |
| 53 | + FailOnExceptionMiddlewareTestJob::$_middleware = [$middleware]; |
| 54 | + $job = new FailOnExceptionMiddlewareTestJob($thrown); |
| 55 | + $instance = new CallQueuedHandler(new Dispatcher($this->app), $this->app); |
| 56 | + |
| 57 | + $fakeJob = new FakeJob(); |
| 58 | + $job->setJob($fakeJob); |
| 59 | + |
| 60 | + try { |
| 61 | + $instance->call($fakeJob, [ |
| 62 | + 'command' => serialize($job), |
| 63 | + ]); |
| 64 | + $this->fail('Did not throw exception'); |
| 65 | + } catch (Throwable $e) { |
| 66 | + $this->assertInstanceOf($thrown, $e); |
| 67 | + } |
| 68 | + |
| 69 | + $expectedToFail ? $job->assertFailed() : $job->assertNotFailed(); |
| 70 | + } |
| 71 | + |
| 72 | + #[TestWith(['abc', true])] |
| 73 | + #[TestWith(['tots', false])] |
| 74 | + public function test_can_test_against_job_properties($value, bool $expectedToFail): void |
| 75 | + { |
| 76 | + FailOnExceptionMiddlewareTestJob::$_middleware = [ |
| 77 | + new FailOnException(fn ($thrown, $job) => $job->value === 'abc'), |
| 78 | + ]; |
| 79 | + |
| 80 | + $job = new FailOnExceptionMiddlewareTestJob(InvalidArgumentException::class, $value); |
| 81 | + $instance = new CallQueuedHandler(new Dispatcher($this->app), $this->app); |
| 82 | + |
| 83 | + $fakeJob = new FakeJob(); |
| 84 | + $job->setJob($fakeJob); |
| 85 | + |
| 86 | + try { |
| 87 | + $instance->call($fakeJob, [ |
| 88 | + 'command' => serialize($job), |
| 89 | + ]); |
| 90 | + $this->fail('Did not throw exception'); |
| 91 | + } catch (Throwable) { |
| 92 | + // |
| 93 | + } |
| 94 | + |
| 95 | + $expectedToFail ? $job->assertFailed() : $job->assertNotFailed(); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +class FailOnExceptionMiddlewareTestJob implements ShouldQueue |
| 100 | +{ |
| 101 | + use InteractsWithQueue; |
| 102 | + use Queueable; |
| 103 | + use Dispatchable; |
| 104 | + |
| 105 | + public static array $_middleware = []; |
| 106 | + |
| 107 | + public int $tries = 2; |
| 108 | + |
| 109 | + public function __construct(private $throws, public $value = null) |
| 110 | + { |
| 111 | + } |
| 112 | + |
| 113 | + public function handle() |
| 114 | + { |
| 115 | + throw new $this->throws; |
| 116 | + } |
| 117 | + |
| 118 | + public function middleware(): array |
| 119 | + { |
| 120 | + return self::$_middleware; |
| 121 | + } |
| 122 | +} |
0 commit comments