mirror of
https://github.com/DirectoryLister/DirectoryLister.git
synced 2025-08-23 14:13:01 +02:00
Refactored HiddenFiles class to not break the underlying Collection class constructor
This commit is contained in:
@@ -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),
|
||||
|
@@ -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
|
||||
{
|
||||
|
@@ -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));
|
||||
}
|
||||
}
|
||||
|
@@ -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
|
||||
|
53
tests/HiddenFilesTest.php
Normal file
53
tests/HiddenFilesTest.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use App\HiddenFiles;
|
||||
use Tightenco\Collect\Support\Collection;
|
||||
|
||||
/** @covers \App\HiddenFiles */
|
||||
class HiddenFilesTest extends TestCase
|
||||
{
|
||||
/** @dataProvider hiddenFilesProvider */
|
||||
public function test_it_creates_a_collection_of_hidden_files(
|
||||
array $hiddenFilesArray,
|
||||
string $hiddenFilesList,
|
||||
bool $hideappFiles,
|
||||
array $expected
|
||||
): void {
|
||||
$this->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'
|
||||
]
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
2
tests/_files/.hidden
Normal file
2
tests/_files/.hidden
Normal file
@@ -0,0 +1,2 @@
|
||||
alpha
|
||||
bravo
|
Reference in New Issue
Block a user