feat: Afficher les statistiques de notes par matière côté administration
L'admin doit pouvoir voir en un coup d'œil quelles matières sont actives (notes saisies) pour décider lesquelles peuvent être supprimées sans perte de données. Auparavant, la suppression d'une matière était silencieuse : elle cascade-deletait évaluations et notes sans avertir. La liste des matières affiche désormais les compteurs d'enseignants, classes, évaluations et notes. La suppression déclenche une confirmation explicite quand la matière contient des notes, avec récapitulatif des volumes impactés, pour rendre l'action irréversible consciente. Côté tests, un endpoint de seeding HTTP remplace les appels docker exec dans les E2E (gain ~30-60s → 5-10s par test), et un trait partagé factorise le SQL de seeding entre les deux suites fonctionnelles.
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Administration\Application\Query\GetSubjectGradeStats;
|
||||
|
||||
use App\Administration\Application\Port\SubjectGradeStats;
|
||||
use App\Administration\Application\Port\SubjectGradeStatsReader;
|
||||
use App\Administration\Application\Query\GetSubjectGradeStats\GetSubjectGradeStatsHandler;
|
||||
use App\Administration\Application\Query\GetSubjectGradeStats\GetSubjectGradeStatsQuery;
|
||||
use App\Administration\Domain\Model\Subject\SubjectId;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use Override;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class GetSubjectGradeStatsHandlerTest extends TestCase
|
||||
{
|
||||
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440001';
|
||||
private const string SUBJECT_ID = '550e8400-e29b-41d4-a716-446655440002';
|
||||
|
||||
#[Test]
|
||||
public function itReturnsZeroStatsWhenSubjectHasNoEvaluations(): void
|
||||
{
|
||||
$handler = new GetSubjectGradeStatsHandler($this->createReader(evaluations: 0, grades: 0));
|
||||
|
||||
$stats = $handler(new GetSubjectGradeStatsQuery(
|
||||
tenantId: self::TENANT_ID,
|
||||
subjectId: self::SUBJECT_ID,
|
||||
));
|
||||
|
||||
self::assertSame(0, $stats->evaluationCount);
|
||||
self::assertSame(0, $stats->gradeCount);
|
||||
self::assertFalse($stats->hasGrades());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itReturnsCountsWhenEvaluationsExist(): void
|
||||
{
|
||||
$handler = new GetSubjectGradeStatsHandler($this->createReader(evaluations: 3, grades: 42));
|
||||
|
||||
$stats = $handler(new GetSubjectGradeStatsQuery(
|
||||
tenantId: self::TENANT_ID,
|
||||
subjectId: self::SUBJECT_ID,
|
||||
));
|
||||
|
||||
self::assertSame(3, $stats->evaluationCount);
|
||||
self::assertSame(42, $stats->gradeCount);
|
||||
self::assertTrue($stats->hasGrades());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itConsidersSubjectWithEvaluationsButNoGradesAsHavingImpact(): void
|
||||
{
|
||||
$handler = new GetSubjectGradeStatsHandler($this->createReader(evaluations: 2, grades: 0));
|
||||
|
||||
$stats = $handler(new GetSubjectGradeStatsQuery(
|
||||
tenantId: self::TENANT_ID,
|
||||
subjectId: self::SUBJECT_ID,
|
||||
));
|
||||
|
||||
self::assertSame(2, $stats->evaluationCount);
|
||||
self::assertSame(0, $stats->gradeCount);
|
||||
self::assertTrue($stats->hasGrades(), 'Une évaluation sans notes reste un impact à signaler.');
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itConsidersSubjectWithGradesButNoEvaluationsAsHavingImpact(): void
|
||||
{
|
||||
// Théoriquement impossible via la FK grades.evaluation_id → evaluations(id),
|
||||
// mais on couvre la logique `||` du value object contre toute régression.
|
||||
$handler = new GetSubjectGradeStatsHandler($this->createReader(evaluations: 0, grades: 5));
|
||||
|
||||
$stats = $handler(new GetSubjectGradeStatsQuery(
|
||||
tenantId: self::TENANT_ID,
|
||||
subjectId: self::SUBJECT_ID,
|
||||
));
|
||||
|
||||
self::assertSame(0, $stats->evaluationCount);
|
||||
self::assertSame(5, $stats->gradeCount);
|
||||
self::assertTrue($stats->hasGrades());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itPassesQueryParamsToReader(): void
|
||||
{
|
||||
$reader = new class implements SubjectGradeStatsReader {
|
||||
public ?string $receivedTenantId = null;
|
||||
public ?string $receivedSubjectId = null;
|
||||
|
||||
#[Override]
|
||||
public function countForSubject(TenantId $tenantId, SubjectId $subjectId): SubjectGradeStats
|
||||
{
|
||||
$this->receivedTenantId = (string) $tenantId;
|
||||
$this->receivedSubjectId = (string) $subjectId;
|
||||
|
||||
return new SubjectGradeStats(0, 0);
|
||||
}
|
||||
};
|
||||
|
||||
$handler = new GetSubjectGradeStatsHandler($reader);
|
||||
|
||||
$handler(new GetSubjectGradeStatsQuery(
|
||||
tenantId: self::TENANT_ID,
|
||||
subjectId: self::SUBJECT_ID,
|
||||
));
|
||||
|
||||
self::assertSame(self::TENANT_ID, $reader->receivedTenantId);
|
||||
self::assertSame(self::SUBJECT_ID, $reader->receivedSubjectId);
|
||||
}
|
||||
|
||||
private function createReader(int $evaluations, int $grades): SubjectGradeStatsReader
|
||||
{
|
||||
return new class($evaluations, $grades) implements SubjectGradeStatsReader {
|
||||
public function __construct(
|
||||
private int $evaluations,
|
||||
private int $grades,
|
||||
) {
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function countForSubject(TenantId $tenantId, SubjectId $subjectId): SubjectGradeStats
|
||||
{
|
||||
return new SubjectGradeStats($this->evaluations, $this->grades);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,232 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Administration\Infrastructure\Api\Processor;
|
||||
|
||||
use ApiPlatform\Metadata\Delete;
|
||||
use App\Administration\Application\Command\ArchiveSubject\ArchiveSubjectHandler;
|
||||
use App\Administration\Application\Port\SubjectGradeStats;
|
||||
use App\Administration\Application\Port\SubjectGradeStatsReader;
|
||||
use App\Administration\Application\Query\GetSubjectGradeStats\GetSubjectGradeStatsHandler;
|
||||
use App\Administration\Domain\Model\SchoolClass\SchoolId;
|
||||
use App\Administration\Domain\Model\Subject\Subject;
|
||||
use App\Administration\Domain\Model\Subject\SubjectCode;
|
||||
use App\Administration\Domain\Model\Subject\SubjectId;
|
||||
use App\Administration\Domain\Model\Subject\SubjectName;
|
||||
use App\Administration\Infrastructure\Api\Processor\DeleteSubjectProcessor;
|
||||
use App\Administration\Infrastructure\Api\Resource\SubjectResource;
|
||||
use App\Administration\Infrastructure\Persistence\InMemory\InMemorySubjectRepository;
|
||||
use App\Administration\Infrastructure\Security\SubjectVoter;
|
||||
use App\Shared\Domain\Clock;
|
||||
use App\Shared\Domain\Tenant\TenantId as DomainTenantId;
|
||||
use App\Shared\Infrastructure\Tenant\TenantConfig;
|
||||
use App\Shared\Infrastructure\Tenant\TenantContext;
|
||||
use App\Shared\Infrastructure\Tenant\TenantId as InfraTenantId;
|
||||
use DateTimeImmutable;
|
||||
use Override;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
|
||||
use Symfony\Component\Messenger\Envelope;
|
||||
use Symfony\Component\Messenger\MessageBusInterface;
|
||||
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
|
||||
|
||||
final class DeleteSubjectProcessorTest extends TestCase
|
||||
{
|
||||
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440002';
|
||||
private const string SCHOOL_ID = '550e8400-e29b-41d4-a716-446655440003';
|
||||
|
||||
private InMemorySubjectRepository $subjectRepository;
|
||||
private TenantContext $tenantContext;
|
||||
private Clock $clock;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->subjectRepository = new InMemorySubjectRepository();
|
||||
$this->clock = new class implements Clock {
|
||||
#[Override]
|
||||
public function now(): DateTimeImmutable
|
||||
{
|
||||
return new DateTimeImmutable('2026-04-16 10:00:00');
|
||||
}
|
||||
};
|
||||
|
||||
$this->tenantContext = new TenantContext();
|
||||
$this->tenantContext->setCurrentTenant(new TenantConfig(
|
||||
tenantId: InfraTenantId::fromString(self::TENANT_ID),
|
||||
subdomain: 'ecole-alpha',
|
||||
databaseUrl: 'postgresql://test',
|
||||
));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itDeletesSubjectWhenNoGradesExist(): void
|
||||
{
|
||||
$subject = $this->persistSubject();
|
||||
$processor = $this->createProcessor(statsReader: $this->statsReader(0, 0));
|
||||
|
||||
$result = $processor->process(
|
||||
SubjectResource::fromDomain($subject),
|
||||
new Delete(),
|
||||
['id' => (string) $subject->id],
|
||||
);
|
||||
|
||||
self::assertNull($result);
|
||||
$reloaded = $this->subjectRepository->get($subject->id);
|
||||
self::assertNotNull($reloaded->deletedAt);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itThrowsConflictWhenGradesExistAndConfirmNotSet(): void
|
||||
{
|
||||
$subject = $this->persistSubject();
|
||||
$processor = $this->createProcessor(statsReader: $this->statsReader(3, 42));
|
||||
|
||||
$this->expectException(ConflictHttpException::class);
|
||||
$this->expectExceptionMessageMatches('/3 évaluation\(s\) et 42 note\(s\)/');
|
||||
|
||||
$processor->process(
|
||||
SubjectResource::fromDomain($subject),
|
||||
new Delete(),
|
||||
['id' => (string) $subject->id],
|
||||
);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itDeletesSubjectWhenConfirmIsTrue(): void
|
||||
{
|
||||
$subject = $this->persistSubject();
|
||||
$processor = $this->createProcessor(
|
||||
statsReader: $this->statsReader(3, 42),
|
||||
request: new Request(query: ['confirm' => 'true']),
|
||||
);
|
||||
|
||||
$result = $processor->process(
|
||||
SubjectResource::fromDomain($subject),
|
||||
new Delete(),
|
||||
['id' => (string) $subject->id],
|
||||
);
|
||||
|
||||
self::assertNull($result);
|
||||
$reloaded = $this->subjectRepository->get($subject->id);
|
||||
self::assertNotNull($reloaded->deletedAt);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itRejectsUnauthorizedAccess(): void
|
||||
{
|
||||
$subject = $this->persistSubject();
|
||||
$processor = $this->createProcessor(granted: false);
|
||||
|
||||
$this->expectException(AccessDeniedHttpException::class);
|
||||
|
||||
$processor->process(
|
||||
SubjectResource::fromDomain($subject),
|
||||
new Delete(),
|
||||
['id' => (string) $subject->id],
|
||||
);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itRejectsWhenTenantNotSet(): void
|
||||
{
|
||||
$subject = $this->persistSubject();
|
||||
$processor = $this->createProcessor(tenantContext: new TenantContext());
|
||||
|
||||
$this->expectException(UnauthorizedHttpException::class);
|
||||
|
||||
$processor->process(
|
||||
SubjectResource::fromDomain($subject),
|
||||
new Delete(),
|
||||
['id' => (string) $subject->id],
|
||||
);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itReturnsNotFoundWhenIdMissing(): void
|
||||
{
|
||||
$subject = $this->persistSubject();
|
||||
$processor = $this->createProcessor();
|
||||
|
||||
$this->expectException(NotFoundHttpException::class);
|
||||
|
||||
$processor->process(SubjectResource::fromDomain($subject), new Delete(), []);
|
||||
}
|
||||
|
||||
private function persistSubject(): Subject
|
||||
{
|
||||
$subject = Subject::creer(
|
||||
tenantId: DomainTenantId::fromString(self::TENANT_ID),
|
||||
schoolId: SchoolId::fromString(self::SCHOOL_ID),
|
||||
name: new SubjectName('Mathématiques'),
|
||||
code: new SubjectCode('MATH'),
|
||||
color: null,
|
||||
createdAt: $this->clock->now(),
|
||||
);
|
||||
|
||||
$this->subjectRepository->save($subject);
|
||||
|
||||
return $subject;
|
||||
}
|
||||
|
||||
private function statsReader(int $evaluations, int $grades): SubjectGradeStatsReader
|
||||
{
|
||||
return new class($evaluations, $grades) implements SubjectGradeStatsReader {
|
||||
public function __construct(
|
||||
private int $evaluations,
|
||||
private int $grades,
|
||||
) {
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function countForSubject(
|
||||
DomainTenantId $tenantId,
|
||||
SubjectId $subjectId,
|
||||
): SubjectGradeStats {
|
||||
return new SubjectGradeStats($this->evaluations, $this->grades);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private function createProcessor(
|
||||
bool $granted = true,
|
||||
?TenantContext $tenantContext = null,
|
||||
?SubjectGradeStatsReader $statsReader = null,
|
||||
?Request $request = null,
|
||||
): DeleteSubjectProcessor {
|
||||
$archiveHandler = new ArchiveSubjectHandler($this->subjectRepository, $this->clock);
|
||||
$gradeStatsHandler = new GetSubjectGradeStatsHandler(
|
||||
$statsReader ?? $this->statsReader(0, 0),
|
||||
);
|
||||
|
||||
$eventBus = $this->createMock(MessageBusInterface::class);
|
||||
$eventBus->method('dispatch')->willReturnCallback(
|
||||
static fn (object $message) => new Envelope($message),
|
||||
);
|
||||
|
||||
$authorizationChecker = $this->createMock(AuthorizationCheckerInterface::class);
|
||||
$authorizationChecker->method('isGranted')
|
||||
->with(SubjectVoter::DELETE)
|
||||
->willReturn($granted);
|
||||
|
||||
$requestStack = new RequestStack();
|
||||
if ($request !== null) {
|
||||
$requestStack->push($request);
|
||||
}
|
||||
|
||||
return new DeleteSubjectProcessor(
|
||||
$archiveHandler,
|
||||
$gradeStatsHandler,
|
||||
$tenantContext ?? $this->tenantContext,
|
||||
$eventBus,
|
||||
$authorizationChecker,
|
||||
$requestStack,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user