有没有办法在功能测试中使用铸造模型进行身份验证?

发布于 2025-01-28 11:58:00 字数 524 浏览 3 评论 0原文

1) App\Tests\Controller\Admin\Api\Promotion\PromotionDeleteControllerTest::test_deleting_promotion
LogicException: The first argument of "Symfony\Bundle\FrameworkBundle\KernelBrowser::loginUser" must be instance of "Symfony\Component\Security\Core\User\UserInterface", "Zenstruck\Foundry\Proxy" provided.

我正在使用phpunit库编写功能测试。我想创建用于使用AdminFactory测试的管理对象(扩展铸造Modelfactory),然后使用带有内置的Symfony方法使用给定对象对我的API请求进行身份验证

$this->client->loginUser($admin, 'admin');

1) App\Tests\Controller\Admin\Api\Promotion\PromotionDeleteControllerTest::test_deleting_promotion
LogicException: The first argument of "Symfony\Bundle\FrameworkBundle\KernelBrowser::loginUser" must be instance of "Symfony\Component\Security\Core\User\UserInterface", "Zenstruck\Foundry\Proxy" provided.

I am writing functional test with phpunit library. I want to create admin object for testing with AdminFactory (extends Foundry ModelFactory) and then authenticate my API request using given object with built in Symfony method

$this->client->loginUser($admin, 'admin');

How can I make it work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萝莉病 2025-02-04 11:58:00

好吧,我自己找到了一个解决方案。您必须做这样的事情:

AdminFactory::new()
    ->create([
        'email' => '[email protected]',
        'active' => true,
        'roles' => ['ROLE_SUPER_ADMIN'],
    ])
;

/** @var AdminRepository $adminRepository */
$adminRepository = static::getContainer()->get(AdminRepository::class);
$admin = $adminRepository->findOneBy(['email' => '[email protected]']);

这应该返回实体而不是铸造代理

Okay, I've found a solution myself. You have to do something like this:

AdminFactory::new()
    ->create([
        'email' => '[email protected]',
        'active' => true,
        'roles' => ['ROLE_SUPER_ADMIN'],
    ])
;

/** @var AdminRepository $adminRepository */
$adminRepository = static::getContainer()->get(AdminRepository::class);
$admin = $adminRepository->findOneBy(['email' => '[email protected]']);

This should return Entity instead of Foundry Proxy

GRAY°灰色天空 2025-02-04 11:58:00

只需要求实际对象:

$admin = AdminFactory::AdminFactory::new()
    ->create([
        /* $attributes */
    ])
    ->object(); // retrieves the actual object

请参阅 zenstruckfundryrybundrybundrybundrybundly文档

Just ask for the actual object:

$admin = AdminFactory::AdminFactory::new()
    ->create([
        /* $attributes */
    ])
    ->object(); // retrieves the actual object

See ZenstruckFoundryBundle Documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文