From 34b0d4d02275119addcc81f7a691d49ba8274894 Mon Sep 17 00:00:00 2001 From: artosepyan Date: Sat, 28 Sep 2024 19:20:46 +0300 Subject: [PATCH] Refactoring tests, Formatters --- src/Differ.php | 7 +++- src/Formatters/Json.php | 3 +- src/Formatters/Plain.php | 3 +- src/Formatters/Stylish.php | 2 +- tests/DiffTest.php | 70 ++++++++++++++++---------------------- 5 files changed, 40 insertions(+), 45 deletions(-) diff --git a/src/Differ.php b/src/Differ.php index 124db19..b64cf34 100644 --- a/src/Differ.php +++ b/src/Differ.php @@ -21,7 +21,12 @@ function genDiff(string $file1, string $file2, string $format = 'stylish'): stri $valueFile2 = parser($extension2, $contentFile2); $valueDiff = buildDiff($valueFile1, $valueFile2); - return format($valueDiff, $format); + + $testt = [ + 'type' => 'root', + 'children' => $valueDiff, + ]; + return format($testt, $format); } function getContents(string $path): array { diff --git a/src/Formatters/Json.php b/src/Formatters/Json.php index 88c1db4..5a7d7e9 100644 --- a/src/Formatters/Json.php +++ b/src/Formatters/Json.php @@ -4,7 +4,6 @@ function render(array $data): string { - $root = '{"type":"root","children":'; $jsonData = json_encode($data, JSON_THROW_ON_ERROR); - return $root . $jsonData . '}'; + return $jsonData; } diff --git a/src/Formatters/Plain.php b/src/Formatters/Plain.php index 7d26c32..ff90ff4 100644 --- a/src/Formatters/Plain.php +++ b/src/Formatters/Plain.php @@ -18,13 +18,14 @@ function render(array $data): string { - $result = iter($data); + $result = iter($data['children']); return rtrim(implode($result), " \n"); } function iter(array $value, array $acc = []): array { $func = function ($val) use ($acc) { + if (!is_array($val)) { return toString($val); } diff --git a/src/Formatters/Stylish.php b/src/Formatters/Stylish.php index 74f5d38..44cf6cd 100644 --- a/src/Formatters/Stylish.php +++ b/src/Formatters/Stylish.php @@ -20,7 +20,7 @@ function render(array $data): string { - $result = iter($data); + $result = iter($data['children']); return $result; } diff --git a/tests/DiffTest.php b/tests/DiffTest.php index 33039be..84021e1 100644 --- a/tests/DiffTest.php +++ b/tests/DiffTest.php @@ -27,68 +27,58 @@ protected function getSecondFilePath(string $type): string public static function dataProvider(): array { return [ - 'stylish format, json - json' => [ - 'stylish', + 'json - json' => [ 'json', 'json', ], - 'stylish format, yml - json' => [ - 'stylish', - 'yml', - 'json', - ], - 'stylish format, yml - yml' => [ - 'stylish', - 'yml', - 'yml', - ], - 'plain format, json - json' => [ - 'plain', - 'json', - 'json', - ], - 'plain format, json - yml' => [ - 'plain', - 'json', - 'yml', - ], - 'plain format, yml - yml' => [ - 'plain', - 'yml', - 'yml', - ], - 'json format, json - json' => [ - 'json', - 'json', - 'json', - ], - 'json format, json - yml' => [ - 'json', + 'json - yml' => [ 'json', 'yml', ], - 'json format, yml - yml' => [ - 'json', + 'yml - yml' => [ 'yml', 'yml', ], ]; } + + #[DataProvider('dataProvider')] + public function testDefault(string $firstFileType, string $secondFileType): void + { + $formatter = "stylish"; + $first = $this->getFirstFilePath($firstFileType); + $second = $this->getSecondFilePath($secondFileType); + $expected = file_get_contents($this->getExpectedPath($formatter)); + $this->assertEquals($expected, genDiff($first, $second)); + } + + #[DataProvider('dataProvider')] + public function testStylish(string $firstFileType, string $secondFileType): void + { + $formatter = "stylish"; + $first = $this->getFirstFilePath($firstFileType); + $second = $this->getSecondFilePath($secondFileType); + $expected = file_get_contents($this->getExpectedPath($formatter)); + $this->assertEquals($expected, genDiff($first, $second, $formatter)); + } + #[DataProvider('dataProvider')] - public function testDiff(string $formatter, string $firstFileType, string $secondFileType): void + public function testJson(string $firstFileType, string $secondFileType): void { + $formatter = "json"; $first = $this->getFirstFilePath($firstFileType); $second = $this->getSecondFilePath($secondFileType); $expected = file_get_contents($this->getExpectedPath($formatter)); $this->assertEquals($expected, genDiff($first, $second, $formatter)); } + #[DataProvider('dataProvider')] - public function testDiffDefault(string $formatter, string $firstFileType, string $secondFileType): void + public function testPlain(string $firstFileType, string $secondFileType): void { - $formatter = "stylish"; + $formatter = "plain"; $first = $this->getFirstFilePath($firstFileType); $second = $this->getSecondFilePath($secondFileType); $expected = file_get_contents($this->getExpectedPath($formatter)); - $this->assertEquals($expected, genDiff($first, $second)); + $this->assertEquals($expected, genDiff($first, $second, $formatter)); } }