7 lines
3.9 KiB
PHP
7 lines
3.9 KiB
PHP
<?php
|
|
require __DIR__.'/sgf-statements-proof-bootstrap.php';if($book!=='regression')throw new LogicException('Regression only');
|
|
$source=json_decode(file_get_contents($run['root'].'/private/receipt-composition-cc50a276c10c/statements-source-private.json'),true);$sourceRows=collect($source['rows'])->keyBy('code');$entries=[];$rows=[];foreach($fsEntries as$entry){$account=(array)$native->table('gl_accounts')->where($f['scope'])->find($entry['account_id'],['id','code','label','currency']);$entries[]=$entry+['account'=>$account,'account_hash'=>hash('sha256',Tempo\Finance\Payables\Payables::canonical($account))];$row=$sourceRows[$account['code']];$row['account_id']=$account['id'];$row['label']=$account['label'];$rows[]=$row;}usort($rows,fn($a,$b)=>strcmp($a['code'],$b['code']));
|
|
$port=new class($rows,$source['totals']){public function __construct(private array $rows,private array $totals){}public function trialBalance($q){return['rows'=>array_slice($this->rows,($q['page']-1)*$q['per_page'],$q['per_page']),'totals'=>$this->totals,'basis'=>['scope'=>['tenant_id'=>1,'company_id'=>2,'unit_id'=>2],'through_on'=>$q['to_on'],'journals'=>0,'last_journal_id'=>0],'pagination'=>['last_page'=>(int)ceil(count($this->rows)/$q['per_page'])]];}};$mapping=['id'=>999,'version'=>2,'data'=>['entries'=>$entries]];$key=(string)Illuminate\Support\Str::uuid();$method=new ReflectionMethod($fs,'capture');$data=['from_on'=>'2026-11-10','to_on'=>'2026-11-10','per_page'=>25];$oldLines=$native->table('fs_result_lines')->count();$oldResults=$native->table('fs_results')->count();$journals=$native->table('gl_journals')->orderBy('id')->get()->toJson();$accounts=$native->table('gl_accounts')->orderBy('id')->get()->toJson();
|
|
$native->beginTransaction();Illuminate\Support\Facades\DB::setDefaultConnection('receipt_native');try{$failId=$rows[1]['account_id'];$native->unprepared("CREATE FUNCTION fs_partial_test() RETURNS trigger LANGUAGE plpgsql AS \$\$ BEGIN IF NEW.account_id=$failId THEN RAISE EXCEPTION 'QA_PARTIAL_FAILURE'; END IF; RETURN NEW; END; \$\$; CREATE TRIGGER fs_partial_failure BEFORE INSERT ON fs_result_lines FOR EACH ROW EXECUTE FUNCTION fs_partial_test();");$failed=false;try{$native->transaction(fn()=>$method->invoke($fs,$port,$f['scope'],$mapping,$data,$key));}catch(Illuminate\Database\QueryException$e){if(!str_contains($e->getMessage(),'QA_PARTIAL_FAILURE'))throw$e;$failed=true;}$ok($failed&&$native->table('fs_result_lines')->count()===$oldLines,'failure on second line rolls back first line');$native->unprepared('DROP TRIGGER fs_partial_failure ON fs_result_lines; DROP FUNCTION fs_partial_test();');$result=$native->transaction(fn()=>$method->invoke($fs,$port,$f['scope'],$mapping,$data,$key));$native->table('fs_results')->insert($f['scope']+['snapshot_key'=>$key,'data'=>json_encode(['fixture'=>'read port double, not a published statement','summary'=>$result['summary']]),'source_basis'=>$result['source_basis'],'csv_sha256'=>str_repeat('0',64),'created_at'=>now()->utc()]);$native->statement('SET CONSTRAINTS ALL IMMEDIATE');$ok($native->table('fs_result_lines')->where('snapshot_key',$key)->count()===19,'same snapshot key recovers19 lines with valid foreign keys');}finally{while($native->transactionLevel()>0)$native->rollBack();Illuminate\Support\Facades\DB::setDefaultConnection('pgsql');}
|
|
$ok($native->table('fs_result_lines')->count()===$oldLines&&$native->table('fs_results')->count()===$oldResults,'partial snapshot fixtures completely rolled back');$ok($native->table('gl_journals')->orderBy('id')->get()->toJson()===$journals&&$native->table('gl_accounts')->orderBy('id')->get()->toJson()===$accounts,'no account or journal inserted or changed');file_put_contents($work.'/statements-snapshot-failure.json',json_encode(['passed'=>true,'checks'=>$checks,'report_port_double'=>true,'actual_new_accounts_or_journals'=>0,'fixtures_rolled_back'=>true],JSON_PRETTY_PRINT));echo json_encode(['passed'=>true,'checks'=>count($checks),'proof'=>$work.'/statements-snapshot-failure.json']).PHP_EOL;
|