diff --git a/app/config/container.php b/app/config/container.php index 62351e9..8d671b9 100644 --- a/app/config/container.php +++ b/app/config/container.php @@ -56,6 +56,7 @@ return [ ], /** Container definitions */ + App\HiddenFiles::class => DI\factory([App\HiddenFiles::class, 'fromContainer']), Symfony\Component\Finder\Finder::class => DI\factory(Factories\FinderFactory::class), Symfony\Contracts\Cache\CacheInterface::class => DI\Factory(Factories\CacheFactory::class), Symfony\Contracts\Translation\TranslatorInterface::class => DI\factory(Factories\TranslationFactory::class), diff --git a/app/src/Factories/FinderFactory.php b/app/src/Factories/FinderFactory.php index 5d0c6f6..2d4080e 100644 --- a/app/src/Factories/FinderFactory.php +++ b/app/src/Factories/FinderFactory.php @@ -9,7 +9,6 @@ use DI\Container; use PHLAK\Splat\Glob; use Symfony\Component\Finder\Finder; use Symfony\Component\Finder\SplFileInfo; -use Tightenco\Collect\Support\Collection; class FinderFactory { diff --git a/app/src/HiddenFiles.php b/app/src/HiddenFiles.php index 5441137..2d72188 100644 --- a/app/src/HiddenFiles.php +++ b/app/src/HiddenFiles.php @@ -7,12 +7,18 @@ use Tightenco\Collect\Support\Collection; class HiddenFiles extends Collection { + /** {@inheritdoc} */ + protected function __construct($items = []) + { + $this->items = $this->getArrayableItems($items); + } + /** * Create a new HiddenFiles collection object. * * @param \DI\Container $container */ - public function __construct(Container $container) + public static function fromContainer(Container $container): self { $items = $container->get('hidden_files'); @@ -26,6 +32,6 @@ class HiddenFiles extends Collection $items = array_merge($items, $container->get('app_files')); } - parent::__construct(array_unique($items)); + return new static(array_unique($items)); } } diff --git a/tests/Factories/FinderFactoryTest.php b/tests/Factories/FinderFactoryTest.php index f766eb9..b69a91d 100644 --- a/tests/Factories/FinderFactoryTest.php +++ b/tests/Factories/FinderFactoryTest.php @@ -14,7 +14,7 @@ class FinderFactoryTest extends TestCase { public function test_it_can_compose_the_finder_component(): void { - $finder = (new FinderFactory($this->container, $this->container->get(HiddenFiles::class)))(); + $finder = (new FinderFactory($this->container, HiddenFiles::fromContainer($this->container)))(); $this->assertInstanceOf(Finder::class, $finder); @@ -37,7 +37,7 @@ class FinderFactoryTest extends TestCase } )); - $finder = (new FinderFactory($this->container, $this->container->get(HiddenFiles::class)))(); + $finder = (new FinderFactory($this->container, HiddenFiles::fromContainer($this->container)))(); $finder->in($this->filePath('subdir'))->depth(0); $this->assertEquals([ @@ -53,7 +53,7 @@ class FinderFactoryTest extends TestCase { $this->container->set('reverse_sort', true); - $finder = (new FinderFactory($this->container, $this->container->get(HiddenFiles::class)))(); + $finder = (new FinderFactory($this->container, HiddenFiles::fromContainer($this->container)))(); $finder->in($this->filePath('subdir'))->depth(0); $this->assertEquals([ @@ -71,9 +71,7 @@ class FinderFactoryTest extends TestCase 'subdir/alpha.scss', 'subdir/charlie.bash', '**/*.yaml' ]); - (new FinderFactory($this->container, $this->container->get(HiddenFiles::class)))(); - - $finder = $this->container->get(Finder::class); + $finder = (new FinderFactory($this->container, HiddenFiles::fromContainer($this->container)))(); $finder->in($this->filePath('subdir'))->depth(0); $this->assertInstanceOf(Finder::class, $finder); @@ -89,7 +87,7 @@ class FinderFactoryTest extends TestCase $this->expectException(InvalidConfiguration::class); - (new FinderFactory($this->container, $this->container->get(HiddenFiles::class)))(); + (new FinderFactory($this->container, HiddenFiles::fromContainer($this->container)))(); } protected function getFilesArray(Finder $finder): array diff --git a/tests/HiddenFilesTest.php b/tests/HiddenFilesTest.php new file mode 100644 index 0000000..9f88187 --- /dev/null +++ b/tests/HiddenFilesTest.php @@ -0,0 +1,53 @@ +container->set('hidden_files', $hiddenFilesArray); + $this->container->set('hidden_files_list', $hiddenFilesList); + $this->container->set('hide_app_files', $hideappFiles); + + $hiddenFiles = HiddenFiles::fromContainer($this->container); + + $this->assertInstanceOf(Collection::class, $hiddenFiles); + $this->assertEquals($expected, $hiddenFiles->values()->toArray()); + } + + public function hiddenFilesProvider(): array + { + return [ + 'None' => [ + [], 'NOT_A_REAL_FILE', false, [] + ], + 'Hidden files array' => [ + ['foo', 'bar', 'baz'], 'NOT_A_REAL_FILE', false, ['foo', 'bar', 'baz'] + ], + 'Hidden files array with duplicates' => [ + ['foo', 'bar', 'foo'], 'NOT_A_REAL_FILE', false, ['foo', 'bar'] + ], + 'Hidden files list' => [ + [], $this->filePath('.hidden'), false, ['alpha', 'bravo'] + ], + 'App files' => [ + [], 'NOT_A_REAL_FILE', true, ['app', 'index.php', '.hidden'] + ], + 'All' => [ + ['foo', 'alpha'], $this->filePath('.hidden'), true, [ + 'foo', 'alpha', 'bravo', 'app', 'index.php', '.hidden' + ] + ], + ]; + } +} diff --git a/tests/_files/.hidden b/tests/_files/.hidden new file mode 100644 index 0000000..f8704fb --- /dev/null +++ b/tests/_files/.hidden @@ -0,0 +1,2 @@ +alpha +bravo