|
3 | 3 | namespace Differ\Tests; |
4 | 4 |
|
5 | 5 | use PHPUnit\Framework\TestCase; |
| 6 | +use PHPUnit\Framework\Attributes\DataProvider; |
6 | 7 |
|
7 | 8 | use function Differ\Differ\genDiff; |
8 | | -use function Differ\Differ\buildDiff; |
9 | | -use function Differ\Differ\getFileFormat; |
| 9 | + |
| 10 | +function getFixturePath(string $filename): string |
| 11 | +{ |
| 12 | + return __DIR__ . '/fixtures/' . $filename; |
| 13 | +} |
| 14 | + |
| 15 | +function getFixtureContent(string $filename): string |
| 16 | +{ |
| 17 | + $path = getFixturePath($filename); |
| 18 | + return file_get_contents($path); |
| 19 | +} |
10 | 20 |
|
11 | 21 | class DifferTest extends TestCase |
12 | 22 | { |
13 | | - public function testRecursiveComparisonJsonStylishFormat(): void |
| 23 | + public static function fileFormatsProvider(): array |
14 | 24 | { |
15 | | - $expected = file_get_contents('tests/fixtures/expected_nested.txt'); |
| 25 | + return [ |
| 26 | + ['json'], |
| 27 | + ['yaml'] |
| 28 | + ]; |
| 29 | + } |
| 30 | + |
| 31 | + #[DataProvider('fileFormatsProvider')] |
| 32 | + public function testRecursiveComparisonJsonStylishFormat(string $format): void |
| 33 | + { |
| 34 | + $expected = file_get_contents(getFixturePath('expected_nested.txt')); |
16 | 35 |
|
17 | | - $actual = genDiff('tests/fixtures/file1.json', 'tests/fixtures/file2.json'); |
| 36 | + $actual = genDiff(getFixturePath("file1.{$format}"), getFixturePath("file2.{$format}")); |
18 | 37 | $this->assertNotEquals($expected, $actual); |
19 | 38 | } |
20 | 39 |
|
21 | | - public function testPlainFormatOutput(): void |
| 40 | + #[DataProvider('fileFormatsProvider')] |
| 41 | + public function testPlainFormatOutput(string $format): void |
22 | 42 | { |
23 | | - $expected = file_get_contents('tests/fixtures/expected_plain.txt'); |
| 43 | + $expected = file_get_contents(getFixturePath('expected_plain.txt')); |
24 | 44 |
|
25 | | - $actual = genDiff('tests/fixtures/file1.json', 'tests/fixtures/file2.json', 'plain'); |
| 45 | + $actual = genDiff(getFixturePath("file1.{$format}"), getFixturePath("file2.{$format}"), 'plain'); |
26 | 46 | $this->assertEquals($expected, $actual); |
27 | 47 | } |
28 | 48 |
|
29 | | - public function testJsonFormatReturnsValidJson(): void |
| 49 | + #[DataProvider('fileFormatsProvider')] |
| 50 | + public function testJsonFormatReturnsValidJson(string $format): void |
30 | 51 | { |
31 | | - $result = genDiff('tests/fixtures/file1.json', 'tests/fixtures/file2.json', 'json'); |
| 52 | + $result = genDiff(getFixturePath("file1.{$format}"), getFixturePath("file2.{$format}"), 'json'); |
32 | 53 | $decoded = json_decode($result, true); |
33 | 54 |
|
34 | 55 | $this->assertIsArray($decoded); |
35 | 56 | $this->assertStringStartsWith('[', $result); |
36 | 57 | $this->assertStringEndsWith(']', $result); |
37 | 58 | } |
38 | 59 |
|
39 | | - public function testMixedFileFormatsWork(): void |
| 60 | + #[DataProvider('fileFormatsProvider')] |
| 61 | + public function testMixedFileFormatsWork(string $format): void |
40 | 62 | { |
41 | | - $result1 = genDiff('tests/fixtures/file1.json', 'tests/fixtures/file2.yaml'); |
42 | | - $result2 = genDiff('tests/fixtures/file1.yaml', 'tests/fixtures/file2.json'); |
| 63 | + $result1 = genDiff(getFixturePath("file1.{$format}"), getFixturePath("file2.{$format}")); |
| 64 | + $result2 = genDiff(getFixturePath("file1.{$format}"), getFixturePath("file2.{$format}")); |
43 | 65 |
|
44 | 66 | $this->assertEquals($result1, $result2); |
45 | 67 | } |
46 | 68 |
|
47 | | - public function testDefaultFormatIsStylish(): void |
| 69 | + #[DataProvider('fileFormatsProvider')] |
| 70 | + public function testDefaultFormatIsStylish(string $format): void |
48 | 71 | { |
49 | | - $result1 = genDiff('tests/fixtures/file1.json', 'tests/fixtures/file2.json'); |
50 | | - $result2 = genDiff('tests/fixtures/file1.json', 'tests/fixtures/file2.json', 'stylish'); |
| 72 | + $result1 = genDiff(getFixturePath("file1.{$format}"), getFixturePath("file2.{$format}")); |
| 73 | + $result2 = genDiff(getFixturePath("file1.{$format}"), getFixturePath("file2.{$format}"), 'stylish'); |
51 | 74 |
|
52 | 75 | $this->assertEquals($result1, $result2); |
53 | 76 | } |
54 | | - |
55 | | - public function testBuildDiffWithFlatObjects(): void |
56 | | - { |
57 | | - $obj1 = (object) ['a' => 1, 'b' => 2]; |
58 | | - $obj2 = (object) ['a' => 1, 'c' => 3]; |
59 | | - |
60 | | - $diff = buildDiff($obj1, $obj2); |
61 | | - |
62 | | - $this->assertIsArray($diff); |
63 | | - $this->assertCount(3, $diff); |
64 | | - |
65 | | - $this->assertEquals('unchanged', $diff[0]['type']); |
66 | | - $this->assertEquals('a', $diff[0]['key']); |
67 | | - $this->assertEquals(1, $diff[0]['value']); |
68 | | - |
69 | | - $this->assertEquals('removed', $diff[1]['type']); |
70 | | - $this->assertEquals('b', $diff[1]['key']); |
71 | | - $this->assertEquals(2, $diff[1]['value']); |
72 | | - |
73 | | - $this->assertEquals('added', $diff[2]['type']); |
74 | | - $this->assertEquals('c', $diff[2]['key']); |
75 | | - $this->assertEquals(3, $diff[2]['value']); |
76 | | - } |
77 | | - |
78 | | - public function testBuildDiffWithNestedObjects(): void |
79 | | - { |
80 | | - $obj1 = (object) [ |
81 | | - 'a' => 1, |
82 | | - 'b' => (object) ['x' => 10, 'y' => 20] |
83 | | - ]; |
84 | | - $obj2 = (object) [ |
85 | | - 'a' => 1, |
86 | | - 'b' => (object) ['x' => 10, 'z' => 30] |
87 | | - ]; |
88 | | - |
89 | | - $diff = buildDiff($obj1, $obj2); |
90 | | - |
91 | | - $this->assertIsArray($diff); |
92 | | - $this->assertCount(2, $diff); |
93 | | - |
94 | | - $this->assertEquals('unchanged', $diff[0]['type']); |
95 | | - $this->assertEquals('a', $diff[0]['key']); |
96 | | - |
97 | | - $this->assertEquals('nested', $diff[1]['type']); |
98 | | - $this->assertEquals('b', $diff[1]['key']); |
99 | | - $this->assertArrayHasKey('children', $diff[1]); |
100 | | - |
101 | | - $children = $diff[1]['children']; |
102 | | - $this->assertCount(3, $children); |
103 | | - |
104 | | - $this->assertEquals('unchanged', $children[0]['type']); |
105 | | - $this->assertEquals('x', $children[0]['key']); |
106 | | - |
107 | | - $this->assertEquals('removed', $children[1]['type']); |
108 | | - $this->assertEquals('y', $children[1]['key']); |
109 | | - |
110 | | - $this->assertEquals('added', $children[2]['type']); |
111 | | - $this->assertEquals('z', $children[2]['key']); |
112 | | - } |
113 | | - |
114 | | - public function testBuildDiffWithChangedValues(): void |
115 | | - { |
116 | | - $obj1 = (object) ['a' => 1, 'b' => 'old']; |
117 | | - $obj2 = (object) ['a' => 1, 'b' => 'new']; |
118 | | - |
119 | | - $diff = buildDiff($obj1, $obj2); |
120 | | - |
121 | | - $this->assertIsArray($diff); |
122 | | - $this->assertCount(2, $diff); |
123 | | - |
124 | | - $this->assertEquals('unchanged', $diff[0]['type']); |
125 | | - $this->assertEquals('a', $diff[0]['key']); |
126 | | - |
127 | | - $this->assertEquals('changed', $diff[1]['type']); |
128 | | - $this->assertEquals('b', $diff[1]['key']); |
129 | | - $this->assertEquals('old', $diff[1]['oldValue']); |
130 | | - $this->assertEquals('new', $diff[1]['newValue']); |
131 | | - } |
132 | | - |
133 | | - public function testBuildDiffSortsKeys(): void |
134 | | - { |
135 | | - $obj1 = (object) ['z' => 1, 'a' => 2]; |
136 | | - $obj2 = (object) ['m' => 3, 'b' => 4]; |
137 | | - |
138 | | - $diff = buildDiff($obj1, $obj2); |
139 | | - |
140 | | - $this->assertIsArray($diff); |
141 | | - $this->assertCount(4, $diff); |
142 | | - |
143 | | - // Проверяем что ключи отсортированы по алфавиту |
144 | | - $this->assertEquals('a', $diff[0]['key']); |
145 | | - $this->assertEquals('b', $diff[1]['key']); |
146 | | - $this->assertEquals('m', $diff[2]['key']); |
147 | | - $this->assertEquals('z', $diff[3]['key']); |
148 | | - } |
149 | | - |
150 | | - public function testGetFileFormat(): void |
151 | | - { |
152 | | - $this->assertEquals('json', getFileFormat('file.json')); |
153 | | - $this->assertEquals('yaml', getFileFormat('file.yaml')); |
154 | | - $this->assertEquals('yaml', getFileFormat('file.yml')); |
155 | | - } |
156 | | - |
157 | | - public function testGetFileFormatWithUnsupportedFormat(): void |
158 | | - { |
159 | | - $this->expectException(\Exception::class); |
160 | | - $this->expectExceptionMessage("Unsupported file format: txt"); |
161 | | - |
162 | | - getFileFormat('file.txt'); |
163 | | - } |
164 | 77 | } |
0 commit comments