Skip to content

Commit 70a7282

Browse files
committed
Fix yii server and add tests.
1 parent 04f5944 commit 70a7282

File tree

3 files changed

+111
-1
lines changed

3 files changed

+111
-1
lines changed

framework/console/controllers/ServeController.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,13 @@ public function actionIndex($address = 'localhost')
8080
}
8181
$this->stdout("Quit the server with CTRL-C or COMMAND-C.\n");
8282

83-
passthru('"' . PHP_BINARY . '"' . " -S {$address} -t \"{$documentRoot}\" \"$router\"");
83+
$command = '"' . PHP_BINARY . '"' . " -S {$address} -t \"{$documentRoot}\"";
84+
85+
if ($this->router !== null && $router !== '') {
86+
$command .= " -r \"{$router}\"";
87+
}
88+
89+
passthru($command);
8490
}
8591

8692
/**
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
/**
3+
* @link https://www.yiiframework.com/
4+
* @copyright Copyright (c) 2008 Yii Software LLC
5+
* @license https://www.yiiframework.com/license/
6+
*/
7+
8+
namespace yiiunit\framework\console\controllers;
9+
10+
use Yii;
11+
use yii\console\controllers\ServeController;
12+
use yiiunit\TestCase;
13+
14+
/**
15+
* Unit test for [[\yii\console\controllers\ServeController]].
16+
* @see ServeController
17+
*
18+
* @group console
19+
*/
20+
class ServeControllerTest extends TestCase
21+
{
22+
public function setUp()
23+
{
24+
$this->mockApplication();
25+
}
26+
27+
public function testActionIndex()
28+
{
29+
if (!\function_exists('pcntl_fork')) {
30+
$this->markTestSkipped('pcntl_fork() is not available');
31+
}
32+
33+
if (!\function_exists('posix_kill')) {
34+
$this->markTestSkipped('posix_kill() is not available');
35+
}
36+
37+
if (!\function_exists('pcntl_waitpid')) {
38+
$this->markTestSkipped('pcntl_waitpid() is not available');
39+
}
40+
41+
$controller = new ServeController('serve', Yii::$app);
42+
$controller->docroot = __DIR__ . '/stub';
43+
$controller->port = 8080;
44+
45+
$pid = \pcntl_fork();
46+
47+
if ($pid == 0) {
48+
\ob_start();
49+
$controller->actionIndex('localhost');
50+
\ob_get_clean();
51+
exit();
52+
}
53+
54+
\sleep(1);
55+
56+
$response = \file_get_contents('http://localhost:8080');
57+
58+
$this->assertEquals('Hello!', $response);
59+
60+
\posix_kill($pid, \SIGTERM);
61+
\pcntl_waitpid($pid, $status);
62+
}
63+
64+
public function testActionIndexWithRouter()
65+
{
66+
if (!\function_exists('pcntl_fork')) {
67+
$this->markTestSkipped('pcntl_fork() is not available');
68+
}
69+
70+
if (!\function_exists('posix_kill')) {
71+
$this->markTestSkipped('posix_kill() is not available');
72+
}
73+
74+
if (!\function_exists('pcntl_waitpid')) {
75+
$this->markTestSkipped('pcntl_waitpid() is not available');
76+
}
77+
78+
$controller = new ServeController('serve', Yii::$app);
79+
$controller->docroot = __DIR__ . '/stub';
80+
$controller->port = 8081;
81+
$controller->router = __DIR__ . '/stub/index.php';
82+
83+
$pid = \pcntl_fork();
84+
85+
if ($pid == 0) {
86+
\ob_start();
87+
$controller->actionIndex('localhost');
88+
\ob_get_clean();
89+
exit();
90+
}
91+
92+
\sleep(1);
93+
94+
$response = \file_get_contents('http://localhost:8081');
95+
96+
$this->assertEquals('Hello!', $response);
97+
98+
\posix_kill($pid, \SIGTERM);
99+
\pcntl_waitpid($pid, $status);
100+
}
101+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
echo "Hello!";

0 commit comments

Comments
 (0)