有没有办法在功能测试中使用 Foundry 模型进行身份验证?
Is there a way to use Foundry Model for Authentification inside Functional Tests?
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(扩展 Foundry ModelFactory)进行测试,然后使用带有内置 Symfony 方法的给定对象验证我的 API 请求
$this->client->loginUser($admin, 'admin');
我怎样才能让它发挥作用?
好的,我自己找到了解决办法。你必须这样做:
AdminFactory::new()
->create([
'email' => 'admin1@admin.com',
'active' => true,
'roles' => ['ROLE_SUPER_ADMIN'],
])
;
/** @var AdminRepository $adminRepository */
$adminRepository = static::getContainer()->get(AdminRepository::class);
$admin = $adminRepository->findOneBy(['email' => 'admin1@admin.com']);
这应该是 return 实体而不是 Foundry 代理
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(扩展 Foundry ModelFactory)进行测试,然后使用带有内置 Symfony 方法的给定对象验证我的 API 请求
$this->client->loginUser($admin, 'admin');
我怎样才能让它发挥作用?
好的,我自己找到了解决办法。你必须这样做:
AdminFactory::new()
->create([
'email' => 'admin1@admin.com',
'active' => true,
'roles' => ['ROLE_SUPER_ADMIN'],
])
;
/** @var AdminRepository $adminRepository */
$adminRepository = static::getContainer()->get(AdminRepository::class);
$admin = $adminRepository->findOneBy(['email' => 'admin1@admin.com']);
这应该是 return 实体而不是 Foundry 代理