Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tip on finding PHP errors, warnings and notices #203

Open
tangix opened this issue Aug 23, 2017 · 3 comments
Open

Tip on finding PHP errors, warnings and notices #203

tangix opened this issue Aug 23, 2017 · 3 comments

Comments

@tangix
Copy link

tangix commented Aug 23, 2017

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"
>
@joel-depiltech
Copy link

Great @tangix ! Many thanks

@kenjis
Copy link
Owner

kenjis commented Aug 24, 2017

@tangix Thank you!

@kenjis
Copy link
Owner

kenjis commented Mar 18, 2018

Add function to detect errors in request output #235

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants