<?php
declare(strict_types=1);
$root=rtrim($argv[1],'/').'/';define('APP_PATH',$root.'application/');define('ROOT_PATH',$root);define('BUSINESS','');define('TP_ENV','local');
require $root.'thinkphp/base.php';\think\Loader::addNamespace('app',APP_PATH);
require $argv[2]??APP_PATH.'p/logic/SupplementMealOrder.php';
$logic=(new ReflectionClass(\app\p\logic\SupplementMealOrder::class))->newInstanceWithoutConstructor();$method=new ReflectionMethod($logic,'formatSettlementAmounts');$method->setAccessible(true);
$n=0;$failed=[];$check=static function($actual,$expected,$label)use(&$n,&$failed){$n++;if($actual!==$expected)$failed[]=$label;};
$money=['order_price','pay_price','discount_amount','management_fee','cash','subsidy','gift'];
$values=[[0,'0.00'],[0.0,'0.00'],[0.01,'0.01'],[1.2,'1.20'],[12345.67,'12345.67'],[99999999.99,'99999999.99'],['1.20','1.20'],['1.200','1.200'],['0','0'],['x','x'],[null,null],[false,false],[-1,-1],[100000000,100000000],[INF,INF],[NAN,NAN],[1.234,1.234]];
$times=[['2024-02-29 23:59:59.000000','2024-02-29 23:59:59'],['2001-02-03 04:05:06.0','2001-02-03 04:05:06'],['2024-02-29 23:59:59.123456','2024-02-29 23:59:59.123456'],['2023-02-29 23:59:59.000000','2023-02-29 23:59:59.000000'],['2001-02-03 24:05:06.000000','2001-02-03 24:05:06.000000'],['2001-02-03 04:60:06.000000','2001-02-03 04:60:06.000000'],['2001-02-03 04:05:60.000000','2001-02-03 04:05:60.000000'],['0999-02-03 04:05:06.000000','0999-02-03 04:05:06.000000'],['0000-00-00 00:00:00.000000','0000-00-00 00:00:00.000000'],['2001-02-03 04:05:06.0000000','2001-02-03 04:05:06.0000000'],['2001-02-03 04:05:06','2001-02-03 04:05:06'],['',''],[null,null],[0,0],[false,false]];
foreach(['mysql',\app\cores\database\Dm::class]as $type){\think\Config::set(['type'=>$type,'hostname'=>'unreachable.invalid','database'=>'TEST_ONLY','username'=>'','password'=>'','deploy'=>0],'database');\think\Db::clear();
 foreach($money as $field)foreach($values as $i=>[$input,$want]){if($type==='mysql')$want=$input;$base=['pay_price'=>'0.00','gift'=>'0.00','refund_amount'=>'0.00','cash_refund_amount'=>'0.00','subsidy_refund_amount'=>'0.00','gift_refund_amount'=>'0.00','other_decimal'=>2.3,'other_time'=>'2024-02-29 01:02:03.000000'];$base[$field]=$input;
 // Non-finite / nonnumeric pay_price/gift already reach original BCMath; test untouched new fields only there.
 if(in_array($field,['pay_price','gift'],true)&&(!is_numeric($input)||!is_finite((float)$input)))continue;
 $got=$method->invoke($logic,$base);if(is_float($want)&&is_nan($want))$check(is_float($got[$field])&&is_nan($got[$field]),true,$type.':nan '.$field);else $check($got[$field],$want,$type.':money '.$field.' '.$i);$check($got['other_decimal'],2.3,'Other decimal');$check($got['other_time'],$base['other_time'],'Other time');
 }
 foreach(['consume_time','create_time','update_time']as $field)foreach($times as $i=>[$input,$want]){$base=['pay_price'=>'0.00','gift'=>'0.00',$field=>$input];$got=$method->invoke($logic,$base);$check($got[$field],$type==='mysql'?$input:$want,$type.':date '.$field.' '.$i);}
 $got=$method->invoke($logic,['pay_price'=>'0.00','gift'=>'0.00']);foreach(['order_price','discount_amount','management_fee','cash','subsidy','consume_time','create_time','update_time']as $absent)$check(array_key_exists($absent,$got),false,$type.':no added key '.$absent);
}
$check(\think\Db::$queryTimes,0,'No framework query');$check(\think\Db::$executeTimes,0,'No framework execute');
$report=['passed'=>$failed===[],'assertions'=>$n,'failures'=>$failed,'db_queries'=>0,'db_executes'=>0,'logic_sha256'=>hash_file('sha256',$argv[2]??APP_PATH.'p/logic/SupplementMealOrder.php')];
if(isset($argv[3])&&in_array($argv[3],['red','candidate','main'],true))file_put_contents(__DIR__.'/contract-'.$argv[3].'.json',json_encode($report,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE));
echo json_encode($report,JSON_UNESCAPED_UNICODE),"\n";exit($failed===[]?0:2);
