[英]Symfony 3.4 Functional tests
我在使用 symfony 3.4 进行功能测试时遇到错误已经有一段时间了。
我的应用程序运行自定义 GuardAuthenticator 以在 CAS 身份验证中对我的用户进行身份验证。 这只是为了解释上下文。 在我的测试中,我不想使用它,我想使用特定的身份验证系统。
我写了功能测试。 我使用以下内容启动了我的测试环境:
# app/config/config_test.yml
imports:
- { resource: config_dev.yml }
framework:
test: ~
session:
storage_id: session.storage.mock_file
name: MOCKSESSION
profiler:
collect: false
根据官方 Symfony 文档,我扩展了 symfony WebTestCase 为我的测试设置特定的身份验证令牌How to Simulate HTTP Authentication in a Functional Test
namespace Test\AppBunble\WebTestCase;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as SfTestCase;
class WebTestCase extends SfTestCase
{
/**
*
* {@inheritDoc}
* @see \PHPUnit_Framework_TestCase::setUp()
*/
public function setUp()
{
$this->client = static::createClient();
$this->container = $this->client->getContainer();
$this->entityManager = $this->container->get('doctrine.orm.entity_manager');
parent::setUp();
}
public function logInAs($username)
{
$user = $this->entityManager->getRepository(User::class)->loadUserByUsername($username);
$session = $this->client->getContainer()->get('session');
$token = new PostAuthenticationGuardToken($user, 'main', $user->getRoles());
$session->set('_security_main', serialize($token));
$session->save();
$cookie = new Cookie($session->getName(), $session->getId());
$this->client->getCookieJar()->set($cookie);
}
}
最后,我写了我的(简单)测试:
namespace Tests\AppBundle\Controller;
use Tests\AppBundle\WebTestCase;
class DefaultControllerTest extends WebTestCase
{
public function testIndex()
{
$this->logInAs('admin');
$crawler = $this->client->request('GET', '/');
$this->assertTrue($client->getResponse()->isSuccessful(), 'response status is 2xx');
}
}
我跑了 phpunit :
$ ./vendor/bin/simple-phpunit
PHPUnit 5.7.27 by Sebastian Bergmann and contributors.
Testing app Test Suite
<br />
<b>Error</b>: <font color="FF0000"><b>Internal script failure</b><br />
为了调试,我修改了我的测试:
public function testIndex()
{
$this->logInAs('admin');
//$crawler = $this->client->request('GET', '/');
//$this->assertTrue($client->getResponse()->isSuccessful(), 'response status is 2xx');
}
我跑了 phpunit :
./vendor/bin/simple-phpunit
PHPUnit 5.7.27 by Sebastian Bergmann and contributors.
Testing app Test Suite
. 1 / 1 (100%)
Time: 797 ms, Memory: 27.75MB
OK (1 test, 0 assertions)
经过大量测试后,我在调用测试时发现:
$this->client->request('GET', '/')
它崩溃了。
更新:另一个测试。
如果我运行这个:
public function testIndex()
{
$this->logInAs('admin');
$token = $this->client->getContainer()->get('security.token_storage');
var_dump($token->getToken());
//$crawler = $this->client->request('GET', '/');
//$this->assertTrue($client->getResponse()->isSuccessful(), 'response status is 2xx');
}
它返回给我NULL
... 没有令牌。
更新 2
我深入代码以回到 symfony 停止的地方。 它在Symfony\\Component\\EventDispatcher\\EventDispatcher
类的doDispatch
方法中。
它触发了 `kernel.event' 事件的监听器。 当它触发名为“Symfony\\Bundle\\SecurityBundle\\Debug\\TraceableFirewallListener”=>“内部脚本失败”的侦听器时。
更新 3如果我比较 dev.log(它代表我在浏览器中运行应用程序时的日志),我会在请求中得到这个日志:
[2020-10-23 13:55:56] request.INFO: Matched route "app_homepage". {"route":"app_homepage","route_parameters":{"_controller":"AppBundle\\Controller\\DefaultController::indexAction","_route":"app_homepage"},"request_uri":"http://localhost:8180/","method":"GET"} []
[2020-10-23 13:55:56] security.DEBUG: Read existing security token from the session. {"key":"_security_main","token_class":"Symfony\\Component\\Security\\Guard\\Token\\PostAuthenticationGuardToken"} []
[2020-10-23 13:55:56] doctrine.DEBUG: SELECT [...]
[2020-10-23 13:55:56] security.DEBUG: User was reloaded from a user provider. [...]
但是当我运行$this->client->request('GET', '/')
,我的 test.log 显示:
[2020-10-23 13:55:56] request.INFO: Matched route "app_homepage". {"route":"app_homepage","route_parameters":{"_controller":"AppBundle\\Controller\\DefaultController::indexAction","_route":"app_homepage"},"request_uri":"http://localhost","method":"GET"} []
安全无踪。
更新 4:另一个测试
我运行了这个测试:
public function testIndex()
{
$crawler = $this->client->request('GET', '/stackTest');
$this->assertTrue($this->client->getResponse()->isSuccessful(), 'response status is 2xx');
}
这是 security.yml 中具有此配置的页面
firewalls:
stack:
pattern: ^/stackTest
security: false
......测试有效!
./vendor/bin/simple-phpunit
PHPUnit 5.7.27 by Sebastian Bergmann and contributors.
Testing app Test Suite
. 1 / 1 (100%)
Time: 1.08 seconds, Memory: 36.50MB
OK (1 test, 1 assertion)
我发现 !
如上所述,我的应用程序的身份验证是通过 CAS 完成的。 为了测试,我想模拟这种身份验证。 所以我创建了一个 FakeGuardAuthenticator,它不是询问 CAS 服务器,而是模拟它并返回凭据。
因此,在我的 config_test.yml 配置文件中,我必须通过传递别名来覆盖 guardAuthenticator。
其中给出:
# app/config/config_test.yml
imports:
- { resource: config_dev.yml }
- { resource: services_test.yml }
# app/config/service_test.yml
services:
_defaults:
autowire: true
autoconfigure: true
public: false
app_cas.guard_authenticator:
class : Tests\AppBundle\FakeGuardAuthenticator
arguments:
# [...]
provider: cas
感谢@vstelmakh 和@Vyctorya 抽出时间回复我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.