'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.

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?



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kuba5822