From 415419a2512c23c0d3d8fe426365673833afe758 Mon Sep 17 00:00:00 2001 From: cyppe Date: Tue, 27 Oct 2026 11:39:18 +0100 Subject: [PATCH] fix(tia): preserve numeric table dependencies across graph round trips (#1990) Co-authored-by: nuno maduro --- src/Plugins/Tia.php | 4 +- src/Plugins/Tia/Graph.php | 2 +- src/Plugins/Tia/Recorder.php | 2 +- tests/.snapshots/success.txt | 8 +- .../Plugins/Tia/NumericTableDependencies.php | 214 ++++++++++++++++++ tests/Visual/Parallel.php | 2 +- 6 files changed, 226 insertions(+), 6 deletions(-) create mode 100644 tests/Unit/Plugins/Tia/NumericTableDependencies.php diff --git a/src/Plugins/Tia.php b/src/Plugins/Tia.php index f334e933..742200c0 100644 --- a/src/Plugins/Tia.php +++ b/src/Plugins/Tia.php @@ -1571,7 +1571,7 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument return [ array_map(array_keys(...), $merged['files']), - array_map(array_keys(...), $merged['tables']), + array_map(static fn (array $tables): array => array_map(strval(...), array_keys($tables)), $merged['tables']), array_map(array_keys(...), $merged['inertia']), ]; } @@ -2291,7 +2291,7 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument foreach (array_keys($perTestUsesDatabase) as $testFile) { $existing = $perTestTables[$testFile] ?? []; $merged = array_fill_keys($existing, true) + $allTables; - $names = array_keys($merged); + $names = array_map(strval(...), array_keys($merged)); sort($names); $perTestTables[$testFile] = $names; } diff --git a/src/Plugins/Tia/Graph.php b/src/Plugins/Tia/Graph.php index aa756512..38e0918a 100644 --- a/src/Plugins/Tia/Graph.php +++ b/src/Plugins/Tia/Graph.php @@ -1093,7 +1093,7 @@ final class Graph } } - $names = array_keys($normalised); + $names = array_map(strval(...), array_keys($normalised)); sort($names); $this->testTables[$testRel] = $names; diff --git a/src/Plugins/Tia/Recorder.php b/src/Plugins/Tia/Recorder.php index 9912b01b..3e12255e 100644 --- a/src/Plugins/Tia/Recorder.php +++ b/src/Plugins/Tia/Recorder.php @@ -272,7 +272,7 @@ final class Recorder $out = []; foreach ($this->perTestTables as $testFile => $tables) { - $names = array_keys($tables); + $names = array_map(strval(...), array_keys($tables)); sort($names); $out[$testFile] = $names; } diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 2ff35acd..e33bb51c 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -1967,6 +1967,12 @@ ✓ it detects an added or removed non-platform dependency ✓ it is independent of package ordering + PASS Tests\Unit\Plugins\Tia\NumericTableDependencies + ✓ it exports numeric table dependencies from the recorder as strings + ✓ it persists numeric tables as JSON strings and selects only their migration readers after reload + ✓ it preserves numeric worker tables through payload cleaning union and graph reload + ✓ it preserves observed and migration added numeric tables when augmenting database tests + PASS Tests\Unit\Plugins\Tia\Recorder ✓ activateLinkTracking() → it tracks tables without a coverage driver ✓ activateLinkTracking() → it tracks inertia components without a coverage driver @@ -2280,4 +2286,4 @@ ✓ pass with dataset with ('my-datas-set-value') ✓ within describe → pass with dataset with ('my-datas-set-value') - Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 35 skipped, 1617 passed (3540 assertions) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 35 skipped, 1621 passed (3582 assertions) diff --git a/tests/Unit/Plugins/Tia/NumericTableDependencies.php b/tests/Unit/Plugins/Tia/NumericTableDependencies.php new file mode 100644 index 00000000..71e7a596 --- /dev/null +++ b/tests/Unit/Plugins/Tia/NumericTableDependencies.php @@ -0,0 +1,214 @@ +activateLinkTracking(); + $recorder->beginTest('Some\Missing\TestClass', 'queries numeric tables', '/project/tests/Unit/NumericTest.php'); + + foreach (['select substring(name from 1 for 3) from users', 'select * from "123"', 'select * from "0"', 'select * from "001"', 'select * from USERS join "123" on true'] as $query) { + foreach (TableExtractor::fromSql($query) as $table) { + $recorder->linkTable($table); + } + } + + $recorder->endTest(); + $tables = $recorder->perTestTables(); + sort($tables['/project/tests/Unit/NumericTest.php'], SORT_STRING); + + expect($tables)->toBe([ + '/project/tests/Unit/NumericTest.php' => ['0', '001', '1', '123', 'users'], + ]); +}); + +it('persists numeric tables as JSON strings and selects only their migration readers after reload', function (): void { + $root = tiaNumericTableProject(); + + try { + tiaAssertNumericTableGraph($root, [], [ + $root.'/tests/Unit/NumericTest.php' => ['123', '0', '001', '123'], + $root.'/tests/Unit/ControlTest.php' => ['USERS', 'users'], + ], [ + 'tests/Unit/NumericTest.php' => ['0', '001', '123'], + 'tests/Unit/ControlTest.php' => ['users'], + ], [ + ['123', ['tests/Unit/NumericTest.php']], + ['0', ['tests/Unit/NumericTest.php']], + ['001', ['tests/Unit/NumericTest.php']], + ['users', ['tests/Unit/ControlTest.php']], + ['orders', []], + ]); + } finally { + tiaRemoveNumericTableProject($root); + } +}); + +it('preserves numeric worker tables through payload cleaning union and graph reload', function (): void { + $root = tiaNumericTableProject(); + + try { + $state = new FileState($root.'/state'); + $tia = new ReflectionClass(Tia::class)->newInstanceWithoutConstructor(); + new ReflectionProperty(Tia::class, 'state')->setValue($tia, $state); + $files = [ + $root.'/tests/Unit/NumericTest.php' => [$root.'/app/Subject.php'], + $root.'/tests/Unit/ControlTest.php' => [$root.'/app/Subject.php'], + ]; + $workers = [ + [ + $root.'/tests/Unit/NumericTest.php' => ['123', 'users'], + $root.'/tests/Unit/ControlTest.php' => ['orders'], + ], + [$root.'/tests/Unit/NumericTest.php' => ['0', '001', '123', 'users']], + ]; + + foreach ($workers as $slot => $tables) { + $before = new ReflectionMethod(Tia::class, 'collectWorkerEdgesPartials')->invoke($tia); + new ReflectionMethod(Tia::class, 'flushWorkerPartial')->invoke($tia, $files, $tables, []); + $keys = new ReflectionMethod(Tia::class, 'collectWorkerEdgesPartials')->invoke($tia); + $written = array_values(array_diff($keys, $before)); + expect($written)->toHaveCount(1); + $key = $written[0]; + $raw = $state->read($key); + expect($raw)->toBeString(); + $payload = json_decode($raw, true, 512, JSON_THROW_ON_ERROR); + + expect($payload)->toBe(['files' => $files, 'tables' => $tables, 'inertia' => []]) + ->and(new ReflectionMethod(Tia::class, 'readPartial')->invoke($tia, $key))->toBe($payload) + ->and(rename($state->pathFor($key), $state->pathFor('worker-edges-fixture-'.$slot.'.json')))->toBeTrue(); + } + + $keys = new ReflectionMethod(Tia::class, 'collectWorkerEdgesPartials')->invoke($tia); + expect($keys)->toHaveCount(2); + [$mergedFiles, $mergedTables, $inertia] = new ReflectionMethod(Tia::class, 'consumePartials')->invoke($tia, $keys); + + expect($mergedFiles)->toBe($files) + ->and($mergedTables)->toBe([ + $root.'/tests/Unit/NumericTest.php' => ['123', 'users', '0', '001'], + $root.'/tests/Unit/ControlTest.php' => ['orders'], + ]) + ->and($inertia)->toBe([]) + ->and(new ReflectionMethod(Tia::class, 'collectWorkerEdgesPartials')->invoke($tia))->toBe([]); + + tiaAssertNumericTableGraph($root, $mergedFiles, $mergedTables, [ + 'tests/Unit/NumericTest.php' => ['0', '001', '123', 'users'], + 'tests/Unit/ControlTest.php' => ['orders'], + ], [ + ['123', ['tests/Unit/NumericTest.php']], + ['0', ['tests/Unit/NumericTest.php']], + ['001', ['tests/Unit/NumericTest.php']], + ['users', ['tests/Unit/NumericTest.php']], + ['orders', ['tests/Unit/ControlTest.php']], + ]); + } finally { + tiaRemoveNumericTableProject($root); + } +}); + +it('preserves observed and migration added numeric tables when augmenting database tests', function (): void { + $root = tiaNumericTableProject(); + + try { + file_put_contents($root.'/database/migrations/create_numeric.php', "newInstanceWithoutConstructor(); + $tables = new ReflectionMethod(Tia::class, 'augmentDatabaseTestTables')->invoke($tia, [ + $root.'/tests/Unit/NumericTest.php' => ['123', '0', '001', 'users'], + $root.'/tests/Unit/ControlTest.php' => ['orders'], + ], [$root.'/tests/Unit/NumericTest.php' => true], $root); + + expect($tables)->toBe([ + $root.'/tests/Unit/NumericTest.php' => ['0', '001', '123', '456', 'users'], + $root.'/tests/Unit/ControlTest.php' => ['orders'], + ]); + + tiaAssertNumericTableGraph($root, [], $tables, [ + 'tests/Unit/NumericTest.php' => ['0', '001', '123', '456', 'users'], + 'tests/Unit/ControlTest.php' => ['orders'], + ], [ + ['123', ['tests/Unit/NumericTest.php']], + ['0', ['tests/Unit/NumericTest.php']], + ['001', ['tests/Unit/NumericTest.php']], + ['456', ['tests/Unit/NumericTest.php']], + ['users', ['tests/Unit/NumericTest.php']], + ['orders', ['tests/Unit/ControlTest.php']], + ]); + } finally { + tiaRemoveNumericTableProject($root); + } +}); + +function tiaNumericTableProject(): string +{ + $root = dirname(__DIR__, 4).'/.temp/tia-numeric-'.bin2hex(random_bytes(8)); + mkdir($root.'/tests/Unit', 0755, true); + mkdir($root.'/app', 0755, true); + mkdir($root.'/database/migrations', 0755, true); + file_put_contents($root.'/tests/Unit/NumericTest.php', '> $files + * @param array> $tables + * @param array> $expected + * @param list}> $selections + */ +function tiaAssertNumericTableGraph(string $root, array $files, array $tables, array $expected, array $selections): void +{ + $graph = new Graph($root); + $graph->replaceEdges($files); + $graph->markKnownTestFiles(array_keys($tables)); + $graph->replaceTestTables($tables); + $json = $graph->encode(); + expect($json)->toBeString(); + $payload = json_decode($json, true, 512, JSON_THROW_ON_ERROR); + expect($payload['test_tables'])->toBe($expected); + $reloaded = Graph::decode($json, $root); + expect($reloaded)->toBeInstanceOf(Graph::class); + + $container = Container::getInstance(); + $previous = $container->get(WatchPatterns::class); + $patterns = new WatchPatterns; + $container->add(WatchPatterns::class, $patterns); + + try { + foreach ($selections as [$table, $readers]) { + $migration = 'database/migrations/change_'.$table.'.php'; + file_put_contents($root.'/'.$migration, "affected([$migration]))->toBe($readers); + } + } finally { + $container->add(WatchPatterns::class, $previous); + } +} + +function tiaRemoveNumericTableProject(string $root): void +{ + $entries = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator($root, FilesystemIterator::SKIP_DOTS), + RecursiveIteratorIterator::CHILD_FIRST, + ); + + foreach ($entries as $entry) { + if ($entry->isDir()) { + rmdir($entry->getPathname()); + } else { + unlink($entry->getPathname()); + } + } + + rmdir($root); +} diff --git a/tests/Visual/Parallel.php b/tests/Visual/Parallel.php index 4e3fbf1c..f2b42cc5 100644 --- a/tests/Visual/Parallel.php +++ b/tests/Visual/Parallel.php @@ -20,7 +20,7 @@ test('parallel', function () use ($run): void { $output = $run('--exclude-group=integration'); $output = implode("\n", array_slice(explode("\n", (string) $output), -10)); - $expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 27 skipped, 1598 passed (3482 assertions)'; + $expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 27 skipped, 1602 passed (3524 assertions)'; expect($output) ->toContain("Tests: {$expected}")