phpunit memory_limit parameter does not apply
Asked Answered
A

3

29

I've just installed phpunit via pear in a mac osx 10.7 and everything works fine except I got memory limit errors (xdebug enabled for reports).

I tried to add the -d memory_limit=512M parameter to phpunit but it is not applying because, on the very first error, I added var_dump(ini_get('memory_limit')); exit; and it prints string(3) "32M"

So, why is it not being applied?

Besides that, if I run

php -d memory_limit=256M -r "echo ini_get('memory_limit');"

it echoes "256M"

Is it possible that phpunit is not executing same php?

Arrogant answered 10/1, 2012 at 20:31 Comment(5)
Did you try ini_set('memory_limit', '512M')?Abutment
@Abutment It works but i don't get why it doesn't with command lineArrogant
This is going to sound stupid, but what happens if you try phpunit -dmemory_limit=512M?Abutment
Does PHPUnit even accept -d?Sechrist
In another machine it worked for meArrogant
M
42

Yes you can set every php option with phpunit -d that can be set with ini_set.

You already opened a bug over in the phpunit bug tracker but well I'm going for the more verbose answer here

Reproduce to show it works in general:

echo "<?php var_dump(ini_get('memory_limit')); " > foo.php

phpunit -d memory_limit=12M --bootstrap foo.php 

Produces:

string(3) "12M"
PHPUnit 3.6.5 by Sebastian Bergmann.

But phpunit only applies this option once before the first test is run!

So chances are your code is somewhere changing the memory limit back to 32M which is something phpunit can't "fix".

Same goes for setting the memory limit in the phpunit.xml file.

Meprobamate answered 10/1, 2012 at 22:29 Comment(3)
That example worked as explained. However, I coud not found any 'memory_limit' in the code. I will continue debugging. Thanks.Arrogant
Thanks for mentioning that my code ofcourse could set the limitFtlb
That, right there, saved my day... "So chances are your code is somewhere changing the memory limit back to 32M which is something phpunit can't "fix".Kodok
A
50

For those who reach this thread and need to get the configuration for phpunit.xml configuration file:

<php>
  <ini name="memory_limit" value="512M" />
</php>

More on section "Setting PHP INI settings, Constants and Global Variables" at https://phpunit.de/manual/6.5/en/appendixes.configuration.html

Aten answered 21/11, 2018 at 13:40 Comment(0)
M
42

Yes you can set every php option with phpunit -d that can be set with ini_set.

You already opened a bug over in the phpunit bug tracker but well I'm going for the more verbose answer here

Reproduce to show it works in general:

echo "<?php var_dump(ini_get('memory_limit')); " > foo.php

phpunit -d memory_limit=12M --bootstrap foo.php 

Produces:

string(3) "12M"
PHPUnit 3.6.5 by Sebastian Bergmann.

But phpunit only applies this option once before the first test is run!

So chances are your code is somewhere changing the memory limit back to 32M which is something phpunit can't "fix".

Same goes for setting the memory limit in the phpunit.xml file.

Meprobamate answered 10/1, 2012 at 22:29 Comment(3)
That example worked as explained. However, I coud not found any 'memory_limit' in the code. I will continue debugging. Thanks.Arrogant
Thanks for mentioning that my code ofcourse could set the limitFtlb
That, right there, saved my day... "So chances are your code is somewhere changing the memory limit back to 32M which is something phpunit can't "fix".Kodok
E
14

If you don't mind about the speed of the tests, found this thread very useful as it's better handling of memory for your app.

Basically; In your phpunit.xml, under the <phpunit> tag, set the processIsolation to true i.e

<?xml version="1.0" encoding="UTF-8"?>
<phpunit 
         ...
         processIsolation="true"
         ... >

Alternatively,

You can just disable memory limiting altogether under the <php> tag, set the memory_limit to -1 i.e

<php>
        ...
        <ini name="memory_limit" value="-1"/>
        ...

Exacting answered 20/4, 2020 at 8:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.