Non è stato davvero facile, ma alla fine sono riuscito a catturare l'errore E_NOTICE
che volevo. Avevo bisogno di ignorare l'attuale error_handler
per generare un'eccezione che prenderò in una dichiarazione try{}
.
function testGotUndefinedIndex() {
// Overriding the error handler
function errorHandlerCatchUndefinedIndex($errno, $errstr, $errfile, $errline) {
// We are only interested in one kind of error
if ($errstr=='Undefined index: bar') {
//We throw an exception that will be catched in the test
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
return false;
}
set_error_handler("errorHandlerCatchUndefinedIndex");
try {
// triggering the error
$foo = array();
echo $foo['bar'];
} catch (ErrorException $e) {
// Very important : restoring the previous error handler
restore_error_handler();
// Manually asserting that the test fails
$this->fail();
return;
}
// Very important : restoring the previous error handler
restore_error_handler();
// Manually asserting that the test succeed
$this->pass();
}
Questo sembra un po 'troppo complicato dover ridichiarare il gestore degli errori per generare un'eccezione solo per prenderlo. L'altra parte difficile stava ripristinando correttamente il gestore_errore sia quando veniva rilevata un'eccezione che non si verificava alcun errore, altrimenti si limitava a fare errori con la gestione degli errori di SimpleTest.
Che linguaggio orribile! – m93a
Se si fornisce E_NOTICE come secondo parametro a set_error_handler, gestirà solo le notifiche. "Può essere usato per mascherare l'attivazione della funzione _error_handler_ proprio come l'impostazione error_reporting ini controlla quali errori sono mostrati. Senza questa maschera, verrà richiamato _error_handler_ per ogni errore indipendentemente dall'impostazione dell'impostazione error_reporting." – nick