Skip to content

Commit

Permalink
Refactoring tests, Formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
artengin committed Sep 28, 2024
1 parent 74eaa7b commit 34b0d4d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 45 deletions.
7 changes: 6 additions & 1 deletion src/Differ.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
3 changes: 1 addition & 2 deletions src/Formatters/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
3 changes: 2 additions & 1 deletion src/Formatters/Plain.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Formatters/Stylish.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

function render(array $data): string
{
$result = iter($data);
$result = iter($data['children']);
return $result;
}

Expand Down
70 changes: 30 additions & 40 deletions tests/DiffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit 34b0d4d

Please sign in to comment.