Complete tests for CliController.
This commit is contained in:
@ -19,9 +19,12 @@ class CliController
|
|||||||
*/
|
*/
|
||||||
protected static $instance;
|
protected static $instance;
|
||||||
|
|
||||||
|
protected $error_stream;
|
||||||
|
|
||||||
protected function __construct()
|
protected function __construct()
|
||||||
{
|
{
|
||||||
ErrorHandler::init();
|
ErrorHandler::init();
|
||||||
|
$this->error_stream = fopen('php://stderr', 'rw');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -51,9 +54,8 @@ class CliController
|
|||||||
echo PHP_EOL . Profiler::getInstance()->getCli();
|
echo PHP_EOL . Profiler::getInstance()->getCli();
|
||||||
}
|
}
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
$stderr = fopen('php://stderr', 'w');
|
fwrite($this->error_stream, PHP_EOL . 'Error: ' . $e->getMessage() . PHP_EOL);
|
||||||
fputs($stderr, PHP_EOL . 'Error: ' . $e->getMessage() . PHP_EOL);
|
fwrite($this->error_stream, PHP_EOL . 'Stack trace: ' . PHP_EOL . $e->getTraceAsString() . PHP_EOL);
|
||||||
fputs($stderr, PHP_EOL . 'Stack trace: ' . PHP_EOL . $e->getTraceAsString() . PHP_EOL);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -23,14 +23,17 @@ require_once __DIR__ . '/../../app/iCli.php';
|
|||||||
*/
|
*/
|
||||||
class CliControllerTest extends PHPUnit_Framework_TestCase
|
class CliControllerTest extends PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
public function _testGetInstance()
|
protected $stream;
|
||||||
|
|
||||||
|
public function testGetInstance()
|
||||||
{
|
{
|
||||||
$instance = CliController::getInstance();
|
$instance = CliController::getInstance();
|
||||||
$this->assertInstanceOf('CliController', $instance);
|
$this->assertInstanceOf('CliController', $instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function _testExecute()
|
public function testExecute()
|
||||||
{
|
{
|
||||||
|
Config::set('PROFILER', false);
|
||||||
$cli_class = $this->getMockForAbstractClass('iCli', array(), '', '', '', '', array('run'));
|
$cli_class = $this->getMockForAbstractClass('iCli', array(), '', '', '', '', array('run'));
|
||||||
$cli_class->expects($this->once())
|
$cli_class->expects($this->once())
|
||||||
->method('run')
|
->method('run')
|
||||||
@ -38,13 +41,58 @@ class CliControllerTest extends PHPUnit_Framework_TestCase
|
|||||||
CliController::getInstance()->execute($cli_class);
|
CliController::getInstance()->execute($cli_class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testExecuteWithProfiler()
|
||||||
|
{
|
||||||
|
ob_start();
|
||||||
|
Config::set('PROFILER', true);
|
||||||
|
$cli_class = $this->getMockForAbstractClass('iCli', array(), '', '', '', '', array('run'));
|
||||||
|
$cli_class->expects($this->once())
|
||||||
|
->method('run')
|
||||||
|
->with();
|
||||||
|
CliController::getInstance()->execute($cli_class);
|
||||||
|
$output = ob_get_clean();
|
||||||
|
$this->assertContains('Elapsed time:', $output);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @runInSeparateProcess
|
* @runInSeparateProcess
|
||||||
*/
|
*/
|
||||||
public function testExecuteImplementError()
|
public function testExecuteImplementError()
|
||||||
{
|
{
|
||||||
$cli_class = new StdClass();
|
$cli_class = new StdClass();
|
||||||
$this->setExpectedException('ErrorException', 'Runner "' . get_class($cli_class) . '" need implement of "iCli" interface.');
|
$cli_controller = CliController::getInstance();
|
||||||
CliController::getInstance()->execute($cli_class);
|
$error_stream_prop = new ReflectionProperty($cli_controller, 'error_stream');
|
||||||
|
$this->stream = fopen('php://memory', 'rw');
|
||||||
|
$error_stream_prop->setAccessible(true);
|
||||||
|
$error_stream_prop->setValue($cli_controller, $this->stream);
|
||||||
|
$cli_controller->execute($cli_class);
|
||||||
|
rewind($this->stream);
|
||||||
|
$this->assertContains('Runner "' . get_class($cli_class) . '" need implement of "iCli" interface.', stream_get_contents($this->stream));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @runInSeparateProcess
|
||||||
|
*/
|
||||||
|
public function testExecuteWithRunThrowError()
|
||||||
|
{
|
||||||
|
Config::set('PROFILER', false);
|
||||||
|
$cli_class = $this->getMockForAbstractClass('iCli', array(), '', '', '', '', array('run'));
|
||||||
|
$cli_class->expects($this->once())
|
||||||
|
->method('run')
|
||||||
|
->with()
|
||||||
|
->will($this->returnCallback(array($this, 'callbackWithThrow')));
|
||||||
|
$cli_controller = CliController::getInstance();
|
||||||
|
$error_stream_prop = new ReflectionProperty($cli_controller, 'error_stream');
|
||||||
|
$this->stream = fopen('php://memory', 'rw');
|
||||||
|
$error_stream_prop->setAccessible(true);
|
||||||
|
$error_stream_prop->setValue($cli_controller, $this->stream);
|
||||||
|
$cli_controller->execute($cli_class);
|
||||||
|
rewind($this->stream);
|
||||||
|
$this->assertContains('Error frdom callback.', stream_get_contents($this->stream));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function callbackWithThrow()
|
||||||
|
{
|
||||||
|
throw new ErrorException('Error from callback.');
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user