Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix yii server and add tests. #20021

Merged
merged 9 commits into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Yii Framework 2 Change Log
2.0.50 under development
------------------------

- Bug #20005: Fix `yii\console\controllers\ServeController` to specify the router script (terabytesoftw)
- Bug #19060: Fix `yii\widgets\Menu` bug when using Closure for active item and adding additional tests in `tests\framework\widgets\MenuTest` (atrandafir)
- Bug #13920: Fixed erroneous validation for specific cases (tim-fischer-maschinensucher)
- 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)
Expand Down
13 changes: 12 additions & 1 deletion framework/console/controllers/ServeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,13 @@
}
$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}\"";
}

$this->runCommand($command);
}

/**
Expand Down Expand Up @@ -122,4 +128,9 @@
fclose($fp);
return true;
}

protected function runCommand($command)

Check warning on line 132 in framework/console/controllers/ServeController.php

View check run for this annotation

Codecov / codecov/patch

framework/console/controllers/ServeController.php#L132

Added line #L132 was not covered by tests
{
passthru($command);
}

Check warning on line 135 in framework/console/controllers/ServeController.php

View check run for this annotation

Codecov / codecov/patch

framework/console/controllers/ServeController.php#L134-L135

Added lines #L134 - L135 were not covered by tests
}
163 changes: 163 additions & 0 deletions tests/framework/console/controllers/ServeControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?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 testAddressTaken()
{
$docroot = __DIR__ . '/stub';

/** @var ServeController $serveController */
$serveController = $this->getMockBuilder(ServeControllerMocK::className())
->setConstructorArgs(['serve', Yii::$app])
->setMethods(['isAddressTaken', 'runCommand'])
->getMock();

$serveController->expects($this->once())->method('isAddressTaken')->willReturn(true);
$serveController->expects($this->never())->method('runCommand');

$serveController->docroot = $docroot;
$serveController->port = 8080;

ob_start();
$serveController->actionIndex('localhost:8080');
ob_end_clean();

$result = $serveController->flushStdOutBuffer();

$this->assertContains('http://localhost:8080 is taken by another process.', $result);
}

public function testDefaultValues()
{
$docroot = __DIR__ . '/stub';

/** @var ServeController $serveController */
$serveController = $this->getMockBuilder(ServeControllerMock::className())
->setConstructorArgs(['serve', Yii::$app])
->setMethods(['runCommand'])
->getMock();

$serveController->docroot = $docroot;
$serveController->port = 8080;

$serveController->expects($this->once())->method('runCommand')->willReturn(true);

ob_start();
$serveController->actionIndex();
ob_end_clean();

$result = $serveController->flushStdOutBuffer();

$this->assertContains('Server started on http://localhost:8080', $result);
$this->assertContains("Document root is \"{$docroot}\"", $result);
$this->assertContains('Quit the server with CTRL-C or COMMAND-C.', $result);
}

public function testDoocRootWithNoExistValue()
{
$docroot = '/not/exist/path';

/** @var ServeController $serveController */
$serveController = $this->getMockBuilder(ServeControllerMock::className())
->setConstructorArgs(['serve', Yii::$app])
->setMethods(['runCommand'])
->getMock();

$serveController->docroot = $docroot;

$serveController->expects($this->any())->method('runCommand')->willReturn(true);

ob_start();
$serveController->actionIndex();
ob_end_clean();

$result = $serveController->flushStdOutBuffer();

$this->assertContains("Document root \"{$docroot}\" does not exist.", $result);
}

public function testWithRouterNoExistValue()
{
$docroot = __DIR__ . '/stub';
$router = '/not/exist/path';

/** @var ServeController $serveController */
$serveController = $this->getMockBuilder(ServeControllerMock::className())
->setConstructorArgs(['serve', Yii::$app])
->setMethods(['runCommand'])
->getMock();

$serveController->docroot = $docroot;
$serveController->port = 8081;
$serveController->router = $router;

$serveController->expects($this->any())->method('runCommand')->willReturn(true);

ob_start();
$serveController->actionIndex();
ob_end_clean();

$result = $serveController->flushStdOutBuffer();

$this->assertContains("Routing file \"$router\" does not exist.", $result);
}

public function testWithRouterValue()
{
$docroot = __DIR__ . '/stub';
$router = __DIR__ . '/stub/index.php';

/** @var ServeController $serveController */
$serveController = $this->getMockBuilder(ServeControllerMock::className())
->setConstructorArgs(['serve', Yii::$app])
->setMethods(['runCommand'])
->getMock();

$serveController->docroot = $docroot;
$serveController->port = 8081;
$serveController->router = $router;

$serveController->expects($this->once())->method('runCommand')->willReturn(true);

ob_start();
$serveController->actionIndex();
ob_end_clean();

$result = $serveController->flushStdOutBuffer();

$this->assertContains('Server started on http://localhost:8081', $result);
$this->assertContains("Document root is \"{$docroot}\"", $result);
$this->assertContains("Routing file is \"{$router}\"", $result);
$this->assertContains('Quit the server with CTRL-C or COMMAND-C.', $result);
}
}

/**
* Mock class for [[\yii\console\controllers\ServeController]].
*/
class ServeControllerMock extends ServeController
{
use StdOutBufferControllerTrait;
}
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!";
Loading