2010-07-16 13 views
15

Vorrei scrivere un test usando simpleTest che fallirebbe se il metodo che sto test risultasse in un PHP E_NOTICE "indice indefinito: foo".Come rilevare un errore "indice non definito" E_NOTICE in simpleTest?

Ho provato expectError() e expectException() senza successo. La pagina web simpleTest indica che simpleTest non è in grado di rilevare errori PHP in fase di compilazione, ma E_NOTICE sembra essere un errore di run time.

C'è un modo per rilevare un tale errore e il test fallisce in questo caso?

risposta

16

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.

+4

Che linguaggio orribile! – m93a

+0

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

3

Non c'è davvero bisogno di rilevare l'errore di avviso. Si potrebbe anche testare l'esito di 'array_key_exists' e quindi procedere da lì.

http://www.php.net/manual/en/function.array-key-exists.php

prova per falso e farlo sicuro.

+0

Se il voto è basso, si prega di spiegarne il motivo. –

+2

C'è un caso quando si proietta un array in un altro array. Non si vuole testare per ogni singola mappa, si vuole cogliere il caso in cui una delle mappe fallisce. Questo sarebbe molto più facile con le eccezioni. – CMCDragonkai

0

Non riuscirai mai a prendere all'interno del blocco try-catch, per fortuna abbiamo set_error_handler():

<?php 
function my_handle(){} 
set_error_handler("my_handle"); 
echo $foo["bar"]; 
?> 

Si può fare tutto quello che vuoi all'interno my_handle() la funzione, o semplicemente lasciare vuota al silenzio l'avviso, anche se, non è raccomandato. Un gestore normale dovrebbe essere così:

function myErrorHandler($errno, $errstr, $errfile, $errline) 
Problemi correlati