macos phpunit memory_limit 参数不适用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8810094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 08:45:46  来源:igfitidea点击:

phpunit memory_limit parameter does not apply

phpmacosphpunitxdebugmemory-limit

提问by jerkan

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).

我刚刚在 mac osx 10.7 中通过 pear 安装了 phpunit,一切正常,除了出现内存限制错误(为报告启用 xdebug)。

I tried to add the -d memory_limit=512Mparameter 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"

我试图将-d memory_limit=512M参数添加到 phpunit 但它没有应用,因为在第一个错误时,我添加var_dump(ini_get('memory_limit')); exit;并打印 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"

它与“256M”相呼应

Is it possible that phpunit is not executing same php?

phpunit 是否有可能没有执行相同的 php?

回答by edorian

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

是的,你可以用每一个PHP选项设置phpunit -d可以与设置ini_set

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

您已经在 phpunit 错误跟踪器中打开了一个错误,但我将在这里寻求更详细的答案

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!

但是 phpunit 在第一个测试运行之前只应用一次这个选项!

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

所以很可能你的代码在某个地方改变了内存限制,32M这是 phpunit 无法“修复”的东西。

Same goes for setting the memory limit in the phpunit.xmlfile.

phpunit.xml文件中设置内存限制也是如此。

回答by ino

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

对于那些到达此线程并需要获取phpunit.xml配置文件的配置的人:

<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

有关“设置 PHP INI 设置、常量和全局变量”部分的更多信息,访问 https://phpunit.de/manual/6.5/en/appendixes.configuration.html

回答by Kevin

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

如果你不介意测试的速度,发现这个线程非常有用,因为它可以更好地处理你的应用程序的内存。

Basically; In your phpunit.xml, under the <phpunit>tag, set the processIsolationto truei.e

基本上; 在您phpunit.xml<phpunit>标签下,将 设置processIsolationtrueie

<?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_limitto -1i.e

您可以在<php>标签下完全禁用内存限制 ,将其设置memory_limit-1ie

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