|
11 | 11 | use Psr\Container\ContainerExceptionInterface; |
12 | 12 | use Queue\App\Message\Message; |
13 | 13 | use Queue\App\Message\MessageHandler; |
14 | | -use Symfony\Component\Messenger\Envelope; |
15 | | -use Symfony\Component\Messenger\Exception\ExceptionInterface; |
| 14 | +use RuntimeException; |
16 | 15 | use Symfony\Component\Messenger\MessageBusInterface; |
17 | | -use Symfony\Component\Messenger\Stamp\DelayStamp; |
18 | 16 |
|
19 | 17 | class MessageHandlerTest extends TestCase |
20 | 18 | { |
@@ -60,94 +58,51 @@ protected function setUp(): void |
60 | 58 | $this->handler = new MessageHandler($this->bus, $this->logger, $this->config); |
61 | 59 | } |
62 | 60 |
|
63 | | - /** |
64 | | - * @throws Exception |
65 | | - */ |
66 | | - public function testInvokeSuccessfulProcessing(): void |
| 61 | + public function testControlMessageDoesNotThrowAndDoesNotSetRetryCount(): void |
67 | 62 | { |
68 | | - $payload = ['foo' => 'control']; |
69 | | - $message = $this->createMock(Message::class); |
70 | | - $message->method('getPayload')->willReturn($payload); |
| 63 | + $handler = $this->handler; |
71 | 64 |
|
72 | | - $this->handler->__invoke($message); |
| 65 | + $message = new Message(['foo' => 'control']); |
| 66 | + $handler($message); |
73 | 67 |
|
74 | | - $this->expectNotToPerformAssertions(); |
| 68 | + $payload = $message->getPayload(); |
| 69 | + $this->assertArrayNotHasKey('retry_count', $payload); |
75 | 70 | } |
76 | 71 |
|
77 | | - /** |
78 | | - * @throws Exception |
79 | | - */ |
80 | | - public function testInvokeFailureTriggersFirstRetry(): void |
| 72 | + public function testRetryMessageThrowsExceptionAndSetsRetryCount(): void |
81 | 73 | { |
82 | | - $payload = ['foo' => 'fail']; |
83 | | - $message = $this->createMock(Message::class); |
84 | | - $message->method('getPayload')->willReturn($payload); |
85 | | - |
86 | | - $this->bus->expects($this->once()) |
87 | | - ->method('dispatch') |
88 | | - ->with( |
89 | | - $this->callback(function ($msg) { |
90 | | - return $msg instanceof Message |
91 | | - && $msg->getPayload()['foo'] === 'fail' |
92 | | - && $msg->getPayload()['retry'] === 1; |
93 | | - }), |
94 | | - $this->callback(function ($stamps) { |
95 | | - return isset($stamps[0]) && $stamps[0] instanceof DelayStamp |
96 | | - && $stamps[0]->getDelay() === 1000; |
97 | | - }) |
98 | | - ) |
99 | | - ->willReturn(new Envelope($message)); |
100 | | - |
101 | | - $this->handler->__invoke($message); |
102 | | - } |
| 74 | + $handler = $this->handler; |
103 | 75 |
|
104 | | - /** |
105 | | - * @throws ExceptionInterface |
106 | | - */ |
107 | | - public function testRetrySecondTime(): void |
108 | | - { |
109 | | - $payload = ['foo' => 'retry_test', 'retry' => 1]; |
110 | | - |
111 | | - $this->bus->expects($this->once()) |
112 | | - ->method('dispatch') |
113 | | - ->with( |
114 | | - $this->callback(function ($msg) { |
115 | | - return $msg instanceof Message |
116 | | - && $msg->getPayload()['retry'] === 2 |
117 | | - && $msg->getPayload()['foo'] === 'retry_test'; |
118 | | - }), |
119 | | - $this->callback(function ($stamps) { |
120 | | - return isset($stamps[0]) && $stamps[0] instanceof DelayStamp |
121 | | - && $stamps[0]->getDelay() === 2000; |
122 | | - }) |
123 | | - ) |
124 | | - ->willReturn(new Envelope(new Message($payload))); |
125 | | - |
126 | | - $this->handler->retry($payload); |
| 76 | + $message = new Message(['foo' => 'retry']); |
| 77 | + |
| 78 | + $this->expectException(RuntimeException::class); |
| 79 | + $this->expectExceptionMessage("Intentional failure for testing retries"); |
| 80 | + |
| 81 | + try { |
| 82 | + $handler($message); |
| 83 | + } finally { |
| 84 | + $payload = $message->getPayload(); |
| 85 | + $this->assertArrayHasKey('retry_count', $payload); |
| 86 | + $this->assertEquals(1, $payload['retry_count']); // first retry |
| 87 | + } |
127 | 88 | } |
128 | 89 |
|
129 | | - /** |
130 | | - * @throws ExceptionInterface |
131 | | - */ |
132 | | - public function testRetryThirdTime(): void |
| 90 | + public function testRetryMessageWithExistingRetryCountIncrementsIt(): void |
133 | 91 | { |
134 | | - $payload = ['foo' => 'retry_test', 'retry' => 2]; |
135 | | - |
136 | | - $this->bus->expects($this->once()) |
137 | | - ->method('dispatch') |
138 | | - ->with( |
139 | | - $this->callback(function ($msg) { |
140 | | - return $msg instanceof Message |
141 | | - && $msg->getPayload()['retry'] === 3 |
142 | | - && $msg->getPayload()['foo'] === 'retry_test'; |
143 | | - }), |
144 | | - $this->callback(function ($stamps) { |
145 | | - return isset($stamps[0]) && $stamps[0] instanceof DelayStamp |
146 | | - && $stamps[0]->getDelay() === 3000; |
147 | | - }) |
148 | | - ) |
149 | | - ->willReturn(new Envelope(new Message($payload))); |
150 | | - |
151 | | - $this->handler->retry($payload); |
| 92 | + $handler = $this->handler; |
| 93 | + |
| 94 | + $message = new Message([ |
| 95 | + 'foo' => 'retry', |
| 96 | + 'retry_count' => 2, |
| 97 | + ]); |
| 98 | + |
| 99 | + $this->expectException(RuntimeException::class); |
| 100 | + |
| 101 | + try { |
| 102 | + $handler($message); |
| 103 | + } finally { |
| 104 | + $payload = $message->getPayload(); |
| 105 | + $this->assertEquals(3, $payload['retry_count']); // incremented from 2 → 3 |
| 106 | + } |
152 | 107 | } |
153 | 108 | } |
0 commit comments