<?php
declare(strict_types=1);
namespace think { class Config { public static $driver='mysql'; public static function get($key){return self::$driver;} } }
namespace {
$root=$argv[1];require $root.'/application/common/service/DishFixedFloatWrite.php';require $root.'/application/common/service/DecimalResponseFormatter.php';require $root.'/application/p/logic/Cater.php';
use app\common\service\DishFixedFloatWrite as H;
$n=0;$fails=[];
$check=static function($ok,$label)use(&$n,&$fails){$n++;if(!$ok)$fails[]=$label;};
$physical=unpack('g',pack('g',1.24))[1];
foreach(['price','ssb','saccharides','food_weight']as$field){
 foreach([false,true]as$dm){
  $row=[$field=>$physical,'sum_'.$field=>$physical,'computed_same_number'=>$physical,'decimal'=>'1.2400'];$out=H::physicalRow($row,[$field],$dm);
  $check($out[$field]===($dm?1.24:$physical),$field.'.physical.'.(int)$dm);
  $check($out['sum_'.$field]===$physical&&$out['computed_same_number']===$physical&&$out['decimal']==='1.2400',$field.'.computed_other_fields_unchanged.'.(int)$dm);
  foreach([null,'1.2400000095367432',1,0.0,1.2345,INF,-INF,[],false]as$v){$a=H::physicalRow([$field=>$v],[$field],$dm);$check($a[$field]===$v,$field.'.nonmatching_preserved.'.gettype($v).'.'.(int)$dm);}
 }
 $check(H::physicalRow([],[$field],true)===[],$field.'.no_new_fields');
}
$r=new \ReflectionClass(\app\p\logic\Cater::class);$o=$r->newInstanceWithoutConstructor();$m=$r->getMethod('normalizeDishReadRow');$m->setAccessible(true);
foreach(['mysql','other','app\\cores\\database\\Dm','\\app\\cores\\database\\Dm']as$driver){\think\Config::$driver=$driver;$dm=ltrim($driver,'\\')==='app\\cores\\database\\Dm';$row=['price'=>$physical,'ssb'=>$physical,'saccharides'=>$physical,'sum_price'=>$physical,'energy'=>'12.30'];$out=$m->invoke($o,$row);foreach(['price','ssb','saccharides']as$f)$check($out[$f]===($dm?1.24:$physical),'cater.driverguard.'.$driver.'.'.$f);$check($out['sum_price']===$physical,'cater.sum_untouched.'.$driver);$check($out['energy']==='12.30','cater.original_decimal_formatter.'.$driver);}
$source=file_get_contents($root.'/application/p/logic/Cater.php');$check(substr_count($source,'$this->normalizeDishReadRow(')===4,'four_actual_cater_calls');
$source=file_get_contents($root.'/application/model/Dish.php');$check(substr_count($source,'$this->normalizePhysicalWeights(')===2,'two_actual_dish_calls');$check(strpos($source,'$this->db(false)->getConnection() instanceof')!==false,'dish_actual_connection_guard');
$source=file_get_contents($root.'/application/model/Bill.php');$check(strpos($source,"physicalRow(\$row, ['price'], \$dm)")!==false,'bill_actual_physical_price_call');$check(strpos($source,'$dm = Db::connect() instanceof')!==false,'bill_actual_connection_guard');
echo json_encode(['assertions'=>$n,'failures'=>$fails,'SQL'=>0,'HTTP'=>0,'limitations'=>'Helper cannot distinguish SUM aliased price from an identical physical float32 value. Call sites must prove physical projection; this test only asserts computed aliases outside selected fields remain unchanged.']),"\n";exit($fails?1:0);
}
