test: add some KernelTestCase

This commit is contained in:
Maël Gangloff
2025-10-14 17:40:48 +02:00
parent 39262814ce
commit 97ea1ab33a
10 changed files with 255 additions and 10 deletions

View File

@@ -0,0 +1,27 @@
<?php
namespace App\Tests\Controller;
use App\Entity\Connector;
use App\Factory\UserFactory;
use App\Tests\AbstractTest;
use Zenstruck\Foundry\Test\Factories;
use Zenstruck\Foundry\Test\ResetDatabase;
final class ConnectorControllerTest extends AbstractTest
{
use ResetDatabase;
use Factories;
public function testGetConnectorCollection(): void
{
$testUser = UserFactory::createOne();
$client = ConnectorControllerTest::createClientWithCredentials(ConnectorControllerTest::getToken($testUser));
$response = $client->request('GET', '/api/connectors');
$this->assertResponseIsSuccessful();
$this->assertMatchesResourceCollectionJsonSchema(Connector::class);
$this->assertCount(0, $response->toArray()['hydra:member']);
}
}

View File

@@ -0,0 +1,26 @@
<?php
namespace App\Tests\Controller;
use App\Entity\Domain;
use App\Factory\UserFactory;
use App\Tests\AbstractTest;
use App\Tests\Service\RDAPServiceTest;
use PHPUnit\Framework\Attributes\DependsExternal;
use Zenstruck\Foundry\Test\Factories;
final class DomainRefreshControllerTest extends AbstractTest
{
use Factories;
#[DependsExternal(RDAPServiceTest::class, 'testUpdateRdapServers')]
public function testRegisterDomain(): void
{
$testUser = UserFactory::createOne();
$client = DomainRefreshControllerTest::createClientWithCredentials(DomainRefreshControllerTest::getToken($testUser));
$client->request('GET', '/api/domains/example.com');
$this->assertResponseIsSuccessful();
$this->assertMatchesResourceItemJsonSchema(Domain::class);
}
}

View File

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

View File

@@ -0,0 +1,78 @@
<?php
namespace App\Tests\Controller;
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use App\Factory\UserFactory;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Zenstruck\Foundry\Test\Factories;
use Zenstruck\Foundry\Test\ResetDatabase;
final class RegistrationControllerTest extends ApiTestCase
{
use ResetDatabase;
use Factories;
protected static ContainerInterface $container;
protected static EntityManagerInterface $entityManager;
protected function setUp(): void
{
RegistrationControllerTest::$container = RegistrationControllerTest::getContainer();
RegistrationControllerTest::$entityManager = RegistrationControllerTest::$container->get(EntityManagerInterface::class);
}
public function testRegister(): void
{
$testUser = UserFactory::createOne();
RegistrationControllerTest::$entityManager->remove($testUser);
RegistrationControllerTest::$entityManager->flush();
$client = $this->createClient();
$client->request('POST', '/api/register', [
'json' => [
'email' => $testUser->getEmail(),
'password' => $testUser->getPlainPassword(),
],
]);
$this->assertResponseIsSuccessful();
$this->assertResponseStatusCodeSame(201);
}
public function testRegisterEmptyEmail(): void
{
$client = $this->createClient();
$client->request('POST', '/api/register', [
'json' => [
'email' => '',
'password' => 'MySuperPassword123',
],
]);
$this->assertResponseStatusCodeSame(400);
}
public function testRegisterEmptyPassword(): void
{
$client = $this->createClient();
$client->request('POST', '/api/register', [
'json' => [
'email' => 'test@domainwatchdog.eu',
'password' => '',
],
]);
$this->assertResponseStatusCodeSame(400);
}
public function testRegisterWeakPassword(): void
{
$client = $this->createClient();
$client->request('POST', '/api/register', [
'json' => [
'email' => 'test@domainwatchdog.eu',
'password' => '123',
],
]);
$this->assertResponseStatusCodeSame(400);
}
}

View File

@@ -0,0 +1,90 @@
<?php
namespace App\Tests\Controller;
use ApiPlatform\Symfony\Bundle\Test\Client;
use App\Entity\WatchList;
use App\Factory\UserFactory;
use App\Tests\AbstractTest;
use App\Tests\Service\RDAPServiceTest;
use PHPUnit\Framework\Attributes\DependsExternal;
use Zenstruck\Foundry\Test\Factories;
final class WatchlistControllerTest extends AbstractTest
{
use Factories;
public function testGetWatchlistCollection(): void
{
$client = $this->createUserAndWatchlist();
$response = $client->request('GET', '/api/watchlists');
$this->assertResponseIsSuccessful();
$this->assertMatchesResourceCollectionJsonSchema(WatchList::class);
$data = $response->toArray();
$this->assertArrayHasKey('hydra:member', $data);
$this->assertCount(1, $data['hydra:member']);
}
#[DependsExternal(RDAPServiceTest::class, 'testUpdateRdapServers')]
public function testCreateWatchlist(): void
{
$this->createUserAndWatchlist();
$this->assertResponseIsSuccessful();
$this->assertResponseStatusCodeSame(201);
$this->assertMatchesResourceItemJsonSchema(WatchList::class);
}
#[DependsExternal(RDAPServiceTest::class, 'testUpdateRdapServers')]
public function testGetTrackedDomains()
{
$client = $this->createUserAndWatchlist();
$response = $client->request('GET', '/api/tracked');
$this->assertResponseIsSuccessful();
$data = $response->toArray();
$this->assertArrayHasKey('hydra:member', $data);
$this->assertCount(1, $data['hydra:member']);
}
#[DependsExternal(RDAPServiceTest::class, 'testUpdateRdapServers')]
public function testGetWatchlistFeeds()
{
$client = $this->createUserAndWatchlist();
$response = $client->request('GET', '/api/watchlists');
$token = $response->toArray()['hydra:member'][0]['token'];
$client->request('GET', '/api/watchlists/'.$token.'/calendar');
$this->assertResponseIsSuccessful();
$this->assertResponseHeaderSame('content-type', 'text/calendar; charset=utf-8');
$client->request('GET', '/api/watchlists/'.$token.'/rss/events');
$this->assertResponseIsSuccessful();
$this->assertResponseHeaderSame('content-type', 'application/atom+xml; charset=utf-8');
$client->request('GET', '/api/watchlists/'.$token.'/rss/status');
$this->assertResponseIsSuccessful();
$this->assertResponseHeaderSame('content-type', 'application/atom+xml; charset=utf-8');
}
private function createUserAndWatchlist(): Client
{
$client = self::createClientWithCredentials(self::getToken(UserFactory::createOne()));
$client->request('POST', '/api/watchlists', ['json' => [
'domains' => ['/api/domains/example.com'],
'name' => 'My Watchlist',
'triggers' => [
['action' => 'email', 'event' => 'last changed'],
['action' => 'email', 'event' => 'transfer'],
['action' => 'email', 'event' => 'expiration'],
['action' => 'email', 'event' => 'deletion'],
],
]]);
return $client;
}
}