@@ -24,78 +24,111 @@ public function setUp()
24
24
$ this ->mockApplication ();
25
25
}
26
26
27
- public function testActionIndex ()
27
+ public function testAddressTaken ()
28
28
{
29
- if (!\function_exists ('pcntl_fork ' )) {
30
- $ this ->markTestSkipped ('pcntl_fork() is not available ' );
31
- }
29
+ $ docroot = __DIR__ . '/stub ' ;
32
30
33
- if (!\function_exists ('posix_kill ' )) {
34
- $ this ->markTestSkipped ('posix_kill() is not available ' );
35
- }
31
+ /** @var ServeController $serveController */
32
+ $ serveController = $ this ->getMockBuilder (ServeControllerMock::class)
33
+ ->setConstructorArgs (['serve ' , Yii::$ app ])
34
+ ->setMethods (['isAddressTaken ' ])
35
+ ->getMock ();
36
36
37
- if (!\function_exists ('pcntl_waitpid ' )) {
38
- $ this ->markTestSkipped ('pcntl_waitpid() is not available ' );
39
- }
37
+ $ serveController ->expects ($ this ->once ())->method ('isAddressTaken ' )->willReturn (true );
40
38
41
- $ controller = new ServeController ('serve ' , Yii::$ app );
42
- $ controller ->docroot = __DIR__ . '/stub ' ;
43
- $ controller ->port = 8080 ;
39
+ $ serveController ->docroot = $ docroot ;
40
+ $ serveController ->port = 8080 ;
44
41
45
- $ pid = \pcntl_fork ();
42
+ ob_start ();
43
+ $ serveController ->actionIndex ('localhost:8080 ' );
44
+ ob_end_clean ();
46
45
47
- if ($ pid == 0 ) {
48
- \ob_start ();
49
- $ controller ->actionIndex ('localhost ' );
50
- \ob_get_clean ();
51
- exit ();
52
- }
46
+ $ result = $ serveController ->flushStdOutBuffer ();
53
47
54
- \sleep (1 );
48
+ $ this ->assertContains ('http://localhost:8080 is taken by another process. ' , $ result );
49
+ }
50
+
51
+ public function testDefautlValues ()
52
+ {
53
+ $ docroot = __DIR__ . '/stub ' ;
54
+
55
+ $ serveController = new ServeControllerMock ('serve ' , Yii::$ app );
56
+ $ serveController ->docroot = $ docroot ;
57
+ $ serveController ->port = 8080 ;
58
+
59
+ ob_start ();
60
+ $ serveController ->actionIndex ();
61
+ ob_end_clean ();
62
+
63
+ $ result = $ serveController ->flushStdOutBuffer ();
64
+
65
+ $ this ->assertContains ('Server started on http://localhost:8080 ' , $ result );
66
+ $ this ->assertContains ("Document root is \"{$ docroot }\"" , $ result );
67
+ $ this ->assertContains ('Quit the server with CTRL-C or COMMAND-C. ' , $ result );
68
+ }
69
+
70
+ public function testDoocRootWithNoExistValue ()
71
+ {
72
+ $ docroot = '/not/exist/path ' ;
73
+
74
+ $ serveController = new ServeControllerMock ('serve ' , Yii::$ app );
75
+ $ serveController ->docroot = $ docroot ;
55
76
56
- $ response = \file_get_contents ('http://localhost:8080 ' );
77
+ ob_start ();
78
+ $ serveController ->actionIndex ();
79
+ ob_end_clean ();
57
80
58
- $ this -> assertEquals ( ' Hello! ' , $ response );
81
+ $ result = $ serveController -> flushStdOutBuffer ( );
59
82
60
- \posix_kill ($ pid , \SIGTERM );
61
- \pcntl_waitpid ($ pid , $ status );
83
+ $ this ->assertContains ("Document root \"{$ docroot }\" does not exist. " , $ result );
62
84
}
63
85
64
- public function testActionIndexWithRouter ()
86
+ public function testWithRouterNoExistValue ()
65
87
{
66
- if (!\function_exists ('pcntl_fork ' )) {
67
- $ this ->markTestSkipped ('pcntl_fork() is not available ' );
68
- }
88
+ $ docroot = __DIR__ . '/stub ' ;
89
+ $ router = '/not/exist/path ' ;
69
90
70
- if (!\function_exists ('posix_kill ' )) {
71
- $ this ->markTestSkipped ('posix_kill() is not available ' );
72
- }
91
+ $ serveController = new ServeControllerMock ('serve ' , Yii::$ app );
92
+ $ serveController ->docroot = $ docroot ;
93
+ $ serveController ->port = 8081 ;
94
+ $ serveController ->router = $ router ;
73
95
74
- if (! \function_exists ( ' pcntl_waitpid ' )) {
75
- $ this -> markTestSkipped ( ' pcntl_waitpid() is not available ' );
76
- }
96
+ ob_start ();
97
+ $ serveController -> actionIndex ( );
98
+ ob_end_clean ();
77
99
78
- $ controller = new ServeController ('serve ' , Yii::$ app );
79
- $ controller ->docroot = __DIR__ . '/stub ' ;
80
- $ controller ->port = 8081 ;
81
- $ controller ->router = __DIR__ . '/stub/index.php ' ;
100
+ $ result = $ serveController ->flushStdOutBuffer ();
82
101
83
- $ pid = \pcntl_fork ();
102
+ $ this ->assertContains ("Routing file \"$ router \" does not exist. " , $ result );
103
+ }
84
104
85
- if ($ pid == 0 ) {
86
- \ob_start ();
87
- $ controller ->actionIndex ('localhost ' );
88
- \ob_get_clean ();
89
- exit ();
90
- }
105
+ public function testWithRouterValue ()
106
+ {
107
+ $ docroot = __DIR__ . '/stub ' ;
108
+ $ router = __DIR__ . '/stub/index.php ' ;
91
109
92
- \sleep (1 );
110
+ $ serveController = new ServeControllerMock ('serve ' , Yii::$ app );
111
+ $ serveController ->docroot = $ docroot ;
112
+ $ serveController ->port = 8081 ;
113
+ $ serveController ->router = $ router ;
93
114
94
- $ response = \file_get_contents ('http://localhost:8081 ' );
115
+ ob_start ();
116
+ $ serveController ->actionIndex ();
117
+ ob_end_clean ();
95
118
96
- $ this -> assertEquals ( ' Hello! ' , $ response );
119
+ $ result = $ serveController -> flushStdOutBuffer ( );
97
120
98
- \posix_kill ($ pid , \SIGTERM );
99
- \pcntl_waitpid ($ pid , $ status );
121
+ $ this ->assertContains ('Server started on http://localhost:8081 ' , $ result );
122
+ $ this ->assertContains ("Document root is \"{$ docroot }\"" , $ result );
123
+ $ this ->assertContains ("Routing file is \"{$ router }\"" , $ result );
124
+ $ this ->assertContains ('Quit the server with CTRL-C or COMMAND-C. ' , $ result );
100
125
}
101
126
}
127
+
128
+ /**
129
+ * Mock class for [[\yii\console\controllers\ServeController]].
130
+ */
131
+ class ServeControllerMock extends ServeController
132
+ {
133
+ use StdOutBufferControllerTrait;
134
+ }
0 commit comments