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?