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' );
public function setUp ()
{
set_error_handler ( array ( 'ErrorHandler' , 'error_handler' ));
ob_start ();
}
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-10-13 14:10:30 +04:00
$this -> assertEquals ( $eh , $my_eh );
2011-10-11 15:20:52 +04:00
}
/**
* @ expectedException ErrorException
* @ expectedExceptionMessage test error
*/
public function testHandleError ()
{
trigger_error ( " test error " , E_USER_ERROR );
}
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 );
} 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 );
2011-10-13 14:10:30 +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 " );
$this -> assertEquals ( " <code>first line<br /> \n second line</code> " , $result );
$result = $method -> invoke ( null , " first line \r \n second line " );
$this -> assertEquals ( " <code>first line<br /> \r \n second line</code> " , $result );
$result = $method -> invoke ( null , " first line \r \n \r \n second line " );
$this -> assertEquals ( " <code>first line<br /> \r \n <br /> \r \n second line</code> " , $result );
2011-10-13 14:10:30 +04:00
}
public function tearDown ()
{
set_error_handler ( $this -> old_eh );
2011-10-11 15:20:52 +04:00
}
}