Skip to content

Commit

Permalink
Fix yii server and add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Oct 20, 2023
1 parent 04f5944 commit 70a7282
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 1 deletion.
8 changes: 7 additions & 1 deletion framework/console/controllers/ServeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,13 @@ public function actionIndex($address = 'localhost')
}
$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);
}

/**
Expand Down
101 changes: 101 additions & 0 deletions tests/framework/console/controllers/ServeControllerTest.php
Original file line number Diff line number Diff line change
@@ -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 changes: 3 additions & 0 deletions tests/framework/console/controllers/stub/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

echo "Hello!";

0 comments on commit 70a7282

Please sign in to comment.