首页 > 解决方案 > 以编程方式对用户进行身份验证以进行 PhpUnit 功能测试 - 不受 Doctrine 管理 - Symfony 4.3

问题描述

我试图让一个简单的“200 响应”测试适用于需要经过身份验证的用户的网站的一部分。我想我已经创建了 Session 工作,因为在调试期间调用了 Controller 函数并检索了 User (使用$this->getUser())。

但是,之后该函数失败并显示以下消息:

1) App\Tests\Controller\SecretControllerTest::testIndex200Response
expected other status code for 'http://localhost/secret_url/':
error:
    Multiple non-persisted new entities were found through the given association graph:

 * A new entity was found through the relationship 'App\Entity\User#role' that was not configured to cascade persist operations for entity: ROLE_FOR_USER. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade
persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}).
 * A new entity was found through the relationship 'App\Entity\User#secret_property' that was not configured to cascade persist operations for entity: test123. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade pe
rsist this association in the mapping for example @ManyToOne(..,cascade={"persist"}). (500 Internal Server Error)

Failed asserting that 500 matches expected 200.

如果这还没有存储在(MySQL)数据库中并使用 Doctrine 检索,这将是有意义的。记录是在每次运行/每次测试时使用夹具创建的。这就是为什么在控制器$this->getUser()中按预期运行的原因。

我想要工作的测试:

public function testIndex200Response(): void
{
    $client = $this->getAuthenticatedSecretUserClient();

    $this->checkPageLoadResponse($client, 'http://localhost/secret_url/');
}

获取用户:

protected function getAuthenticatedSecretUserClient(): HttpKernelBrowser
{
    $this->loadFixtures(
        [
            RoleFixture::class,
            SecretUserFixture::class,
        ]
    );

    /** @var User $user */
    $user = $this->entityManager->getRepository(User::class)->findOneBy(['username' => 'secret_user']);

    $client = self::createClient(
        [],
        [
            'PHP_AUTH_USER' => $user->getUsername(),
            'PHP_AUTH_PW'   => $user->getPlainPassword(),
        ]
    );

    $this->createClientSession($user, $client);

    return $client;
}

创建会话:

// Based on https://symfony.com/doc/current/testing/http_authentication.html#using-a-faster-authentication-mechanism-only-for-tests
protected function createClientSession(User $user, HttpKernelBrowser $client): void
{
    $authenticatedGuardToken = new PostAuthenticationGuardToken($user, 'chain_provider', $user->getRoles());
    $tokenStorage            = new TokenStorage();
    $tokenStorage->setToken($authenticatedGuardToken);

    $session = self::$container->get('session');
    $session->set('_security_<security_context>', serialize($authenticatedGuardToken));
    $session->save();

    $cookie = new Cookie($session->getName(), $session->getId());
    $client->getCookieJar()->set($cookie);

    self::$container->set('security.token_storage', $tokenStorage);
}

这适用于创建客户端、会话和 cookie。

当请求执行到$url第一个函数时,它会进入端点,确认用户确实已通过身份验证。

根据此处的文档,应该通过配置的提供程序(在这种情况下使用 Doctrine)“刷新”用户,以检查给定对象是否与存储的对象匹配。

[..] 在下一个请求开始时,它被反序列化,然后传递给您的用户提供程序以“刷新”它(例如,新用户的 Doctrine 查询)。

我希望这也将确保会话用户被一个 Doctrine 管理的用户对象替换,以防止上述错误。

如何解决会话中的用户在 PhpUnit 测试期间成为托管用户的问题?

(注意:生产代码没有任何问题,这个问题只在测试期间出现(遗留代码现在开始测试))

标签: symfonydoctrine-ormphpunitsymfony4

解决方案


好的,有多个问题,但它正在执行以下操作:

首先,使用不正确的密码创建客户端,我正在创建(在 Fixtures 中)用户实体,username并且password是相同的。function getPlainPassword,虽然存在于界面中,但不是存储的东西,所以是一个空白值。

更正的代码:

$client = self::createClient(
    [],
    [
        'PHP_AUTH_USER' => $user->getUsername(),
        'PHP_AUTH_PW'   => $user->getUsername(),
    ]
);

接下来,未刷新的用户需要更多时间。

config/packages/security.yaml中,添加以下内容:

security:
  firewalls:
    test:
      security: ~

这是为了创建“测试”键,因为立即在下一个文件中创建它会导致权限被拒绝错误。在config/packages/test/security.yaml中,创建以下内容:

security:
  providers:
    test_user_provider:
      id: App\Tests\Functional\Security\UserProvider
  firewalls:
    test:
      http_basic:
        provider: test_user_provider

这添加了一个UserProvider专门用于测试目的的自定义(因此使用App\Tests\命名空间)。您必须在您的config/services_test.yaml

services:
    App\Tests\Functional\Security\:
        resource: '../tests/Functional/Security'

不确定您是否需要它,但我添加config/packages/test/routing.yaml了以下内容:

parameters:
    protocol: http

由于 PhpUnit 正在通过 CLI 进行测试,默认情况下没有安全连接,可能因环境而异,因此请查看是否需要它。

最后,配置测试框架config/packages/test/framework.yaml

framework:
    test: true
    session:
        storage_id: session.storage.mock_file

上述所有配置(除了http位)都是为了确保在测试期间使用自定义UserProvider来提供User对象。

这对其他人来说可能太过分了,但我们的设置(旧版)有一些自定义工作来为用户提供身份验证(这似乎非常相关,但远远超出了我当前的问题范围)。

回到 UserProvider,它的设置如下:

namespace App\Tests\Functional\Security;

use App\Entity\User;
use App\Repository\UserRepository;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;

class UserProvider implements UserProviderInterface
{
    /** @var UserRepository */
    private $userRepository;

    public function __construct(UserRepository $userRepository)
    {
        $this->userRepository = $userRepository;
    }

    public function loadUserByUsername($username)
    {
        try {
            return $this->userRepository->getByUsername($username);
        } catch (UserNotFoundException $e) {
            throw new UsernameNotFoundException("Username: $username unknown");
        }
    }

    public function refreshUser(UserInterface $user)
    {
        return $this->loadUserByUsername($user->getUsername());
    }

    public function supportsClass($class)
    {
        return User::class === $class;
    }
}

注意:如果你使用它,你需要getByUsername在你的 UserRepository 中有一个函数。


请注意,这可能不是您的解决方案。也许你需要改变它,也许它完全关闭了。无论哪种方式,都想为任何未来的灵魂留下解决方案。


推荐阅读