2011-03-05 19:23:35 +00:00
< ? php
/*
* This file is part of the Symfony package .
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2011-03-05 19:23:35 +00:00
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
2013-03-20 17:09:46 +00:00
namespace Symfony\Component\Debug\Tests ;
2011-03-05 19:23:35 +00:00
2013-03-20 17:09:46 +00:00
use Symfony\Component\Debug\ErrorHandler ;
2013-05-28 18:12:41 +01:00
use Symfony\Component\Debug\Exception\ClassNotFoundException ;
use Symfony\Component\Debug\ExceptionHandler ;
2011-03-05 19:23:35 +00:00
/**
* ErrorHandlerTest
*
* @ author Robert Schönthal < seroscho @ googlemail . com >
*/
class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
{
2011-04-15 20:12:02 +01:00
2011-03-05 19:23:35 +00:00
public function testConstruct ()
{
2011-05-05 07:46:20 +01:00
$handler = ErrorHandler :: register ( 3 );
2011-04-15 20:12:02 +01:00
2011-05-05 07:46:20 +01:00
$level = new \ReflectionProperty ( $handler , 'level' );
2011-03-05 19:23:35 +00:00
$level -> setAccessible ( true );
2011-04-15 20:12:02 +01:00
2011-05-05 07:46:20 +01:00
$this -> assertEquals ( 3 , $level -> getValue ( $handler ));
2011-04-15 20:12:02 +01:00
2011-05-05 07:46:20 +01:00
restore_error_handler ();
2011-03-05 19:23:35 +00:00
}
public function testHandle ()
{
2011-05-05 07:46:20 +01:00
$handler = ErrorHandler :: register ( 0 );
$this -> assertFalse ( $handler -> handle ( 0 , 'foo' , 'foo.php' , 12 , 'foo' ));
restore_error_handler ();
$handler = ErrorHandler :: register ( 3 );
$this -> assertFalse ( $handler -> handle ( 4 , 'foo' , 'foo.php' , 12 , 'foo' ));
2011-04-15 20:12:02 +01:00
2011-05-05 07:46:20 +01:00
restore_error_handler ();
2011-04-15 20:12:02 +01:00
2011-05-05 07:46:20 +01:00
$handler = ErrorHandler :: register ( 3 );
try {
2012-10-07 20:03:04 +01:00
$handler -> handle ( 111 , 'foo' , 'foo.php' , 12 , 'foo' );
2011-05-05 07:46:20 +01:00
} catch ( \ErrorException $e ) {
2012-10-07 20:03:04 +01:00
$this -> assertSame ( '111: foo in foo.php line 12' , $e -> getMessage ());
$this -> assertSame ( 111 , $e -> getSeverity ());
2012-06-25 21:04:54 +01:00
$this -> assertSame ( 'foo.php' , $e -> getFile ());
$this -> assertSame ( 12 , $e -> getLine ());
2011-03-05 19:23:35 +00:00
}
2011-05-05 07:46:20 +01:00
restore_error_handler ();
2012-12-01 11:25:00 +00:00
$handler = ErrorHandler :: register ( E_USER_DEPRECATED );
$this -> assertTrue ( $handler -> handle ( E_USER_DEPRECATED , 'foo' , 'foo.php' , 12 , 'foo' ));
restore_error_handler ();
$handler = ErrorHandler :: register ( E_DEPRECATED );
$this -> assertTrue ( $handler -> handle ( E_DEPRECATED , 'foo' , 'foo.php' , 12 , 'foo' ));
restore_error_handler ();
2013-01-09 09:41:17 +00:00
$logger = $this -> getMock ( 'Psr\Log\LoggerInterface' );
2012-12-07 11:47:14 +00:00
$that = $this ;
$warnArgCheck = function ( $message , $context ) use ( $that ) {
$that -> assertEquals ( 'foo' , $message );
$that -> assertArrayHasKey ( 'type' , $context );
$that -> assertEquals ( $context [ 'type' ], ErrorHandler :: TYPE_DEPRECATION );
$that -> assertArrayHasKey ( 'stack' , $context );
$that -> assertInternalType ( 'array' , $context [ 'stack' ]);
};
$logger
-> expects ( $this -> once ())
2013-01-09 09:41:17 +00:00
-> method ( 'warning' )
2012-12-07 11:47:14 +00:00
-> will ( $this -> returnCallback ( $warnArgCheck ))
;
2012-12-01 11:25:00 +00:00
$handler = ErrorHandler :: register ( E_USER_DEPRECATED );
$handler -> setLogger ( $logger );
$handler -> handle ( E_USER_DEPRECATED , 'foo' , 'foo.php' , 12 , 'foo' );
restore_error_handler ();
2011-05-05 07:46:20 +01:00
}
2013-05-28 18:12:41 +01:00
public function provideClassNotFoundData ()
{
return array (
array (
array (
'type' => 1 ,
'line' => 12 ,
'file' => 'foo.php' ,
2013-07-23 19:05:03 +01:00
'message' => 'Class "WhizBangFactory" not found' ,
2013-05-28 18:12:41 +01:00
),
2013-07-23 19:05:03 +01:00
'Attempted to load class "WhizBangFactory" from the global namespace in foo.php line 12. Did you forget a use statement for this class?' ,
2013-05-28 18:12:41 +01:00
),
array (
array (
'type' => 1 ,
'line' => 12 ,
'file' => 'foo.php' ,
2013-07-23 19:05:03 +01:00
'message' => 'Class "Foo\\Bar\\WhizBangFactory" not found' ,
2013-05-28 18:12:41 +01:00
),
2013-07-23 19:05:03 +01:00
'Attempted to load class "WhizBangFactory" from namespace "Foo\\Bar" in foo.php line 12. Do you need to "use" it from another namespace?' ,
2013-05-28 18:12:41 +01:00
),
array (
array (
'type' => 1 ,
'line' => 12 ,
'file' => 'foo.php' ,
2013-07-24 04:56:44 +01:00
'message' => 'Class "UndefinedFunctionException" not found' ,
2013-05-28 18:12:41 +01:00
),
2013-07-24 04:56:44 +01:00
'Attempted to load class "UndefinedFunctionException" from the global namespace in foo.php line 12. Did you forget a use statement for this class? Perhaps you need to add a use statement for one of the following class: Symfony\Component\Debug\Exception\UndefinedFunctionException.' ,
2013-05-28 18:12:41 +01:00
),
array (
array (
'type' => 1 ,
'line' => 12 ,
'file' => 'foo.php' ,
2013-07-24 04:56:44 +01:00
'message' => 'Class "PEARClass" not found' ,
2013-05-28 18:12:41 +01:00
),
2013-07-24 04:56:44 +01:00
'Attempted to load class "PEARClass" from the global namespace in foo.php line 12. Did you forget a use statement for this class? Perhaps you need to add a use statement for one of the following class: Symfony_Component_Debug_Tests_Fixtures_PEARClass.' ,
),
array (
array (
'type' => 1 ,
'line' => 12 ,
'file' => 'foo.php' ,
'message' => 'Class "Foo\\Bar\\UndefinedFunctionException" not found' ,
),
'Attempted to load class "UndefinedFunctionException" from namespace "Foo\Bar" in foo.php line 12. Do you need to "use" it from another namespace? Perhaps you need to add a use statement for one of the following class: Symfony\Component\Debug\Exception\UndefinedFunctionException.' ,
2013-05-28 18:12:41 +01:00
),
);
}
/**
* @ dataProvider provideClassNotFoundData
*/
public function testClassNotFound ( $error , $translatedMessage )
{
$handler = ErrorHandler :: register ( 3 );
2013-07-23 19:05:03 +01:00
$m = new \ReflectionMethod ( $handler , 'handleFatalError' );
$m -> setAccessible ( true );
2013-05-28 18:12:41 +01:00
$exceptionHandler = new MockExceptionHandler ;
set_exception_handler ( array ( $exceptionHandler , 'handle' ));
2013-07-23 19:05:03 +01:00
$m -> invoke ( $handler , $error );
2013-05-28 18:12:41 +01:00
$this -> assertNotNull ( $exceptionHandler -> e );
$this -> assertSame ( $translatedMessage , $exceptionHandler -> e -> getMessage ());
$this -> assertSame ( $error [ 'type' ], $exceptionHandler -> e -> getSeverity ());
$this -> assertSame ( $error [ 'file' ], $exceptionHandler -> e -> getFile ());
$this -> assertSame ( $error [ 'line' ], $exceptionHandler -> e -> getLine ());
restore_exception_handler ();
restore_error_handler ();
}
public function provideUndefinedFunctionData ()
{
return array (
array (
array (
'type' => 1 ,
'line' => 12 ,
'file' => 'foo.php' ,
2013-07-23 19:05:03 +01:00
'message' => 'Call to undefined function test_namespaced_function()' ,
2013-05-28 18:12:41 +01:00
),
2013-07-23 19:05:03 +01:00
'Attempted to call function "test_namespaced_function" from the global namespace in foo.php line 12. Did you mean to call: "\\symfony\\component\\debug\\tests\\test_namespaced_function"?' ,
2013-05-28 18:12:41 +01:00
),
array (
array (
'type' => 1 ,
'line' => 12 ,
'file' => 'foo.php' ,
2013-07-23 19:05:03 +01:00
'message' => 'Call to undefined function Foo\\Bar\\Baz\\test_namespaced_function()' ,
2013-05-28 18:12:41 +01:00
),
2013-07-23 19:05:03 +01:00
'Attempted to call function "test_namespaced_function" from namespace "Foo\\Bar\\Baz" in foo.php line 12. Did you mean to call: "\\symfony\\component\\debug\\tests\\test_namespaced_function"?' ,
2013-05-28 18:12:41 +01:00
),
array (
array (
'type' => 1 ,
'line' => 12 ,
'file' => 'foo.php' ,
2013-07-23 19:05:03 +01:00
'message' => 'Call to undefined function foo()' ,
2013-05-28 18:12:41 +01:00
),
2013-07-23 19:05:03 +01:00
'Attempted to call function "foo" from the global namespace in foo.php line 12.' ,
2013-05-28 18:12:41 +01:00
),
array (
array (
'type' => 1 ,
'line' => 12 ,
'file' => 'foo.php' ,
2013-07-23 19:05:03 +01:00
'message' => 'Call to undefined function Foo\\Bar\\Baz\\foo()' ,
2013-05-28 18:12:41 +01:00
),
2013-07-23 19:05:03 +01:00
'Attempted to call function "foo" from namespace "Foo\Bar\Baz" in foo.php line 12.' ,
2013-05-28 18:12:41 +01:00
),
);
}
/**
* @ dataProvider provideUndefinedFunctionData
*/
public function testUndefinedFunction ( $error , $translatedMessage )
{
$handler = ErrorHandler :: register ( 3 );
2013-07-23 19:05:03 +01:00
$m = new \ReflectionMethod ( $handler , 'handleFatalError' );
$m -> setAccessible ( true );
2013-05-28 18:12:41 +01:00
$exceptionHandler = new MockExceptionHandler ;
set_exception_handler ( array ( $exceptionHandler , 'handle' ));
2013-07-23 19:05:03 +01:00
$m -> invoke ( $handler , $error );
2013-05-28 18:12:41 +01:00
$this -> assertNotNull ( $exceptionHandler -> e );
$this -> assertSame ( $translatedMessage , $exceptionHandler -> e -> getMessage ());
$this -> assertSame ( $error [ 'type' ], $exceptionHandler -> e -> getSeverity ());
$this -> assertSame ( $error [ 'file' ], $exceptionHandler -> e -> getFile ());
$this -> assertSame ( $error [ 'line' ], $exceptionHandler -> e -> getLine ());
restore_exception_handler ();
restore_error_handler ();
}
}
function test_namespaced_function ()
{
2011-03-05 19:23:35 +00:00
}