Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TASK] Add test for FileFinder #3

Merged
merged 2 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/lint_test_pull_requests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
matrix:
composer-command:
- name: PHP Code Style
command: 'style:php'
command: 'style:php:check'
- name: Composer normalize
command: 'normalize --dry-run'
- name: PHPStan
Expand Down
3 changes: 2 additions & 1 deletion extension-installer/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
},
"scripts": {
"analyze:php": "phpstan analyze",
"style:php": "ecs"
"style:php:check": "ecs",
"style:php:fix": "ecs --fix"
}
}
3 changes: 2 additions & 1 deletion fractor-xml/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
},
"scripts": {
"analyze:php": "phpstan analyze",
"style:php": "ecs",
"style:php:check": "ecs",
"style:php:fix": "ecs --fix",
"test:php": "phpunit tests/"
}
}
3 changes: 2 additions & 1 deletion fractor/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
},
"scripts": {
"analyze:php": "phpstan analyze",
"style:php": "ecs",
"style:php:check": "ecs",
"style:php:fix": "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
3 changes: 2 additions & 1 deletion typo3-fractor/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
},
"scripts": {
"analyze:php": "phpstan analyze",
"style:php": "ecs",
"style:php:check": "ecs",
"style:php:fix": "ecs --fix",
"test:php": "phpunit tests/"
}
}