diff --git a/framework/console/controllers/ServeController.php b/framework/console/controllers/ServeController.php index d02c982042..4ba39240f6 100644 --- a/framework/console/controllers/ServeController.php +++ b/framework/console/controllers/ServeController.php @@ -80,7 +80,13 @@ class ServeController extends Controller } $this->stdout("Quit the server with CTRL-C or COMMAND-C.\n"); - passthru('"' . PHP_BINARY . '"' . " -S {$address} -t \"{$documentRoot}\" \"$router\""); + $command = '"' . PHP_BINARY . '"' . " -S {$address} -t \"{$documentRoot}\""; + + if ($this->router !== null && $router !== '') { + $command .= " -r \"{$router}\""; + } + + passthru($command); } /** diff --git a/tests/framework/console/controllers/ServeControllerTest.php b/tests/framework/console/controllers/ServeControllerTest.php new file mode 100644 index 0000000000..af8af4eaaf --- /dev/null +++ b/tests/framework/console/controllers/ServeControllerTest.php @@ -0,0 +1,101 @@ +mockApplication(); + } + + public function testActionIndex() + { + if (!\function_exists('pcntl_fork')) { + $this->markTestSkipped('pcntl_fork() is not available'); + } + + if (!\function_exists('posix_kill')) { + $this->markTestSkipped('posix_kill() is not available'); + } + + if (!\function_exists('pcntl_waitpid')) { + $this->markTestSkipped('pcntl_waitpid() is not available'); + } + + $controller = new ServeController('serve', Yii::$app); + $controller->docroot = __DIR__ . '/stub'; + $controller->port = 8080; + + $pid = \pcntl_fork(); + + if ($pid == 0) { + \ob_start(); + $controller->actionIndex('localhost'); + \ob_get_clean(); + exit(); + } + + \sleep(1); + + $response = \file_get_contents('http://localhost:8080'); + + $this->assertEquals('Hello!', $response); + + \posix_kill($pid, \SIGTERM); + \pcntl_waitpid($pid, $status); + } + + public function testActionIndexWithRouter() + { + if (!\function_exists('pcntl_fork')) { + $this->markTestSkipped('pcntl_fork() is not available'); + } + + if (!\function_exists('posix_kill')) { + $this->markTestSkipped('posix_kill() is not available'); + } + + if (!\function_exists('pcntl_waitpid')) { + $this->markTestSkipped('pcntl_waitpid() is not available'); + } + + $controller = new ServeController('serve', Yii::$app); + $controller->docroot = __DIR__ . '/stub'; + $controller->port = 8081; + $controller->router = __DIR__ . '/stub/index.php'; + + $pid = \pcntl_fork(); + + if ($pid == 0) { + \ob_start(); + $controller->actionIndex('localhost'); + \ob_get_clean(); + exit(); + } + + \sleep(1); + + $response = \file_get_contents('http://localhost:8081'); + + $this->assertEquals('Hello!', $response); + + \posix_kill($pid, \SIGTERM); + \pcntl_waitpid($pid, $status); + } +} diff --git a/tests/framework/console/controllers/stub/index.php b/tests/framework/console/controllers/stub/index.php new file mode 100644 index 0000000000..8b57cd3113 --- /dev/null +++ b/tests/framework/console/controllers/stub/index.php @@ -0,0 +1,3 @@ +