feat: Permettre l'import d'enseignants via fichier CSV ou XLSX
L'établissement a besoin d'importer en masse ses enseignants depuis les exports des logiciels de vie scolaire (Pronote, EDT, etc.), comme c'est déjà possible pour les élèves. Le wizard en 4 étapes (upload → mapping → aperçu → import) réutilise l'architecture de l'import élèves tout en ajoutant la gestion des matières et des classes enseignées. Corrections de la review #2 intégrées : - La commande ImportTeachersCommand est routée en async via Messenger pour ne pas bloquer la requête HTTP sur les gros fichiers. - Le handler est protégé par un try/catch Throwable pour marquer le batch en échec si une erreur inattendue survient, évitant qu'il reste bloqué en statut "processing". - Les domain events (UtilisateurInvite) sont dispatchés sur l'event bus après chaque création d'utilisateur, déclenchant l'envoi des emails d'invitation. - L'option "mettre à jour les enseignants existants" (AC5) permet de choisir entre ignorer ou mettre à jour nom/prénom et ajouter les affectations manquantes pour les doublons détectés par email.
This commit is contained in:
@@ -0,0 +1,242 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Administration\Application\Service\Import;
|
||||
|
||||
use App\Administration\Application\Service\Import\DuplicateDetector;
|
||||
use App\Administration\Domain\Model\Import\ImportRow;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class DuplicateDetectorTest extends TestCase
|
||||
{
|
||||
private DuplicateDetector $detector;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->detector = new DuplicateDetector();
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function detectsDuplicateByEmail(): void
|
||||
{
|
||||
$existing = [
|
||||
$this->student('Jean', 'Dupont', 'jean@example.com', null, '6A'),
|
||||
];
|
||||
$rows = [
|
||||
$this->createRow(['lastName' => 'Dupont', 'firstName' => 'Jean', 'email' => 'jean@example.com', 'className' => '6A']),
|
||||
];
|
||||
|
||||
$result = $this->detector->detecter($rows, $existing);
|
||||
|
||||
self::assertFalse($result[0]->estValide());
|
||||
self::assertSame('_duplicate', $result[0]->errors[0]->column);
|
||||
self::assertStringContainsString('email', $result[0]->errors[0]->message);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function detectsDuplicateByEmailCaseInsensitive(): void
|
||||
{
|
||||
$existing = [
|
||||
$this->student('Jean', 'Dupont', 'Jean.Dupont@Example.COM', null, '6A'),
|
||||
];
|
||||
$rows = [
|
||||
$this->createRow(['lastName' => 'Dupont', 'firstName' => 'Jean', 'email' => 'jean.dupont@example.com', 'className' => '6A']),
|
||||
];
|
||||
|
||||
$result = $this->detector->detecter($rows, $existing);
|
||||
|
||||
self::assertFalse($result[0]->estValide());
|
||||
self::assertStringContainsString('email', $result[0]->errors[0]->message);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function detectsDuplicateByStudentNumber(): void
|
||||
{
|
||||
$existing = [
|
||||
$this->student('Jean', 'Dupont', null, 'STU-001', '6A'),
|
||||
];
|
||||
$rows = [
|
||||
$this->createRow(['lastName' => 'Dupont', 'firstName' => 'Jean', 'studentNumber' => 'STU-001', 'className' => '6A']),
|
||||
];
|
||||
|
||||
$result = $this->detector->detecter($rows, $existing);
|
||||
|
||||
self::assertFalse($result[0]->estValide());
|
||||
self::assertStringContainsString('numéro élève', $result[0]->errors[0]->message);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function detectsDuplicateByNameAndClass(): void
|
||||
{
|
||||
$existing = [
|
||||
$this->student('Jean', 'Dupont', null, null, '6A'),
|
||||
];
|
||||
$rows = [
|
||||
$this->createRow(['lastName' => 'Dupont', 'firstName' => 'Jean', 'className' => '6A']),
|
||||
];
|
||||
|
||||
$result = $this->detector->detecter($rows, $existing);
|
||||
|
||||
self::assertFalse($result[0]->estValide());
|
||||
self::assertStringContainsString('nom + classe', $result[0]->errors[0]->message);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function nameAndClassMatchIsCaseInsensitive(): void
|
||||
{
|
||||
$existing = [
|
||||
$this->student('JEAN', 'DUPONT', null, null, '6A'),
|
||||
];
|
||||
$rows = [
|
||||
$this->createRow(['lastName' => 'dupont', 'firstName' => 'jean', 'className' => '6a']),
|
||||
];
|
||||
|
||||
$result = $this->detector->detecter($rows, $existing);
|
||||
|
||||
self::assertFalse($result[0]->estValide());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function sameNameDifferentClassIsNotDuplicate(): void
|
||||
{
|
||||
$existing = [
|
||||
$this->student('Jean', 'Dupont', null, null, '6A'),
|
||||
];
|
||||
$rows = [
|
||||
$this->createRow(['lastName' => 'Dupont', 'firstName' => 'Jean', 'className' => '6B']),
|
||||
];
|
||||
|
||||
$result = $this->detector->detecter($rows, $existing);
|
||||
|
||||
self::assertTrue($result[0]->estValide());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function detectsIntraFileDuplicate(): void
|
||||
{
|
||||
$existing = [];
|
||||
$rows = [
|
||||
$this->createRow(['lastName' => 'Dupont', 'firstName' => 'Jean', 'className' => '6A'], 1),
|
||||
$this->createRow(['lastName' => 'Dupont', 'firstName' => 'Jean', 'className' => '6A'], 2),
|
||||
];
|
||||
|
||||
$result = $this->detector->detecter($rows, $existing);
|
||||
|
||||
self::assertTrue($result[0]->estValide());
|
||||
self::assertFalse($result[1]->estValide());
|
||||
self::assertSame('_duplicate', $result[1]->errors[0]->column);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function detectsIntraFileDuplicateByEmail(): void
|
||||
{
|
||||
$existing = [];
|
||||
$rows = [
|
||||
$this->createRow(['lastName' => 'Dupont', 'firstName' => 'Jean', 'email' => 'jean@example.com', 'className' => '6A'], 1),
|
||||
$this->createRow(['lastName' => 'Martin', 'firstName' => 'Pierre', 'email' => 'jean@example.com', 'className' => '5B'], 2),
|
||||
];
|
||||
|
||||
$result = $this->detector->detecter($rows, $existing);
|
||||
|
||||
self::assertTrue($result[0]->estValide());
|
||||
self::assertFalse($result[1]->estValide());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function rowWithoutEmailOrNumberOrClassIsNotDuplicate(): void
|
||||
{
|
||||
$existing = [
|
||||
$this->student('Jean', 'Dupont', null, null, '6A'),
|
||||
];
|
||||
$rows = [
|
||||
$this->createRow(['lastName' => 'Dupont', 'firstName' => 'Jean', 'className' => '']),
|
||||
];
|
||||
|
||||
$result = $this->detector->detecter($rows, $existing);
|
||||
|
||||
self::assertTrue($result[0]->estValide());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function multipleRowsMixedDuplicatesAndValid(): void
|
||||
{
|
||||
$existing = [
|
||||
$this->student('Jean', 'Dupont', 'jean@example.com', null, '6A'),
|
||||
];
|
||||
$rows = [
|
||||
$this->createRow(['lastName' => 'Dupont', 'firstName' => 'Jean', 'email' => 'jean@example.com', 'className' => '6A'], 1),
|
||||
$this->createRow(['lastName' => 'Martin', 'firstName' => 'Pierre', 'className' => '6A'], 2),
|
||||
$this->createRow(['lastName' => 'Bernard', 'firstName' => 'Claire', 'className' => '6B'], 3),
|
||||
];
|
||||
|
||||
$result = $this->detector->detecter($rows, $existing);
|
||||
|
||||
self::assertFalse($result[0]->estValide());
|
||||
self::assertTrue($result[1]->estValide());
|
||||
self::assertTrue($result[2]->estValide());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function emailMatchTakesPriorityOverNameClass(): void
|
||||
{
|
||||
$existing = [
|
||||
$this->student('Jean', 'Dupont', 'jean@example.com', null, '6A'),
|
||||
];
|
||||
$rows = [
|
||||
$this->createRow(['lastName' => 'Dupont', 'firstName' => 'Jean', 'email' => 'jean@example.com', 'className' => '6A']),
|
||||
];
|
||||
|
||||
$result = $this->detector->detecter($rows, $existing);
|
||||
|
||||
self::assertStringContainsString('email', $result[0]->errors[0]->message);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function preservesExistingValidationErrors(): void
|
||||
{
|
||||
$existing = [
|
||||
$this->student('Jean', 'Dupont', null, null, '6A'),
|
||||
];
|
||||
|
||||
$row = new ImportRow(
|
||||
lineNumber: 1,
|
||||
rawData: ['lastName' => 'Dupont', 'firstName' => 'Jean', 'className' => '6A'],
|
||||
mappedData: ['lastName' => 'Dupont', 'firstName' => 'Jean', 'className' => '6A'],
|
||||
errors: [new \App\Administration\Domain\Model\Import\ImportRowError('email', 'Email invalide.')],
|
||||
);
|
||||
|
||||
$result = $this->detector->detecter([$row], $existing);
|
||||
|
||||
self::assertCount(2, $result[0]->errors);
|
||||
self::assertSame('email', $result[0]->errors[0]->column);
|
||||
self::assertSame('_duplicate', $result[0]->errors[1]->column);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, string> $mappedData
|
||||
*/
|
||||
private function createRow(array $mappedData, int $lineNumber = 1): ImportRow
|
||||
{
|
||||
return new ImportRow(
|
||||
lineNumber: $lineNumber,
|
||||
rawData: $mappedData,
|
||||
mappedData: $mappedData,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{firstName: string, lastName: string, email: ?string, studentNumber: ?string, className: ?string}
|
||||
*/
|
||||
private function student(string $firstName, string $lastName, ?string $email, ?string $studentNumber, ?string $className): array
|
||||
{
|
||||
return [
|
||||
'firstName' => $firstName,
|
||||
'lastName' => $lastName,
|
||||
'email' => $email,
|
||||
'studentNumber' => $studentNumber,
|
||||
'className' => $className,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Administration\Application\Service\Import;
|
||||
|
||||
use App\Administration\Application\Service\Import\MultiValueParser;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class MultiValueParserTest extends TestCase
|
||||
{
|
||||
private MultiValueParser $parser;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->parser = new MultiValueParser();
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function parseSingleValue(): void
|
||||
{
|
||||
$result = $this->parser->parse('Mathématiques');
|
||||
|
||||
self::assertSame(['Mathématiques'], $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function parseMultipleValues(): void
|
||||
{
|
||||
$result = $this->parser->parse('Mathématiques, Physique');
|
||||
|
||||
self::assertSame(['Mathématiques', 'Physique'], $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function parseTrimsWhitespace(): void
|
||||
{
|
||||
$result = $this->parser->parse(' Mathématiques , Physique , Chimie ');
|
||||
|
||||
self::assertSame(['Mathématiques', 'Physique', 'Chimie'], $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function parseEmptyStringReturnsEmptyArray(): void
|
||||
{
|
||||
$result = $this->parser->parse('');
|
||||
|
||||
self::assertSame([], $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function parseOnlyWhitespaceReturnsEmptyArray(): void
|
||||
{
|
||||
$result = $this->parser->parse(' ');
|
||||
|
||||
self::assertSame([], $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function parseWithCustomSeparator(): void
|
||||
{
|
||||
$result = $this->parser->parse('6A;6B;5A', ';');
|
||||
|
||||
self::assertSame(['6A', '6B', '5A'], $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function parseFiltersOutEmptyValues(): void
|
||||
{
|
||||
$result = $this->parser->parse('Mathématiques,,Physique,');
|
||||
|
||||
self::assertSame(['Mathématiques', 'Physique'], $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function parseWithClassNames(): void
|
||||
{
|
||||
$result = $this->parser->parse('6A, 6B, 5A');
|
||||
|
||||
self::assertSame(['6A', '6B', '5A'], $result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Administration\Application\Service\Import;
|
||||
|
||||
use App\Administration\Application\Service\Import\TeacherColumnMappingSuggester;
|
||||
use App\Administration\Domain\Model\Import\KnownImportFormat;
|
||||
use App\Administration\Domain\Model\Import\TeacherImportField;
|
||||
|
||||
use function count;
|
||||
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
use const SORT_REGULAR;
|
||||
|
||||
final class TeacherColumnMappingSuggesterTest extends TestCase
|
||||
{
|
||||
private TeacherColumnMappingSuggester $suggester;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->suggester = new TeacherColumnMappingSuggester();
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function suggestGenericMappingByKeywords(): void
|
||||
{
|
||||
$columns = ['Nom', 'Prénom', 'Email', 'Matières', 'Classes'];
|
||||
|
||||
$mapping = $this->suggester->suggerer($columns, KnownImportFormat::CUSTOM);
|
||||
|
||||
self::assertSame(TeacherImportField::LAST_NAME, $mapping['Nom']);
|
||||
self::assertSame(TeacherImportField::FIRST_NAME, $mapping['Prénom']);
|
||||
self::assertSame(TeacherImportField::EMAIL, $mapping['Email']);
|
||||
self::assertSame(TeacherImportField::SUBJECTS, $mapping['Matières']);
|
||||
self::assertSame(TeacherImportField::CLASSES, $mapping['Classes']);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function suggestHandlesEnglishColumnNames(): void
|
||||
{
|
||||
$columns = ['Last Name', 'First Name', 'Email', 'Subject', 'Class'];
|
||||
|
||||
$mapping = $this->suggester->suggerer($columns, KnownImportFormat::CUSTOM);
|
||||
|
||||
self::assertSame(TeacherImportField::LAST_NAME, $mapping['Last Name']);
|
||||
self::assertSame(TeacherImportField::FIRST_NAME, $mapping['First Name']);
|
||||
self::assertSame(TeacherImportField::EMAIL, $mapping['Email']);
|
||||
self::assertSame(TeacherImportField::SUBJECTS, $mapping['Subject']);
|
||||
self::assertSame(TeacherImportField::CLASSES, $mapping['Class']);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function suggestNormalizesAccentsAndCase(): void
|
||||
{
|
||||
$columns = ['NOM', 'PRÉNOM', 'EMAIL', 'MATIÈRES', 'CLASSES'];
|
||||
|
||||
$mapping = $this->suggester->suggerer($columns, KnownImportFormat::CUSTOM);
|
||||
|
||||
self::assertSame(TeacherImportField::LAST_NAME, $mapping['NOM']);
|
||||
self::assertSame(TeacherImportField::FIRST_NAME, $mapping['PRÉNOM']);
|
||||
self::assertSame(TeacherImportField::EMAIL, $mapping['EMAIL']);
|
||||
self::assertSame(TeacherImportField::SUBJECTS, $mapping['MATIÈRES']);
|
||||
self::assertSame(TeacherImportField::CLASSES, $mapping['CLASSES']);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function suggestDoesNotDuplicateFields(): void
|
||||
{
|
||||
$columns = ['Nom', 'Nom de famille', 'Prénom', 'Email'];
|
||||
|
||||
$mapping = $this->suggester->suggerer($columns, KnownImportFormat::CUSTOM);
|
||||
|
||||
$mappedFields = array_values($mapping);
|
||||
$uniqueFields = array_unique($mappedFields, SORT_REGULAR);
|
||||
|
||||
self::assertCount(count($uniqueFields), $mappedFields);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function suggestHandlesUnknownColumns(): void
|
||||
{
|
||||
$columns = ['ColonneInconnue', 'AutreColonne', 'Nom', 'Email'];
|
||||
|
||||
$mapping = $this->suggester->suggerer($columns, KnownImportFormat::CUSTOM);
|
||||
|
||||
self::assertArrayNotHasKey('ColonneInconnue', $mapping);
|
||||
self::assertArrayNotHasKey('AutreColonne', $mapping);
|
||||
self::assertArrayHasKey('Nom', $mapping);
|
||||
self::assertArrayHasKey('Email', $mapping);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function suggestHandsDisciplineKeyword(): void
|
||||
{
|
||||
$columns = ['Nom', 'Prénom', 'Courriel', 'Discipline'];
|
||||
|
||||
$mapping = $this->suggester->suggerer($columns, KnownImportFormat::CUSTOM);
|
||||
|
||||
self::assertSame(TeacherImportField::EMAIL, $mapping['Courriel']);
|
||||
self::assertSame(TeacherImportField::SUBJECTS, $mapping['Discipline']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Administration\Application\Service\Import;
|
||||
|
||||
use App\Administration\Application\Service\Import\TeacherDuplicateDetector;
|
||||
use App\Administration\Domain\Model\Import\ImportRow;
|
||||
use App\Administration\Domain\Model\Import\ImportRowError;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class TeacherDuplicateDetectorTest extends TestCase
|
||||
{
|
||||
private TeacherDuplicateDetector $detector;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->detector = new TeacherDuplicateDetector();
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function detectsDuplicateByEmail(): void
|
||||
{
|
||||
$existing = [
|
||||
$this->teacher('Jean', 'Dupont', 'jean@example.com'),
|
||||
];
|
||||
$rows = [
|
||||
$this->createRow(['lastName' => 'Dupont', 'firstName' => 'Jean', 'email' => 'jean@example.com']),
|
||||
];
|
||||
|
||||
$result = $this->detector->detecter($rows, $existing);
|
||||
|
||||
self::assertFalse($result[0]->estValide());
|
||||
self::assertSame('_duplicate', $result[0]->errors[0]->column);
|
||||
self::assertStringContainsString('email', $result[0]->errors[0]->message);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function detectsDuplicateByEmailCaseInsensitive(): void
|
||||
{
|
||||
$existing = [
|
||||
$this->teacher('Jean', 'Dupont', 'Jean.Dupont@Example.COM'),
|
||||
];
|
||||
$rows = [
|
||||
$this->createRow(['lastName' => 'Dupont', 'firstName' => 'Jean', 'email' => 'jean.dupont@example.com']),
|
||||
];
|
||||
|
||||
$result = $this->detector->detecter($rows, $existing);
|
||||
|
||||
self::assertFalse($result[0]->estValide());
|
||||
self::assertStringContainsString('email', $result[0]->errors[0]->message);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function detectsIntraFileDuplicateByEmail(): void
|
||||
{
|
||||
$existing = [];
|
||||
$rows = [
|
||||
$this->createRow(['lastName' => 'Dupont', 'firstName' => 'Jean', 'email' => 'jean@example.com'], 1),
|
||||
$this->createRow(['lastName' => 'Martin', 'firstName' => 'Pierre', 'email' => 'jean@example.com'], 2),
|
||||
];
|
||||
|
||||
$result = $this->detector->detecter($rows, $existing);
|
||||
|
||||
self::assertTrue($result[0]->estValide());
|
||||
self::assertFalse($result[1]->estValide());
|
||||
self::assertSame('_duplicate', $result[1]->errors[0]->column);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function doesNotFlagDifferentEmails(): void
|
||||
{
|
||||
$existing = [
|
||||
$this->teacher('Jean', 'Dupont', 'jean@example.com'),
|
||||
];
|
||||
$rows = [
|
||||
$this->createRow(['lastName' => 'Dupont', 'firstName' => 'Jean', 'email' => 'jean.d@other.com']),
|
||||
];
|
||||
|
||||
$result = $this->detector->detecter($rows, $existing);
|
||||
|
||||
self::assertTrue($result[0]->estValide());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function preservesExistingValidationErrors(): void
|
||||
{
|
||||
$existing = [
|
||||
$this->teacher('Jean', 'Dupont', 'jean@example.com'),
|
||||
];
|
||||
|
||||
$row = new ImportRow(
|
||||
lineNumber: 1,
|
||||
rawData: ['lastName' => 'Dupont', 'firstName' => 'Jean', 'email' => 'jean@example.com'],
|
||||
mappedData: ['lastName' => 'Dupont', 'firstName' => 'Jean', 'email' => 'jean@example.com'],
|
||||
errors: [new ImportRowError('subjects', 'Matière inexistante.')],
|
||||
);
|
||||
|
||||
$result = $this->detector->detecter([$row], $existing);
|
||||
|
||||
self::assertCount(2, $result[0]->errors);
|
||||
self::assertSame('subjects', $result[0]->errors[0]->column);
|
||||
self::assertSame('_duplicate', $result[0]->errors[1]->column);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function multipleRowsMixedDuplicatesAndValid(): void
|
||||
{
|
||||
$existing = [
|
||||
$this->teacher('Jean', 'Dupont', 'jean@example.com'),
|
||||
];
|
||||
$rows = [
|
||||
$this->createRow(['lastName' => 'Dupont', 'firstName' => 'Jean', 'email' => 'jean@example.com'], 1),
|
||||
$this->createRow(['lastName' => 'Martin', 'firstName' => 'Pierre', 'email' => 'pierre@example.com'], 2),
|
||||
$this->createRow(['lastName' => 'Bernard', 'firstName' => 'Claire', 'email' => 'claire@example.com'], 3),
|
||||
];
|
||||
|
||||
$result = $this->detector->detecter($rows, $existing);
|
||||
|
||||
self::assertFalse($result[0]->estValide());
|
||||
self::assertTrue($result[1]->estValide());
|
||||
self::assertTrue($result[2]->estValide());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, string> $mappedData
|
||||
*/
|
||||
private function createRow(array $mappedData, int $lineNumber = 1): ImportRow
|
||||
{
|
||||
return new ImportRow(
|
||||
lineNumber: $lineNumber,
|
||||
rawData: $mappedData,
|
||||
mappedData: $mappedData,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{firstName: string, lastName: string, email: string}
|
||||
*/
|
||||
private function teacher(string $firstName, string $lastName, string $email): array
|
||||
{
|
||||
return [
|
||||
'firstName' => $firstName,
|
||||
'lastName' => $lastName,
|
||||
'email' => $email,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,198 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Administration\Application\Service\Import;
|
||||
|
||||
use App\Administration\Application\Service\Import\CsvParser;
|
||||
use App\Administration\Application\Service\Import\ImportFormatDetector;
|
||||
use App\Administration\Application\Service\Import\ImportReport;
|
||||
use App\Administration\Application\Service\Import\TeacherColumnMappingSuggester;
|
||||
use App\Administration\Application\Service\Import\TeacherImportRowValidator;
|
||||
use App\Administration\Domain\Model\Import\ImportRow;
|
||||
use App\Administration\Domain\Model\Import\KnownImportFormat;
|
||||
use App\Administration\Domain\Model\Import\TeacherImportField;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Test d'intégration de la chaîne complète d'import enseignants avec de vrais fichiers CSV.
|
||||
*
|
||||
* Parse → Détection format → Mapping → Validation → Rapport
|
||||
*/
|
||||
final class TeacherImportIntegrationTest extends TestCase
|
||||
{
|
||||
#[Test]
|
||||
public function fullTeacherImportPipeline(): void
|
||||
{
|
||||
$filePath = $this->fixture('enseignants_simple.csv');
|
||||
|
||||
// 1. Parser le fichier
|
||||
$parser = new CsvParser();
|
||||
$parseResult = $parser->parse($filePath);
|
||||
|
||||
self::assertSame(3, $parseResult->totalRows());
|
||||
self::assertSame(['Nom', 'Prénom', 'Email', 'Matières', 'Classes'], $parseResult->columns);
|
||||
|
||||
// 2. Détecter le format
|
||||
$detector = new ImportFormatDetector();
|
||||
$format = $detector->detecter($parseResult->columns);
|
||||
|
||||
self::assertSame(KnownImportFormat::CUSTOM, $format);
|
||||
|
||||
// 3. Suggérer le mapping
|
||||
$suggester = new TeacherColumnMappingSuggester();
|
||||
$suggestedMapping = $suggester->suggerer($parseResult->columns, $format);
|
||||
|
||||
self::assertSame(TeacherImportField::LAST_NAME, $suggestedMapping['Nom']);
|
||||
self::assertSame(TeacherImportField::FIRST_NAME, $suggestedMapping['Prénom']);
|
||||
self::assertSame(TeacherImportField::EMAIL, $suggestedMapping['Email']);
|
||||
self::assertSame(TeacherImportField::SUBJECTS, $suggestedMapping['Matières']);
|
||||
self::assertSame(TeacherImportField::CLASSES, $suggestedMapping['Classes']);
|
||||
|
||||
// 4. Appliquer le mapping sur les lignes
|
||||
$rows = [];
|
||||
$lineNumber = 1;
|
||||
|
||||
foreach ($parseResult->rows as $rawData) {
|
||||
$mappedData = [];
|
||||
foreach ($suggestedMapping as $column => $field) {
|
||||
$mappedData[$field->value] = $rawData[$column] ?? '';
|
||||
}
|
||||
$rows[] = new ImportRow($lineNumber, $rawData, $mappedData);
|
||||
++$lineNumber;
|
||||
}
|
||||
|
||||
self::assertCount(3, $rows);
|
||||
self::assertSame('Dupont', $rows[0]->mappedData[TeacherImportField::LAST_NAME->value]);
|
||||
self::assertSame('Mathématiques', $rows[0]->mappedData[TeacherImportField::SUBJECTS->value]);
|
||||
self::assertSame('6A, 6B', $rows[0]->mappedData[TeacherImportField::CLASSES->value]);
|
||||
|
||||
// 5. Valider les lignes
|
||||
$validator = new TeacherImportRowValidator();
|
||||
$validatedRows = $validator->validerTout($rows);
|
||||
|
||||
foreach ($validatedRows as $row) {
|
||||
self::assertTrue($row->estValide(), "Ligne {$row->lineNumber} devrait être valide");
|
||||
}
|
||||
|
||||
// 6. Générer le rapport
|
||||
$report = ImportReport::fromValidatedRows($validatedRows);
|
||||
|
||||
self::assertSame(3, $report->totalRows);
|
||||
self::assertSame(3, $report->importedCount);
|
||||
self::assertSame(0, $report->errorCount);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function teacherImportWithInvalidRows(): void
|
||||
{
|
||||
$filePath = $this->fixture('enseignants_complet.csv');
|
||||
|
||||
$parser = new CsvParser();
|
||||
$parseResult = $parser->parse($filePath);
|
||||
|
||||
self::assertSame(8, $parseResult->totalRows());
|
||||
self::assertContains('Téléphone', $parseResult->columns);
|
||||
|
||||
$detector = new ImportFormatDetector();
|
||||
$format = $detector->detecter($parseResult->columns);
|
||||
|
||||
$suggester = new TeacherColumnMappingSuggester();
|
||||
$suggestedMapping = $suggester->suggerer($parseResult->columns, $format);
|
||||
|
||||
// La colonne Téléphone ne doit pas être mappée
|
||||
self::assertArrayNotHasKey('Téléphone', $suggestedMapping);
|
||||
|
||||
$rows = [];
|
||||
$lineNumber = 1;
|
||||
|
||||
foreach ($parseResult->rows as $rawData) {
|
||||
$mappedData = [];
|
||||
foreach ($suggestedMapping as $column => $field) {
|
||||
$mappedData[$field->value] = $rawData[$column] ?? '';
|
||||
}
|
||||
$rows[] = new ImportRow($lineNumber, $rawData, $mappedData);
|
||||
++$lineNumber;
|
||||
}
|
||||
|
||||
$validator = new TeacherImportRowValidator();
|
||||
$validatedRows = $validator->validerTout($rows);
|
||||
|
||||
$report = ImportReport::fromValidatedRows($validatedRows);
|
||||
|
||||
// Moreau (ligne 5) : email manquant → erreur
|
||||
// Petit (ligne 6) : email invalide → erreur
|
||||
self::assertSame(8, $report->totalRows);
|
||||
self::assertSame(6, $report->importedCount);
|
||||
self::assertSame(2, $report->errorCount);
|
||||
|
||||
// Vérifie les lignes en erreur
|
||||
$errorLines = array_map(
|
||||
static fn (ImportRow $row) => $row->lineNumber,
|
||||
$report->errorRows,
|
||||
);
|
||||
self::assertContains(5, $errorLines);
|
||||
self::assertContains(6, $errorLines);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function teacherImportCsvCommaFormat(): void
|
||||
{
|
||||
$filePath = $this->fixture('enseignants_comma.csv');
|
||||
|
||||
$parser = new CsvParser();
|
||||
$parseResult = $parser->parse($filePath);
|
||||
|
||||
self::assertSame(2, $parseResult->totalRows());
|
||||
self::assertSame(['Nom', 'Prénom', 'Email'], $parseResult->columns);
|
||||
|
||||
$suggester = new TeacherColumnMappingSuggester();
|
||||
$suggestedMapping = $suggester->suggerer($parseResult->columns, KnownImportFormat::CUSTOM);
|
||||
|
||||
// Pas de colonnes SUBJECTS ni CLASSES
|
||||
self::assertCount(3, $suggestedMapping);
|
||||
self::assertSame(TeacherImportField::LAST_NAME, $suggestedMapping['Nom']);
|
||||
self::assertSame(TeacherImportField::FIRST_NAME, $suggestedMapping['Prénom']);
|
||||
self::assertSame(TeacherImportField::EMAIL, $suggestedMapping['Email']);
|
||||
|
||||
$rows = [];
|
||||
$lineNumber = 1;
|
||||
|
||||
foreach ($parseResult->rows as $rawData) {
|
||||
$mappedData = [];
|
||||
foreach ($suggestedMapping as $column => $field) {
|
||||
$mappedData[$field->value] = $rawData[$column] ?? '';
|
||||
}
|
||||
$rows[] = new ImportRow($lineNumber, $rawData, $mappedData);
|
||||
++$lineNumber;
|
||||
}
|
||||
|
||||
$validator = new TeacherImportRowValidator();
|
||||
$validatedRows = $validator->validerTout($rows);
|
||||
|
||||
$report = ImportReport::fromValidatedRows($validatedRows);
|
||||
|
||||
self::assertSame(2, $report->totalRows);
|
||||
self::assertSame(2, $report->importedCount);
|
||||
self::assertSame(0, $report->errorCount);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function multiValueSubjectsWithPipeSeparator(): void
|
||||
{
|
||||
$filePath = $this->fixture('enseignants_complet.csv');
|
||||
|
||||
$parser = new CsvParser();
|
||||
$parseResult = $parser->parse($filePath);
|
||||
|
||||
// Ligne 3 : Bernard;Pierre;...;Physique | Chimie;4A
|
||||
$bernardRow = $parseResult->rows[2];
|
||||
self::assertSame('Physique | Chimie', $bernardRow['Matières']);
|
||||
}
|
||||
|
||||
private function fixture(string $filename): string
|
||||
{
|
||||
return __DIR__ . '/../../../../../fixtures/import/' . $filename;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,231 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Administration\Application\Service\Import;
|
||||
|
||||
use App\Administration\Application\Service\Import\TeacherImportRowValidator;
|
||||
use App\Administration\Domain\Model\Import\ImportRow;
|
||||
use App\Administration\Domain\Model\Import\TeacherImportField;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class TeacherImportRowValidatorTest extends TestCase
|
||||
{
|
||||
#[Test]
|
||||
public function validRowPassesValidation(): void
|
||||
{
|
||||
$validator = new TeacherImportRowValidator();
|
||||
|
||||
$row = $this->createRow([
|
||||
TeacherImportField::LAST_NAME->value => 'Dupont',
|
||||
TeacherImportField::FIRST_NAME->value => 'Jean',
|
||||
TeacherImportField::EMAIL->value => 'jean.dupont@ecole.fr',
|
||||
]);
|
||||
|
||||
$validated = $validator->valider($row);
|
||||
|
||||
self::assertTrue($validated->estValide());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function missingLastNameCreatesError(): void
|
||||
{
|
||||
$validator = new TeacherImportRowValidator();
|
||||
|
||||
$row = $this->createRow([
|
||||
TeacherImportField::LAST_NAME->value => '',
|
||||
TeacherImportField::FIRST_NAME->value => 'Jean',
|
||||
TeacherImportField::EMAIL->value => 'jean@ecole.fr',
|
||||
]);
|
||||
|
||||
$validated = $validator->valider($row);
|
||||
|
||||
self::assertFalse($validated->estValide());
|
||||
self::assertCount(1, $validated->errors);
|
||||
self::assertSame('lastName', $validated->errors[0]->column);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function missingFirstNameCreatesError(): void
|
||||
{
|
||||
$validator = new TeacherImportRowValidator();
|
||||
|
||||
$row = $this->createRow([
|
||||
TeacherImportField::LAST_NAME->value => 'Dupont',
|
||||
TeacherImportField::FIRST_NAME->value => '',
|
||||
TeacherImportField::EMAIL->value => 'jean@ecole.fr',
|
||||
]);
|
||||
|
||||
$validated = $validator->valider($row);
|
||||
|
||||
self::assertFalse($validated->estValide());
|
||||
self::assertCount(1, $validated->errors);
|
||||
self::assertSame('firstName', $validated->errors[0]->column);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function missingEmailCreatesError(): void
|
||||
{
|
||||
$validator = new TeacherImportRowValidator();
|
||||
|
||||
$row = $this->createRow([
|
||||
TeacherImportField::LAST_NAME->value => 'Dupont',
|
||||
TeacherImportField::FIRST_NAME->value => 'Jean',
|
||||
TeacherImportField::EMAIL->value => '',
|
||||
]);
|
||||
|
||||
$validated = $validator->valider($row);
|
||||
|
||||
self::assertFalse($validated->estValide());
|
||||
self::assertCount(1, $validated->errors);
|
||||
self::assertSame('email', $validated->errors[0]->column);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function invalidEmailCreatesError(): void
|
||||
{
|
||||
$validator = new TeacherImportRowValidator();
|
||||
|
||||
$row = $this->createRow([
|
||||
TeacherImportField::LAST_NAME->value => 'Dupont',
|
||||
TeacherImportField::FIRST_NAME->value => 'Jean',
|
||||
TeacherImportField::EMAIL->value => 'not-an-email',
|
||||
]);
|
||||
|
||||
$validated = $validator->valider($row);
|
||||
|
||||
self::assertFalse($validated->estValide());
|
||||
self::assertSame('email', $validated->errors[0]->column);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function unknownSubjectCreatesErrorWhenExistingSubjectsProvided(): void
|
||||
{
|
||||
$validator = new TeacherImportRowValidator(
|
||||
existingSubjectNames: ['Mathématiques', 'Physique'],
|
||||
);
|
||||
|
||||
$row = $this->createRow([
|
||||
TeacherImportField::LAST_NAME->value => 'Dupont',
|
||||
TeacherImportField::FIRST_NAME->value => 'Jean',
|
||||
TeacherImportField::EMAIL->value => 'jean@ecole.fr',
|
||||
TeacherImportField::SUBJECTS->value => 'Mathématiques, Chimie',
|
||||
]);
|
||||
|
||||
$validated = $validator->valider($row);
|
||||
|
||||
self::assertFalse($validated->estValide());
|
||||
self::assertSame('subjects', $validated->errors[0]->column);
|
||||
self::assertStringContainsString('Chimie', $validated->errors[0]->message);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function knownSubjectsPassValidation(): void
|
||||
{
|
||||
$validator = new TeacherImportRowValidator(
|
||||
existingSubjectNames: ['Mathématiques', 'Physique'],
|
||||
);
|
||||
|
||||
$row = $this->createRow([
|
||||
TeacherImportField::LAST_NAME->value => 'Dupont',
|
||||
TeacherImportField::FIRST_NAME->value => 'Jean',
|
||||
TeacherImportField::EMAIL->value => 'jean@ecole.fr',
|
||||
TeacherImportField::SUBJECTS->value => 'Mathématiques, Physique',
|
||||
]);
|
||||
|
||||
$validated = $validator->valider($row);
|
||||
|
||||
self::assertTrue($validated->estValide());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function unknownClassCreatesErrorWhenExistingClassesProvided(): void
|
||||
{
|
||||
$validator = new TeacherImportRowValidator(
|
||||
existingClassNames: ['6A', '6B', '5A'],
|
||||
);
|
||||
|
||||
$row = $this->createRow([
|
||||
TeacherImportField::LAST_NAME->value => 'Dupont',
|
||||
TeacherImportField::FIRST_NAME->value => 'Jean',
|
||||
TeacherImportField::EMAIL->value => 'jean@ecole.fr',
|
||||
TeacherImportField::CLASSES->value => '6A, 4C',
|
||||
]);
|
||||
|
||||
$validated = $validator->valider($row);
|
||||
|
||||
self::assertFalse($validated->estValide());
|
||||
self::assertSame('classes', $validated->errors[0]->column);
|
||||
self::assertStringContainsString('4C', $validated->errors[0]->message);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function emptySubjectsPassValidation(): void
|
||||
{
|
||||
$validator = new TeacherImportRowValidator(
|
||||
existingSubjectNames: ['Mathématiques'],
|
||||
);
|
||||
|
||||
$row = $this->createRow([
|
||||
TeacherImportField::LAST_NAME->value => 'Dupont',
|
||||
TeacherImportField::FIRST_NAME->value => 'Jean',
|
||||
TeacherImportField::EMAIL->value => 'jean@ecole.fr',
|
||||
TeacherImportField::SUBJECTS->value => '',
|
||||
]);
|
||||
|
||||
$validated = $validator->valider($row);
|
||||
|
||||
self::assertTrue($validated->estValide());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function validerToutValidatesAllRows(): void
|
||||
{
|
||||
$validator = new TeacherImportRowValidator();
|
||||
|
||||
$rows = [
|
||||
$this->createRow([
|
||||
TeacherImportField::LAST_NAME->value => 'Dupont',
|
||||
TeacherImportField::FIRST_NAME->value => 'Jean',
|
||||
TeacherImportField::EMAIL->value => 'jean@ecole.fr',
|
||||
]),
|
||||
$this->createRow([
|
||||
TeacherImportField::LAST_NAME->value => '',
|
||||
TeacherImportField::FIRST_NAME->value => 'Marie',
|
||||
TeacherImportField::EMAIL->value => 'marie@ecole.fr',
|
||||
]),
|
||||
];
|
||||
|
||||
$validated = $validator->validerTout($rows);
|
||||
|
||||
self::assertCount(2, $validated);
|
||||
self::assertTrue($validated[0]->estValide());
|
||||
self::assertFalse($validated[1]->estValide());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function subjectsNotCheckedWhenNoExistingSubjectsProvided(): void
|
||||
{
|
||||
$validator = new TeacherImportRowValidator();
|
||||
|
||||
$row = $this->createRow([
|
||||
TeacherImportField::LAST_NAME->value => 'Dupont',
|
||||
TeacherImportField::FIRST_NAME->value => 'Jean',
|
||||
TeacherImportField::EMAIL->value => 'jean@ecole.fr',
|
||||
TeacherImportField::SUBJECTS->value => 'N\'importe quoi',
|
||||
]);
|
||||
|
||||
$validated = $validator->valider($row);
|
||||
|
||||
self::assertTrue($validated->estValide());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, string> $mappedData
|
||||
*/
|
||||
private function createRow(array $mappedData, int $line = 1): ImportRow
|
||||
{
|
||||
return new ImportRow($line, $mappedData, $mappedData);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user