Files
Classeo/backend/tests/Unit/SuperAdmin/Application/Command/CreateEstablishment/CreateEstablishmentHandlerTest.php
Mathias STRASSER dc2be898d5
Some checks failed
CI / Backend Tests (push) Has been cancelled
CI / Frontend Tests (push) Has been cancelled
CI / E2E Tests (push) Has been cancelled
CI / Naming Conventions (push) Has been cancelled
CI / Build Check (push) Has been cancelled
feat: Provisionner automatiquement un nouvel établissement
Lorsqu'un super-admin crée un établissement via l'interface, le système
doit automatiquement créer la base tenant, exécuter les migrations,
créer le premier utilisateur admin et envoyer l'invitation — le tout
de manière asynchrone pour ne pas bloquer la réponse HTTP.

Ce mécanisme rend chaque établissement opérationnel dès sa création
sans intervention manuelle sur l'infrastructure.
2026-04-16 09:27:25 +02:00

75 lines
2.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Unit\SuperAdmin\Application\Command\CreateEstablishment;
use App\Shared\Domain\Clock;
use App\SuperAdmin\Application\Command\CreateEstablishment\CreateEstablishmentCommand;
use App\SuperAdmin\Application\Command\CreateEstablishment\CreateEstablishmentHandler;
use App\SuperAdmin\Infrastructure\Persistence\InMemory\InMemoryEstablishmentRepository;
use DateTimeImmutable;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
final class CreateEstablishmentHandlerTest extends TestCase
{
private const string SUPER_ADMIN_ID = '550e8400-e29b-41d4-a716-446655440001';
private InMemoryEstablishmentRepository $repository;
private CreateEstablishmentHandler $handler;
protected function setUp(): void
{
$this->repository = new InMemoryEstablishmentRepository();
$clock = new class implements Clock {
public function now(): DateTimeImmutable
{
return new DateTimeImmutable('2026-02-16 10:00:00');
}
};
$this->handler = new CreateEstablishmentHandler(
$this->repository,
$clock,
);
}
#[Test]
public function createsEstablishmentAndReturnsIt(): void
{
$command = new CreateEstablishmentCommand(
name: 'École Alpha',
subdomain: 'ecole-alpha',
adminEmail: 'admin@ecole-alpha.fr',
superAdminId: self::SUPER_ADMIN_ID,
);
$establishment = ($this->handler)($command);
self::assertNotEmpty((string) $establishment->id);
self::assertNotEmpty((string) $establishment->tenantId);
self::assertSame('École Alpha', $establishment->name);
self::assertSame('ecole-alpha', $establishment->subdomain);
self::assertStringStartsWith('classeo_tenant_', $establishment->databaseName);
}
#[Test]
public function savesEstablishmentToRepository(): void
{
$command = new CreateEstablishmentCommand(
name: 'École Beta',
subdomain: 'ecole-beta',
adminEmail: 'admin@ecole-beta.fr',
superAdminId: self::SUPER_ADMIN_ID,
);
$establishment = ($this->handler)($command);
$establishments = $this->repository->findAll();
self::assertCount(1, $establishments);
self::assertSame((string) $establishment->id, (string) $establishments[0]->id);
}
}