refactor: add AuthenticatedUserTrait

This commit is contained in:
Maël Gangloff 2025-10-14 18:48:40 +02:00
parent d77cbb30f2
commit ea61c0bf12
No known key found for this signature in database
GPG Key ID: 11FDC81C24A7F629
6 changed files with 48 additions and 45 deletions

View File

@ -1,37 +0,0 @@
<?php
namespace App\Tests;
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use ApiPlatform\Symfony\Bundle\Test\Client;
use App\Entity\User;
class AbstractTest extends ApiTestCase
{
private ?string $token = null;
public function setUp(): void
{
self::bootKernel();
}
protected function createClientWithCredentials($token = null): Client
{
return static::createClient([], ['headers' => ['authorization' => 'Bearer '.$token]]);
}
protected function getToken(User $testUser): string
{
if ($this->token) {
return $this->token;
}
$response = static::createClient()->request('POST', '/api/login', ['json' => ['email' => $testUser->getEmail(), 'password' => $testUser->getPlainPassword()]]);
$this->assertResponseIsSuccessful();
$data = $response->toArray();
$this->token = $data['token'];
return $data['token'];
}
}

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

View File

@ -2,16 +2,18 @@
namespace App\Tests\Controller;
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use App\Entity\Connector;
use App\Factory\UserFactory;
use App\Tests\AbstractTest;
use App\Tests\AuthenticatedUserTrait;
use Zenstruck\Foundry\Test\Factories;
use Zenstruck\Foundry\Test\ResetDatabase;
final class ConnectorControllerTest extends AbstractTest
final class ConnectorControllerTest extends ApiTestCase
{
use ResetDatabase;
use Factories;
use AuthenticatedUserTrait;
public function testGetConnectorCollection(): void
{

View File

@ -2,16 +2,18 @@
namespace App\Tests\Controller;
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use App\Entity\Domain;
use App\Factory\UserFactory;
use App\Tests\AbstractTest;
use App\Tests\AuthenticatedUserTrait;
use App\Tests\Service\RDAPServiceTest;
use PHPUnit\Framework\Attributes\DependsExternal;
use Zenstruck\Foundry\Test\Factories;
final class DomainRefreshControllerTest extends AbstractTest
final class DomainRefreshControllerTest extends ApiTestCase
{
use Factories;
use AuthenticatedUserTrait;
#[DependsExternal(RDAPServiceTest::class, 'testUpdateRdapServers')]
public function testRegisterDomain(): void

View File

@ -2,17 +2,19 @@
namespace App\Tests\Controller;
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use App\Entity\User;
use App\Factory\UserFactory;
use App\Tests\AbstractTest;
use App\Tests\AuthenticatedUserTrait;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Zenstruck\Foundry\Test\Factories;
use Zenstruck\Foundry\Test\ResetDatabase;
final class MeControllerTest extends AbstractTest
final class MeControllerTest extends ApiTestCase
{
use ResetDatabase;
use Factories;
use AuthenticatedUserTrait;
/**
* @throws TransportExceptionInterface

View File

@ -2,17 +2,19 @@
namespace App\Tests\Controller;
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use ApiPlatform\Symfony\Bundle\Test\Client;
use App\Entity\WatchList;
use App\Factory\UserFactory;
use App\Tests\AbstractTest;
use App\Tests\AuthenticatedUserTrait;
use App\Tests\Service\RDAPServiceTest;
use PHPUnit\Framework\Attributes\DependsExternal;
use Zenstruck\Foundry\Test\Factories;
final class WatchlistControllerTest extends AbstractTest
final class WatchlistControllerTest extends ApiTestCase
{
use Factories;
use AuthenticatedUserTrait;
public function testGetWatchlistCollection(): void
{