Skip to content

Commit bc56545

Browse files
authored
Merge pull request #20021 from terabytesoftw/yii-serve-fix
Fix `yii server` and add tests.
2 parents 5eeb12f + bee7f72 commit bc56545

File tree

4 files changed

+179
-1
lines changed

4 files changed

+179
-1
lines changed

framework/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Yii Framework 2 Change Log
44
2.0.50 under development
55
------------------------
66

7+
- Bug #20005: Fix `yii\console\controllers\ServeController` to specify the router script (terabytesoftw)
78
- Bug #19060: Fix `yii\widgets\Menu` bug when using Closure for active item and adding additional tests in `tests\framework\widgets\MenuTest` (atrandafir)
89
- Bug #13920: Fixed erroneous validation for specific cases (tim-fischer-maschinensucher)
910
- Bug #19927: Fixed `console\controllers\MessageController` when saving translations to database: fixed FK error when adding new string and language at the same time, checking/regenerating all missing messages and dropping messages for unused languages (atrandafir)

framework/console/controllers/ServeController.php

Lines changed: 12 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+
$this->runCommand($command);
8490
}
8591

8692
/**
@@ -122,4 +128,9 @@ protected function isAddressTaken($address)
122128
fclose($fp);
123129
return true;
124130
}
131+
132+
protected function runCommand($command)
133+
{
134+
passthru($command);
135+
}
125136
}
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
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 testAddressTaken()
28+
{
29+
$docroot = __DIR__ . '/stub';
30+
31+
/** @var ServeController $serveController */
32+
$serveController = $this->getMockBuilder(ServeControllerMocK::className())
33+
->setConstructorArgs(['serve', Yii::$app])
34+
->setMethods(['isAddressTaken', 'runCommand'])
35+
->getMock();
36+
37+
$serveController->expects($this->once())->method('isAddressTaken')->willReturn(true);
38+
$serveController->expects($this->never())->method('runCommand');
39+
40+
$serveController->docroot = $docroot;
41+
$serveController->port = 8080;
42+
43+
ob_start();
44+
$serveController->actionIndex('localhost:8080');
45+
ob_end_clean();
46+
47+
$result = $serveController->flushStdOutBuffer();
48+
49+
$this->assertContains('http://localhost:8080 is taken by another process.', $result);
50+
}
51+
52+
public function testDefaultValues()
53+
{
54+
$docroot = __DIR__ . '/stub';
55+
56+
/** @var ServeController $serveController */
57+
$serveController = $this->getMockBuilder(ServeControllerMock::className())
58+
->setConstructorArgs(['serve', Yii::$app])
59+
->setMethods(['runCommand'])
60+
->getMock();
61+
62+
$serveController->docroot = $docroot;
63+
$serveController->port = 8080;
64+
65+
$serveController->expects($this->once())->method('runCommand')->willReturn(true);
66+
67+
ob_start();
68+
$serveController->actionIndex();
69+
ob_end_clean();
70+
71+
$result = $serveController->flushStdOutBuffer();
72+
73+
$this->assertContains('Server started on http://localhost:8080', $result);
74+
$this->assertContains("Document root is \"{$docroot}\"", $result);
75+
$this->assertContains('Quit the server with CTRL-C or COMMAND-C.', $result);
76+
}
77+
78+
public function testDoocRootWithNoExistValue()
79+
{
80+
$docroot = '/not/exist/path';
81+
82+
/** @var ServeController $serveController */
83+
$serveController = $this->getMockBuilder(ServeControllerMock::className())
84+
->setConstructorArgs(['serve', Yii::$app])
85+
->setMethods(['runCommand'])
86+
->getMock();
87+
88+
$serveController->docroot = $docroot;
89+
90+
$serveController->expects($this->any())->method('runCommand')->willReturn(true);
91+
92+
ob_start();
93+
$serveController->actionIndex();
94+
ob_end_clean();
95+
96+
$result = $serveController->flushStdOutBuffer();
97+
98+
$this->assertContains("Document root \"{$docroot}\" does not exist.", $result);
99+
}
100+
101+
public function testWithRouterNoExistValue()
102+
{
103+
$docroot = __DIR__ . '/stub';
104+
$router = '/not/exist/path';
105+
106+
/** @var ServeController $serveController */
107+
$serveController = $this->getMockBuilder(ServeControllerMock::className())
108+
->setConstructorArgs(['serve', Yii::$app])
109+
->setMethods(['runCommand'])
110+
->getMock();
111+
112+
$serveController->docroot = $docroot;
113+
$serveController->port = 8081;
114+
$serveController->router = $router;
115+
116+
$serveController->expects($this->any())->method('runCommand')->willReturn(true);
117+
118+
ob_start();
119+
$serveController->actionIndex();
120+
ob_end_clean();
121+
122+
$result = $serveController->flushStdOutBuffer();
123+
124+
$this->assertContains("Routing file \"$router\" does not exist.", $result);
125+
}
126+
127+
public function testWithRouterValue()
128+
{
129+
$docroot = __DIR__ . '/stub';
130+
$router = __DIR__ . '/stub/index.php';
131+
132+
/** @var ServeController $serveController */
133+
$serveController = $this->getMockBuilder(ServeControllerMock::className())
134+
->setConstructorArgs(['serve', Yii::$app])
135+
->setMethods(['runCommand'])
136+
->getMock();
137+
138+
$serveController->docroot = $docroot;
139+
$serveController->port = 8081;
140+
$serveController->router = $router;
141+
142+
$serveController->expects($this->once())->method('runCommand')->willReturn(true);
143+
144+
ob_start();
145+
$serveController->actionIndex();
146+
ob_end_clean();
147+
148+
$result = $serveController->flushStdOutBuffer();
149+
150+
$this->assertContains('Server started on http://localhost:8081', $result);
151+
$this->assertContains("Document root is \"{$docroot}\"", $result);
152+
$this->assertContains("Routing file is \"{$router}\"", $result);
153+
$this->assertContains('Quit the server with CTRL-C or COMMAND-C.', $result);
154+
}
155+
}
156+
157+
/**
158+
* Mock class for [[\yii\console\controllers\ServeController]].
159+
*/
160+
class ServeControllerMock extends ServeController
161+
{
162+
use StdOutBufferControllerTrait;
163+
}
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)