foundry icon indicating copy to clipboard operation
foundry copied to clipboard

[HELP] Working with Factory before ->request(

Open abozhinov opened this issue 2 years ago • 4 comments

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');

abozhinov avatar Apr 20 '22 10:04 abozhinov

What type of event listener?

kbond avatar Apr 20 '22 11:04 kbond

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], ], ]; }

abozhinov avatar Apr 20 '22 11:04 abozhinov

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.

abozhinov avatar Apr 20 '22 11:04 abozhinov

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?

kbond avatar Apr 20 '22 11:04 kbond