open($p,ZipArchive::OVERWRITE);
$ns='http://schemas.openxmlformats.org/spreadsheetml/2006/main';
$z->addFromString('xl/workbook.xml','');
$z->addFromString('xl/_rels/workbook.xml.rels','');
$z->addFromString('xl/worksheets/sheet.xml',''.$rows.'');
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 ''.htmlspecialchars($v,ENT_XML1,'UTF-8').'';}
$styles='';
$heads=''.textCell('A1','Date').textCell('B1','Heure').textCell('C1','Licence').textCell('D1','Nom').'';
$row='452920.84375123'.textCell('D2','Éric & André').'';
$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('45292','NOW()45292',$row),$styles);try{rejects(fn()=>Xlsx::read($p),'Formule non exécutée et refusée');}finally{unlink($p);}
$p=fixture(''.textCell('A1','Titre du rapport').''.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(''.textCell('A1','Nom').textCell('B1','Nom').'');try{rejects(fn()=>Xlsx::read($p),'En-têtes dupliqués rejetés');}finally{unlink($p);}
$p=fixture(''.textCell('A1','Nom').'0','',false, 'Éric Dupont');
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";