3838
3939class AppTest extends TestCase
4040{
41- /**
42- * @var array
43- */
44- private $ serverArgs ;
45-
46- protected function setUp (): void
47- {
48- // Store a snapshot of $_SERVER
49- $ this ->serverArgs = $ _SERVER ;
50- }
51-
52- protected function tearDown (): void
53- {
54- // Restore $_SERVER as it was before
55- foreach ($ _SERVER as $ key => $ value ) {
56- if (!\array_key_exists ($ key , $ this ->serverArgs )) {
57- unset($ _SERVER [$ key ]);
58- continue ;
59- }
60- $ _SERVER [$ key ] = $ value ;
61- }
62- }
63-
6441 public function testConstructWithMiddlewareAssignsGivenMiddleware ()
6542 {
6643 $ middleware = function () { };
@@ -626,14 +603,17 @@ public function testRunWillReportListeningAddressAndRunLoopWithSocketServer()
626603 $ app ->run ();
627604 }
628605
629- public function testRunWillReportListeningAddressFromEnvironmentAndRunLoopWithSocketServer ()
606+ public function testRunWillReportListeningAddressFromContainerEnvironmentAndRunLoopWithSocketServer ()
630607 {
631608 $ socket = @stream_socket_server ('127.0.0.1:0 ' );
632609 $ addr = stream_socket_get_name ($ socket , false );
633610 fclose ($ socket );
634611
635- $ _SERVER ['X_LISTEN ' ] = $ addr ;
636- $ app = new App ();
612+ $ container = new Container ([
613+ 'X_LISTEN ' => $ addr
614+ ]);
615+
616+ $ app = new App ($ container );
637617
638618 // lovely: remove socket server on next tick to terminate loop
639619 Loop::futureTick (function () {
@@ -650,10 +630,13 @@ public function testRunWillReportListeningAddressFromEnvironmentAndRunLoopWithSo
650630 $ app ->run ();
651631 }
652632
653- public function testRunWillReportListeningAddressFromEnvironmentWithRandomPortAndRunLoopWithSocketServer ()
633+ public function testRunWillReportListeningAddressFromContainerEnvironmentWithRandomPortAndRunLoopWithSocketServer ()
654634 {
655- $ _SERVER ['X_LISTEN ' ] = '127.0.0.1:0 ' ;
656- $ app = new App ();
635+ $ container = new Container ([
636+ 'X_LISTEN ' => '127.0.0.1:0 '
637+ ]);
638+
639+ $ app = new App ($ container );
657640
658641 // lovely: remove socket server on next tick to terminate loop
659642 Loop::futureTick (function () {
@@ -672,8 +655,11 @@ public function testRunWillReportListeningAddressFromEnvironmentWithRandomPortAn
672655
673656 public function testRunWillRestartLoopUntilSocketIsClosed ()
674657 {
675- $ _SERVER ['X_LISTEN ' ] = '127.0.0.1:0 ' ;
676- $ app = new App ();
658+ $ container = new Container ([
659+ 'X_LISTEN ' => '127.0.0.1:0 '
660+ ]);
661+
662+ $ app = new App ($ container );
677663
678664 // lovely: remove socket server on next tick to terminate loop
679665 Loop::futureTick (function () {
@@ -700,8 +686,11 @@ public function testRunWillRestartLoopUntilSocketIsClosed()
700686 */
701687 public function testRunWillStopWhenReceivingSigint ()
702688 {
703- $ _SERVER ['X_LISTEN ' ] = '127.0.0.1:0 ' ;
704- $ app = new App ();
689+ $ container = new Container ([
690+ 'X_LISTEN ' => '127.0.0.1:0 '
691+ ]);
692+
693+ $ app = new App ($ container );
705694
706695 Loop::futureTick (function () {
707696 posix_kill (getmypid (), defined ('SIGINT ' ) ? SIGINT : 2 );
@@ -717,8 +706,11 @@ public function testRunWillStopWhenReceivingSigint()
717706 */
718707 public function testRunWillStopWhenReceivingSigterm ()
719708 {
720- $ _SERVER ['X_LISTEN ' ] = '127.0.0.1:0 ' ;
721- $ app = new App ();
709+ $ container = new Container ([
710+ 'X_LISTEN ' => '127.0.0.1:0 '
711+ ]);
712+
713+ $ app = new App ($ container );
722714
723715 Loop::futureTick (function () {
724716 posix_kill (getmypid (), defined ('SIGTERM ' ) ? SIGTERM : 15 );
@@ -730,8 +722,12 @@ public function testRunWillStopWhenReceivingSigterm()
730722
731723 public function testRunAppWithEmptyAddressThrows ()
732724 {
733- $ _SERVER ['X_LISTEN ' ] = '' ;
734- $ app = new App ();
725+ $ container = new Container ([
726+ 'X_LISTEN ' => ''
727+ ]);
728+
729+ $ app = new App ($ container );
730+
735731
736732 $ this ->expectException (\InvalidArgumentException::class);
737733 $ app ->run ();
@@ -746,8 +742,11 @@ public function testRunAppWithBusyPortThrows()
746742 $ this ->markTestSkipped ('System does not prevent listening on same address twice ' );
747743 }
748744
749- $ _SERVER ['X_LISTEN ' ] = $ addr ;
750- $ app = new App ();
745+ $ container = new Container ([
746+ 'X_LISTEN ' => $ addr
747+ ]);
748+
749+ $ app = new App ($ container );
751750
752751 $ this ->expectException (\RuntimeException::class);
753752 $ this ->expectExceptionMessage ('Failed to listen on ' );
0 commit comments