我是Symfony的新手,我尝试进行一些验收测试。直到现在都很好,但是当我运行测试时,我得到如下响应:
当我使用PostMan运行我的API控制器时,我没有得到任何相关信息。只有在我的命令行中运行测试时才会输出此输出。
根据输出消息:
The "UsersBundle\Controller\UsersController" service is private, getting it from the contains is deprecated since Symfony 3.2 and will fail in 4.0. You should either make the service public, or stop using the container directly and use dependency injection instead.
除此之外,我不直接在我的测试方法中使用UsersController
:
public function test_cannot_send_multiple_possword_reset_requests() {
$client = static::createClient( $this->defaults );
$client->request(
'POST',
'/api/v1/user-account/request/reset',
[
'username' => 'merianos',
],
[],
[
'Content-Type' => 'application/json',
]
);
$this->assertEquals( 400, $client->getResponse()->getStatusCode() );
$this->assertContains(
'An email containing your password reset token has been send.',
$client->getResponse()->getContent()
);
}
我想知道是否可以在运行时仅为我的单位/验收测试覆盖以下设置:
# /src/UsersBundle/Resources/config/services.yml
services:
_defaults:
public: false
当然,如果还有其他方法可以达到同样的效果。
请注意,我使用的是Symfony 3.4。
答案 0 :(得分:1)
您应该可以在config_test.yml
中覆盖它。您可以在此处详细了解有关环境的信息:https://symfony.com/doc/current/configuration/environments.html
答案 1 :(得分:1)
最后我以完全不同的方式破解它:)
这是我的解决方案:
<!-- ./phpunit.xml.dist -->
<phpunit other="settings-here">
<php>
<!-- some other settings here -->
<env name="environment" value="test" />
</php>
<!-- Rest of the settings -->
</phpunit>
然后我这样做了:
<?php
// ./src/UsersBundle/Resources/config/environment-setup.php
if (
isset( $_ENV['environment'] ) &&
in_array( $_ENV['environment'], [ 'test', 'acceptance' ] )
) {
$container->setParameter( 'public_services', true );
} else {
$container->setParameter( 'public_services', false );
}
最后我这样做了:
# ./src/UsersBundle/Resources/config/serviecs.yml
imports:
- { resource: environment-setup.php }
services:
_defaults:
public: '%public_services%'
# Rest of the setup.