<?php
function getStoreId(){return 10001;}
define('APP_PATH',$argv[2].'/');define('RUNTIME_PATH',__DIR__.'/runtime-sql-only/');require $argv[1].'/thinkphp/base.php';
require APP_PATH.'common/service/BaseService.php';require APP_PATH.'common/service/SecurityAuditService.php';
class AuditNoIoConnection extends \think\db\connector\Mysql {
 public static $captured=[];
 public function getFields($table){$out=[];foreach(['id','store_id','is_delete','event_code','event_name','operator','target_id','message','request_path','result','create_time']as $f)$out[$f]=['name'=>$f,'type'=>in_array($f,['id','store_id','is_delete'],true)?'int':'varchar','primary'=>$f==='id'];return $out;}
 public function query($sql,$bind=[],$master=false,$pdo=false){self::$captured[]=['sql'=>$sql,'bind'=>$bind];return $pdo?new class{public function fetchColumn(){return 3;}}:[];}
 public function quote($str,$master=true){return "'".str_replace("'","''",$str)."'";}
}
\think\Config::set(['type'=>'\\AuditNoIoConnection','builder'=>'\\think\\db\\builder\\Mysql','query'=>'\\think\\db\\Query','prefix'=>'ydy_','database'=>'synthetic_sql_only','fields_strict'=>true,'debug'=>false],'database');
\think\Db::clear();$service=new \app\common\service\SecurityAuditService();$params=['page'=>2,'page_size'=>2,'event_code'=>'evt_scope','keyword'=>"中文'参数",'result'=>'success','operator'=>'synthetic_actor','start_date'=>'2010-01-01','end_date'=>'2010-01-02'];$keyword=($argv[3]??'')!=='no-keyword';if(!$keyword)unset($params['keyword']);try{$service->lists($params);}catch(\Throwable $e){echo json_encode(['total'=>1,'failed'=>['actual_framework_query_exception'],'exception_class'=>get_class($e),'exception_message'=>$e->getMessage()]),"\n";exit(1);}
$n=0;$failed=[];$check=static function($ok,$why)use(&$n,&$failed){$n++;if(!$ok)$failed[]=$why;};$sqls=AuditNoIoConnection::$captured;$check(count($sqls)===2,'exact_count_and_list_queries');
foreach($sqls as $i=>$query){foreach(['store_id','is_delete','event_code','operator','result','create_time']as $field)$check(stripos($query['sql'],'`'.$field.'`')!==false,($i?'list':'count').'_predicate_'.$field);$values=array_map(static function($v){return is_array($v)?$v[0]:$v;},$query['bind']);$expected=[10001,0,'evt_scope','success','synthetic_actor','2010-01-01 00:00:00','2010-01-02 23:59:59'];if($keyword)$expected[]="%中文'参数%";foreach($expected as $value)$check(in_array($value,$values,true),($i?'list':'count').'_bound_'.hash('sha256',(string)$value));$check(stripos($query['sql'],"中文'参数")===false,($i?'list':'count').'_keyword_not_interpolated');if($keyword)$check(substr_count($query['sql'],' LIKE ')===6&&substr_count($query['sql'],' OR ')===5,($i?'list':'count').'_six_keyword_or_group');}
$check(stripos($sqls[1]['sql'],'LIMIT 2,2')!==false,'second_page_offset');$check(stripos($sqls[1]['sql'],'ORDER BY `id` DESC')!==false,'descending_id');
echo json_encode(['total'=>$n,'failed'=>$failed,'queries'=>$sqls],JSON_UNESCAPED_UNICODE),"\n";exit($failed?1:0);
