test: add tests for handler

This commit is contained in:
Maël Gangloff
2025-10-25 21:47:11 +02:00
parent 1ce55010d1
commit d91ff6ca86
4 changed files with 77 additions and 3 deletions

View File

@@ -330,7 +330,7 @@ class Domain
return $this;
}
private function setUpdatedAt(?\DateTimeImmutable $updatedAt): void
public function setUpdatedAt(?\DateTimeImmutable $updatedAt): void
{
$this->updatedAt = $updatedAt;
}

View File

@@ -289,7 +289,6 @@ class RDAPService
}
$domain->setDeleted(true);
$this->em->persist($domain);
$this->em->flush();
}

View File

@@ -13,6 +13,7 @@ use App\Exception\UnknownRdapServerException;
use App\Message\SendDomainEventNotif;
use App\Repository\DomainRepository;
use App\Service\RDAPService;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\OptimisticLockException;
use Psr\Log\LoggerInterface;
use Random\Randomizer;
@@ -42,6 +43,7 @@ readonly class AutoRegisterDomainProvider implements ProviderInterface
private DomainRepository $domainRepository,
private MessageBusInterface $bus,
private RequestStack $requestStack,
private EntityManagerInterface $em,
) {
}
@@ -61,6 +63,8 @@ readonly class AutoRegisterDomainProvider implements ProviderInterface
*/
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
$fromWatchlist = Watchlist::class === $context['root_operation']?->getClass();
$userId = $this->security->getUser()->getUserIdentifier();
$idnDomain = RDAPService::convertToIdn($uriVariables['ldhName']);
@@ -97,7 +101,30 @@ readonly class AutoRegisterDomainProvider implements ProviderInterface
}
$updatedAt = null === $domain ? new \DateTimeImmutable('now') : $domain->getUpdatedAt();
$domain = $this->RDAPService->registerDomain($idnDomain);
try {
$domain = $this->RDAPService->registerDomain($idnDomain);
} catch (DomainNotFoundException $exception) {
if (!$fromWatchlist) {
throw $exception;
}
$domain = $this->domainRepository->findOneBy(['ldhName' => $idnDomain]);
if (null !== $domain) {
return $domain;
}
$domain = (new Domain())
->setLdhName($idnDomain)
->setTld($this->RDAPService->getTld($idnDomain))
->setDelegationSigned(false)
->setDeleted(true);
$this->em->persist($domain);
$this->em->flush();
return $domain;
}
$randomizer = new Randomizer();
$watchlists = $randomizer->shuffleArray($domain->getWatchlists()->toArray());

View File

@@ -0,0 +1,48 @@
<?php
namespace App\Tests\MessageHandler;
use App\Entity\Domain;
use App\Entity\Watchlist;
use App\Message\UpdateDomainsFromWatchlist;
use App\MessageHandler\UpdateDomainsFromWatchlistHandler;
use App\Service\RDAPService;
use App\Tests\State\WatchlistUpdateProcessorTest;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\Attributes\DependsExternal;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Messenger\TraceableMessageBus;
use Symfony\Component\Uid\UuidV4;
final class UpdateDomainsFromWatchlistHandlerTest extends KernelTestCase
{
#[DependsExternal(WatchlistUpdateProcessorTest::class, 'testCreateWatchlist')]
public function testMessage()
{
$container = self::getContainer();
$entityManager = $container->get(EntityManagerInterface::class);
$handler = $container->get(UpdateDomainsFromWatchlistHandler::class);
$bus = $container->get('messenger.bus.default');
$deletedDomainLdhName = new UuidV4().'.com';
$client = WatchlistUpdateProcessorTest::createUserAndWatchlist(null, ['/api/domains/'.$deletedDomainLdhName]);
/** @var Domain $domain */
$domain = $entityManager->getRepository(Domain::class)->findOneBy(['ldhName' => $deletedDomainLdhName]);
$domain->setUpdatedAt((new \DateTimeImmutable())->setTimestamp(0));
$entityManager->flush();
$response = $client->request('GET', '/api/watchlists');
/** @var Watchlist $watchlist */
$watchlist = $entityManager->getRepository(Watchlist::class)->findOneBy(['token' => $response->toArray()['hydra:member'][0]['token']]);
/* @var TraceableMessageBus $bus */
$bus->reset();
$handler(new UpdateDomainsFromWatchlist($watchlist->getToken()));
$this->expectNotToPerformAssertions();
}
}