foundry
foundry copied to clipboard
[HELP] Working with Factory before ->request(
Hi, I use EventListener in Symfony to set Database credentials. I have a problem using Foundry in my WebCaseTest. When I use AdminFactory before ->request() my EventListeners are not loaded and have errors ... because of missing database credentials. When I move the AdminFactory below ->request() my EventListeners are triggered and everything works. Can you give me advice on how to fix this problem?
$client = self::createClient(); $admin = AdminFactory::repository()->findOneBy(['active' => true]); $client->request('GET', '/cms/admin/index'); $client->loginUser($admin->object(), 'cms');
What type of event listener?
I have Request & Command Listeners. For that reason when first sending a request and then using AdminFactory everything works.
public static function getSubscribedEvents(): array { return [ KernelEvents::REQUEST => [ ['loadFromRequest', 4096], ['setTenantToTemplate', -10], ], ConsoleEvents::COMMAND => [ ['loadFromCommand', 4096], ], ]; }
I'm not sure exactly what's happened. After sending the request and all Events are triggered the AdminFactory use the same instances of these classes and for this reason, everything works. How should execute my test to be sure that AdminFactory will work in the setUp function inside my WebCaseTest.
Basically, when you make the request with the client, the event listener is called, credentials set. Foundry has a special object manager that's lazy - it uses the most recent container (the same one as the request).
I don't really see a great solution. Maybe somehow have some default test credentials so the event listener isn't required?