diff --git a/app/Bootstrap/FinderComposer.php b/app/Bootstrap/FinderComposer.php index cf694f1..461b480 100644 --- a/app/Bootstrap/FinderComposer.php +++ b/app/Bootstrap/FinderComposer.php @@ -87,7 +87,10 @@ class FinderComposer )->when($this->config->get('app.hide_app_files', true), function (Collection $collection) { return $collection->merge(self::APP_FILES); })->map(function (string $file) { - return glob($file, GLOB_BRACE | GLOB_NOSORT); + return glob( + $this->container->get('app.root') . '/' . $file, + GLOB_BRACE | GLOB_NOSORT + ); })->flatten()->map(function (string $file) { return realpath($file); }); diff --git a/tests/Unit/Bootstrap/FinderComposerTest.php b/tests/Unit/Bootstrap/FinderComposerTest.php index e81f29f..7a8b50c 100644 --- a/tests/Unit/Bootstrap/FinderComposerTest.php +++ b/tests/Unit/Bootstrap/FinderComposerTest.php @@ -6,17 +6,100 @@ use App\Bootstrap\FinderComposer; use DI\Container; use PHLAK\Config\Config; use PHPUnit\Framework\TestCase; +use RuntimeException; use Symfony\Component\Finder\Finder; +use Symfony\Component\Finder\SplFileInfo; class FinderComposerTest extends TestCase { - public function test_it_can_compose_the_finder_component() - { - $container = new Container(); - (new FinderComposer($container, new Config))(); + /** @var Container The application container */ + protected $container; - $finder = $container->get(Finder::class); + /** @var Config The application config */ + protected $config; + + public function setUp(): void + { + $this->container = new Container(); + $this->container->set('app.root', realpath(__DIR__ . '/../../files')); + + $this->config = new Config([ + 'app' => [ + 'sort_order' => 'type', + 'reverse_sort' => false, + 'hidden_files' => [], + 'hide_app_files' => true, + 'hide_vcs_files' => false, + ] + ]); + } + + public function test_it_can_compose_the_finder_component(): void + { + (new FinderComposer($this->container, $this->config))(); + + $finder = $this->container->get(Finder::class); + $finder->in($this->container->get('app.root')); $this->assertInstanceOf(Finder::class, $finder); + foreach ($finder as $file) { + $this->assertInstanceOf(SplFileInfo::class, $file); + } + } + + public function test_it_can_sort_by_a_user_provided_closure(): void + { + $this->config->set('app.sort_order', function (SplFileInfo $file1, SplFileInfo $file2) { + return $file1->getSize() <=> $file2->getSize(); + }); + + (new FinderComposer($this->container, $this->config))(); + + $finder = $this->container->get(Finder::class); + $finder->in($this->container->get('app.root')); + + $this->assertEquals([ + 'alpha.scss', + 'bravo.js', + 'echo.yaml', + 'charlie.bash', + 'delta.html', + ], $this->getFilesArray($finder)); + } + + public function test_it_can_reverse_the_sort_order() + { + $this->config->set('app.reverse_sort', true); + + (new FinderComposer($this->container, $this->config))(); + + $finder = $this->container->get(Finder::class); + $finder->in($this->container->get('app.root')); + + $this->assertEquals([ + 'echo.yaml', + 'delta.html', + 'charlie.bash', + 'bravo.js', + 'alpha.scss', + ], $this->getFilesArray($finder)); + } + + public function test_it_throws_a_runtime_exception_with_an_invalid_sort_order(): void + { + $this->config->set('app.sort_order', 'invalid'); + + $this->expectException(RuntimeException::class); + + (new FinderComposer($this->container, $this->config))(); + } + + protected function getFilesArray(Finder $finder): array + { + $files = array_map(function (SplFileInfo $file) { + return $file->getFilename(); + }, iterator_to_array($finder)); + + return array_values($files); } } diff --git a/tests/Unit/Controllers/FileInfoControllerTest.php b/tests/Unit/Controllers/FileInfoControllerTest.php index f376362..828b948 100644 --- a/tests/Unit/Controllers/FileInfoControllerTest.php +++ b/tests/Unit/Controllers/FileInfoControllerTest.php @@ -10,14 +10,46 @@ use Slim\Psr7\Response; class FileInfoControllerTest extends TestCase { - public function test_it_returns_a_response() - { - $controller = new FileInfoController( - $this->createMock(Config::class) - ); + /** @var Config The application config */ + protected $config; - $response = $controller(new Response(), 'tests/files'); + public function setUp(): void + { + $this->config = new Config([ + 'app' => [ + 'max_hash_size' => 1000000000 + ], + ]); + } + + public function test_it_can_return_a_successful_response() + { + $controller = new FileInfoController($this->config); + + $response = $controller(new Response(), __DIR__ . '/../../files/alpha.scss'); $this->assertInstanceOf(ResponseInterface::class, $response); + $this->assertEquals(200, $response->getStatusCode()); + } + + public function test_it_can_return_a_not_found_response() + { + $controller = new FileInfoController($this->config); + + $response = $controller(new Response(), 'not_a_file.test'); + + $this->assertInstanceOf(ResponseInterface::class, $response); + $this->assertEquals(404, $response->getStatusCode()); + } + + public function test_it_returns_an_error_when_file_size_is_too_large() + { + $this->config->set('app.max_hash_size', 10); + $controller = new FileInfoController($this->config); + + $response = $controller(new Response(), __DIR__ . '/../../files/alpha.scss'); + + $this->assertInstanceOf(ResponseInterface::class, $response); + $this->assertEquals(500, $response->getStatusCode()); } } diff --git a/tests/files/alpha.scss b/tests/files/alpha.scss new file mode 100644 index 0000000..e3097bf --- /dev/null +++ b/tests/files/alpha.scss @@ -0,0 +1,3 @@ +body { + background-color: #AC3; +} diff --git a/tests/files/bravo.js b/tests/files/bravo.js new file mode 100644 index 0000000..3c4119b --- /dev/null +++ b/tests/files/bravo.js @@ -0,0 +1 @@ +console.log('Test file; please ignore'); diff --git a/tests/files/charlie.bash b/tests/files/charlie.bash new file mode 100644 index 0000000..a22b41a --- /dev/null +++ b/tests/files/charlie.bash @@ -0,0 +1,4 @@ +#!/usr/bin/env bash +set -o errexit + +echo "Test file; please ignore" diff --git a/tests/files/delta.html b/tests/files/delta.html new file mode 100644 index 0000000..35829c4 --- /dev/null +++ b/tests/files/delta.html @@ -0,0 +1,12 @@ + + + + + + + Document + + +

Test file; please ignore

+ + diff --git a/tests/files/echo.yaml b/tests/files/echo.yaml new file mode 100644 index 0000000..a3db537 --- /dev/null +++ b/tests/files/echo.yaml @@ -0,0 +1,2 @@ +file: + contents: 'Test file; please ignore' diff --git a/tests/files/index.php b/tests/files/index.php new file mode 100644 index 0000000..d183f7d --- /dev/null +++ b/tests/files/index.php @@ -0,0 +1,3 @@ +