mirror of
https://github.com/maelgangloff/domain-watchdog.git
synced 2025-12-18 10:15:41 +00:00
refactor: add AuthenticatedUserTrait
This commit is contained in:
parent
d77cbb30f2
commit
ea61c0bf12
@ -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'];
|
||||
}
|
||||
}
|
||||
32
tests/AuthenticatedUserTrait.php
Normal file
32
tests/AuthenticatedUserTrait.php
Normal 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'];
|
||||
}
|
||||
}
|
||||
@ -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
|
||||
{
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
{
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user