test(commercial) : cover RG-1.01..1.29 except role-gated (M1) + polish stack (#38)
Auto Tag Develop / tag (push) Successful in 7s
Auto Tag Develop / tag (push) Successful in 7s
Dernier wagon de la stack back M1. ERP-60 = polish stack + couverture de tests PHPUnit NON dépendante des rôles métier (cf. spec § 7 / § 8.1). ## Phase 0 — polish stack (déjà mergé dans les branches basses via rebase) - ERP-59 : route sidebar `/clients` (au lieu de `/commercial/clients`), cohérente avec `/suppliers`. - One-liner pagination Client abandonné : `pagination_client_enabled: true` est déjà le défaut global → `?pagination=false` marche déjà sur `/api/clients` (décision P7). ## Phase 1 — tests (combler les trous, zéro duplication) 8 nouvelles suites couvrant les RG non encore testées par ERP-55/56/57/58 : - `ClientFormulaireMainTest` — RG-1.02 (téléphone secondaire, max 2). - `ClientAddressTest` — RG-1.06/07/08 + RG-1.11 (CHECK BDD prospect/billing). - `ClientUniquenessTest` — RG-1.15/1.17 (Q4 : SIREN/email NON uniques). - `ClientArchiveTest` — **RG-1.23 : 409 restauration en conflit (gap P1)**. - `ClientAuditTest` — RG-1.27 (created* figés / updatedBy modificateur) + iban/bic présents dans le diff audité. - `ClientMigrationTest` — index partiel unique `uq_client_company_name_active` (1 seul) ; pas d'index siren/email. - `ClientSecurityTest` — 401 anonyme + 403 sans `commercial.clients.view`. - `ClientPatchStrictTest` — RG-1.28 (403 strict mix de groupes, fonctionnel). Cahier de test complet (mapping de TOUTES les RG → test) : `docs/specs/M1-clients/cahier-test-back-M1.md`. ## Délégué à ERP-74 (#493) Matrice RBAC différenciée (bureau/compta/commerciale/usine) + RG-1.04 fonctionnel — exigent les rôles métier seedés après le merge de la stack. ## Gaps documentés (cahier) - RG-1.29 validation écriture (catégorie type sur adresse → 422) non implémentée back (hors § 8.1, ticket test-only). - Violations CHECK adresse → rejet (≥400) sans mapping fin 422 (amélioration possible). ## Vérifs `make db-reset && make php-cs-fixer-allow-risky && make test` → **421 tests OK, 1386 assertions, 0 risky**. Nouveaux tests : 17, 71 assertions. --------- Co-authored-by: Matthieu <contact@malio.fr> Reviewed-on: #38 Co-authored-by: THOLOT DECHENE Matthieu <matthieu@yuno.malio.fr> Co-committed-by: THOLOT DECHENE Matthieu <matthieu@yuno.malio.fr>
This commit was merged in pull request #38.
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Shared\Infrastructure\Export;
|
||||
|
||||
use App\Shared\Domain\Contract\SpreadsheetExporterInterface;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* Implementation XLSX du contrat d'export via la librairie PhpSpreadsheet.
|
||||
*
|
||||
* Strictement technique : ecrit la ligne d'en-tete puis les lignes de donnees
|
||||
* dans l'unique feuille du classeur, et retourne le binaire. Aucune logique
|
||||
* metier, aucune reference a une entite d'un module — le mapping des colonnes
|
||||
* est de la responsabilite de l'appelant.
|
||||
*/
|
||||
final class PhpSpreadsheetExporter implements SpreadsheetExporterInterface
|
||||
{
|
||||
// Excel limite le titre d'un onglet a 31 caracteres et interdit certains
|
||||
// caracteres ; on assainit pour ne jamais faire echouer setTitle().
|
||||
private const int MAX_SHEET_TITLE_LENGTH = 31;
|
||||
private const string INVALID_TITLE_CHARS = '*:/\?[]';
|
||||
|
||||
public function export(string $sheetTitle, array $headers, iterable $rows): string
|
||||
{
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
$sheet->setTitle($this->sanitizeSheetTitle($sheetTitle));
|
||||
|
||||
// Ligne 1 : en-tete.
|
||||
$sheet->fromArray($headers, null, 'A1');
|
||||
|
||||
// Lignes 2..n : donnees. Iteration manuelle pour supporter un iterable
|
||||
// paresseux (generator) sans tout materialiser en memoire.
|
||||
$rowNumber = 2;
|
||||
foreach ($rows as $row) {
|
||||
$sheet->fromArray($row, null, 'A'.$rowNumber);
|
||||
++$rowNumber;
|
||||
}
|
||||
|
||||
return $this->toBinary($spreadsheet);
|
||||
}
|
||||
|
||||
private function toBinary(Spreadsheet $spreadsheet): string
|
||||
{
|
||||
$writer = new Xlsx($spreadsheet);
|
||||
|
||||
// Le writer ecrit vers un chemin de fichier : on passe par un fichier
|
||||
// temporaire puis on lit son contenu binaire.
|
||||
$tmpFile = tempnam(sys_get_temp_dir(), 'xlsx_export_');
|
||||
if (false === $tmpFile) {
|
||||
throw new RuntimeException('Impossible de creer un fichier temporaire pour l\'export XLSX.');
|
||||
}
|
||||
|
||||
try {
|
||||
$writer->save($tmpFile);
|
||||
$binary = file_get_contents($tmpFile);
|
||||
if (false === $binary) {
|
||||
throw new RuntimeException('Lecture du fichier XLSX temporaire impossible.');
|
||||
}
|
||||
|
||||
return $binary;
|
||||
} finally {
|
||||
// Libere les references internes de PhpSpreadsheet puis supprime le
|
||||
// fichier temporaire, meme en cas d'exception.
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
@unlink($tmpFile);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retire les caracteres interdits et tronque a 31 caracteres ; renvoie un
|
||||
* titre par defaut si la chaine resultante est vide.
|
||||
*/
|
||||
private function sanitizeSheetTitle(string $title): string
|
||||
{
|
||||
$clean = str_replace(str_split(self::INVALID_TITLE_CHARS), '', $title);
|
||||
$clean = mb_substr($clean, 0, self::MAX_SHEET_TITLE_LENGTH);
|
||||
|
||||
return '' === $clean ? 'Export' : $clean;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user