Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHPUnit覆盖率:允许内存大小536870912字节已用尽_Php_Docker_Phpunit_Phpdbg - Fatal编程技术网

PHPUnit覆盖率:允许内存大小536870912字节已用尽

PHPUnit覆盖率:允许内存大小536870912字节已用尽,php,docker,phpunit,phpdbg,Php,Docker,Phpunit,Phpdbg,我正在尝试使用以下命令为PHPUnit和phpdbg的PHP项目生成代码测试覆盖率: phpdbg -dmemory_limit=512M -qrr ./bin/phpunit -c .phpunit.cover.xml 这非常好: PHPUnit 6.2.4 by Sebastian Bergmann and contributors. ........ 8 / 8 (10

我正在尝试使用以下命令为PHPUnit和phpdbg的PHP项目生成代码测试覆盖率:

phpdbg -dmemory_limit=512M -qrr ./bin/phpunit -c .phpunit.cover.xml
这非常好:

PHPUnit 6.2.4 by Sebastian Bergmann and contributors.

........                                                            8 / 8 (100%)

Time: 114 ms, Memory: 14.00MB

OK (8 tests, 13 assertions)

Generating code coverage report in HTML format ... done
但是,当我在docker容器中使用完全相同的命令时:

docker run -it --name YM4UPltmiPMjObaVULwsIPIkPL2bGL0T -e USER=sasan -v "/home/sasan/Project/phpredmin:/phpredmin" -w "/phpredmin" --user "1000:www-data" php:7.0-apache phpdbg -dmemory_limit=512M -qrr ./bin/phpunit -c .phpunit.cover.xml
我得到以下错误:

PHPUnit 6.2.4 by Sebastian Bergmann and contributors.

[PHP Fatal error:  Allowed memory size of 536870912 bytes exhausted (tried to allocate 561514763337856 bytes) in /phpredmin/vendor/phpunit/phpunit/src/Util/GlobalState.php on line 166]
我不明白为什么PHPUnit需要分配561514763337856字节的内存。我怀疑它会卡在一个循环中,但为什么这不会发生在容器之外呢?这是我机器上的PHP版本:

PHP 7.0.22-0ubuntu0.17.04.1 (cli) (built: Aug  8 2017 22:03:30) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.22-0ubuntu0.17.04.1, Copyright (c) 1999-2017, by Zend Technologies
下面是.phpunit.cover.xml文件:

<phpunit
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.3/phpunit.xsd"
        backupGlobals="false"
        backupStaticAttributes="false"
        bootstrap="vendor/autoload.php"
        cacheTokens="false"
        colors="false"
        convertErrorsToExceptions="true"
        convertNoticesToExceptions="true"
        convertWarningsToExceptions="true"
        processIsolation="false"
        stopOnError="true"
        stopOnFailure="true"
        stopOnIncomplete="false"
        stopOnSkipped="false"
        stopOnRisky="false"
        timeoutForSmallTests="1"
        timeoutForMediumTests="10"
        timeoutForLargeTests="60"
        verbose="false">
    <testsuites>
            <testsuite name="PhpRedmin PHP source">
            <directory>src-test/</directory>
            </testsuite>
    </testsuites>
    <logging>
        <log type="coverage-html" target="cover/" lowUpperBound="35" 
highLowerBound="70"/>
    </logging>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">src-test/</directory>
            <directory suffix=".php">src/</directory>
        </whitelist>
    </filter>
</phpunit>

src试验/
src试验/
src/
--Edit1--

我发现它与@runinsepareprocess有关。当我删除带有@runinsepareprocess的测试时,它就开始工作了。但我还是不知道问题出在哪里

--Edit2--


我还发现,如果我不在Docker容器中装载代码目录,一切正常

您装载的文件夹可能包含所有供应商和缓存文件。尝试仅装载源文件夹。

当我们使用
@runinsepareprocess
时,PHPUnit将尝试序列化包含的文件、ini设置、全局变量和常量,以将它们传递给新进程。在本例中,PHPUnit在序列化其中一个项时似乎遇到了递归场景,这耗尽了PHP进程可用的内存。我们需要确定您的本地环境和Docker容器之间发生了什么变化

首先,我们可以尝试禁用此序列化行为,以验证是否应该沿着此路径继续。将以下
@preserveGlobalState
注释添加到失败的测试方法:

/**
 * @runInSeparateProcess
 * @preserveGlobalState disabled
 */
public function testInSeparateProcess()
{
    // ...
}
如果这解决了问题,或者如果我们得到一个新的错误,我们可以开始查找Docker容器中可能导致问题的差异。如果对代码和环境没有更多的了解,很难建议从何处开始,但以下是一些想法:

  • 比较来自每个环境的
    php-i
    的输出。注意其中一个中存在的PHP扩展,而另一个中不存在
  • 使用
    phpdbg
    设置断点并单步执行代码。我们已经在使用它来生成覆盖率,但它也是一个有用的调试工具。我们正在寻找导致无限递归的项。注意,我们需要在PHPUnit执行测试用例之前设置断点,例如在引导文件或PHPUnit源代码中(TestCase的第810行可以工作)
  • 绑定装入卷时,请确保容器中的
    www-data
    用户与主机上拥有文件的用户具有相同的UID
  • 尝试在没有内存限制的情况下运行测试。显然,我们无法分配错误提示我们可能需要的数量,但内存限制可能会掩盖另一个潜在问题。如果需要,我们可以杀死容器

我无法在类似的环境中使用虚拟测试重现这个问题(尽可能接近我可以得到的具有卷的相同容器),因此测试中的代码可能会导致这个问题

这是phpdbg错误。注意它试图分配多少内存?这太疯狂了。我试图找出错误的确切位置:如果测试脚本全名(目录+脚本文件名)超过一定的大小,那么它将溢出堆栈(是的,stackoverflow;),并覆盖指定必须分配多少内存的整数。然后,正常的错误处理开始了:没有足够的内存来分配如此惊人的数量。这是Krakjoe要解决的问题。或者我会在业余时间为它做一个补丁。如果您现在需要解析:请确保脚本所在的目录路径很短。

在测试之前添加'-d memory\u limit=-1'

如果使用composer,请使用以下代码

./vendor/bin/simple-phpunit -d memory_limit=-1 tests/

phpdbg -d memory_limit=-1 -qrr vendor/bin/phpunit --coverage-text
如果使用phpdbg,请使用以下代码

./vendor/bin/simple-phpunit -d memory_limit=-1 tests/

phpdbg -d memory_limit=-1 -qrr vendor/bin/phpunit --coverage-text

您使用的是什么docker图像?php是作为服务还是作为apache模块,尝试增加php.ini中的php内存。您在什么操作系统上运行此操作?@JoaquinJavi,正如您看到的,它是php:7.0-apache:docker run-it--name ym4upltmipmipmjobavulwsipipipipl2bgl0t-e USER=sasan-v”/home/sasan/Project/phpredmin:/phpredmin“-w”/phpredmin--USER“1000:www-data”php:7.0-apachephpdbg-dmemory_limit=512M-qrr./bin/phpunit-c.phpunit.cover.xml我无法将内存限制增加到561514763337856bytes@TarunLalwani不,不是这样的。你能提供一个git回购协议,并提供一个我可以用来复制它的最小示例吗?但是我测试中需要的所有phpunit文件和第三方文件都在供应商那里。不安装它们有什么意义?您仍然可以访问供应商和phpunit。该卷在某种程度上是指向文件的符号链接,这意味着即使在容器停止之后,对这些文件的更改也将被持久化。您可以使用这个
docker run-it--name YM4UPltmiPMjObaVULwsIPIkPL2bGL0T-e USER=sasan-v”/home/sasan/Project/phpredmin/src:/phpredmin/src“-w”/phpredmin”-USER“1000:www-data”php:7.0-apache-phpdbg-dmemory_limit=512M-qrr./bin/phpunit-c.phpunit.cover.xml
我假设
phpdbg
中有一个bug,或者我的代码中只有在测试时才会出现的递归循环,但是,唉,解决方案完全如您所述:
phpdbg
只需要更多内存!我不得不将128M的默认值加倍,并使用256M(我不喜欢使用无限的想法)。