Enable PHP assertion errors on Laravel Homestead for development and testing

Laravel’s Homestead vagrant setup is handy to get going quickly on projects and keep them consistent to work on.

I was surprised that it has PHP assertion errors disabled by default. It’s good to have these enabled for local development and testing so that you can catch more problems earlier.

To enable PHP assertion errors in Homestead you can run this:

for phpConf in cli fpm; do
  sudo sed -i 's/zend.assertions = -1/zend.assertions = 1/g' /etc/php/7.*/${phpConf}/php.ini
  sudo sed -i 's/;assert.active = On/assert.active = On/g' /etc/php/7.*/${phpConf}/php.ini
  sudo sed -i 's/;assert.exception = On/assert.exception = On/g' /etc/php/7.*/${phpConf}/php.ini

That will enable PHP assertions and assertion errors for cli and web for all versions of PHP 7 in Homestead. I have it on a script so that it’s always run and I won’t forget about it.

You can also enable these at runtime, e.g. if you have a test bootstrap file in phpunit.xml like this:

<phpunit bootstrap="bootstrap/bootstrapTests.php">

Then you can enable PHP assertions and stricter errors during tests with:



assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_EXCEPTION, 1);

require __DIR__ . '/autoload.php';

Tech mentioned