<?php
namespace think {class Exception extends \Exception{} class Db{}}
namespace app\cores\database {class Dm{}}
namespace {
 $GLOBALS['status_test_db']='mysql';function config($key){if($key!=='database.type')throw new \RuntimeException('Unexpected config key');return $GLOBALS['status_test_db'];}
 $base=$argv[1]??dirname(__DIR__,5).'/zhctproject/store/application/p/logic';require $base.'/NutritionTime.php';require $base.'/Nutrition.php';
 class StateModel {
  public $persisted=0;public $found=true;public $calls=[];public $force=false;
  public function read($uuid){return $this->found?['uuid'=>$uuid,'status'=>1]:null;}
  public function modify($uuid,$data){$this->calls[]=['modify',$uuid,$data];if($this->force)return false;$same=(string)$this->persisted===(string)$data['status'];$this->persisted=$data['status'];return $GLOBALS['status_test_db']==='mysql'&&$same?0:1;}
  public function modifyByWhere($where,$data){$this->calls[]=['modifyByWhere',$where,$data];if($where!==['uuid'=>'own-uuid','status'=>['<>',$data['status']]])throw new \RuntimeException('Missing atomic uuid/status predicate');if($this->force)return false;if((string)$this->persisted===(string)$data['status'])return 0;$this->persisted=$data['status'];return 1;}
 }
 $checks=0;$fail=[];$check=static function($actual,$expected,$label)use(&$checks,&$fail){++$checks;if($actual!==$expected)$fail[]=$label;};
 foreach(['time','set'] as $kind){$class=$kind==='time'?'app\\p\\logic\\NutritionTime':'app\\p\\logic\\Nutrition';$method=$kind==='time'?'able':'ableSet';$property=$kind==='time'?'model':'modelSet';$error=$kind==='time'?'状态修改失败':'修改失败';$success=$kind==='time'?'状态修改成功':'修改成功';$invoke=static function($logic,$status)use($kind){return $kind==='time'?$logic->able(['uuid'=>'own-uuid','status'=>$status]):$logic->ableSet('own-uuid',$status);};
 foreach(['mysql','app\\cores\\database\\Dm','\\app\\cores\\database\\Dm'] as $type){$GLOBALS['status_test_db']=$type;$dm=$type!=='mysql';
  foreach([0,1,'0','1'] as $desired)foreach([false,true] as $same){$m=new StateModel();$m->persisted=$same?$desired:1-(int)$desired;$r=new \ReflectionClass($class);$logic=$r->newInstanceWithoutConstructor();$assign=static function($model)use($r,$property,$logic){if($r->hasProperty($property)){$p=$r->getProperty($property);$p->setAccessible(true);$p->setValue($logic,$model);}else{$logic->$property=$model;}};$assign($m);$result=$invoke($logic,$desired);$prefix=$kind.'/'.$type.'/'.gettype($desired).$desired.'/'.($same?'repeat':'change');$check($result,['code'=>$same?1:0,'message'=>$same?$error:$success,'data'=>[]],$prefix.' original result');$check(count($m->calls),1,$prefix.' one atomic write');$check($m->calls[0][0],$dm?'modifyByWhere':'modify',$prefix.' original/DM path');$check($m->persisted,$same?$desired:$desired,$prefix.' persisted value');}
  $m=new StateModel();$m->persisted='0';$r=new \ReflectionClass($class);$logic=$r->newInstanceWithoutConstructor();$assign=static function($model)use($r,$property,$logic){if($r->hasProperty($property)){$p=$r->getProperty($property);$p->setAccessible(true);$p->setValue($logic,$model);}else{$logic->$property=$model;}};$assign($m);$check($invoke($logic,'0'),['code'=>1,'message'=>$error,'data'=>[]],$kind.'/'.$type.' stale read concurrent no-op');
  foreach([null,'','00','01','0.0',0.0,1.0,false,true,2,'2'] as $input){$m=new StateModel();$assign($m);$invoke($logic,$input);$check($m->calls,[['modify','own-uuid',['status'=>$input]]],$kind.'/'.$type.' noncanonical fallback '.gettype($input).json_encode($input));}
  $m=new StateModel();$m->force=true;$assign($m);$check($invoke($logic,0),['code'=>1,'message'=>$error,'data'=>[]],$kind.'/'.$type.' false DB result');
  $m=new StateModel();$m->found=false;$assign($m);$check($invoke($logic,0),['code'=>1,'message'=>$kind==='time'?'服用时间不存在':'分类不存在','data'=>[]],$kind.'/'.$type.' missing row original error');$check($m->calls,[],$kind.'/'.$type.' missing row no write');
 }
 }
 echo json_encode(['assertions'=>$checks,'failures'=>count($fail),'failed'=>$fail],JSON_UNESCAPED_UNICODE),"\n";exit($fail?1:0);
}
