ci-phpunit-test icon indicating copy to clipboard operation
ci-phpunit-test copied to clipboard

Tip on finding PHP errors, warnings and notices

Open tangix opened this issue 7 years ago • 3 comments

Just posting a tip here that we stumbled on in our CI flow. Some PHP code is pretty tough to test. In our case it was an array() argument to usort() where the name of the class was misspelled. Testing did not find this error and a bad version went out on the staging servers where it luckily was caught by the users before put into production. Checking the output from the phpunit tests it is quite obvious that the code is wrong - it generates output such as:

A PHP Error was encountered

Severity:    Warning
Message:     usort() expects parameter 2 to be a valid callback, class 'the_class_name' not found
Filename:    C:\ci-project\application\libraries\the_class_name.php
Line Number: 276

However, the tests ran successfully report all clear. The solution we now have implemented is to generate exceptions when runtime errors are encountered. In our setUp() and tearDown() we have now added the following code:

	public function setUp()
	{
		set_error_handler(function($errno, $errstr, $errfile, $errline) {
			throw new RuntimeException($errstr . " on line " . $errline . " in file " . $errfile);
		});

		$this->resetInstance();
	}

	public function tearDown()
	{
		parent::tearDown();
		restore_error_handler();
	}

Just a friendly reminder that testing may not find all problems in your code :-)

UPDATE: Just remember that you will need to add settings (convert*) to your phpunit.xml to generate exceptions:

<phpunit
        bootstrap="./Bootstrap.php"
        colors="true"
        convertErrorsToExceptions="true"
        convertNoticesToExceptions="true"
        convertWarningsToExceptions="true"
        strict="true"
>

tangix avatar Aug 23 '17 08:08 tangix

Great @tangix ! Many thanks

joel-depiltech avatar Aug 23 '17 08:08 joel-depiltech

@tangix Thank you!

kenjis avatar Aug 24 '17 00:08 kenjis

Add function to detect errors in request output #235

kenjis avatar Mar 18 '18 13:03 kenjis