Skip to content

Commit 9637849

Browse files
committed
status check 2
1 parent 73b2225 commit 9637849

File tree

7 files changed

+48
-316
lines changed

7 files changed

+48
-316
lines changed

bin/gendiff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ try {
2828
$format = $response['--format'];
2929

3030
$diff = genDiff($file1, $file2, $format);
31-
echo $diff . "\n";
31+
echo $diff;
3232
} catch (Exception $e) {
3333
echo "Error: " . $e->getMessage() . "\n";
3434
exit(1);

src/Differ.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ function genDiff(string $filePath1, string $filePath2, string $format = 'stylish
1111
$content1 = getFileContent($filePath1);
1212
$content2 = getFileContent($filePath2);
1313

14-
$data1 = parse($content1, getFileFormat($filePath1));
15-
$data2 = parse($content2, getFileFormat($filePath2));
14+
$extension1 = pathinfo($filePath1, PATHINFO_EXTENSION);
15+
$extension2 = pathinfo($filePath2, PATHINFO_EXTENSION);
16+
17+
$data1 = parse($content1, $extension1);
18+
$data2 = parse($content2, $extension2);
1619

1720
$diff = buildDiff($data1, $data2);
1821
return render($diff, $format);
@@ -32,17 +35,6 @@ function getFileContent(string $filePath): string
3235
return $content;
3336
}
3437

35-
function getFileFormat(string $filePath): string
36-
{
37-
$extension = pathinfo($filePath, PATHINFO_EXTENSION);
38-
39-
return match ($extension) {
40-
'json' => 'json',
41-
'yaml', 'yml' => 'yaml',
42-
default => throw new \Exception("Unsupported file format: {$extension}")
43-
};
44-
}
45-
4638
function buildDiff(object $data1, object $data2): array
4739
{
4840
$keys1 = array_keys(get_object_vars($data1));

src/Parsers.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
use Symfony\Component\Yaml\Yaml;
66
use Exception;
77

8-
function parse(string $content, string $format): object
8+
function parse(string $content, string $extension): object
99
{
10-
return match ($format) {
10+
return match ($extension) {
1111
'json' => parseJson($content),
1212
'yaml', 'yml' => parseYaml($content),
13-
default => throw new \Exception("Unsupported format: {$format}")
13+
default => throw new \Exception("Unsupported format: {$extension}")
1414
};
1515
}
1616

tests/CliTest.php

Lines changed: 0 additions & 57 deletions
This file was deleted.

tests/DifferTest.php

Lines changed: 39 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -3,162 +3,75 @@
33
namespace Differ\Tests;
44

55
use PHPUnit\Framework\TestCase;
6+
use PHPUnit\Framework\Attributes\DataProvider;
67

78
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+
}
1020

1121
class DifferTest extends TestCase
1222
{
13-
public function testRecursiveComparisonJsonStylishFormat(): void
23+
public static function fileFormatsProvider(): array
1424
{
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'));
1635

17-
$actual = genDiff('tests/fixtures/file1.json', 'tests/fixtures/file2.json');
36+
$actual = genDiff(getFixturePath("file1.{$format}"), getFixturePath("file2.{$format}"));
1837
$this->assertNotEquals($expected, $actual);
1938
}
2039

21-
public function testPlainFormatOutput(): void
40+
#[DataProvider('fileFormatsProvider')]
41+
public function testPlainFormatOutput(string $format): void
2242
{
23-
$expected = file_get_contents('tests/fixtures/expected_plain.txt');
43+
$expected = file_get_contents(getFixturePath('expected_plain.txt'));
2444

25-
$actual = genDiff('tests/fixtures/file1.json', 'tests/fixtures/file2.json', 'plain');
45+
$actual = genDiff(getFixturePath("file1.{$format}"), getFixturePath("file2.{$format}"), 'plain');
2646
$this->assertEquals($expected, $actual);
2747
}
2848

29-
public function testJsonFormatReturnsValidJson(): void
49+
#[DataProvider('fileFormatsProvider')]
50+
public function testJsonFormatReturnsValidJson(string $format): void
3051
{
31-
$result = genDiff('tests/fixtures/file1.json', 'tests/fixtures/file2.json', 'json');
52+
$result = genDiff(getFixturePath("file1.{$format}"), getFixturePath("file2.{$format}"), 'json');
3253
$decoded = json_decode($result, true);
3354

3455
$this->assertIsArray($decoded);
3556
$this->assertStringStartsWith('[', $result);
3657
$this->assertStringEndsWith(']', $result);
3758
}
3859

39-
public function testMixedFileFormatsWork(): void
60+
#[DataProvider('fileFormatsProvider')]
61+
public function testMixedFileFormatsWork(string $format): void
4062
{
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}"));
4365

4466
$this->assertEquals($result1, $result2);
4567
}
4668

47-
public function testDefaultFormatIsStylish(): void
69+
#[DataProvider('fileFormatsProvider')]
70+
public function testDefaultFormatIsStylish(string $format): void
4871
{
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');
5174

5275
$this->assertEquals($result1, $result2);
5376
}
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-
}
16477
}

tests/FormattersTest.php

Lines changed: 0 additions & 58 deletions
This file was deleted.

0 commit comments

Comments
 (0)