1111use Psr \Container \ContainerExceptionInterface ;
1212use Queue \App \Message \Message ;
1313use Queue \App \Message \MessageHandler ;
14- use Symfony \Component \Messenger \Envelope ;
15- use Symfony \Component \Messenger \Exception \ExceptionInterface ;
14+ use RuntimeException ;
1615use Symfony \Component \Messenger \MessageBusInterface ;
17- use Symfony \Component \Messenger \Stamp \DelayStamp ;
18-
19- use const PHP_EOL ;
2016
2117class MessageHandlerTest extends TestCase
2218{
2319 private MessageBusInterface |MockObject $ bus ;
20+ private Logger $ logger ;
21+ private array $ config ;
2422 private MessageHandler $ handler ;
2523
2624 /**
@@ -29,17 +27,16 @@ class MessageHandlerTest extends TestCase
2927 */
3028 protected function setUp (): void
3129 {
32- $ this ->bus = $ this ->createMock (MessageBusInterface::class);
33-
34- $ logger = new Logger ([
30+ $ this ->bus = $ this ->createMock (MessageBusInterface::class);
31+ $ this ->logger = new Logger ([
3532 'writers ' => [
3633 'FileWriter ' => [
3734 'name ' => 'null ' ,
3835 'level ' => Logger::ALERT ,
3936 ],
4037 ],
4138 ]);
42- $ config = [
39+ $ this -> config = [
4340 'fail-safe ' => [
4441 'first_retry ' => 1000 ,
4542 'second_retry ' => 2000 ,
@@ -50,107 +47,62 @@ protected function setUp(): void
5047 'protocol ' => 'tcp ' ,
5148 'host ' => 'localhost ' ,
5249 'port ' => '8556 ' ,
53- 'eof ' => PHP_EOL ,
50+ 'eof ' => "\n" ,
5451 ],
5552 ],
5653 'application ' => [
5754 'name ' => 'dotkernel ' ,
5855 ],
5956 ];
6057
61- $ this ->handler = new MessageHandler ($ this ->bus , $ logger , $ config );
58+ $ this ->handler = new MessageHandler ($ this ->bus , $ this -> logger , $ this -> config );
6259 }
6360
64- /**
65- * @throws Exception
66- * @throws ExceptionInterface
67- */
68- public function testInvokeSuccessfulProcessing (): void
61+ public function testControlMessageDoesNotThrowAndDoesNotSetRetryCount (): void
6962 {
70- $ payload = ['foo ' => 'control ' ];
71- $ message = $ this ->createMock (Message::class);
72- $ message ->method ('getPayload ' )->willReturn ($ payload );
63+ $ handler = $ this ->handler ;
7364
74- $ this ->handler ->__invoke ($ message );
65+ $ message = new Message (['foo ' => 'control ' ]);
66+ $ handler ($ message );
7567
76- $ this ->expectNotToPerformAssertions ();
68+ $ payload = $ message ->getPayload ();
69+ $ this ->assertArrayNotHasKey ('retry_count ' , $ payload );
7770 }
7871
79- /**
80- * @throws Exception
81- * @throws ExceptionInterface
82- */
83- public function testInvokeFailureTriggersFirstRetry (): void
72+ public function testRetryMessageThrowsExceptionAndSetsRetryCount (): void
8473 {
85- $ payload = ['foo ' => 'fail ' ];
86- $ message = $ this ->createMock (Message::class);
87- $ message ->method ('getPayload ' )->willReturn ($ payload );
88-
89- $ this ->bus ->expects ($ this ->once ())
90- ->method ('dispatch ' )
91- ->with (
92- $ this ->callback (function ($ msg ) {
93- return $ msg instanceof Message
94- && $ msg ->getPayload ()['foo ' ] === 'fail '
95- && $ msg ->getPayload ()['retry ' ] === 1 ;
96- }),
97- $ this ->callback (function ($ stamps ) {
98- return isset ($ stamps [0 ]) && $ stamps [0 ] instanceof DelayStamp
99- && $ stamps [0 ]->getDelay () === 1000 ;
100- })
101- )
102- ->willReturn (new Envelope ($ message ));
103-
104- $ this ->handler ->__invoke ($ message );
105- }
74+ $ handler = $ this ->handler ;
10675
107- /**
108- * @throws ExceptionInterface
109- */
110- public function testRetrySecondTime (): void
111- {
112- $ payload = ['foo ' => 'retry_test ' , 'retry ' => 1 ];
113-
114- $ this ->bus ->expects ($ this ->once ())
115- ->method ('dispatch ' )
116- ->with (
117- $ this ->callback (function ($ msg ) {
118- return $ msg instanceof Message
119- && $ msg ->getPayload ()['retry ' ] === 2
120- && $ msg ->getPayload ()['foo ' ] === 'retry_test ' ;
121- }),
122- $ this ->callback (function ($ stamps ) {
123- return isset ($ stamps [0 ]) && $ stamps [0 ] instanceof DelayStamp
124- && $ stamps [0 ]->getDelay () === 2000 ;
125- })
126- )
127- ->willReturn (new Envelope (new Message ($ payload )));
128-
129- $ 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+ }
13088 }
13189
132- /**
133- * @throws ExceptionInterface
134- */
135- public function testRetryThirdTime (): void
90+ public function testRetryMessageWithExistingRetryCountIncrementsIt (): void
13691 {
137- $ payload = ['foo ' => 'retry_test ' , 'retry ' => 2 ];
138-
139- $ this ->bus ->expects ($ this ->once ())
140- ->method ('dispatch ' )
141- ->with (
142- $ this ->callback (function ($ msg ) {
143- return $ msg instanceof Message
144- && $ msg ->getPayload ()['retry ' ] === 3
145- && $ msg ->getPayload ()['foo ' ] === 'retry_test ' ;
146- }),
147- $ this ->callback (function ($ stamps ) {
148- return isset ($ stamps [0 ]) && $ stamps [0 ] instanceof DelayStamp
149- && $ stamps [0 ]->getDelay () === 3000 ;
150- })
151- )
152- ->willReturn (new Envelope (new Message ($ payload )));
153-
154- $ 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+ }
155107 }
156108}
0 commit comments