PHPUnit silent fail

Debugging PHPUnit silent failures

Sometimes PHPUnit silently fails like this

PHPUnit silent fail
PHPUnit silent fail

Even if I can very well understand that something went wrong PHPUnit CLI is not giving me any hint.

Go verbose

Running PHPUnit via the CLI using the -v option makes PHPUnit print all of its possible outputs and, maybe, help in solving the silent death. Even better: I use PHPUnit configuration file, phpunit.xml, to avoid any lengthy console entry as PHPUnit states in the picture above.

Wrong includes and requires make PHPUnit go crazy

Many times PHPUnit fails due to some wrong includes or requires instructions: I always check those first thing for any kind of typos or mis-direction in paths.

Use a linter

A code linter is something that checks my code for any kind of syntax error. It usually comes embedded in any kind of software IDE and many IDEs also offer some dynamic linking to methods and functions defined in other files for the purpose of auto-completion and real-time check.
I do not use an IDE proper but chose Sublime Text instead and its plugin SublimeLinter to check my PHP code while working on it.

Error spotted by linter
Error spotted by linter

A linter will catch many PHPUnit-killer errors before they happen and will avoid some PHPUnit’ non-ablative screen of death.

Read the log

All of my PHP development actually revolves around WordPress and using MAMP allows me to quickly access the error-log of the PHP version that comes with it

View error log in MAMP
View error log in MAMP

The error log, the picture below is taken on a Mac machine, allows me to have a snapshot of what went wrong with PHP in the last run and try to fix it.
PHP error log on a Mac machine
PHP error log on a Mac machine

If you run the version of PHP that comes embedded with your OS of choice then simply search for the php_error.log file (its default name) and read it.

Last in, first out

Nine times on ten the guilty code is the one you added last so commenting the last lines out and trying to run the tests again might help. Then un-comment the lines one by one and repeat.

Last added code commented
Last added code commented

Make my tests work THEN make my tests pass

I also try to dumb down my tests to the simplest form, sometimes empty, and use simpler versions of it. If I know a function will call some_method once with an argument and will require the mock to return some precise value I will write the lines below one by one incrementally adding code to my expectation and running the tests in between each increment

$this->mocks->filters->expects($this->once())->method('methodName'); $this->mocks->filters->expects($this->once())->method('methodName')->with($this->equalTo('some_argument')); $this->mocks->filters->expects($this->once())->method('methodName')->with($this->equalTo('some_argument'))->will($this->returnValue('some_value')); 

Adding incrementally allows me to know it’s the last addition that broke my code.

I appreciate your input