test: add some KernelTestCase

This commit is contained in:
Maël Gangloff
2025-10-14 00:09:01 +02:00
parent 398ca8df2f
commit 6154759507
15 changed files with 884 additions and 3 deletions

37
tests/AbstractTest.php Normal file
View File

@@ -0,0 +1,37 @@
<?php
namespace App\Tests;
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use ApiPlatform\Symfony\Bundle\Test\Client;
use App\Entity\User;
abstract 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,24 @@
<?php
namespace App\Tests\Entity;
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use App\Entity\Instance;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Zenstruck\Foundry\Test\Factories;
final class InstanceTest extends ApiTestCase
{
use Factories;
/**
* @throws TransportExceptionInterface
*/
public function testInstance(): void
{
$client = InstanceTest::createClient();
$client->request('GET', '/api/config');
$this->assertResponseIsSuccessful();
$this->assertMatchesResourceItemJsonSchema(Instance::class);
}
}

View File

@@ -0,0 +1,76 @@
<?php
namespace App\Tests\Service;
use App\Entity\RdapServer;
use App\Exception\DomainNotFoundException;
use App\Exception\TldNotSupportedException;
use App\Exception\UnknownRdapServerException;
use App\Message\UpdateRdapServers;
use App\MessageHandler\UpdateRdapServersHandler;
use App\Service\RDAPService;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\Attributes\Depends;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpClient\Exception\ClientException;
use Symfony\Component\HttpClient\Exception\TransportException;
class RDAPServiceTest extends KernelTestCase
{
protected static ContainerInterface $container;
protected static EntityManagerInterface $entityManager;
protected static RDAPService $RDAPService;
protected function setUp(): void
{
static::$container = static::getContainer();
static::$entityManager = static::$container->get(EntityManagerInterface::class);
static::$RDAPService = static::$container->get(RDAPService::class);
}
public function testUpdateRdapServers(): void
{
$updateRdapServersHandler = self::$container->get(UpdateRdapServersHandler::class);
$message = new UpdateRdapServers();
$updateRdapServersHandler($message);
self::$entityManager->flush();
$rdapServerRepository = self::$entityManager->getRepository(RdapServer::class);
$this->assertNotEmpty($rdapServerRepository->findAll());
}
#[Depends('testUpdateRdapServers')]
public function testRegisterDomain(): void
{
$rdapServerRepository = self::$entityManager->getRepository(RdapServer::class);
$testedTldList = ['com', 'net', 'org', 'fr', 'de', 'ch', 'ca', 'leclerc', 'uz'];
/** @var RdapServer $rdapServer */
foreach ($rdapServerRepository->findAll() as $rdapServer) {
if (!in_array($rdapServer->getTld()->getTld(), $testedTldList)) {
continue;
}
try {
self::$RDAPService->registerDomain('nic.'.$rdapServer->getTld()->getTld());
} catch (DomainNotFoundException|ClientException|TransportException) {
}
}
$this->expectNotToPerformAssertions();
}
#[Depends('testUpdateRdapServers')]
public function testUnknownRdapServer(): void
{
$this->expectException(UnknownRdapServerException::class);
self::$RDAPService->registerDomain('nic.arpa');
}
#[Depends('testUpdateRdapServers')]
public function testUnknownTld(): void
{
$this->expectException(TldNotSupportedException::class);
self::$RDAPService->registerDomain('nic.noexist');
}
}

30
tests/UserTest.php Normal file
View File

@@ -0,0 +1,30 @@
<?php
namespace App\Tests;
use App\Entity\User;
use App\Factory\UserFactory;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Zenstruck\Foundry\Test\Factories;
use Zenstruck\Foundry\Test\ResetDatabase;
final class UserTest extends AbstractTest
{
use ResetDatabase;
use Factories;
/**
* @throws TransportExceptionInterface
*/
public function testGetMyProfile(): void
{
$testUser = UserFactory::createOne();
$client = UserTest::createClientWithCredentials(UserTest::getToken($testUser));
$client->loginUser($testUser);
$client->request('GET', '/api/me');
$this->assertResponseIsSuccessful();
$this->assertMatchesResourceItemJsonSchema(User::class);
}
}