|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use CalebDW\SqlEntities\Concerns\SortsTopologically; |
| 6 | + |
| 7 | +beforeEach(function () { |
| 8 | + test()->harness = new TopologicalSortTestHarness(); |
| 9 | +}); |
| 10 | + |
| 11 | +it('sorts linear dependencies', function () { |
| 12 | + $graph = [ |
| 13 | + 'c' => ['b'], |
| 14 | + 'b' => ['a'], |
| 15 | + 'a' => [], |
| 16 | + ]; |
| 17 | + |
| 18 | + $sorted = test()->harness->sortTopologically( |
| 19 | + array_keys($graph), |
| 20 | + fn ($node) => $graph[$node], |
| 21 | + ); |
| 22 | + |
| 23 | + expect($sorted)->toBe(['a', 'b', 'c']); |
| 24 | +}); |
| 25 | + |
| 26 | +it('sorts complex DAG with branches', function () { |
| 27 | + $graph = [ |
| 28 | + 'd' => ['b', 'c'], |
| 29 | + 'c' => ['a'], |
| 30 | + 'b' => ['a'], |
| 31 | + 'a' => [], |
| 32 | + ]; |
| 33 | + |
| 34 | + $sorted = test()->harness->sortTopologically( |
| 35 | + array_keys($graph), |
| 36 | + fn (string $node) => $graph[$node], |
| 37 | + ); |
| 38 | + |
| 39 | + expect($sorted)->toBe(['a', 'b', 'c', 'd']); |
| 40 | +}); |
| 41 | + |
| 42 | +it('handles disconnected graphs', function () { |
| 43 | + $graph = [ |
| 44 | + 'b' => [], |
| 45 | + 'a' => [], |
| 46 | + 'c' => ['a'], |
| 47 | + ]; |
| 48 | + |
| 49 | + $sorted = test()->harness->sortTopologically( |
| 50 | + array_keys($graph), |
| 51 | + fn ($node) => $graph[$node], |
| 52 | + ); |
| 53 | + |
| 54 | + expect($sorted)->toContain('a', 'b', 'c'); |
| 55 | + expect(array_search('a', $sorted))->toBeLessThan(array_search('c', $sorted)); |
| 56 | +}); |
| 57 | + |
| 58 | +it('throws on circular reference', function () { |
| 59 | + $graph = [ |
| 60 | + 'a' => ['b'], |
| 61 | + 'b' => ['a'], |
| 62 | + ]; |
| 63 | + |
| 64 | + test()->harness->sortTopologically( |
| 65 | + array_keys($graph), |
| 66 | + fn ($node) => $graph[$node], |
| 67 | + ); |
| 68 | +})->throws('Circular reference detected for [a]'); |
| 69 | + |
| 70 | +it('works with object nodes', function () { |
| 71 | + $a = new TestNode('a'); |
| 72 | + $b = new TestNode('b', [$a]); |
| 73 | + $c = new TestNode('c', [$b]); |
| 74 | + $d = new TestNode('d', [$b, $c]); |
| 75 | + |
| 76 | + $sorted = test()->harness->sortTopologically( |
| 77 | + [$d, $c, $b, $a], |
| 78 | + fn ($n) => $n->deps, |
| 79 | + fn ($n) => $n->id, |
| 80 | + ); |
| 81 | + |
| 82 | + expect($sorted)->toBe([$a, $b, $c, $d]); |
| 83 | +}); |
| 84 | + |
| 85 | +it('detects cycles with object nodes', function () { |
| 86 | + $a = new TestNode('a'); |
| 87 | + $b = new TestNode('b'); |
| 88 | + $a->deps = [$b]; |
| 89 | + $b->deps = [$a]; |
| 90 | + |
| 91 | + test()->harness->sortTopologically( |
| 92 | + [$a, $b], |
| 93 | + fn (TestNode $n) => $n->deps, |
| 94 | + fn (TestNode $n) => $n->id, |
| 95 | + ); |
| 96 | +})->throws('Circular reference detected for [a]'); |
| 97 | + |
| 98 | +class TopologicalSortTestHarness |
| 99 | +{ |
| 100 | + use SortsTopologically; |
| 101 | +} |
| 102 | + |
| 103 | +class TestNode |
| 104 | +{ |
| 105 | + public function __construct( |
| 106 | + public string $id, |
| 107 | + /** @var list<TestNode> */ |
| 108 | + public array $deps = [], |
| 109 | + ) { |
| 110 | + } |
| 111 | +} |
0 commit comments