PHP running out of memory during test run

I’m using gitlab.com with a free plan.

As my app has grown, my tests are using more memory, and it seems after a Laravel 10 upgrade, I’ve reached a threshold that breaks the test runner. I’ve seen docs that say that the test runner instance has several Gb of memory available, so the instance itself is fine. The runner is built with a popular image: lorisleiva/laravel-docker:8.2. My tests are run using a stock PHPUnit config, and by default PHP has a memory_limit of 128M, which is the limit it’s hitting. I have tried increasing that to 256M when triggering the test run using PHP’s -d option, but it seems to be ignored.

I run my tests with this command:

$ php -d memory_limit=256M artisan test

a whole bunch of tests pass, but then:

   PASS  Tests\Feature\SocialGrantTest
  ✓ new usercan authenticate using social auth                           0.38s  
  ✓ social provider must be valid                                        0.28s  
  ✓ finds user if it already exists                                      0.13s  
In Connection.php line 419:
                                                                               
  Allowed memory size of 134217728 bytes exhausted (tried to allocate 16384 b  
  ytes)                                                                        

134217728 is 128M, not 256M, so that directive evidently didn’t work.

How can I increase the PHP memory allocation?

You can include a memory_limit setting in the the <php /> section (alongside, for instance, environment variables) of your phpunit.xml:

<php>
  ....
  <ini name="memory_limit" value="256M" />
  ....
</php>

This has worked for me in several projects that were hitting the 128M limit in tests.