Separate function ErrorHandler::logError() and tests
This commit is contained in:
@ -27,6 +27,42 @@ class ErrorHandler
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static public function logError($exception)
|
||||||
|
{
|
||||||
|
$error = 0;
|
||||||
|
if ($exception instanceof ErrorException) {
|
||||||
|
$error = $exception->getSeverity();
|
||||||
|
}
|
||||||
|
|
||||||
|
switch ($error) {
|
||||||
|
case E_NOTICE:
|
||||||
|
$error = 'Notice';
|
||||||
|
break;
|
||||||
|
case E_WARNING:
|
||||||
|
$error = 'Warning';
|
||||||
|
break;
|
||||||
|
case E_ERROR:
|
||||||
|
$error = 'Fatal Error';
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
$error = 'Unknown Error';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$message = 'PHP ' . $error . ': ' . $exception->getMessage() . ' in ' . $exception->getFile() . ':' . $exception->getLine() . ' \nStack trace:\n' . $exception->getTraceAsString() . self::getHTTPEErrorConditions();
|
||||||
|
// PHP Fatal error: Uncaught exception 'LogicException' in /www/test.tfs/face/htdocs/index.php:11\nStack trace:\n#0 {main}\n thrown in /www/test.tfs/face/htdocs/index.php on line 11, referer: http://test.tfs.manekeno.netmonsters.ru/news/create
|
||||||
|
error_log($message);
|
||||||
|
}
|
||||||
|
|
||||||
|
static public function getHTTPEErrorConditions()
|
||||||
|
{
|
||||||
|
$text = null;
|
||||||
|
if (!is_null(Env::Server('REQUEST_METHOD')) && !is_null(Env::Server('REQUEST_URI'))) {
|
||||||
|
$text = ', URL: ' . Env::Server('REQUEST_METHOD') . ' ' . Env::Server('REQUEST_URI');
|
||||||
|
$text .= ', referrer: ' . Env::Server('HTTP_REFERER');
|
||||||
|
}
|
||||||
|
return $text;
|
||||||
|
}
|
||||||
|
|
||||||
static protected function getSource($file, $hiline)
|
static protected function getSource($file, $hiline)
|
||||||
{
|
{
|
||||||
$code = array();
|
$code = array();
|
||||||
|
@ -17,13 +17,15 @@ require_once dirname(__FILE__) . '/../../exception/ErrorHandler.php';
|
|||||||
class ErrorHandlerTest extends PHPUnit_Framework_TestCase
|
class ErrorHandlerTest extends PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
public $old_eh = array('PHPUnit_Util_ErrorHandler', 'handleError');
|
public $old_eh = array('PHPUnit_Util_ErrorHandler', 'handleError');
|
||||||
|
|
||||||
|
protected $log_file_name = 'error_log_file';
|
||||||
|
|
||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
set_error_handler(array('ErrorHandler', 'error_handler'));
|
set_error_handler(array('ErrorHandler', 'error_handler'));
|
||||||
ob_start();
|
ob_start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testErrorHandlerInit()
|
public function testErrorHandlerInit()
|
||||||
{
|
{
|
||||||
$my_eh = array('ErrorHandler', 'error_handler');
|
$my_eh = array('ErrorHandler', 'error_handler');
|
||||||
@ -59,13 +61,14 @@ class ErrorHandlerTest extends PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
throw new ErrorException("test error", E_USER_ERROR);
|
throw new ErrorException("test error", E_USER_ERROR);
|
||||||
} catch (ErrorException $e) {
|
}
|
||||||
|
catch (ErrorException $e) {
|
||||||
$_SESSION['some'] = 'value';
|
$_SESSION['some'] = 'value';
|
||||||
$result = ErrorHandler::showDebug($e);
|
$result = ErrorHandler::showDebug($e);
|
||||||
$this->assertNotEmpty($result);
|
$this->assertNotEmpty($result);
|
||||||
$this->assertStringStartsWith('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">', $result);
|
$this->assertStringStartsWith('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">', $result);
|
||||||
$this->assertStringEndsWith('</html>', $result);
|
$this->assertStringEndsWith('</html>', $result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -84,10 +87,94 @@ class ErrorHandlerTest extends PHPUnit_Framework_TestCase
|
|||||||
$result = $method->invoke(null, "first line\r\n\r\nsecond line");
|
$result = $method->invoke(null, "first line\r\n\r\nsecond line");
|
||||||
$this->assertSame("<code>first line<br />\r\n<br />\r\nsecond line</code>", $result);
|
$this->assertSame("<code>first line<br />\r\n<br />\r\nsecond line</code>", $result);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function tearDown()
|
/**
|
||||||
|
* @runInSeparateProcess
|
||||||
|
*/
|
||||||
|
public function testLogErrorDefaultException()
|
||||||
{
|
{
|
||||||
set_error_handler($this->old_eh);
|
$ex = new Exception('message', 123);
|
||||||
|
ini_set('error_log', $this->log_file_name);
|
||||||
|
ErrorHandler::logError($ex);
|
||||||
|
$log = file_get_contents($this->log_file_name);
|
||||||
|
$this->assertContains('PHP Unknown Error: message in ', $log);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @runInSeparateProcess
|
||||||
|
*/
|
||||||
|
public function testLogErrorDefaultExceptionWithHTTP()
|
||||||
|
{
|
||||||
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
||||||
|
$_SERVER['REQUEST_URI'] = '/somelongurl';
|
||||||
|
$_SERVER['HTTP_REFERER'] = 'http://referrer/url';
|
||||||
|
$this->assertEquals('GET', ENV::Server('REQUEST_METHOD'));
|
||||||
|
$this->assertEquals('/somelongurl', ENV::Server('REQUEST_URI'));
|
||||||
|
$this->assertEquals('http://referrer/url', ENV::Server('HTTP_REFERER'));
|
||||||
|
|
||||||
|
$ex = new Exception('message', 123);
|
||||||
|
ini_set('error_log', $this->log_file_name);
|
||||||
|
ErrorHandler::logError($ex);
|
||||||
|
$log = file_get_contents($this->log_file_name);
|
||||||
|
$this->assertContains('PHP Unknown Error: message in ', $log);
|
||||||
|
$this->assertContains('URL: GET /somelongurl, referrer: http://referrer/url', $log);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @runInSeparateProcess
|
||||||
|
*/
|
||||||
|
public function testLogErrorCustomException()
|
||||||
|
{
|
||||||
|
$ex = new LogicException('Logic', 333);
|
||||||
|
ini_set('error_log', $this->log_file_name);
|
||||||
|
ErrorHandler::logError($ex);
|
||||||
|
$log = file_get_contents($this->log_file_name);
|
||||||
|
$this->assertContains('PHP Unknown Error: Logic in ', $log);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @runInSeparateProcess
|
||||||
|
*/
|
||||||
|
public function testLogErrorErrorExceptionNotice()
|
||||||
|
{
|
||||||
|
$ex = new ErrorException('message', 321, E_NOTICE);
|
||||||
|
ini_set('error_log', $this->log_file_name);
|
||||||
|
ErrorHandler::logError($ex);
|
||||||
|
$log = file_get_contents($this->log_file_name);
|
||||||
|
$this->assertContains('PHP Notice: message in ', $log);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @runInSeparateProcess
|
||||||
|
*/
|
||||||
|
public function testLogErrorErrorExceptionWarning()
|
||||||
|
{
|
||||||
|
$ex = new ErrorException('message', 321, E_WARNING);
|
||||||
|
ini_set('error_log', $this->log_file_name);
|
||||||
|
ErrorHandler::logError($ex);
|
||||||
|
$log = file_get_contents($this->log_file_name);
|
||||||
|
$this->assertContains('PHP Warning: message in ', $log);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @runInSeparateProcess
|
||||||
|
*/
|
||||||
|
public function testLogErrorErrorExceptionFatal()
|
||||||
|
{
|
||||||
|
$ex = new ErrorException('message', 321, E_ERROR);
|
||||||
|
ini_set('error_log', $this->log_file_name);
|
||||||
|
ErrorHandler::logError($ex);
|
||||||
|
$log = file_get_contents($this->log_file_name);
|
||||||
|
$this->assertContains('PHP Fatal Error: message in ', $log);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function tearDown()
|
||||||
|
{
|
||||||
|
if (file_exists($this->log_file_name) && is_writable($this->log_file_name)) {
|
||||||
|
unlink($this->log_file_name);
|
||||||
|
}
|
||||||
|
ini_set('error_log', 'php://stderr');
|
||||||
|
set_error_handler($this->old_eh);
|
||||||
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user