![](/img/trans.png)
[英]Keep getting 'You have requested a non-existent service “test.client”' in Symfony2 Beta3
[英]symfony simple-phpunit error 'You have requested a non-existent service “test.client”' only in deployment script
的PHP: 7.2.7
SYMFONY: 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环境未设置为“测试”有关。 我的phpunit.xml使用正确的值:
<env name="APP_ENV" value="test" />
疯狂的是,当我自己(以MY-USER)运行时,单元测试运行得很好。 当我以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');
好吧,好像我也被PHPUnit的这种变化所困扰: https : //github.com/sebastianbergmann/phpunit/issues/2353
将force="true"
属性添加到我所有的env
元素都解决了该问题。
这里的问题是,在进入此phpunit步骤之前,我正在加载(通过Dotenv
)我的本地.env
文件。 没有上面提到的force
属性, phpunit.xml
中的环境变量不会覆盖.env
已经设置的环境变量。 因此,将使用APP_ENV=dev
而不是APP_ENV=test
来运行APP_ENV=test
,这将导致无法识别test.client
服务,因为该服务只能在test
环境中访问。 显然,这是新的phpunit
功能。 早期版本的phpunit
默认情况下将允许覆盖变量,这解释了为什么我仅在升级PHP(以及phpunit)之后才开始遇到此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.