|
6 | 6 |
|
7 | 7 | use Jfcherng\Diff\DiffHelper; |
8 | 8 | use PHPUnit\Framework\TestCase; |
| 9 | +use Symfony\Component\Finder\Finder; |
| 10 | +use Symfony\Component\Finder\SplFileInfo; |
9 | 11 |
|
10 | 12 | /** |
11 | 13 | * @coversNothing |
|
15 | 17 | final class DiffHelperTest extends TestCase |
16 | 18 | { |
17 | 19 | /** |
18 | | - * Data provider for DiffHelper::calculate. |
| 20 | + * Test renderer output. |
19 | 21 | * |
20 | | - * @return array the data provider |
| 22 | + * @covers \Jfcherng\Diff\DiffHelper::calculate |
| 23 | + * @covers \Jfcherng\Diff\Renderer\Text\Context |
| 24 | + * @covers \Jfcherng\Diff\Renderer\Text\Unified |
| 25 | + * |
| 26 | + * @dataProvider rendererOutputDataProvider |
| 27 | + * |
| 28 | + * @param string $rendererName The renderer name |
| 29 | + * @param int $idx The index |
| 30 | + * @param SplFileInfo[] $testFiles The test files |
21 | 31 | */ |
22 | | - public function calculateDataProvider(): array |
| 32 | + public function testRendererOutput(string $rendererName, int $idx, array $testFiles): void |
23 | 33 | { |
24 | | - return [ |
25 | | - [ |
26 | | - <<<'EOT' |
27 | | -apples |
28 | | -oranges |
29 | | -kiwis |
30 | | -carrots |
31 | | -EOT |
32 | | - , |
33 | | - <<<'EOT' |
34 | | -apples |
35 | | -kiwis |
36 | | -carrots |
37 | | -grapefruits |
38 | | -EOT |
39 | | - , |
40 | | - [ |
41 | | - 'Unified' => <<<'EOT' |
42 | | -@@ -1,4 +1,4 @@ |
43 | | - apples |
44 | | --oranges |
45 | | - kiwis |
46 | | - carrots |
47 | | -+grapefruits |
48 | | - |
49 | | -EOT |
50 | | - , |
51 | | - 'Context' => <<<'EOT' |
52 | | -*************** |
53 | | -*** 1,4 **** |
54 | | - apples |
55 | | -- oranges |
56 | | - kiwis |
57 | | - carrots |
58 | | ---- 1,4 ---- |
59 | | - apples |
60 | | - kiwis |
61 | | - carrots |
62 | | -+ grapefruits |
63 | | - |
64 | | -EOT |
65 | | - , |
66 | | - ], |
67 | | - ], |
68 | | - ]; |
| 34 | + if (!isset($testFiles['old'], $testFiles['new'], $testFiles['result'])) { |
| 35 | + static::markTestSkipped("Renderer output test '{$rendererName}' #{$idx} is imcomplete."); |
| 36 | + } |
| 37 | + |
| 38 | + $result = DiffHelper::calculate( |
| 39 | + $testFiles['old']->getContents(), |
| 40 | + $testFiles['new']->getContents(), |
| 41 | + $rendererName |
| 42 | + ); |
| 43 | + |
| 44 | + static::assertSame( |
| 45 | + $testFiles['result']->getContents(), |
| 46 | + $result, |
| 47 | + "Renderer output test '{$rendererName}' #{$idx} failed..." |
| 48 | + ); |
69 | 49 | } |
70 | 50 |
|
71 | 51 | /** |
72 | | - * Test the DiffHelper::calculate with the 'Unified' renderer. |
73 | | - * |
74 | | - * @covers \Jfcherng\Diff\DiffHelper::calculate |
75 | | - * @dataProvider calculateDataProvider |
| 52 | + * Test the DiffHelper::getStyleSheet. |
76 | 53 | * |
77 | | - * @param string $old the old |
78 | | - * @param string $new the new |
79 | | - * @param array $expecteds the expecteds |
| 54 | + * @covers \Jfcherng\Diff\DiffHelper::getStyleSheet |
80 | 55 | */ |
81 | | - public function testCalculateUnified(string $old, string $new, array $expecteds): void |
| 56 | + public function testGetStyleSheet(): void |
82 | 57 | { |
83 | | - static::assertSame( |
84 | | - $expecteds['Unified'], |
85 | | - DiffHelper::calculate($old, $new, 'Unified') |
86 | | - ); |
| 58 | + static::assertIsString(DiffHelper::getStyleSheet()); |
87 | 59 | } |
88 | 60 |
|
89 | 61 | /** |
90 | | - * Test the DiffHelper::calculate with the 'Context' renderer. |
91 | | - * |
92 | | - * @covers \Jfcherng\Diff\DiffHelper::calculate |
93 | | - * @dataProvider calculateDataProvider |
94 | | - * |
95 | | - * @param string $old the old |
96 | | - * @param string $new the new |
97 | | - * @param array $expecteds the expecteds |
| 62 | + * Data provider for self::testRendererOutput. |
98 | 63 | */ |
99 | | - public function testCalculateContext(string $old, string $new, array $expecteds): void |
| 64 | + public function rendererOutputDataProvider(): array |
100 | 65 | { |
101 | | - static::assertSame( |
102 | | - $expecteds['Context'], |
103 | | - DiffHelper::calculate($old, $new, 'Context') |
104 | | - ); |
| 66 | + $rendererNames = DiffHelper::getAvailableRenderers(); |
| 67 | + |
| 68 | + $data = []; |
| 69 | + |
| 70 | + foreach ($rendererNames as $rendererName) { |
| 71 | + $tests = $this->findRendererOutputTestFiles($rendererName); |
| 72 | + |
| 73 | + foreach ($tests as $idx => $files) { |
| 74 | + $data[] = [$rendererName, $idx, $files]; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return $data; |
105 | 79 | } |
106 | 80 |
|
107 | 81 | /** |
108 | | - * Test the DiffHelper::getStyleSheet. |
| 82 | + * Find renderer output test files. |
109 | 83 | * |
110 | | - * @covers \Jfcherng\Diff\DiffHelper::getStyleSheet |
| 84 | + * The structure is like [ |
| 85 | + * 1 => ['old' => SplFileInfo, 'new' => SplFileInfo, 'result' => SplFileInfo], |
| 86 | + * ... |
| 87 | + * ] |
| 88 | + * |
| 89 | + * @param string $rendererName The renderer name |
111 | 90 | */ |
112 | | - public function testGetStyleSheet(): void |
| 91 | + protected function findRendererOutputTestFiles(string $rendererName): array |
113 | 92 | { |
114 | | - static::assertIsString(DiffHelper::getStyleSheet()); |
| 93 | + $rendererNameRegex = \preg_quote($rendererName, '/'); |
| 94 | + $fileNameRegex = "/{$rendererNameRegex}-(?P<idx>[0-9]+)-(?P<name>[^.\-]+)\.txt$/u"; |
| 95 | + |
| 96 | + $finder = (new Finder()) |
| 97 | + ->files() |
| 98 | + ->name($fileNameRegex) |
| 99 | + ->in(__DIR__ . '/data/renderer_outputs'); |
| 100 | + |
| 101 | + $ret = []; |
| 102 | + |
| 103 | + /** @var SplFileInfo $file */ |
| 104 | + foreach ($finder as $file) { |
| 105 | + \preg_match($fileNameRegex, $file->getFilename(), $matches); |
| 106 | + $idx = (int) $matches['idx']; |
| 107 | + $name = $matches['name']; |
| 108 | + |
| 109 | + $ret[$idx] = $ret[$idx] ?? []; |
| 110 | + $ret[$idx][$name] = $file; |
| 111 | + } |
| 112 | + |
| 113 | + return $ret; |
115 | 114 | } |
116 | 115 | } |
0 commit comments