Skip to content

Commit

Permalink
Add DirectoriesFinder
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed Apr 29, 2024
1 parent 372a987 commit 50bd3ea
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## v0.31.2

### Added

- Add `DirectoriesFinder`

## v0.31.1

### Added
Expand Down
26 changes: 26 additions & 0 deletions src/Codegen/DirectoriesFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types=1);

namespace Spawnia\Sailor\Codegen;

class DirectoriesFinder implements Finder
{
/** @var array<DirectoryFinder> */
protected array $directoryFinders;

/** @param array<DirectoryFinder> $directoryFinders */
public function __construct(array $directoryFinders)
{
$this->directoryFinders = $directoryFinders;
}

public function documents(): array
{
$documents = [];
foreach ($this->directoryFinders as $directory) {
// Merge ensures uniqueness of the found paths
$documents = array_merge($documents, $directory->documents());
}

return $documents;
}
}
35 changes: 35 additions & 0 deletions tests/Unit/Codegen/DirectoriesFinderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php declare(strict_types=1);

namespace Spawnia\Sailor\Tests\Unit\Codegen;

use Spawnia\Sailor\Codegen\DirectoriesFinder;
use Spawnia\Sailor\Codegen\DirectoryFinder;
use Spawnia\Sailor\Tests\TestCase;

final class DirectoriesFinderTest extends TestCase
{
public function testCombinesDirectoryFinders(): void
{
$suffixFinder = new DirectoryFinder(__DIR__ . '/finder', '/^.+\.suffix\.graphql$/');
$subFinder = new DirectoryFinder(__DIR__ . '/finder/sub');

$directoriesFinder = new DirectoriesFinder([$suffixFinder, $subFinder]);

self::assertSame([
__DIR__ . '/finder/custom.suffix.graphql',
__DIR__ . '/finder/sub/should-also-be-found.graphql',
], array_keys($directoriesFinder->documents()));
}

public function testDeduplicates(): void
{
$subFinder1 = new DirectoryFinder(__DIR__ . '/finder/sub');
$subFinder2 = new DirectoryFinder(__DIR__ . '/finder/sub');

$directoriesFinder = new DirectoriesFinder([$subFinder1, $subFinder2]);

self::assertSame([
__DIR__ . '/finder/sub/should-also-be-found.graphql',
], array_keys($directoriesFinder->documents()));
}
}

0 comments on commit 50bd3ea

Please sign in to comment.