Skip to content

Commit

Permalink
set constant for fixtures directory and developed function findDirect…
Browse files Browse the repository at this point in the history
…ory, that doesnt work
  • Loading branch information
vladimir-xz committed Feb 19, 2024
1 parent 2cf00cf commit 18ea560
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
16 changes: 16 additions & 0 deletions src/FilesProcessing.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,19 @@ function getFilesContent(string $absolutePath)
}
return file_get_contents($absolutePath, true);
}

function findDirectory(string $search, string $mainDirectory = "..")
{
$dirAbsolute = makePathAbsolute($mainDirectory);
$scannedDirectory = array_diff(scandir($dirAbsolute), array('..', '.'));
if (in_array($search, $scannedDirectory) && is_dir($mainDirectory . '/' . $search)) {
return $dirAbsolute . '/' . $search;
}
$dir = array_map(function ($file) use ($search, $dirAbsolute) {
$filePath = $dirAbsolute . '/' . $file;
if (is_dir($filePath)) {
return findDirectory($search, $filePath);
}
}, $scannedDirectory);
return implode('', $dir);
}
29 changes: 16 additions & 13 deletions tests/DifferTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,28 @@
use PHPUnit\Framework\Attributes\DataProvider;

use function Differ\Differ\genDiff;
use function Differ\FilesProcessing\findDirectory;

const FIXTURES_DIR = __DIR__ . '/fixtures/';
// define("FIXTURES_DIR", findDirectory('fixtures'));

final class DifferTest extends TestCase
{
public static function additionProvider(): array
{
$nestedFirstFileJson = __DIR__ . "/fixtures/NestedOne.json";
$nestedSecondFileJson = __DIR__ . "/fixtures/NestedTwo.json";
$resultNested = file_get_contents(__DIR__ . "/fixtures/ExampleNested.txt");
$nestedFirstFileYaml = __DIR__ . "/fixtures/NestedOne.yaml";
$nestedSecondFileYaml = __DIR__ . "/fixtures/NestedTwo.yml";
$resultPlain = file_get_contents(__DIR__ . "/fixtures/ResultPlain.txt");
$resultJson = __DIR__ . "/fixtures/ResultOfJson.json";
$resultJsonContent = file_get_contents($resultJson, true);
$nestedFirstFileJson = "/NestedOne.json";
$nestedSecondFileJson = "/NestedTwo.json";
$resultNested = "/ExampleNested.txt";
$nestedFirstFileYaml = "/NestedOne.yaml";
$nestedSecondFileYaml = "/NestedTwo.yml";
$resultPlain = "/ResultPlain.txt";
$resultJson = "/ResultOfJson.json";

return [
'Nested Json' => [$nestedFirstFileJson, $nestedSecondFileJson, 'stylish', $resultNested],
'Nested Yaml' => [$nestedFirstFileYaml, $nestedSecondFileYaml, 'stylish', $resultNested],
'Plain' => [$nestedFirstFileYaml, $nestedSecondFileYaml, 'plain', $resultPlain],
'Json output' => [$nestedFirstFileYaml, $nestedSecondFileYaml, 'json', $resultJsonContent],
'Nested Json' => [FIXTURES_DIR . $nestedFirstFileJson, FIXTURES_DIR . $nestedSecondFileJson, 'stylish', FIXTURES_DIR . $resultNested],
'Nested Yaml' => [FIXTURES_DIR . $nestedFirstFileYaml, FIXTURES_DIR . $nestedSecondFileYaml, 'stylish', FIXTURES_DIR . $resultNested],
'Plain' => [FIXTURES_DIR . $nestedFirstFileYaml, FIXTURES_DIR . $nestedSecondFileYaml, 'plain', FIXTURES_DIR . $resultPlain],
'Json output' => [FIXTURES_DIR . $nestedFirstFileYaml, FIXTURES_DIR . $nestedSecondFileYaml, 'json', FIXTURES_DIR . $resultJson],
];
}

Expand All @@ -35,6 +38,6 @@ public static function additionProvider(): array
*/
public function testGenDiff(string $arrayOne, string $arrayTwo, string $format, string $expected): void
{
$this->assertEquals($expected, genDiff($arrayOne, $arrayTwo, $format));
$this->assertStringEqualsFile($expected, genDiff($arrayOne, $arrayTwo, $format));
}
}

0 comments on commit 18ea560

Please sign in to comment.