2011-10-11 15:20:52 +04:00
< ? php
/*
* @ copyright NetMonsters < team @ netmonsters . ru >
* @ link http :// netmonsters . ru
* @ package Majestic
* @ subpackage UnitTests
* @ since 2011 - 10 - 11
*
* Unit tests for ErrorHandler class
*/
require_once dirname ( __FILE__ ) . '/../../classes/Env.class.php' ;
require_once dirname ( __FILE__ ) . '/../../session/Session.php' ;
require_once dirname ( __FILE__ ) . '/../../exception/ErrorHandler.php' ;
class ErrorHandlerTest extends PHPUnit_Framework_TestCase
{
2011-10-13 14:10:30 +04:00
public $old_eh = array ( 'PHPUnit_Util_ErrorHandler' , 'handleError' );
2012-12-11 15:03:48 +04:00
protected $log_file_name = 'error_log_file' ;
2011-10-13 14:10:30 +04:00
public function setUp ()
2012-12-11 15:03:48 +04:00
{
2011-10-13 14:10:30 +04:00
set_error_handler ( array ( 'ErrorHandler' , 'error_handler' ));
ob_start ();
}
2012-12-11 15:03:48 +04:00
2011-10-11 15:20:52 +04:00
public function testErrorHandlerInit ()
{
$my_eh = array ( 'ErrorHandler' , 'error_handler' );
ErrorHandler :: init ();
$eh = set_error_handler ( $my_eh );
$this -> assertInternalType ( 'array' , $eh );
2011-12-02 17:22:31 +04:00
$this -> assertSame ( $eh , $my_eh );
2011-10-11 15:20:52 +04:00
}
public function testHandleError ()
{
2011-12-01 12:25:27 +04:00
$this -> setExpectedException ( 'ErrorException' , 'test error' );
2011-10-11 15:20:52 +04:00
trigger_error ( " test error " , E_USER_ERROR );
}
2012-10-05 19:38:24 +04:00
public function testHandleAt ()
{
$my_eh = array ( 'ErrorHandler' , 'error_handler' );
ErrorHandler :: init ();
set_error_handler ( $my_eh );
$var = '' ;
$ok = @ $var [ 'some' ];
$this -> assertSame ( '' , $var );
ob_start ();
$this -> setExpectedException ( 'ErrorException' );
$ex = $var [ 'some' ];
}
2011-10-14 13:10:48 +04:00
/**
* @ TODO : ErrorHandler -> wrapTrace () not used
*/
2011-10-11 15:20:52 +04:00
public function testShowDebug ()
{
try {
throw new ErrorException ( " test error " , E_USER_ERROR );
2012-12-11 15:03:48 +04:00
}
catch ( ErrorException $e ) {
2011-10-14 13:10:48 +04:00
$_SESSION [ 'some' ] = 'value' ;
2011-10-11 15:20:52 +04:00
$result = ErrorHandler :: showDebug ( $e );
$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 -> assertStringEndsWith ( '</html>' , $result );
2012-12-11 15:03:48 +04:00
}
2011-10-28 13:50:15 +04:00
}
/**
* @ TODO : ErrorHandler :: wrapTrace not used
* @ TODO : nl2br () adds html < br /> but leaves original linebreak line \n
*/
public function testWrapTrace ()
{
$class = new ReflectionClass ( 'ErrorHandler' );
$method = $class -> getMethod ( 'WrapTrace' );
$method -> setAccessible ( true );
$result = $method -> invoke ( null , " first line \n second line " );
2011-12-02 17:22:31 +04:00
$this -> assertSame ( " <code>first line<br /> \n second line</code> " , $result );
2011-10-28 13:50:15 +04:00
$result = $method -> invoke ( null , " first line \r \n second line " );
2011-12-02 17:22:31 +04:00
$this -> assertSame ( " <code>first line<br /> \r \n second line</code> " , $result );
2011-10-28 13:50:15 +04:00
$result = $method -> invoke ( null , " first line \r \n \r \n second line " );
2011-12-02 17:22:31 +04:00
$this -> assertSame ( " <code>first line<br /> \r \n <br /> \r \n second line</code> " , $result );
2011-10-13 14:10:30 +04:00
}
2012-12-11 15:03:48 +04:00
/**
* @ runInSeparateProcess
*/
public function testLogErrorDefaultException ()
{
$ex = new Exception ( 'message' , 123 );
ini_set ( 'error_log' , $this -> log_file_name );
ErrorHandler :: logError ( $ex );
$log = file_get_contents ( $this -> log_file_name );
2012-12-11 19:05:39 +04:00
$this -> assertContains ( 'PHP Unknown Error: Exception: message in ' , $log );
2012-12-11 15:03:48 +04:00
}
/**
* @ 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 );
2012-12-11 19:05:39 +04:00
$this -> assertContains ( 'PHP Unknown Error: Exception: message in ' , $log );
2012-12-11 15:03:48 +04:00
$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 );
2012-12-11 19:05:39 +04:00
$this -> assertContains ( 'PHP Unknown Error: LogicException: Logic in ' , $log );
2012-12-11 15:03:48 +04:00
}
/**
* @ 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 );
}
2011-10-13 14:10:30 +04:00
public function tearDown ()
{
2012-12-11 15:03:48 +04:00
if ( file_exists ( $this -> log_file_name ) && is_writable ( $this -> log_file_name )) {
unlink ( $this -> log_file_name );
}
ini_set ( 'error_log' , 'php://stderr' );
2011-10-13 14:10:30 +04:00
set_error_handler ( $this -> old_eh );
2011-10-11 15:20:52 +04:00
}
}