[Phpmyadmin-devel] PHPUnit and our error handler

Hi, I want to modify a test to fit with a modification I am working on. In this modification, the tested function uses $GLOBALS['error_handler']. How can I define this global inside my test? -- Marc Delisle http://infomarc.info

On 01/14/2013 06:00 PM, Marc Delisle wrote:
Hi, I want to modify a test to fit with a modification I am working on. In this modification, the tested function uses $GLOBALS['error_handler']. How can I define this global inside my test?
How about the below code? Totally untested, off the top of my head, but should work.... protected function setUp() { $GLOBALS['error_handler'] = new PMA_Error_Handler(); } Bye, Rouslan

Rouslan Placella a écrit :
On 01/14/2013 06:00 PM, Marc Delisle wrote:
Hi, I want to modify a test to fit with a modification I am working on. In this modification, the tested function uses $GLOBALS['error_handler']. How can I define this global inside my test?
How about the below code? Totally untested, off the top of my head, but should work....
protected function setUp() { $GLOBALS['error_handler'] = new PMA_Error_Handler(); }
Thanks. I added this (plus the "require" for the error class) but now I get Failed asserting that exception of type "PHPUnit_Framework_Error" is thrown. See attached patch. -- Marc Delisle http://infomarc.info diff --git a/libraries/Error_Handler.class.php b/libraries/Error_Handler.class.php index b6b9bf0..609791c 100644 --- a/libraries/Error_Handler.class.php +++ b/libraries/Error_Handler.class.php @@ -99,8 +99,16 @@ class PMA_Error_Handler */ public function handleError($errno, $errstr, $errfile, $errline) { + $this->addError($errstr, $errno, $errfile, $errline, $escape=true); + } + + public function addError($errstr, $errno, $errfile, $errline, $escape=true) + { + if ($escape) { + $errstr = htmlspecialchars($errstr); + } // create error object - $error = new PMA_Error($errno, htmlspecialchars($errstr), $errfile, $errline); + $error = new PMA_Error($errno, $errstr, $errfile, $errline); // do not repeat errors $this->_errors[$error->getHash()] = $error; @@ -131,6 +139,7 @@ class PMA_Error_Handler } } + /** * log error to configured log facility * diff --git a/libraries/core.lib.php b/libraries/core.lib.php index 93be5fe..eb698ff 100644 --- a/libraries/core.lib.php +++ b/libraries/core.lib.php @@ -277,7 +277,12 @@ function PMA_warnMissingExtension($extension, $fatal = false, $extra = '') if ($fatal) { PMA_fatalError($message); } else { - trigger_error($message, E_USER_WARNING); + $GLOBALS['error_handler']->addError( + $message, + E_USER_WARNING, + '', + '', + $escape=false); } } diff --git a/test/libraries/core/PMA_warnMissingExtension_test.php b/test/libraries/core/PMA_warnMissingExtension_test.php index 1e11c73..661e6c7 100644 --- a/test/libraries/core/PMA_warnMissingExtension_test.php +++ b/test/libraries/core/PMA_warnMissingExtension_test.php @@ -15,6 +15,11 @@ require_once 'libraries/core.lib.php'; class PMA_warnMissingExtension_test extends PHPUnit_Framework_TestCase { + protected function setUp() { + require_once './libraries/Error_Handler.class.php'; + $GLOBALS['error_handler'] = new PMA_Error_Handler(); + } + function testMissingExtention(){ $ext = 'php_ext'; $this->setExpectedException('PHPUnit_Framework_Error',

On 01/14/2013 06:34 PM, Marc Delisle wrote:
Rouslan Placella a écrit :
On 01/14/2013 06:00 PM, Marc Delisle wrote:
Hi, I want to modify a test to fit with a modification I am working on. In this modification, the tested function uses $GLOBALS['error_handler']. How can I define this global inside my test?
How about the below code? Totally untested, off the top of my head, but should work....
protected function setUp() { $GLOBALS['error_handler'] = new PMA_Error_Handler(); }
Thanks. I added this (plus the "require" for the error class) but now I get
Failed asserting that exception of type "PHPUnit_Framework_Error" is thrown. See attached patch.
Is the test supposed to throw a PHP error (I haven't run the test yet)? If yes, you just need to tell the test to expect it. This is explained in the "Testing Exceptions" section of phpunit documentation: http://www.phpunit.de/manual/3.2/en/writing-tests-for-phpunit.html#id323367 Bye, Rouslan

Hi Dne Mon, 14 Jan 2013 13:34:24 -0500 Marc Delisle <marc@infomarc.info> napsal(a):
Rouslan Placella a écrit : Failed asserting that exception of type "PHPUnit_Framework_Error" is thrown. See attached patch.
[..]
diff --git a/libraries/core.lib.php b/libraries/core.lib.php index 93be5fe..eb698ff 100644 --- a/libraries/core.lib.php +++ b/libraries/core.lib.php @@ -277,7 +277,12 @@ function PMA_warnMissingExtension($extension, $fatal = false, $extra = '') if ($fatal) { PMA_fatalError($message); } else { - trigger_error($message, E_USER_WARNING); + $GLOBALS['error_handler']->addError( + $message, + E_USER_WARNING, + '', + '', + $escape=false);
You no longer trigger PHP error here.
} }
diff --git a/test/libraries/core/PMA_warnMissingExtension_test.php b/test/libraries/core/PMA_warnMissingExtension_test.php index 1e11c73..661e6c7 100644 --- a/test/libraries/core/PMA_warnMissingExtension_test.php +++ b/test/libraries/core/PMA_warnMissingExtension_test.php @@ -15,6 +15,11 @@ require_once 'libraries/core.lib.php'; class PMA_warnMissingExtension_test extends PHPUnit_Framework_TestCase {
+ protected function setUp() { + require_once './libraries/Error_Handler.class.php'; + $GLOBALS['error_handler'] = new PMA_Error_Handler(); + } + function testMissingExtention(){ $ext = 'php_ext'; $this->setExpectedException('PHPUnit_Framework_Error',
So it can not pop up as en error here. The addError method just adds it to list of errors in error handle for future display, but that does not trigger PHP error. -- Michal Čihař | http://cihar.com | http://blog.cihar.com

Michal Čihař a écrit :
Hi
Dne Mon, 14 Jan 2013 13:34:24 -0500 Marc Delisle <marc@infomarc.info> napsal(a):
Rouslan Placella a écrit : Failed asserting that exception of type "PHPUnit_Framework_Error" is thrown. See attached patch.
[..]
diff --git a/libraries/core.lib.php b/libraries/core.lib.php index 93be5fe..eb698ff 100644 --- a/libraries/core.lib.php +++ b/libraries/core.lib.php @@ -277,7 +277,12 @@ function PMA_warnMissingExtension($extension, $fatal = false, $extra = '') if ($fatal) { PMA_fatalError($message); } else { - trigger_error($message, E_USER_WARNING); + $GLOBALS['error_handler']->addError( + $message, + E_USER_WARNING, + '', + '', + $escape=false);
You no longer trigger PHP error here.
Indeed. I see no way of passing the additional parameter I need (to avoid escaping) via the error handler, as the number of parameters is not changeable.
} }
diff --git a/test/libraries/core/PMA_warnMissingExtension_test.php b/test/libraries/core/PMA_warnMissingExtension_test.php index 1e11c73..661e6c7 100644 --- a/test/libraries/core/PMA_warnMissingExtension_test.php +++ b/test/libraries/core/PMA_warnMissingExtension_test.php @@ -15,6 +15,11 @@ require_once 'libraries/core.lib.php'; class PMA_warnMissingExtension_test extends PHPUnit_Framework_TestCase {
+ protected function setUp() { + require_once './libraries/Error_Handler.class.php'; + $GLOBALS['error_handler'] = new PMA_Error_Handler(); + } + function testMissingExtention(){ $ext = 'php_ext'; $this->setExpectedException('PHPUnit_Framework_Error',
So it can not pop up as en error here. The addError method just adds it to list of errors in error handle for future display, but that does not trigger PHP error.
-- Marc Delisle http://infomarc.info

Michal Čihař a écrit :
Hi
Dne Mon, 14 Jan 2013 13:34:24 -0500 Marc Delisle <marc@infomarc.info> napsal(a):
Rouslan Placella a écrit : Failed asserting that exception of type "PHPUnit_Framework_Error" is thrown. See attached patch.
[..]
diff --git a/libraries/core.lib.php b/libraries/core.lib.php index 93be5fe..eb698ff 100644 --- a/libraries/core.lib.php +++ b/libraries/core.lib.php @@ -277,7 +277,12 @@ function PMA_warnMissingExtension($extension, $fatal = false, $extra = '') if ($fatal) { PMA_fatalError($message); } else { - trigger_error($message, E_USER_WARNING); + $GLOBALS['error_handler']->addError( + $message, + E_USER_WARNING, + '', + '', + $escape=false);
You no longer trigger PHP error here.
} }
diff --git a/test/libraries/core/PMA_warnMissingExtension_test.php b/test/libraries/core/PMA_warnMissingExtension_test.php index 1e11c73..661e6c7 100644 --- a/test/libraries/core/PMA_warnMissingExtension_test.php +++ b/test/libraries/core/PMA_warnMissingExtension_test.php @@ -15,6 +15,11 @@ require_once 'libraries/core.lib.php'; class PMA_warnMissingExtension_test extends PHPUnit_Framework_TestCase {
+ protected function setUp() { + require_once './libraries/Error_Handler.class.php'; + $GLOBALS['error_handler'] = new PMA_Error_Handler(); + } + function testMissingExtention(){ $ext = 'php_ext'; $this->setExpectedException('PHPUnit_Framework_Error',
So it can not pop up as en error here. The addError method just adds it to list of errors in error handle for future display, but that does not trigger PHP error.
I think I understand now, working on it... -- Marc Delisle http://infomarc.info
participants (3)
-
Marc Delisle
-
Michal Čihař
-
Rouslan Placella