From b39007c0835b38c8c2183bc483e81a678223007c Mon Sep 17 00:00:00 2001 From: Chris Kankiewicz Date: Mon, 6 Jan 2020 14:32:48 -0700 Subject: [PATCH] Updated tests due to the application structure refactor --- app/Controllers/DirectoryController.php | 6 ++- app/Controllers/FileInfoController.php | 12 +++++- tests/Bootstrap/AppManangerTest.php | 19 +++++++++ .../Controllers/DirectoryControllerTest.php | 40 +++++++++++++++---- .../Controllers/FileInfoControllerTest.php | 12 +++--- .../ConfigProviderTest.php} | 8 ++-- .../FinderProviderTest.php} | 25 +++++++----- .../TwigProviderTest.php} | 10 ++--- .../SortMethods/AccessedTest.php | 4 +- .../SortMethods/ChangedTest.php | 4 +- .../SortMethods/ModifiedTest.php | 4 +- .../Bootstrap => }/SortMethods/NameTest.php | 4 +- .../SortMethods/NaturalTest.php | 4 +- .../Bootstrap => }/SortMethods/TypeTest.php | 4 +- tests/{Unit => }/Support/HelpersTest.php | 2 +- tests/TestCase.php | 14 ++++++- .../ViewFunctions/AssetTest.php | 4 +- .../ViewFunctions/ConfigTest.php | 4 +- .../Bootstrap => }/ViewFunctions/IconTest.php | 4 +- .../ViewFunctions/SizeForHumansTest.php | 4 +- tests/{files => _files}/alpha.scss | 0 tests/{files => _files}/bravo.js | 0 tests/{files => _files}/charlie.bash | 0 tests/{files => _files}/delta.html | 0 tests/{files => _files}/echo.yaml | 0 tests/{files => _files}/index.php | 0 tests/_files/subdir/foxtrot.json | 3 ++ tests/_files/subdir/golf.txt | 1 + tests/_files/subdir/hotel.md | 2 + 29 files changed, 137 insertions(+), 57 deletions(-) create mode 100644 tests/Bootstrap/AppManangerTest.php rename tests/{Unit => }/Controllers/DirectoryControllerTest.php (64%) rename tests/{Unit => }/Controllers/FileInfoControllerTest.php (71%) rename tests/{Unit/Bootstrap/ConfigComposerTest.php => Providers/ConfigProviderTest.php} (71%) rename tests/{Unit/Bootstrap/FinderComposerTest.php => Providers/FinderProviderTest.php} (77%) rename tests/{Unit/Bootstrap/ViewComposerTest.php => Providers/TwigProviderTest.php} (83%) rename tests/{Unit/Bootstrap => }/SortMethods/AccessedTest.php (80%) rename tests/{Unit/Bootstrap => }/SortMethods/ChangedTest.php (80%) rename tests/{Unit/Bootstrap => }/SortMethods/ModifiedTest.php (80%) rename tests/{Unit/Bootstrap => }/SortMethods/NameTest.php (80%) rename tests/{Unit/Bootstrap => }/SortMethods/NaturalTest.php (81%) rename tests/{Unit/Bootstrap => }/SortMethods/TypeTest.php (80%) rename tests/{Unit => }/Support/HelpersTest.php (97%) rename tests/{Unit/Bootstrap => }/ViewFunctions/AssetTest.php (78%) rename tests/{Unit/Bootstrap => }/ViewFunctions/ConfigTest.php (90%) rename tests/{Unit/Bootstrap => }/ViewFunctions/IconTest.php (94%) rename tests/{Unit/Bootstrap => }/ViewFunctions/SizeForHumansTest.php (96%) rename tests/{files => _files}/alpha.scss (100%) rename tests/{files => _files}/bravo.js (100%) rename tests/{files => _files}/charlie.bash (100%) rename tests/{files => _files}/delta.html (100%) rename tests/{files => _files}/echo.yaml (100%) rename tests/{files => _files}/index.php (100%) create mode 100644 tests/_files/subdir/foxtrot.json create mode 100644 tests/_files/subdir/golf.txt create mode 100644 tests/_files/subdir/hotel.md diff --git a/app/Controllers/DirectoryController.php b/app/Controllers/DirectoryController.php index 5f94ac5..c63105c 100644 --- a/app/Controllers/DirectoryController.php +++ b/app/Controllers/DirectoryController.php @@ -60,6 +60,8 @@ class DirectoryController Response $response, string $path = '.' ) { + $path = realpath($this->container->get('base_path') . '/' . $path); + try { $files = $files->in($path); } catch (DirectoryNotFoundException $exception) { @@ -100,7 +102,9 @@ class DirectoryController */ protected function breadcrumbs(string $path): array { - $breadcrumbs = Collection::make(array_filter(explode('/', $path))); + $breadcrumbs = Collection::make(explode('/', $path))->diff( + explode('/', $this->container->get('base_path')) + )->filter(); return $breadcrumbs->filter(function (string $crumb) { return $crumb !== '.'; diff --git a/app/Controllers/FileInfoController.php b/app/Controllers/FileInfoController.php index b64566d..9bc1727 100644 --- a/app/Controllers/FileInfoController.php +++ b/app/Controllers/FileInfoController.php @@ -2,22 +2,28 @@ namespace App\Controllers; +use DI\Container; use PHLAK\Config\Config; use Slim\Psr7\Response; use SplFileInfo; class FileInfoController { + /** @var Container The application container */ + protected $container; + /** @var Config App configuration component */ protected $config; /** * Create a new FileInfoController object. * + * @param \DI\Container $container * @param \PHLAK\Config\Config $config */ - public function __construct(Config $config) + public function __construct(Container $container, Config $config) { + $this->container = $container; $this->config = $config; } @@ -29,7 +35,9 @@ class FileInfoController */ public function __invoke(Response $response, string $path = '.') { - $file = new SplFileInfo($path); + $file = new SplFileInfo( + realpath($this->container->get('base_path') . '/' . $path) + ); if (! $file->isFile()) { return $response->withStatus(404, 'File not found'); diff --git a/tests/Bootstrap/AppManangerTest.php b/tests/Bootstrap/AppManangerTest.php new file mode 100644 index 0000000..8c60d1e --- /dev/null +++ b/tests/Bootstrap/AppManangerTest.php @@ -0,0 +1,19 @@ +container->get(CallableResolver::class); + $app = (new AppManager($this->container, $this->config, $callableResolver))(); + + $this->assertInstanceOf(App::class, $app); + } +} diff --git a/tests/Unit/Controllers/DirectoryControllerTest.php b/tests/Controllers/DirectoryControllerTest.php similarity index 64% rename from tests/Unit/Controllers/DirectoryControllerTest.php rename to tests/Controllers/DirectoryControllerTest.php index ef25e6c..6722a1f 100644 --- a/tests/Unit/Controllers/DirectoryControllerTest.php +++ b/tests/Controllers/DirectoryControllerTest.php @@ -2,8 +2,9 @@ namespace Tests\Controllers; -use App\Bootstrap\ViewComposer; use App\Controllers\DirectoryController; +use App\Providers\TwigProvider; +use Parsedown; use Psr\Http\Message\ResponseInterface; use Slim\Psr7\Request; use Slim\Psr7\Response; @@ -13,13 +14,35 @@ use Tests\TestCase; class DirectoryControllerTest extends TestCase { - public function test_it_returns_a_response(): void + public function test_it_returns_a_successful_response(): void { - $this->container->call(ViewComposer::class); + $this->container->call(TwigProvider::class); $controller = new DirectoryController( $this->container, $this->config, + new Parsedown(), + $this->container->get(Twig::class) + ); + + $response = $controller( + new Finder(), + $this->createMock(Request::class), + new Response() + ); + + $this->assertInstanceOf(ResponseInterface::class, $response); + $this->assertEquals(200, $response->getStatusCode()); + } + + public function test_it_returns_a_successful_response_when_listing_a_subdirectory(): void + { + $this->container->call(TwigProvider::class); + + $controller = new DirectoryController( + $this->container, + $this->config, + new Parsedown(), $this->container->get(Twig::class) ); @@ -27,7 +50,7 @@ class DirectoryControllerTest extends TestCase new Finder(), $this->createMock(Request::class), new Response(), - 'tests/files' + 'subdir' ); $this->assertInstanceOf(ResponseInterface::class, $response); @@ -36,11 +59,12 @@ class DirectoryControllerTest extends TestCase public function test_it_returns_a_404_error_when_not_found() { - $this->container->call(ViewComposer::class); + $this->container->call(TwigProvider::class); $controller = new DirectoryController( $this->container, $this->config, + new Parsedown(), $this->container->get(Twig::class) ); @@ -57,11 +81,12 @@ class DirectoryControllerTest extends TestCase public function test_it_returns_a_successful_response_for_a_search() { - $this->container->call(ViewComposer::class); + $this->container->call(TwigProvider::class); $controller = new DirectoryController( $this->container, $this->config, + new Parsedown(), $this->container->get(Twig::class) ); @@ -73,8 +98,7 @@ class DirectoryControllerTest extends TestCase $response = $controller( new Finder(), $request, - new Response(), - 'tests/files' + new Response() ); $this->assertInstanceOf(ResponseInterface::class, $response); diff --git a/tests/Unit/Controllers/FileInfoControllerTest.php b/tests/Controllers/FileInfoControllerTest.php similarity index 71% rename from tests/Unit/Controllers/FileInfoControllerTest.php rename to tests/Controllers/FileInfoControllerTest.php index 1e2f23e..7face63 100644 --- a/tests/Unit/Controllers/FileInfoControllerTest.php +++ b/tests/Controllers/FileInfoControllerTest.php @@ -1,6 +1,6 @@ config); + $controller = new FileInfoController($this->container, $this->config); - $response = $controller(new Response(), __DIR__ . '/../../files/alpha.scss'); + $response = $controller(new Response(), 'alpha.scss'); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertEquals(200, $response->getStatusCode()); @@ -21,7 +21,7 @@ class FileInfoControllerTest extends TestCase public function test_it_can_return_a_not_found_response(): void { - $controller = new FileInfoController($this->config); + $controller = new FileInfoController($this->container, $this->config); $response = $controller(new Response(), 'not_a_file.test'); @@ -32,9 +32,9 @@ class FileInfoControllerTest extends TestCase public function test_it_returns_an_error_when_file_size_is_too_large(): void { $this->config->set('app.max_hash_size', 10); - $controller = new FileInfoController($this->config); + $controller = new FileInfoController($this->container, $this->config); - $response = $controller(new Response(), __DIR__ . '/../../files/alpha.scss'); + $response = $controller(new Response(), 'alpha.scss'); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertEquals(500, $response->getStatusCode()); diff --git a/tests/Unit/Bootstrap/ConfigComposerTest.php b/tests/Providers/ConfigProviderTest.php similarity index 71% rename from tests/Unit/Bootstrap/ConfigComposerTest.php rename to tests/Providers/ConfigProviderTest.php index 3ef2ca4..02db312 100644 --- a/tests/Unit/Bootstrap/ConfigComposerTest.php +++ b/tests/Providers/ConfigProviderTest.php @@ -1,16 +1,16 @@ container))(); + (new ConfigProvider($this->container))(); $config = $this->container->get(Config::class); diff --git a/tests/Unit/Bootstrap/FinderComposerTest.php b/tests/Providers/FinderProviderTest.php similarity index 77% rename from tests/Unit/Bootstrap/FinderComposerTest.php rename to tests/Providers/FinderProviderTest.php index 2608364..35bc5cb 100644 --- a/tests/Unit/Bootstrap/FinderComposerTest.php +++ b/tests/Providers/FinderProviderTest.php @@ -1,26 +1,31 @@ container, $this->config))(); $finder = $this->container->get(Finder::class); - $finder->in($this->container->get('base_path')); + $finder->in($this->container->get('base_path'))->depth(0); $this->assertInstanceOf(Finder::class, $finder); - foreach ($finder as $file) { - $this->assertInstanceOf(SplFileInfo::class, $file); - } + $this->assertEquals([ + 'subdir', + 'alpha.scss', + 'bravo.js', + 'charlie.bash', + 'delta.html', + 'echo.yaml', + ], $this->getFilesArray($finder)); } public function test_it_can_sort_by_a_user_provided_closure(): void @@ -32,7 +37,7 @@ class FinderComposerTest extends TestCase (new FinderProvider($this->container, $this->config))(); $finder = $this->container->get(Finder::class); - $finder->in($this->container->get('base_path')); + $finder->in($this->container->get('base_path'))->depth(0); $this->assertEquals([ 'alpha.scss', @@ -40,6 +45,7 @@ class FinderComposerTest extends TestCase 'echo.yaml', 'charlie.bash', 'delta.html', + 'subdir', ], $this->getFilesArray($finder)); } @@ -50,7 +56,7 @@ class FinderComposerTest extends TestCase (new FinderProvider($this->container, $this->config))(); $finder = $this->container->get(Finder::class); - $finder->in($this->container->get('base_path')); + $finder->in($this->container->get('base_path'))->depth(0); $this->assertEquals([ 'echo.yaml', @@ -58,6 +64,7 @@ class FinderComposerTest extends TestCase 'charlie.bash', 'bravo.js', 'alpha.scss', + 'subdir', ], $this->getFilesArray($finder)); } diff --git a/tests/Unit/Bootstrap/ViewComposerTest.php b/tests/Providers/TwigProviderTest.php similarity index 83% rename from tests/Unit/Bootstrap/ViewComposerTest.php rename to tests/Providers/TwigProviderTest.php index 0c496ba..44c4e6f 100644 --- a/tests/Unit/Bootstrap/ViewComposerTest.php +++ b/tests/Providers/TwigProviderTest.php @@ -1,18 +1,18 @@ container, new Config))(); + (new TwigProvider($this->container, new Config))(); $twig = $this->container->get(Twig::class); diff --git a/tests/Unit/Bootstrap/SortMethods/AccessedTest.php b/tests/SortMethods/AccessedTest.php similarity index 80% rename from tests/Unit/Bootstrap/SortMethods/AccessedTest.php rename to tests/SortMethods/AccessedTest.php index c55deee..fdb5936 100644 --- a/tests/Unit/Bootstrap/SortMethods/AccessedTest.php +++ b/tests/SortMethods/AccessedTest.php @@ -1,8 +1,8 @@ testFilesPath . '/' . $filePath); + } } diff --git a/tests/Unit/Bootstrap/ViewFunctions/AssetTest.php b/tests/ViewFunctions/AssetTest.php similarity index 78% rename from tests/Unit/Bootstrap/ViewFunctions/AssetTest.php rename to tests/ViewFunctions/AssetTest.php index 41d9099..35cde64 100644 --- a/tests/Unit/Bootstrap/ViewFunctions/AssetTest.php +++ b/tests/ViewFunctions/AssetTest.php @@ -1,8 +1,8 @@