modified CliController to use ErrorStream conf var and separate logging by LOGGING/PROFILER settings
This commit is contained in:
@ -24,7 +24,7 @@ class CliController
|
|||||||
protected function __construct()
|
protected function __construct()
|
||||||
{
|
{
|
||||||
ErrorHandler::init();
|
ErrorHandler::init();
|
||||||
$this->error_stream = fopen('php://stderr', 'rw');
|
$this->error_stream = Config::get('ErrorStream');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -51,11 +51,20 @@ class CliController
|
|||||||
}
|
}
|
||||||
$cli_class->run();
|
$cli_class->run();
|
||||||
if (Config::get('PROFILER')) {
|
if (Config::get('PROFILER')) {
|
||||||
echo PHP_EOL . Profiler::getInstance()->getCli();
|
$profile = Profiler::getInstance()->getCli();
|
||||||
|
if (Config::get('LOGGING')) {
|
||||||
|
Logger::getInstance()->log($profile);
|
||||||
|
} else {
|
||||||
|
echo $profile;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
fwrite($this->error_stream, PHP_EOL . 'Error: ' . $e->getMessage() . PHP_EOL);
|
$code = $e->getCode();
|
||||||
fwrite($this->error_stream, PHP_EOL . 'Stack trace: ' . PHP_EOL . $e->getTraceAsString() . PHP_EOL);
|
if ($e instanceof ErrorException) {
|
||||||
|
$code = $e->getSeverity();
|
||||||
|
}
|
||||||
|
file_put_contents($this->error_stream, PHP_EOL . 'Error ' . '#' . $code . ': ' . $e->getMessage() . PHP_EOL, FILE_APPEND);
|
||||||
|
file_put_contents($this->error_stream, PHP_EOL . 'Stack trace: ' . PHP_EOL . $e->getTraceAsString() . PHP_EOL, FILE_APPEND);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -57,38 +57,41 @@ class CliControllerTest extends PHPUnit_Framework_TestCase
|
|||||||
/**
|
/**
|
||||||
* @runInSeparateProcess
|
* @runInSeparateProcess
|
||||||
*/
|
*/
|
||||||
public function testExecuteImplementError()
|
public function testExecuteImplementErrorToFile()
|
||||||
{
|
{
|
||||||
|
Config::set('ErrorStream', __DIR__ . '/temp.txt');
|
||||||
|
touch(Config::get('ErrorStream'));
|
||||||
$cli_class = new StdClass();
|
$cli_class = new StdClass();
|
||||||
$cli_controller = CliController::getInstance();
|
CliController::getInstance()->execute($cli_class);
|
||||||
$error_stream_prop = new ReflectionProperty($cli_controller, 'error_stream');
|
$this->assertContains('Runner "' . get_class($cli_class) . '" need implement of "iCli" interface.', file_get_contents(Config::get('ErrorStream')));
|
||||||
$this->stream = fopen('php://memory', 'rw');
|
unlink(Config::get('ErrorStream'));
|
||||||
$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
|
* @runInSeparateProcess
|
||||||
*/
|
*/
|
||||||
|
public function testExecuteImplementErrorToConsole()
|
||||||
|
{
|
||||||
|
Config::set('ErrorStream', 'php://output');
|
||||||
|
$cli_class = new StdClass();
|
||||||
|
$this->expectOutputRegex('/.*Runner "' . get_class($cli_class) . '" need implement of "iCli" interface\..*/');
|
||||||
|
CliController::getInstance()->execute($cli_class);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @runInSeparateProcess
|
||||||
|
*/
|
||||||
public function testExecuteWithRunThrowError()
|
public function testExecuteWithRunThrowError()
|
||||||
{
|
{
|
||||||
|
Config::set('ErrorStream', 'php://output');
|
||||||
Config::set('PROFILER', false);
|
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')
|
||||||
->with()
|
->with()
|
||||||
->will($this->returnCallback(array($this, 'callbackWithThrow')));
|
->will($this->returnCallback(array($this, 'callbackWithThrow')));
|
||||||
$cli_controller = CliController::getInstance();
|
|
||||||
$error_stream_prop = new ReflectionProperty($cli_controller, 'error_stream');
|
$this->expectOutputRegex('/.*Error from callback\..*/');
|
||||||
$this->stream = fopen('php://memory', 'rw');
|
CliController::getInstance()->execute($cli_class);
|
||||||
$error_stream_prop->setAccessible(true);
|
|
||||||
$error_stream_prop->setValue($cli_controller, $this->stream);
|
|
||||||
$cli_controller->execute($cli_class);
|
|
||||||
rewind($this->stream);
|
|
||||||
$this->assertContains('Error from callback.', stream_get_contents($this->stream));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function callbackWithThrow()
|
public function callbackWithThrow()
|
||||||
|
Reference in New Issue
Block a user