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.

Similar Posts

  • PHP Quality Tools

    Curious about checking out the quality of your PHP project, but don’t know where to start? https://github.com/jakzal/phpqa is a project providing docker images of various tools to help measure aspects of your PHP code. will run the phploc tool on your current folder But… you can alias the tool, then simply run $ phpqa <toolname>…

  • Four Thousand Weeks

    I’m starting to read “Four Thousand Weeks” from Oliver Burkeman. I initially listened to much of the audio book, then bought a copy (link above to Amazon – no affiliate link). Have not finished yet, but the core message of the book seems to be There’s certainly more to it than this, and again, I’m…

  • Open Source TechFinder

    Inspired by the AUTM conference, I got inspired to look at some of the common processes techtransfer folks do. Main idea was to try to develop something relatively ‘standalone’ that might address a use case I learned about, so I decided on building a web-based open source techfinder tool to publish licensable technologies. The notion…

  • CyclopsMonitor

    I’ve been posting more about this new service over on linkedin, but haven’t posted much here. CyclopsMonitor is a web monitoring service – checking if a web address is up, how fast it responds, if specific content is available, when SSL/TLS certs expire, when domain name expires, and… sending you notifications when problems occur. Currently,…

Leave a Reply

Your email address will not be published. Required fields are marked *