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

  • Laravel down migrations

    I get an email newsletter from Martin Joo every week or so. The newsletters generally have some useful tips around the Laravel framework or sometimes just general development tips. I’ve learned a couple of neat tricks here and there, and will continue to receive. This morning I received an email with a Laravel ‘tip’ regarding…

  • Identity and habits

    “Your present identity should not constrain your future habits”. For this quote from the audio book “Hello, Habits“. Obviously the book is about ‘habits’, but the phrase could easily have been “Your present identity should not constrain your future self”. In either reading of this, it’s been stuck in my head for while now.  How…

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

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