代码之家  ›  专栏  ›  技术社区  ›  crmpicco

Symfony 3.4升级打破了围绕预验证令牌SSO测试的PHPUnit测试

  •  0
  • crmpicco  · 技术社区  · 7 年前

    我正在将我的Symfony 2.8应用程序升级到3.4,我遇到了一些现有PHPUnit测试的问题。

    这是我的 security.yml :

    security:
       firewalls:
            default:
                simple_preauth:
                    provider: fos_userbundle
                    authenticator: appbundle.admin.tokenauthenticator
    

    我最近升级了 friendsofsymfony/user-bundle v2.1.2 作为Symfony 3.4升级的一部分。

    config_test.yml (对于PHPUnit):

    security:
        providers:
            appbundle.security.api.key_user_provider:
                apiusers:
                    users:
                        server:
                            apikey: testUserValidRole
                            roles:
                                - 'ROLE_API_USER'
    

    我还将PHP升级到7.2,将PHPUnit升级到7.4.3。

    在我的测试中,我有:

    $crawler = $client->request('POST', '/api/sso', [
        'apikey' => 'testUserValidRole',
        'site' => $site->getId(),
    ]);
    
    $this->assertEquals(
         \Symfony\Component\HttpFoundation\Response::HTTP_OK,
         $client->getResponse()->getStatusCode()
    );
    
    $response = json_decode($client->getResponse()->getContent());
    $this->assertTrue(isset($response->target));
    
    $security = $client->getProfile()->getCollector('security');
    
    // The user should only be authenticated anonymously.
    $this->assertTrue($security->isAuthenticated());
    $this->assertEquals(
         'Symfony\Component\Security\Core\Authentication\Token\AnonymousToken',
         $security->getTokenClass()
    );
    
    // Check that we can login with a valid loginToken.
    // Note: A successful login will redirect and remove the loginToken.
    $client->enableProfiler();
    $crawler = $client->request('GET', $response->target);
    
    // The user should be authenticated correctly.
    $security = $client->getProfile()->getCollector('security');
    $this->assertTrue($security->isAuthenticated());
    $this->assertEquals(
        'Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken',
        $security->getTokenClass()
    );
    

    关于 PreAuthenticatedToken

    断言Symfony\Component\VarDumper\Cloner\Data对象失败 &00000000 299A8BD300600690A732B预期匹配 'Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken'。

    https://gist.github.com/crmpicco/a927716570a4949caafec4ca1361bf63

    在Symfony升级说明中,我看不到任何关于安全性部分的内容可以指出导致此错误的原因,我必须承认,我现在对此感到有点困惑。我有一些错误的配置吗?

    1 回复  |  直到 7 年前
        1
  •  0
  •   DrKey    7 年前

    我不知道确切的原因,但似乎 SecurityDataCollector::getTokenClass() 仅当 Symfony\Component\VarDumper\Caster\ClassStub 类不存在(即。 VarDumper 未加载/安装),如您所见 here .

    所以你应该打电话 $security->getTokenClass()->getValue() getTokenClass() 已返回一个字符串:

    $this->assertEquals(
        'Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken',
        (string) $security->getTokenClass()
    );