Fix yii server and add tests.

This commit is contained in:
Wilmer Arambula
2023-10-20 10:45:21 -03:00
parent 04f59442d9
commit 70a7282fec
3 changed files with 111 additions and 1 deletions

View File

@ -80,7 +80,13 @@ class ServeController extends Controller
} }
$this->stdout("Quit the server with CTRL-C or COMMAND-C.\n"); $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);
} }
/** /**

View 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);
}
}

View File

@ -0,0 +1,3 @@
<?php
echo "Hello!";