Skip to content

Commit

Permalink
[TASK] Add test for FileFinder
Browse files Browse the repository at this point in the history
  • Loading branch information
sabbelasichon committed Apr 8, 2024
1 parent 1bf2a64 commit 5735491
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 4 deletions.
1 change: 1 addition & 0 deletions .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -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}}
2 changes: 1 addition & 1 deletion fractor/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
},
"scripts": {
"analyze:php": "phpstan analyze",
"style:php": "ecs",
"style:php": "ecs --fix",
"test:php": "phpunit tests/"
}
}
9 changes: 6 additions & 3 deletions fractor/src/FileSystem/FileFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
39 changes: 39 additions & 0 deletions fractor/tests/FileSystem/FileFinderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace FileSystem;

use a9f\Fractor\FileSystem\FileFinder;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use UnexpectedValueException;

final class FileFinderTest extends TestCase
{
private FileFinder $subject;

protected function setUp(): void
{
$this->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']));
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
My file
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
yaml:
test: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
My file

0 comments on commit 5735491

Please sign in to comment.