Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignoring the PHP warnings in PHPUnit

Tags:

Am using PHPUnit for unit testing my functions when ever any warning comes in code the test script will not be executed for that functions, can anyone tell me how to ignore the warnings and proceed with testing

like image 850
somu.web Avatar asked Dec 07 '11 09:12

somu.web


2 Answers

As Juhana commented you should first of all fix your code where the warning(s) appear. It's a sign that the code is not working properly / strictly.

By default, PHPUnit converts PHP errors, warnings, and notices that are triggered during the execution of a test to an exception.

See Testing PHP Errors which has more information how to test for your warnings (and how to ignore warnings in sub-routines you call in tests).

To disable the default behaviour, you can tell PHPUnit to do so in your tests, e.g. within the setUp of your test or the test itself by setting a static variable in the global namespace:

# Warning: PHPUnit_Framework_Error_Warning::$enabled = FALSE;  # notice, strict: PHPUnit_Framework_Error_Notice::$enabled = FALSE; 

Another option to change the default behaviour is to configure the testrunner with an XML file with the following settings:

<phpunit convertErrorsToExceptions="false"          convertNoticesToExceptions="false"          convertWarningsToExceptions="false"> </phpunit> 

These three options are not available as command-line switches.

See as well the related question: test the return value of a method that triggers an error with PHPUnit.

like image 82
hakre Avatar answered Oct 06 '22 02:10

hakre


The documentented strategy to do this at a per-test level is to use the @ error suppression operator when your test calls the function that would trigger a warning or notice.

The following code is the example from the PHPUnit documentation:

<?php class ErrorSuppressionTest extends PHPUnit_Framework_TestCase {     public function testFileWriting() {         $writer = new FileWriter;         $this->assertFalse(@$writer->write('/is-not-writeable/file', 'stuff'));     } } class FileWriter {     public function write($file, $content) {         $file = fopen($file, 'w');         if($file == false) {             return false;         }         // ...     } } 
like image 35
Courtney Miles Avatar answered Oct 06 '22 04:10

Courtney Miles