简体   繁体   English

Symfony 3.4 功能测试

[英]Symfony 3.4 Functional tests

I have been stuck for some time on an error to do my functional tests with symfony 3.4.我在使用 symfony 3.4 进行功能测试时遇到错误已经有一段时间了。

My app run a custom GuardAuthenticator for authenticate my users within CAS authentication.我的应用程序运行自定义 GuardAuthenticator 以在 CAS 身份验证中对我的用户进行身份验证。 This is just for explain the context.这只是为了解释上下文。 In my tests, I don't want to use it, I want to use a specific authentication system.在我的测试中,我不想使用它,我想使用特定的身份验证系统。

I wrote functional tests.我写了功能测试。 I started my test environment with the following :我使用以下内容启动了我的测试环境:

# app/config/config_test.yml
imports:
    - { resource: config_dev.yml }

framework:
    test: ~
    session:
        storage_id: session.storage.mock_file
        name: MOCKSESSION
    profiler:
        collect: false

According to the official Symfony doc, I extends symfony WebTestCase for set a specific authentication token for my tests How to Simulate HTTP Authentication in a Functional Test根据官方 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);
    }
}

Finally, I wrote my (simple) test :最后,我写了我的(简单)测试:

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');
    }
}

I ran phpunit :我跑了 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 />

For debugging, I modified my test :为了调试,我修改了我的测试:

    public function testIndex()
    {
        $this->logInAs('admin');
        //$crawler = $this->client->request('GET', '/');
        //$this->assertTrue($client->getResponse()->isSuccessful(), 'response status is 2xx');
    }

I ran phpunit :我跑了 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)

After lots of tests I found when I call in my test :经过大量测试后,我在调用测试时发现:

$this->client->request('GET', '/')

It crash.它崩溃了。

UPDATE : another test.更新:另一个测试。

If I ran this :如果我运行这个:

    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');
    }

It return me NULL ... no token.它返回给我NULL ... 没有令牌。

UPDATE 2更新 2

I dove into the code to go back to the point where symfony stops.我深入代码以回到 symfony 停止的地方。 It's in the doDispatch method of Symfony\\Component\\EventDispatcher\\EventDispatcher class.它在Symfony\\Component\\EventDispatcher\\EventDispatcher类的doDispatch方法中。

It trigger the listeners on the `kernel.event' event.它触发了 `kernel.event' 事件的监听器。 And when it trigger the listener named "Symfony\\Bundle\\SecurityBundle\\Debug\\TraceableFirewallListener" => "internal script failure".当它触发名为“Symfony\\Bundle\\SecurityBundle\\Debug\\TraceableFirewallListener”=>“内部脚本失败”的侦听器时。

UPDATE 3 If I compare dev.log (which represents log when I run app in browser), I have this log on a request :更新 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.  [...]

But when I run $this->client->request('GET', '/') , my test.log show :但是当我运行$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"} []

No trace from security.安全无踪。

UPDATE 4 : another test更新 4:另一个测试

I ran this test :我运行了这个测试:

    public function testIndex()
    {
        $crawler = $this->client->request('GET', '/stackTest');
        $this->assertTrue($this->client->getResponse()->isSuccessful(), 'response status is 2xx');
    }

It's a page with this configuration in security.yml这是 security.yml 中具有此配置的页面

firewalls:
    stack:
        pattern: ^/stackTest
        security: false

... and the test works! ......测试有效!

 ./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)

I found !我发现 !

As said above, the authentication of my app is done via CAS.如上所述,我的应用程序的身份验证是通过 CAS 完成的。 For testing, I want to simulate this authentication.为了测试,我想模拟这种身份验证。 So I created a FakeGuardAuthenticator which instead of interrogating the CAS server, simulates it and returns the credentials.所以我创建了一个 FakeGuardAuthenticator,它不是询问 CAS 服务器,而是模拟它并返回凭据。

In my config_test.yml configuration file, I therefore have to override the guardAuthenticator by passing through an alias.因此,在我的 config_test.yml 配置文件中,我必须通过传递别名来覆盖 guardAuthenticator。

Which give :其中给出:

# 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

Thansk to @vstelmakh and @Vyctorya for taking time to respond to me.感谢@vstelmakh 和@Vyctorya 抽出时间回复我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM