1717use Ymir \Runtime \Lambda \Handler \ConsoleCommandLambdaEventHandler ;
1818use Ymir \Runtime \Lambda \Response \ProcessResponse ;
1919use Ymir \Runtime \Tests \Mock \ConsoleCommandEventMockTrait ;
20+ use Ymir \Runtime \Tests \Mock \ContextMockTrait ;
2021use Ymir \Runtime \Tests \Mock \InvocationEventInterfaceMockTrait ;
2122use Ymir \Runtime \Tests \Mock \LoggerMockTrait ;
2223
2324class ConsoleCommandLambdaEventHandlerTest extends TestCase
2425{
2526 use ConsoleCommandEventMockTrait;
27+ use ContextMockTrait;
2628 use InvocationEventInterfaceMockTrait;
2729 use LoggerMockTrait;
2830
31+ public static function provideTimeouts (): \Iterator
32+ {
33+ yield [60000 , 59.0 ];
34+ yield [10000 , 9.0 ];
35+ yield [2000 , 1.0 ];
36+ yield [1000 , 1.0 ];
37+ yield [500 , 1.0 ];
38+ }
39+
2940 public function testCanHandlePingEventType (): void
3041 {
3142 $ this ->assertTrue ((new ConsoleCommandLambdaEventHandler ($ this ->getLoggerMock ()))->canHandle ($ this ->getConsoleCommandEventMock ()));
@@ -36,15 +47,56 @@ public function testCanHandleWrongEventType(): void
3647 $ this ->assertFalse ((new ConsoleCommandLambdaEventHandler ($ this ->getLoggerMock ()))->canHandle ($ this ->getInvocationEventInterfaceMock ()));
3748 }
3849
50+ /**
51+ * @dataProvider provideTimeouts
52+ */
53+ public function testHandleSetsCorrectTimeout (int $ remainingTimeInMs , float $ expectedTimeout ): void
54+ {
55+ $ context = $ this ->getContextMock ();
56+ $ event = $ this ->getConsoleCommandEventMock ();
57+ $ logger = $ this ->getLoggerMock ();
58+
59+ $ event ->expects ($ this ->once ())
60+ ->method ('getCommand ' )
61+ ->willReturn ('ls -la ' );
62+
63+ $ event ->expects ($ this ->once ())
64+ ->method ('getContext ' )
65+ ->willReturn ($ context );
66+
67+ $ context ->expects ($ this ->once ())
68+ ->method ('getRemainingTimeInMs ' )
69+ ->willReturn ($ remainingTimeInMs );
70+
71+ $ response = (new ConsoleCommandLambdaEventHandler ($ logger ))->handle ($ event );
72+
73+ $ reflection = new \ReflectionClass (ProcessResponse::class);
74+ $ property = $ reflection ->getProperty ('process ' );
75+ $ property ->setAccessible (true );
76+
77+ $ process = $ property ->getValue ($ response );
78+
79+ $ this ->assertEquals ($ expectedTimeout , $ process ->getTimeout ());
80+ }
81+
3982 public function testHandleWithSuccessfulCommand (): void
4083 {
84+ $ context = $ this ->getContextMock ();
4185 $ event = $ this ->getConsoleCommandEventMock ();
4286 $ logger = $ this ->getLoggerMock ();
4387
4488 $ event ->expects ($ this ->once ())
4589 ->method ('getCommand ' )
4690 ->willReturn ('ls -la ' );
4791
92+ $ event ->expects ($ this ->once ())
93+ ->method ('getContext ' )
94+ ->willReturn ($ context );
95+
96+ $ context ->expects ($ this ->once ())
97+ ->method ('getRemainingTimeInMs ' )
98+ ->willReturn (60000 );
99+
48100 $ logger ->expects ($ this ->once ())
49101 ->method ('info ' );
50102
@@ -58,13 +110,22 @@ public function testHandleWithSuccessfulCommand(): void
58110
59111 public function testHandleWithUnsuccessfulCommand (): void
60112 {
113+ $ context = $ this ->getContextMock ();
61114 $ event = $ this ->getConsoleCommandEventMock ();
62115 $ logger = $ this ->getLoggerMock ();
63116
64117 $ event ->expects ($ this ->once ())
65118 ->method ('getCommand ' )
66119 ->willReturn ('foo ' );
67120
121+ $ event ->expects ($ this ->once ())
122+ ->method ('getContext ' )
123+ ->willReturn ($ context );
124+
125+ $ context ->expects ($ this ->once ())
126+ ->method ('getRemainingTimeInMs ' )
127+ ->willReturn (60000 );
128+
68129 $ logger ->expects ($ this ->once ())
69130 ->method ('info ' );
70131
0 commit comments