Sas*_*ose 18 php phpunit docker phpdbg
我正在尝试使用以下命令使用PHPUnit和phpdbg为我的PHP项目生成代码测试覆盖:
phpdbg -dmemory_limit=512M -qrr ./bin/phpunit -c .phpunit.cover.xml
Run Code Online (Sandbox Code Playgroud)
这非常好用:
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
Run Code Online (Sandbox Code Playgroud)
但是,当我在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
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
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]
Run Code Online (Sandbox Code Playgroud)
我不明白为什么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
Run Code Online (Sandbox Code Playgroud)
这是.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>
Run Code Online (Sandbox Code Playgroud)
- 编辑1 -
我发现它与@runInSeparateProcess有关.当我删除具有@runInSeparateProcess的测试时,它开始工作.但我仍然不知道是什么问题
- 编辑2 -
另外我发现如果我没有在Docker容器中挂载我的代码目录,一切正常
当我们使用 时@runInSeparateProcess,PHPUnit 将尝试序列化包含的文件、ini 设置、全局变量和常量,以将它们传递给新进程。在这种情况下,PHPUnit 在序列化其中一项时似乎遇到了递归场景,这耗尽了 PHP 进程可用的内存。我们需要确定本地环境和 Docker 容器之间发生了什么变化。
首先,我们可以尝试禁用此序列化行为,以验证我们是否应该沿着这条路径继续进行。@preserveGlobalState在失败的测试方法中添加如下注解:
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testInSeparateProcess()
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
如果这解决了问题,或者出现新错误,我们可以开始查找 Docker 容器中可能导致问题的差异。如果没有更多地了解代码和环境,就很难建议从哪里开始,但这里有一些想法:
php -i比较每个环境的输出。留意其中一个中存在但另一个中不存在的 PHP 扩展。phpdbg设置断点并单步执行代码。我们已经使用它来生成覆盖率,但它也是一个有用的调试工具。我们正在寻找导致无限递归的项目。请注意,我们需要在 PHPUnit 执行测试用例之前TestCase设置断点,例如在引导文件或 PHPUnit 源代码中(第 810 行可能有效)。www-data容器中的用户与拥有主机上文件的用户具有相同的 UID。我无法在类似的环境中使用虚拟测试来重现此问题(尽可能接近 - 具有相同体积的容器),因此正在测试的代码可能会导致该问题。
在测试前添加“-d memory_limit=-1”
如果使用作曲家使用下面的代码
./vendor/bin/simple-phpunit -d memory_limit=-1 tests/
Run Code Online (Sandbox Code Playgroud)
如果您使用 phpdbg,请使用以下代码
phpdbg -d memory_limit=-1 -qrr vendor/bin/phpunit --coverage-text
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3445 次 |
| 最近记录: |