From 57354912717d2e1f33d6bc60af514b18764bb7f7 Mon Sep 17 00:00:00 2001 From: Sebastian Schreiber Date: Mon, 8 Apr 2024 12:26:57 +0200 Subject: [PATCH] [TASK] Add test for FileFinder --- .phpunit.result.cache | 1 + fractor/composer.json | 2 +- fractor/src/FileSystem/FileFinder.php | 9 +++-- fractor/tests/FileSystem/FileFinderTest.php | 39 +++++++++++++++++++ .../DirectorToSearchIn/my_empty_file.txt | 0 .../Fixture/DirectorToSearchIn/my_file.txt | 1 + .../Fixture/DirectorToSearchIn/my_file.yaml | 2 + .../Fixture/DirectorToSearchIn/my_json.json | 3 ++ .../Fixture/DirectorToSearchIn/my_txt.yaml | 1 + 9 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 .phpunit.result.cache create mode 100644 fractor/tests/FileSystem/FileFinderTest.php create mode 100644 fractor/tests/FileSystem/Fixture/DirectorToSearchIn/my_empty_file.txt create mode 100644 fractor/tests/FileSystem/Fixture/DirectorToSearchIn/my_file.txt create mode 100644 fractor/tests/FileSystem/Fixture/DirectorToSearchIn/my_file.yaml create mode 100644 fractor/tests/FileSystem/Fixture/DirectorToSearchIn/my_json.json create mode 100644 fractor/tests/FileSystem/Fixture/DirectorToSearchIn/my_txt.yaml diff --git a/.phpunit.result.cache b/.phpunit.result.cache new file mode 100644 index 00000000..fee39c19 --- /dev/null +++ b/.phpunit.result.cache @@ -0,0 +1 @@ +{"version":1,"defects":{"FileSystem\\FileFinderTest::test":8,"FileSystem\\FileFinderTest::testThatWeFindAllNonEmptyFilesInGivenDirectories":7,"FileSystem\\FileFinderTest::testThatWeFindAllNonEmptyFilesInGivenDirectoriesWithGivenExtensions":7,"FileSystem\\FileFinderTest::test_that_an_exception_is_thrown_when_directories_are_empty":7},"times":{"FileSystem\\FileFinderTest::test":0.006,"FileSystem\\FileFinderTest::testThatWeFindAllNonEmptyFilesInGivenDirectories":0.003,"FileSystem\\FileFinderTest::testThatWeFindAllNonEmptyFilesInGivenDirectoriesWithGivenExtensions":0.002,"FileSystem\\FileFinderTest::test_that_an_exception_is_thrown_when_directories_are_empty":0.003,"FileSystem\\FileFinderTest::test_that_we_find_all_non_empty_files_in_given_directories":0.004,"FileSystem\\FileFinderTest::test_that_we_find_all_files_with_given_extensions_in_given_directories":0.002,"FileSystem\\FileFinderTest::an_exception_is_thrown_when_directories_are_empty":0.003,"FileSystem\\FileFinderTest::find_all_non_empty_files_in_given_directories":0.003,"FileSystem\\FileFinderTest::find_all_files_with_given_extensions_in_given_directories":0.001,"FileSystem\\FileFinderTest::anExceptionIsThrownWhenDirectoriesAreEmpty":0.003,"FileSystem\\FileFinderTest::findAllNonEmptyFilesInGivenDirectories":0.003,"FileSystem\\FileFinderTest::findAllNonEmptyFilesInGivenDirectoriesWithGivenExtensions":0.002}} \ No newline at end of file diff --git a/fractor/composer.json b/fractor/composer.json index bac69afb..b72d2dd7 100644 --- a/fractor/composer.json +++ b/fractor/composer.json @@ -45,7 +45,7 @@ }, "scripts": { "analyze:php": "phpstan analyze", - "style:php": "ecs", + "style:php": "ecs --fix", "test:php": "phpunit tests/" } } diff --git a/fractor/src/FileSystem/FileFinder.php b/fractor/src/FileSystem/FileFinder.php index fa3225ee..8f267991 100644 --- a/fractor/src/FileSystem/FileFinder.php +++ b/fractor/src/FileSystem/FileFinder.php @@ -13,15 +13,18 @@ final class FileFinder */ public function findFiles(array $directories, array $fileExtensions): array { + if ($directories === []) { + throw new \UnexpectedValueException('Directories must not be an empty array'); + } + $finder = Finder::create() ->files() // skip empty files ->size('> 0') ->in($directories); - if ($fileExtensions !== []) { - $pattern = sprintf('/(%s)/', implode('|', $fileExtensions)); - $finder->name($pattern); + foreach ($fileExtensions as $fileExtension) { + $finder->name('*.' . $fileExtension); } $files = []; diff --git a/fractor/tests/FileSystem/FileFinderTest.php b/fractor/tests/FileSystem/FileFinderTest.php new file mode 100644 index 00000000..16e4e732 --- /dev/null +++ b/fractor/tests/FileSystem/FileFinderTest.php @@ -0,0 +1,39 @@ +subject = new FileFinder(); + } + + #[Test] + public function anExceptionIsThrownWhenDirectoriesAreEmpty(): void + { + $this->expectException(UnexpectedValueException::class); + $this->subject->findFiles([], []); + } + + #[Test] + public function findAllNonEmptyFilesInGivenDirectories(): void + { + self::assertCount(4, $this->subject->findFiles([__DIR__ . '/Fixture/DirectorToSearchIn'], [])); + } + + #[Test] + public function findAllNonEmptyFilesInGivenDirectoriesWithGivenExtensions(): void + { + self::assertCount(2, $this->subject->findFiles([__DIR__ . '/Fixture/DirectorToSearchIn'], ['txt', 'json'])); + } +} diff --git a/fractor/tests/FileSystem/Fixture/DirectorToSearchIn/my_empty_file.txt b/fractor/tests/FileSystem/Fixture/DirectorToSearchIn/my_empty_file.txt new file mode 100644 index 00000000..e69de29b diff --git a/fractor/tests/FileSystem/Fixture/DirectorToSearchIn/my_file.txt b/fractor/tests/FileSystem/Fixture/DirectorToSearchIn/my_file.txt new file mode 100644 index 00000000..14863065 --- /dev/null +++ b/fractor/tests/FileSystem/Fixture/DirectorToSearchIn/my_file.txt @@ -0,0 +1 @@ +My file \ No newline at end of file diff --git a/fractor/tests/FileSystem/Fixture/DirectorToSearchIn/my_file.yaml b/fractor/tests/FileSystem/Fixture/DirectorToSearchIn/my_file.yaml new file mode 100644 index 00000000..12e3988a --- /dev/null +++ b/fractor/tests/FileSystem/Fixture/DirectorToSearchIn/my_file.yaml @@ -0,0 +1,2 @@ +yaml: + test: 1 \ No newline at end of file diff --git a/fractor/tests/FileSystem/Fixture/DirectorToSearchIn/my_json.json b/fractor/tests/FileSystem/Fixture/DirectorToSearchIn/my_json.json new file mode 100644 index 00000000..0e0dcd23 --- /dev/null +++ b/fractor/tests/FileSystem/Fixture/DirectorToSearchIn/my_json.json @@ -0,0 +1,3 @@ +{ + +} \ No newline at end of file diff --git a/fractor/tests/FileSystem/Fixture/DirectorToSearchIn/my_txt.yaml b/fractor/tests/FileSystem/Fixture/DirectorToSearchIn/my_txt.yaml new file mode 100644 index 00000000..14863065 --- /dev/null +++ b/fractor/tests/FileSystem/Fixture/DirectorToSearchIn/my_txt.yaml @@ -0,0 +1 @@ +My file \ No newline at end of file