mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-14 06:11:35 +08:00
Fix yii server
and add tests.
This commit is contained in:
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
101
tests/framework/console/controllers/ServeControllerTest.php
Normal file
101
tests/framework/console/controllers/ServeControllerTest.php
Normal file
@ -0,0 +1,101 @@
|
||||
<?php
|
||||
/**
|
||||
* @link https://www.yiiframework.com/
|
||||
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||
* @license https://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
namespace yiiunit\framework\console\controllers;
|
||||
|
||||
use Yii;
|
||||
use yii\console\controllers\ServeController;
|
||||
use yiiunit\TestCase;
|
||||
|
||||
/**
|
||||
* Unit test for [[\yii\console\controllers\ServeController]].
|
||||
* @see ServeController
|
||||
*
|
||||
* @group console
|
||||
*/
|
||||
class ServeControllerTest extends TestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
$this->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);
|
||||
}
|
||||
}
|
3
tests/framework/console/controllers/stub/index.php
Normal file
3
tests/framework/console/controllers/stub/index.php
Normal file
@ -0,0 +1,3 @@
|
||||
<?php
|
||||
|
||||
echo "Hello!";
|
Reference in New Issue
Block a user