domain-watchdog/tests/Service/RDAPServiceTest.php

125 lines
4.3 KiB
PHP
Raw Normal View History

2025-10-14 00:09:01 +02:00
<?php
namespace App\Tests\Service;
use App\Entity\RdapServer;
2025-10-14 11:15:56 +02:00
use App\Entity\Tld;
2025-10-14 00:09:01 +02:00
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;
2025-10-14 23:34:17 +02:00
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Contracts\HttpClient\HttpClientInterface;
2025-10-14 00:09:01 +02:00
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);
$rdapServerRepository = self::$entityManager->getRepository(RdapServer::class);
$this->assertNotEmpty($rdapServerRepository->findAll());
}
#[Depends('testUpdateRdapServers')]
2025-10-14 11:15:56 +02:00
public function testRegisterDomainByTld(): void
2025-10-14 00:09:01 +02:00
{
2025-10-14 11:15:56 +02:00
self::$RDAPService->registerDomains(['arpa']);
2025-10-14 00:09:01 +02:00
2025-10-14 11:15:56 +02:00
$rdapServerRepository = static::$entityManager->getRepository(RdapServer::class);
$rdapServerList = $rdapServerRepository->findBy(['tld' => array_map(
fn (string $tld) => static::$entityManager->getReference(Tld::class, $tld),
['com', 'net', 'fr', 'de', 'ch', 'ca', 'uz', 'google', 'ovh']
),
]);
2025-10-14 00:09:01 +02:00
2025-10-14 11:15:56 +02:00
foreach ($rdapServerList as $rdapServer) {
2025-10-14 00:09:01 +02:00
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');
}
2025-10-14 11:15:56 +02:00
#[Depends('testUpdateRdapServers')]
2025-10-14 23:34:17 +02:00
public function testDomainDeleted()
2025-10-14 11:15:56 +02:00
{
2025-10-14 23:34:17 +02:00
self::$RDAPService->registerDomain('example.com');
self::ensureKernelShutdown();
self::bootKernel();
static::getContainer()->set(HttpClientInterface::class, new MockHttpClient(
new MockResponse('', ['http_code' => 404])
));
$rdapService = static::getContainer()->get(RDAPService::class);
2025-10-14 11:15:56 +02:00
$this->expectException(DomainNotFoundException::class);
2025-10-14 23:34:17 +02:00
$rdapService->registerDomain('example.com');
}
#[Depends('testDomainDeleted')]
public function testDomainUpdateStatus(): void
{
$domain = self::$RDAPService->registerDomain('example.com');
$domain->setStatus(['pending delete']);
self::$entityManager->flush();
self::$RDAPService->registerDomain('example.com');
$this->expectNotToPerformAssertions();
}
#[Depends('testUpdateRdapServers')]
public function testHttpClientException()
{
self::ensureKernelShutdown();
self::bootKernel();
static::getContainer()->set(HttpClientInterface::class, new MockHttpClient(
fn () => throw new TransportException()
));
$rdapService = static::getContainer()->get(RDAPService::class);
$this->expectException(TransportException::class);
$rdapService->registerDomain('example.com');
2025-10-14 11:15:56 +02:00
}
2025-10-14 00:09:01 +02:00
}