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

  • Importance of backups

    Well… here we are.  10 years later, and … no backups.  Or… none of the data that’s important. Recently had a drive crash in my main server where this blog is hosted.  Had it happen 2 years ago, but the data was recovered, and I put everything on automatic backups.  Using virtualmin, a great control…

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

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

Leave a Reply

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