55 lines
5.6 KiB
PHP
55 lines
5.6 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
require dirname(__DIR__).'/bootstrap.php';
|
|
use App\{Xlsx,FootclubsCalendar};
|
|
$n=0;
|
|
function ok(bool $condition,string $label): void {global $n;$n++;if(!$condition)throw new RuntimeException($label);echo "OK XLSX $n - $label\n";}
|
|
function rejects(callable $fn,string $label): void {try{$fn();}catch(RuntimeException $e){ok(true,$label);return;}ok(false,$label);}
|
|
/** Minimal wire-format fixtures, including malformed cases; never user deliverables. */
|
|
function fixture(string $rows,string $styles='',bool $epoch=false,string $shared=''): string {
|
|
$p=tempnam(sys_get_temp_dir(),'esbsc-xlsx-');$z=new ZipArchive();$z->open($p,ZipArchive::OVERWRITE);
|
|
$ns='http://schemas.openxmlformats.org/spreadsheetml/2006/main';
|
|
$z->addFromString('xl/workbook.xml','<x:workbook xmlns:x="'.$ns.'" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><x:workbookPr date1904="'.($epoch?'1':'0').'"/><x:sheets><x:sheet name="Données" sheetId="1" r:id="r1"/></x:sheets></x:workbook>');
|
|
$z->addFromString('xl/_rels/workbook.xml.rels','<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="r1" Target="/xl/worksheets/sheet.xml"/></Relationships>');
|
|
$z->addFromString('xl/worksheets/sheet.xml','<x:worksheet xmlns:x="'.$ns.'"><x:sheetData>'.$rows.'</x:sheetData></x:worksheet>');
|
|
if($styles!=='')$z->addFromString('xl/styles.xml',$styles);
|
|
if($shared!=='')$z->addFromString('xl/sharedStrings.xml',$shared);
|
|
$z->close();return $p;
|
|
}
|
|
function textCell(string $ref,string $v): string {return '<x:c r="'.$ref.'" t="inlineStr"><x:is><x:t>'.htmlspecialchars($v,ENT_XML1,'UTF-8').'</x:t></x:is></x:c>';}
|
|
$styles='<x:styleSheet xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><x:numFmts><x:numFmt numFmtId="164" formatCode="000000"/></x:numFmts><x:cellXfs><x:xf numFmtId="0"/><x:xf numFmtId="14"/><x:xf numFmtId="20"/><x:xf numFmtId="164"/></x:cellXfs></x:styleSheet>';
|
|
$heads='<x:row r="1">'.textCell('A1','Date').textCell('B1','Heure').textCell('C1','Licence').textCell('D1','Nom').'</x:row>';
|
|
$row='<x:row r="2"><x:c r="A2" s="1"><x:v>45292</x:v></x:c><x:c r="B2" s="2"><x:v>0.84375</x:v></x:c><x:c r="C2" s="3"><x:v>123</x:v></x:c>'.textCell('D2','Éric & André').'</x:row>';
|
|
$p=fixture($heads.$row,$styles);try{
|
|
$r=Xlsx::read($p);$d=$r['rows'][0]['data'];
|
|
ok($r['sheet']==='Données','Nom de feuille et XML avec préfixes');
|
|
ok($d['date']==='2024-01-01','Date numérique Excel 1900');
|
|
ok($d['heure']==='20:15:00','Heure numérique Excel');
|
|
ok($d['licence']==='000123','Zéros initiaux selon le format');
|
|
ok($d['nom']==='Éric & André','Accents et entités XML');
|
|
rejects(fn()=>Xlsx::read($p,'Inconnu'),'Onglet inconnu refusé');
|
|
rejects(fn()=>Xlsx::read($p,'',0),'Ligne des titres invalide refusée');
|
|
}finally{unlink($p);}
|
|
$p=fixture($heads.str_replace('45292','0',$row),$styles,true);try{ok(Xlsx::read($p)['rows'][0]['data']['date']==='1904-01-01','Calendrier Excel 1904');}finally{unlink($p);}
|
|
$p=fixture($heads.str_replace('45292','60',$row),$styles);try{rejects(fn()=>Xlsx::read($p),'Date Excel fictive de 1900 rejetée');}finally{unlink($p);}
|
|
$p=fixture($heads.str_replace('<x:v>45292</x:v>','<x:f>NOW()</x:f><x:v>45292</x:v>',$row),$styles);try{rejects(fn()=>Xlsx::read($p),'Formule non exécutée et refusée');}finally{unlink($p);}
|
|
$p=fixture('<x:row r="1">'.textCell('A1','Titre du rapport').'</x:row>'.str_replace('r="1"','r="3"',str_replace(['A1','B1','C1','D1'],['A3','B3','C3','D3'],$heads)).str_replace(['r="2"','A2','B2','C2','D2'],['r="4"','A4','B4','C4','D4'],$row),$styles);try{ok(Xlsx::read($p,'',3)['rows'][0]['line']===4,'Titres situés après des lignes introductives');}finally{unlink($p);}
|
|
$p=fixture('<x:row r="1">'.textCell('A1','Nom').textCell('B1','Nom').'</x:row>');try{rejects(fn()=>Xlsx::read($p),'En-têtes dupliqués rejetés');}finally{unlink($p);}
|
|
$p=fixture('<x:row r="1">'.textCell('A1','Nom').'</x:row><x:row r="2"><x:c r="A2" t="s"><x:v>0</x:v></x:c></x:row>','',false, '<x:sst xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><x:si><x:r><x:t>Éric</x:t></x:r><x:r><x:t> Dupont</x:t></x:r></x:si></x:sst>');
|
|
try{ok(Xlsx::read($p)['rows'][0]['data']['nom']==='Éric Dupont','Texte partagé avec plusieurs segments');}finally{unlink($p);}
|
|
$file=$argv[1]??getenv('FOOTCLUBS_CALENDAR_FILE');
|
|
if($file){
|
|
$rows=Xlsx::read($file)['rows'];ok(count($rows)===25,'Fichier transmis : 25 rencontres lues');
|
|
$normalized=array_map(fn($r)=>FootclubsCalendar::normalize($r['data'],'E1',true),$rows);
|
|
ok(count(array_filter($normalized,fn($r)=>$r['statut']==='played'))===3,'Trois résultats après confirmation explicite');
|
|
ok(count(array_filter($normalized,fn($r)=>$r['statut']==='scheduled'))===22,'Vingt-deux rencontres sans résultat restent prévues');
|
|
$byId=array_column($normalized,null,'numero_match');
|
|
ok($byId['56085584']['heure']==='20:15'&&$byId['56085584']['home_away']==='away','Périgny : 20H15 et extérieur');
|
|
ok($byId['79131186']['adversaire']==='BRESSUIRE FC 1'&&$byId['79131186']['home_away']==='home','Adversaire et domicile reconnus');
|
|
rejects(fn()=>FootclubsCalendar::normalize($rows[0]['data'],'E2',true),'Fichier E1 refusé si E2 sélectionnée');
|
|
rejects(fn()=>FootclubsCalendar::normalize($rows[0]['data'],'E1',false),'Un score seul ne prouve pas une purge');
|
|
$special=$rows[0]['data'];$special['reporte_rejoue']='Reporté';rejects(fn()=>FootclubsCalendar::normalize($special,'E1',true),'Report ambigu signalé');
|
|
$special=$rows[0]['data'];$special['date_du_match_2']='13/09/2026';rejects(fn()=>FootclubsCalendar::normalize($special,'E1',true),'Deux dates contradictoires signalées');
|
|
}
|
|
echo "$n tests Excel réussis.\n";
|