<?php
declare(strict_types=1);
namespace think { class Exception extends \RuntimeException {} }
namespace app\p\logic {
    function getCommonSign($param, $key, $a, $b) { $GLOBALS['zhipan_signature_key']=$key; return 'synthetic-signature'; }
    function writeLog($value) {}
    function http_post($param, $url) { $GLOBALS['zhipan_request']=[$param,$url]; return '{"code":1}'; }
}
namespace {
    require ($argv[1]??dirname(__DIR__,2)).'/application/p/logic/Bill.php';
    set_error_handler(static function($n,$s,$f,$l){throw new \ErrorException($s,0,$n,$f,$l);});
    $fixtures=[
      'mysql'=>['zpTerm'=>'term','zpKey'=>'key','zpApiUrl'=>'http://synthetic.invalid/'],
      'dm'=>['zpterm'=>'term','zpkey'=>'key','zpapiurl'=>'http://synthetic.invalid/'],
      'both'=>['zpTerm'=>'term','zpKey'=>'key','zpApiUrl'=>'http://synthetic.invalid/','zpterm'=>'wrong','zpkey'=>'wrong','zpapiurl'=>'http://wrong.invalid/']
    ];
    $fail=[];$checks=0;
    foreach($fixtures as $name=>$row){
      $r=new \ReflectionClass(\app\p\logic\Bill::class);$o=$r->newInstanceWithoutConstructor();
      $model=new class($row){private $row;function __construct($r){$this->row=$r;}function readById($id){if($id!==1)throw new \RuntimeException('restaurant mismatch');return $this->row;}};
      $p=$r->getProperty('RestaurantModel');$p->setAccessible(true);$p->setValue($o,$model);
      try{
        $result=$o->zhipanApi(['card'=>'synthetic'],'endpoint',1);
        foreach(['success'=>$result['code']===0,'term'=>$GLOBALS['zhipan_request'][0]['term']==='term','key'=>$GLOBALS['zhipan_signature_key']==='key','url'=>$GLOBALS['zhipan_request'][1]==='http://synthetic.invalid/endpoint']as$test=>$pass){$checks++;if(!$pass)$fail[]=$name.'.'.$test;}
      }catch(\Throwable $e){$fail[]=$name.':'.get_class($e);}
    }
    restore_error_handler();echo json_encode(['checks'=>$checks,'failures'=>$fail,'network_requests'=>0]),PHP_EOL;exit($fail?1:0);
}
