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

  • 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,…

  • Onboarding freelancers

    Maari Casey over at uncompany had a recent LinkedIn post about planning for freelancers. She made some good points, but I think skipped one, and it’s not just relevant for freelancers. Even well before an organization might need extra work – be it freelance or employee – companies need to have a plan for onboarding….

  • Bad I9 PDF form

    Have been needing to programmatically fill out an I9 PDF, retrieved from gov site. Should be fairly straightforward, right? Well… the field names are… a mess. Field names like topmostSubform[0].Page1[0].U\.S\._Social_Security_Number__Last_4_numbers_[0]topmostSubform[0].Page1[0].expiration_date__if_applicable__mm_dd_yyyy[0] topmostSubform[0].Page2[0].Employers_Business_or_Organization_Address_Street_Number_and_Name[0] and so on make it pretty… not straightforward to create a usable key/value combination to search and replace. But… today, I noticed it got…

Leave a Reply

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