refactor: add AuthenticatedUserTrait

This commit is contained in:
Maël Gangloff
2025-10-14 18:48:40 +02:00
parent d77cbb30f2
commit ea61c0bf12
6 changed files with 48 additions and 45 deletions

View File

@@ -0,0 +1,32 @@
<?php
namespace App\Tests;
use ApiPlatform\Symfony\Bundle\Test\Client;
use App\Entity\User;
trait AuthenticatedUserTrait
{
public static function createClientWithCredentials(string $token): Client
{
return static::createClient([], [
'headers' => [
'authorization' => 'Bearer '.$token,
],
]);
}
public static function getToken(User $testUser): string
{
$response = static::createClient()->request('POST', '/api/login', [
'json' => [
'email' => $testUser->getEmail(),
'password' => $testUser->getPlainPassword(),
],
]);
$data = $response->toArray();
return $data['token'];
}
}