PHP: 7.2.7
交响乐:3.4.12
PHPUNIT-BRIDGE:4.1.1(phpunit 6.5.8)
我有一个用 PHP 编写的以 root 用户身份运行的部署脚本,自从从 7.0 升级到 php 7.2 并相应地更新我的代码后,它在运行 phpunit 测试时开始莫名其妙地失败。我确定我在这里有错,但是经过数小时的调试后我一无所获,希望有人能指出我正确的方向。
该脚本以 root 身份运行,并在执行以下操作时:
exec("runuser MY-USER -c 'bin/simple-phpunit'", $output, $returnCode);
我所有的测试都失败了:
You have requested a non-existent service "test.client"
关于这个错误有很多 SO 问题,它与 phpunit 环境未设置为“test”有关。我的 phpunit.xml 使用正确的值:
<env name="APP_ENV" value="test" />
疯狂的部分是,当我自己(作为我的用户)运行单元测试时,它们运行得很好。当我以 root 身份登录并执行时,它们甚至可以正常运行
runuser MY-USER -c 'bin/simple-phpunit'
他们甚至在我跑的时候跑
$ sudo su
$ php -a
php > exec("runuser MY-USER -c 'bin/simple-phpunit'", $output, $returnCode);
当我绝望并开始将变量转储到控制台时,似乎一切正常:
echo "ENV = " .getenv('APP_ENV'); // [OUTPUT BELOW]
ENV = dev
exec('bin/console debug:container test.client -e test', $output, $return);
var_dump(implode("\n", $output)); // [OUTPUT BELOW]
Information for Service "test.client"
=====================================
---------------- ---------------------------------------
Option Value
---------------- ---------------------------------------
Service ID test.client
Class Symfony\Bundle\FrameworkBundle\Client
Tags -
Public yes
Synthetic no
Lazy no
Shared no
Abstract no
Autowired no
Autoconfigured no
exec("runuser arderyp -c 'bin/simple-phpunit --debug'", $output, $return);
var_dump(implode("\n", $output)); // [OUTPUT BELOW]
# Everything fails in the same fashion as below
8) App\Tests\Authentication\Security\SomeTest::testSomething
Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: You have requested a non-existent service "test.client".
在运行测试之前,我使用 symfony 标准方法正确解析了我的 .env:
$dotenv = new Dotenv();
$dotenv->load('.env');