PHP assert not working in Laravel with Sail?
Recently, I hit a weird ‘bug’.
<?php
...
$file = ReportService::generateReport(4);
assert(file_exists($file));
...
// do more stuff
The $file was not being created, so I’d added a quick ‘assert’ in the code, and… file_exists($file) was indeed false (checked when stepping through debugger), but assert was not stopping/failing.
But… running a simple assert from command line was working. This was something different only when running under the Laravel Sail docker setup. I dug in a bit more…
The Laravel Sail system is a convenient way to run a local dev environment using a pre-defined docker compose configuration. At heart, the docker compose system is pulling from a current ubuntu and grabbing PHP packages from ppa-ondrej. Those packages, however, are configured for production, and … I don’t know when this happened, but the basic ‘assert’ function in PHP somehow got extended with a bunch of ini-based configuration stuff, meaning what used to be a basic understandable function now has … many ways to not work as expected (well, my earlier expectations, at any rate).
If you’ve not published the sail assets yet, run
artisan sail:publish
This will put specific PHP versions and Dockerfile configs in /docker folder.
In which PHP version you’re using, find the php.ini file and add a line under the [PHP] section with ‘zend.assertions=1’. The ‘default’ for zend.assertions is ‘1’, but the production PHP packages must be setting it to -1 (for production, which sort of makes sense?)
[PHP]
post_max_size = 100M
upload_max_filesize = 100M
variables_order = EGPCS
zend.assertions = 1
Then rebuild the sail container (./vendor/bin/sail build –no-cache)
This will set the zend.assertions to ‘1’ which is the development mode behaviour. This is more appropriate for sail, which is intended as a development platform, not production.
The behaviour of ‘assert‘ has grown a lot over the years, and I’ve not kept up with any of that (which shows how often I use it!). The ‘zend.assertions‘ ini setting is useful, but I don’t know when it was added.