5

I'm building a test for a Symfony 5.4 application.

I have created a test like this:

public function testCreateProduct() {
    $client = static::createClient();
    /** @var User $mainAdmin */
    $mainAdmin = static::getContainer()->get(UserRepository::class)->find(1);

    //$client->catchExceptions(false);
    $client->loginUser($mainAdmin);

    $crawler = $client->request('GET', '/en/product/new');

    $this->assertResponseIsSuccessful();
    $this->assertSelectorTextContains('span.username', $mainAdmin->getUsername());

}

But the login is not working, I get a 302 redirect to the login page, and if I set catchExceptions(false) I get an AccessDeniedException.

How can I debug this?


Edit: I tried to change the patch to a public route, then I did a dd($this->getUser(), $request)

getUser() is null, but the request session contains the user:

#session: Symfony\Component\HttpFoundation\Session\Session {#16616
#storage: Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage {#16631
  -savePath: "C:\www\project\var\cache\test/sessions"
  #id: "a9d00704e1a0211d06ebddadfaabbf0188e9d65d94faac05afbdc63bb9fb7caa"
  #name: "MOCKSESSID"
  #started: true
  #closed: false
  #data: array:3 [
    "_sf2_attributes" => &1 array:1 [
      "_security_main" => "O:52:"Symfony\Bundle\FrameworkBundle\Test\TestBrowserToken":2:{i:0;s:4:"main";i:1;a:5:{i:0;C:15:"App\Entity\User":118:{a:5:{i:0;i:2;i:1;s:5:"admin";i:2;s:60:"$2y$13$abcdefghi......";i:3;i:1;i:4;b:0;}}i:1;b:1;i:2;N;i:3;a:0:{}i:4;a:9:{i:0;s:9:"ROLE_USER";i:1;s:10:"ROLE_ADMIN";i:2;[...]}}}"
    ]
    "_sf2_meta" => &2 array:3 [
      "u" => 1645718565
      "c" => 1645718565
      "l" => 0
    ]
    "_symfony_flashes" => &3 []
  ]

I see a difference between $request->getSession()->getBag('attributes') done in dev (working) vs in test: in dev I have _security_secured_area, while in test I have _security_main. May this be the reason?

2 Answers 2

4

Solved.

Since I was using a custom firewall name in security.yaml, in my case secured_area, I had to pass that as the second parameter of loginUser():

$client->loginUser($mainAdmin, 'secured_area');
1
  • 2
    Thanks; this saved me some time! The documentation (symfony.com/doc/6.0/…) doesn't mention this. Still, I feel stupid that I didn't notice this myself by simply checking the methods parameters. Commented Apr 2, 2022 at 12:38
0

Yes, using secured_area like this :

$client->loginUser($mainAdmin, 'secured_area');//call context

will rely the config/package/security.config like this :

firewalls:
    main:
        name: secured_area

But also, that will call (i am under symfony 7) a new database called like yours (called "db"), add "_test" : "db_test.

And also, that will ignore the .env.local, so rename it .env.test, if needed.

That don't works if i put the context name (and login way to connection, who also can be a custom_identificator with an entryPoint) in the dev section.

If i do all that, that works for me. If you know how to simplify all that, an answer will be welcome.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.