77use PHPUnit \Framework \MockObject \Exception ;
88use PHPUnit \Framework \MockObject \MockObject ;
99use PHPUnit \Framework \TestCase ;
10+ use Queue \App \Message \Message ;
1011use Queue \Swoole \Command \GetQueuedMessagesCommand ;
1112use Redis ;
1213use RedisException ;
1314use Symfony \Component \Console \Command \Command ;
1415use Symfony \Component \Console \Exception \ExceptionInterface ;
1516use Symfony \Component \Console \Input \ArrayInput ;
1617use Symfony \Component \Console \Output \BufferedOutput ;
18+ use Symfony \Component \Messenger \Envelope ;
19+ use Symfony \Component \Messenger \Stamp \RedeliveryStamp ;
1720
21+ use function addslashes ;
1822use function array_keys ;
1923use function count ;
24+ use function json_encode ;
25+ use function serialize ;
2026
2127class GetQueuedMessagesCommandTest extends TestCase
2228{
@@ -42,7 +48,7 @@ public function testExecuteWithNoMessages(): void
4248 ->willReturn ([]);
4349
4450 $ command = new GetQueuedMessagesCommand ($ this ->redisMock );
45- $ input = new ArrayInput ([]);
51+ $ input = new ArrayInput ([' --stream ' => ' messages ' ]);
4652 $ output = new BufferedOutput ();
4753
4854 $ exitCode = $ command ->run ($ input , $ output );
@@ -55,11 +61,11 @@ public function testExecuteWithNoMessages(): void
5561 /**
5662 * @throws ExceptionInterface
5763 */
58- public function testExecuteWithMessages (): void
64+ public function testExecuteWithSimpleMessages (): void
5965 {
6066 $ fakeMessages = [
61- '1691000000000-0 ' => ['type ' => 'testEmail ' , 'payload ' => '{"to":"test@dotkernel .com"} ' ],
62- '1691000000001-0 ' => ['type ' => 'testSms ' , 'payload ' => '{"to":"+123456789 "} ' ],
67+ '1691000000000-0 ' => ['type ' => 'testEmail ' , 'payload ' => '{"to":"test@example .com"} ' ],
68+ '1691000000001-0 ' => [
'type ' =>
'testEmail2 ' ,
'payload ' =>
'{"to":"[email protected] "} ' ],
6369 ];
6470
6571 $ this ->redisMock
@@ -69,7 +75,7 @@ public function testExecuteWithMessages(): void
6975 ->willReturn ($ fakeMessages );
7076
7177 $ command = new GetQueuedMessagesCommand ($ this ->redisMock );
72- $ input = new ArrayInput ([]);
78+ $ input = new ArrayInput ([' --stream ' => ' messages ' ]);
7379 $ output = new BufferedOutput ();
7480
7581 $ exitCode = $ command ->run ($ input , $ output );
@@ -97,10 +103,59 @@ public function testRedisThrowsException(): void
97103 ->willThrowException (new RedisException ('Redis unavailable ' ));
98104
99105 $ command = new GetQueuedMessagesCommand ($ this ->redisMock );
100- $ input = new ArrayInput ([]);
106+ $ input = new ArrayInput ([' --stream ' => ' messages ' ]);
101107 $ output = new BufferedOutput ();
102108
103109 $ this ->expectException (RedisException::class);
104110 $ command ->run ($ input , $ output );
105111 }
112+
113+ public function testExecuteWithInvalidBody (): void
114+ {
115+ $ invalidBodyJson = json_encode (['body ' => 'not-a-serialized-envelope ' ]);
116+
117+ $ this ->redisMock
118+ ->expects ($ this ->once ())
119+ ->method ('xRange ' )
120+ ->willReturn ([
121+ '2-0 ' => ['body ' => $ invalidBodyJson ],
122+ ]);
123+
124+ $ command = new GetQueuedMessagesCommand ($ this ->redisMock );
125+ $ input = new ArrayInput (['--stream ' => 'messages ' ]);
126+ $ output = new BufferedOutput ();
127+
128+ $ exitCode = $ command ->run ($ input , $ output );
129+ $ outputText = $ output ->fetch ();
130+
131+ $ this ->assertEquals (Command::SUCCESS , $ exitCode );
132+ $ this ->assertStringContainsString ('failed to unserialize envelope ' , $ outputText );
133+ }
134+
135+ public function testExecuteWithValidEnvelope (): void
136+ {
137+ $ message = new Message (['foo ' => 'bar ' ]);
138+ $ envelope = new Envelope ($ message , [new RedeliveryStamp (1 )]);
139+ $ serializedEnvelope = serialize ($ envelope );
140+ $ jsonBody = json_encode (['body ' => addslashes ($ serializedEnvelope )]);
141+
142+ $ this ->redisMock
143+ ->expects ($ this ->once ())
144+ ->method ('xRange ' )
145+ ->willReturn ([
146+ '100-0 ' => ['body ' => $ jsonBody ],
147+ ]);
148+
149+ $ command = new GetQueuedMessagesCommand ($ this ->redisMock );
150+ $ input = new ArrayInput (['--stream ' => 'messages ' ]);
151+ $ output = new BufferedOutput ();
152+
153+ $ exitCode = $ command ->run ($ input , $ output );
154+ $ outputText = $ output ->fetch ();
155+
156+ $ this ->assertEquals (Command::SUCCESS , $ exitCode );
157+ $ this ->assertStringContainsString ('Message Class ' , $ outputText );
158+ $ this ->assertStringContainsString ('Payload ' , $ outputText );
159+ $ this ->assertStringContainsString ('Timestamps ' , $ outputText );
160+ }
106161}
0 commit comments