<?php
declare(strict_types=1);require __DIR__.'/bootstrap.php';require $argv[1];class SignActionContractModel extends \app\common\model\SignAction {protected $schema=['start_time'=>'string','end_time'=>'string'];}
$configs=$app->config->get('database');$original=$configs['connections']['mysql'];$configs['connections']['sign_test_mysql']=array_merge($original,['type'=>'mysql']);$configs['connections']['sign_test_dm']=array_merge($original,['type'=>\app\common\database\Dm::class]);$app->config->set($configs,'database');$app->db->setConfig($app->config);$sql=[];$app->db->listen(static function($s)use(&$sql){$sql[]=$s;});$passed=0;$failed=[];
$values=[null,0,12,1.5,false,true,[],['x'=>'y'],'','not-a-date.000000','2026-02-30 08:30:00.000000','0000-01-01 00:00:00.000000','2026-10-20 24:00:00.0','2026-10-20 23:60:00.0','2026-10-20 23:59:60.0','2026-10-20T08:30:00.000000','2026-10-20 08:30:00.0000000','2026-10-20 08:30:00.123456','2026-10-20 08:30:00','2024-02-29 08:30:00.0','2026-10-20 08:30:00.000000'];
foreach(['mysql','dm'] as $engine)foreach(['start_time','end_time'] as $field)foreach($values as $i=>$value){$want=$value;if($engine==='dm'&&in_array($i,[19,20],true))$want=substr($value,0,19);$m=(new SignActionContractModel([$field=>$value]))->setConnection('sign_test_'.$engine);try{$got=$m->getAttr($field);if($got!==$want)throw new RuntimeException('getAttr value/type');$out=$m->toArray();if(!array_key_exists($field,$out)||$out[$field]!==$want)throw new RuntimeException('toArray value/type');$passed+=2;}catch(Throwable $e){$failed[]=['engine'=>$engine,'field'=>$field,'case'=>$i,'error'=>$e->getMessage()];}}
foreach(['mysql','dm'] as $engine){$m=(new SignActionContractModel([]))->setConnection('sign_test_'.$engine);try{if($m->getAttr('start_time')!==null||array_key_exists('start_time',$m->toArray()))throw new RuntimeException('Missing key changed');$passed+=2;}catch(Throwable $e){$failed[]=['engine'=>$engine,'case'=>'missing','error'=>$e->getMessage()];}}
$sql=array_values(array_filter($sql,static function($s){return strpos((string)$s,'CONNECT:')!==0;}));$out=['passed'=>$passed,'failed'=>$failed,'observed_SQL_count'=>count($sql),'SQL_categories'=>array_map(static function($s){return ['operation'=>strtok(trim((string)$s),' '),'sha256'=>hash('sha256',(string)$s),'metadata_only'=>preg_match('/^SHOW (?:FULL )?COLUMNS/i',(string)$s)===1];},$sql),'real_ORM_getAttr_toArray'=>true];file_put_contents(__DIR__.'/model-test-'.(strpos($argv[1],'-v1')!==false?'v1':'v2').'.json',json_encode($out,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE));echo json_encode($out,JSON_UNESCAPED_UNICODE),"\n";exit(count($failed)||count($sql)?1:0);
