<?php
declare(strict_types=1);
require __DIR__.'/ConfigWriteAudit.php';$r=tempnam(sys_get_temp_dir(),'cdx-ex-audit-');unlink($r);mkdir($r,0700);file_put_contents($r.'/private-owned-map.json',json_encode(['rows'=>['yoshop_express'=>['701'=>2147001121,'702'=>2147001122]]]));$a=new ConfigWriteAudit($r,2147001121);
$cases=[
['insert-own',"INSERT INTO yoshop_express (store_id,express_name) VALUES (2147001121,'CDXEX10_甲')",true],
['insert-set-own',"INSERT INTO yoshop_express SET store_id=2147001121,express_name='CDXEX10_甲'",true],
['edit-own',"UPDATE yoshop_express SET sort=2 WHERE yoshop_express.store_id=2147001121 AND express_id=701",true],
['delete-own',"DELETE FROM yoshop_express WHERE yoshop_express.store_id='2147001121' AND express_id=701",true],
['insert-other-store',"INSERT INTO yoshop_express (store_id) VALUES (2147001122)",false],
['insert-fraction-store',"INSERT INTO yoshop_express (store_id) VALUES (2147001121.1)",false],
['other-id-edit',"UPDATE yoshop_express SET sort=1 WHERE store_id=2147001121 AND express_id=702",false],
['other-id-delete',"DELETE FROM yoshop_express WHERE store_id=2147001121 AND express_id=702",false],
['other-store-delete',"DELETE FROM yoshop_express WHERE store_id=2147001122 AND express_id=701",false],
['no-store-delete',"DELETE FROM yoshop_express WHERE express_id=701",false],
['or-delete',"DELETE FROM yoshop_express WHERE store_id=2147001121 AND express_id=701 OR express_id=702",false],
['no-store-edit',"UPDATE yoshop_express SET sort=1 WHERE express_id=701",false],
['change-store',"UPDATE yoshop_express SET store_id=2147001122 WHERE store_id=2147001121 AND express_id=701",false],
['rekey',"UPDATE yoshop_express SET express_id=702 WHERE store_id=2147001121 AND express_id=701",false],
['other-table',"UPDATE yoshop_order SET sort=1 WHERE store_id=2147001121 AND express_id=701",false],
['foreign-schema',"DELETE FROM other.yoshop_express WHERE store_id=2147001121 AND express_id=701",false],
['duplicate-where',"DELETE FROM yoshop_express WHERE express_id=701 AND express_id=701",false],
];foreach($cases as [$name,$sql,$want]){$ok=true;try{$a->observe($sql);}catch(RuntimeException $e){$ok=false;}if($ok!==$want)throw new RuntimeException('Express write audit '.$name);}echo json_encode(['cases'=>count($cases),'all_passed'=>true,'database_called'=>false,'post_execution_only'=>true]),"\n";foreach(['private-owned-map.json','private-write-sql.jsonl','write-audit.jsonl'] as $f)unlink($r.'/'.$f);rmdir($r);
