【问题标题】:Symfony 3.4 upgrade has broken PHPUnit test around PreAuthenticationToken SSO testingSymfony 3.4 升级打破了围绕 PreAuthenticationToken SSO 测试的 PHPUnit 测试
【发布时间】:2019-04-05 08:32:46
【问题描述】:

我正在将我的 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 上的最后一个断言失败,PHPUnit 吐出错误:

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

完整输出在这里:https://gist.github.com/crmpicco/a927716570a4949caafec4ca1361bf63

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

【问题讨论】:

  • 您是否在您的 app/logs/test.log 中看到任何其他错误或弃用?

标签: php unit-testing symfony symfony-3.4 php-7.2


【解决方案1】:

我不知道确切的原因,但似乎SecurityDataCollector::getTokenClass() 仅在Symfony\Component\VarDumper\Caster\ClassStub 类不存在时才返回一个字符串(即VarDumper 未加载/安装),如您所见@987654321 @。

因此,您应该调用$security->getTokenClass()->getValue() 来获取字符串值,或者更好地将其转换为字符串以避免getTokenClass() 已经返回字符串时出错:

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

【讨论】:

  • 谢谢,但是这样做只会将getTokenClass 输出转换为AnonymousToken,而它应该是PreAuthenticatedToken
  • 嗯,这个我还真想不通,跟测试本身和背后的系统有关……
猜你喜欢
  • 2017-09-16
  • 1970-01-01
  • 2020-12-04
  • 2019-01-14
  • 2020-11-03
  • 2021-02-06
  • 2014-03-31
  • 1970-01-01
  • 2015-04-26
相关资源
最近更新 更多