From cd89500314573cc522a272adb86a592130ca398f Mon Sep 17 00:00:00 2001 From: Chris Kankiewicz Date: Tue, 7 Jan 2020 21:05:35 -0700 Subject: [PATCH] Reafactored and updated some tests --- .gitignore | 2 +- app/Controllers/DirectoryController.php | 15 +++-------- app/ViewFunctions/Asset.php | 12 ++++++--- tests/Controllers/DirectoryControllerTest.php | 7 +---- tests/Controllers/FileInfoControllerTest.php | 4 +-- tests/Providers/FinderProviderTest.php | 27 ++++++++++++++----- tests/ViewFunctions/AssetTest.php | 18 ++++++++++--- tests/ViewFunctions/ConfigTest.php | 8 +++--- tests/ViewFunctions/IconTest.php | 10 ++++--- tests/ViewFunctions/MarkdownTest.php | 19 +++++++++++++ tests/ViewFunctions/SizeForHumansTest.php | 17 ++++++------ tests/_files/README.md | 1 + tests/_files/README.txt | 1 + tests/_files/{subdir => }/foxtrot.json | 0 tests/_files/{subdir => }/golf.txt | 0 tests/_files/{subdir => }/hotel.md | 0 tests/_files/mix-manifest.json | 4 +++ tests/_files/{ => subdir}/alpha.scss | 0 tests/_files/{ => subdir}/bravo.js | 0 tests/_files/{ => subdir}/charlie.bash | 0 tests/_files/{ => subdir}/delta.html | 0 tests/_files/{ => subdir}/echo.yaml | 0 22 files changed, 96 insertions(+), 49 deletions(-) create mode 100644 tests/ViewFunctions/MarkdownTest.php create mode 100644 tests/_files/README.md create mode 100644 tests/_files/README.txt rename tests/_files/{subdir => }/foxtrot.json (100%) rename tests/_files/{subdir => }/golf.txt (100%) rename tests/_files/{subdir => }/hotel.md (100%) create mode 100644 tests/_files/mix-manifest.json rename tests/_files/{ => subdir}/alpha.scss (100%) rename tests/_files/{ => subdir}/bravo.js (100%) rename tests/_files/{ => subdir}/charlie.bash (100%) rename tests/_files/{ => subdir}/delta.html (100%) rename tests/_files/{ => subdir}/echo.yaml (100%) diff --git a/.gitignore b/.gitignore index 0b4d763..046738a 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,4 @@ .env .php_cs.cache .phpunit.result.cache -mix-manifest.json +/mix-manifest.json diff --git a/app/Controllers/DirectoryController.php b/app/Controllers/DirectoryController.php index 53c3e3e..2e99fd2 100644 --- a/app/Controllers/DirectoryController.php +++ b/app/Controllers/DirectoryController.php @@ -3,7 +3,6 @@ namespace App\Controllers; use DI\Container; -use Parsedown; use PHLAK\Config\Config; use Slim\Psr7\Request; use Slim\Psr7\Response; @@ -21,9 +20,6 @@ class DirectoryController /** @var Container Application container */ protected $container; - /** @var Parsedown Parsedown component */ - protected $parsedown; - /** @var Twig Twig templating component */ protected $view; @@ -34,15 +30,10 @@ class DirectoryController * @param \PHLAK\Config\Config $config * @param \Slim\Views\Twig $view */ - public function __construct( - Container $container, - Config $config, - Parsedown $parsedown, - Twig $view - ) { + public function __construct(Container $container, Config $config, Twig $view) + { $this->container = $container; $this->config = $config; - $this->parsedown = $parsedown; $this->view = $view; } @@ -137,7 +128,7 @@ class DirectoryController * * @return \Symfony\Component\Finder\SplFileInfo|null */ - protected function readme($path): SplFileInfo + protected function readme($path): ?SplFileInfo { $readmes = Finder::create()->in($path)->depth(0)->name('/^README(?:\..+)?$/i'); $readmes->filter(function (SplFileInfo $file) { diff --git a/app/ViewFunctions/Asset.php b/app/ViewFunctions/Asset.php index ac9d5ef..29444d9 100644 --- a/app/ViewFunctions/Asset.php +++ b/app/ViewFunctions/Asset.php @@ -37,8 +37,14 @@ class Asset extends ViewFunction */ protected function mixManifest(): Collection { - return Collection::make(json_decode(file_get_contents( - $this->container->get('base_path') . '/mix-manifest.json' - ), true)); + $mixManifest = $this->container->get('base_path') . '/mix-manifest.json'; + + if (! is_file($mixManifest)) { + return new Collection(); + } + + return Collection::make( + json_decode(file_get_contents($mixManifest), true) ?? [] + ); } } diff --git a/tests/Controllers/DirectoryControllerTest.php b/tests/Controllers/DirectoryControllerTest.php index 976c311..9762ea3 100644 --- a/tests/Controllers/DirectoryControllerTest.php +++ b/tests/Controllers/DirectoryControllerTest.php @@ -4,7 +4,6 @@ namespace Tests\Controllers; use App\Controllers\DirectoryController; use App\Providers\TwigProvider; -use Parsedown; use Psr\Http\Message\ResponseInterface; use Slim\Psr7\Request; use Slim\Psr7\Response; @@ -21,7 +20,6 @@ class DirectoryControllerTest extends TestCase $controller = new DirectoryController( $this->container, $this->config, - new Parsedown(), $this->container->get(Twig::class) ); @@ -42,7 +40,6 @@ class DirectoryControllerTest extends TestCase $controller = new DirectoryController( $this->container, $this->config, - new Parsedown(), $this->container->get(Twig::class) ); @@ -64,7 +61,6 @@ class DirectoryControllerTest extends TestCase $controller = new DirectoryController( $this->container, $this->config, - new Parsedown(), $this->container->get(Twig::class) ); @@ -79,14 +75,13 @@ class DirectoryControllerTest extends TestCase $this->assertEquals(404, $response->getStatusCode()); } - public function test_it_returns_a_successful_response_for_a_search() + public function test_it_returns_a_successful_response_for_a_search(): void { $this->container->call(TwigProvider::class); $controller = new DirectoryController( $this->container, $this->config, - new Parsedown(), $this->container->get(Twig::class) ); diff --git a/tests/Controllers/FileInfoControllerTest.php b/tests/Controllers/FileInfoControllerTest.php index 7face63..65c7442 100644 --- a/tests/Controllers/FileInfoControllerTest.php +++ b/tests/Controllers/FileInfoControllerTest.php @@ -13,7 +13,7 @@ class FileInfoControllerTest extends TestCase { $controller = new FileInfoController($this->container, $this->config); - $response = $controller(new Response(), 'alpha.scss'); + $response = $controller(new Response(), 'README.md'); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertEquals(200, $response->getStatusCode()); @@ -34,7 +34,7 @@ class FileInfoControllerTest extends TestCase $this->config->set('app.max_hash_size', 10); $controller = new FileInfoController($this->container, $this->config); - $response = $controller(new Response(), 'alpha.scss'); + $response = $controller(new Response(), 'README.md'); $this->assertInstanceOf(ResponseInterface::class, $response); $this->assertEquals(500, $response->getStatusCode()); diff --git a/tests/Providers/FinderProviderTest.php b/tests/Providers/FinderProviderTest.php index 35bc5cb..cc5de8b 100644 --- a/tests/Providers/FinderProviderTest.php +++ b/tests/Providers/FinderProviderTest.php @@ -15,11 +15,10 @@ class FinderProviderTest extends TestCase (new FinderProvider($this->container, $this->config))(); $finder = $this->container->get(Finder::class); - $finder->in($this->container->get('base_path'))->depth(0); + $finder->in($this->filePath('subdir'))->depth(0); $this->assertInstanceOf(Finder::class, $finder); $this->assertEquals([ - 'subdir', 'alpha.scss', 'bravo.js', 'charlie.bash', @@ -37,7 +36,7 @@ class FinderProviderTest extends TestCase (new FinderProvider($this->container, $this->config))(); $finder = $this->container->get(Finder::class); - $finder->in($this->container->get('base_path'))->depth(0); + $finder->in($this->filePath('subdir'))->depth(0); $this->assertEquals([ 'alpha.scss', @@ -45,7 +44,6 @@ class FinderProviderTest extends TestCase 'echo.yaml', 'charlie.bash', 'delta.html', - 'subdir', ], $this->getFilesArray($finder)); } @@ -56,7 +54,7 @@ class FinderProviderTest extends TestCase (new FinderProvider($this->container, $this->config))(); $finder = $this->container->get(Finder::class); - $finder->in($this->container->get('base_path'))->depth(0); + $finder->in($this->filePath('subdir'))->depth(0); $this->assertEquals([ 'echo.yaml', @@ -64,7 +62,24 @@ class FinderProviderTest extends TestCase 'charlie.bash', 'bravo.js', 'alpha.scss', - 'subdir', + ], $this->getFilesArray($finder)); + } + + public function test_it_does_not_return_hidden_files(): void + { + $this->config->set('app.hidden_files', [ + 'subdir/alpha.scss', 'subdir/charlie.bash', '**/*.yaml' + ]); + + (new FinderProvider($this->container, $this->config))(); + + $finder = $this->container->get(Finder::class); + $finder->in($this->filePath('subdir'))->depth(0); + + $this->assertInstanceOf(Finder::class, $finder); + $this->assertEquals([ + 'bravo.js', + 'delta.html', ], $this->getFilesArray($finder)); } diff --git a/tests/ViewFunctions/AssetTest.php b/tests/ViewFunctions/AssetTest.php index 35cde64..542cb44 100644 --- a/tests/ViewFunctions/AssetTest.php +++ b/tests/ViewFunctions/AssetTest.php @@ -3,15 +3,27 @@ namespace Tests\ViewFunctions; use App\ViewFunctions\Asset; -use PHLAK\Config\Config; -use PHPUnit\Framework\TestCase; +use Tests\TestCase; class AssetTest extends TestCase { public function test_it_can_return_an_asset_path(): void { - $asset = new Asset($this->createMock(Config::class)); + $asset = new Asset($this->container, $this->config); $this->assertEquals('/app/dist/test.css', $asset('test.css')); + $this->assertEquals( + '/app/dist/app.css?id=417c7a9bc03852aafb27', + $asset('app.css') + ); + } + + public function test_it_can_return_an_asset_path_without_a_mix_manifest(): void + { + $this->container->set('base_path', $this->filePath('subdir')); + $asset = new Asset($this->container, $this->config); + + $this->assertEquals('/app/dist/test.css', $asset('test.css')); + $this->assertEquals('/app/dist/app.css', $asset('app.css')); } } diff --git a/tests/ViewFunctions/ConfigTest.php b/tests/ViewFunctions/ConfigTest.php index 9d38169..eae8981 100644 --- a/tests/ViewFunctions/ConfigTest.php +++ b/tests/ViewFunctions/ConfigTest.php @@ -4,7 +4,7 @@ namespace Tests\ViewFunctions; use App\ViewFunctions\Config; use PHLAK\Config\Config as AppConfig; -use PHPUnit\Framework\TestCase; +use Tests\TestCase; class ConfigTest extends TestCase { @@ -13,6 +13,8 @@ class ConfigTest extends TestCase public function setUp(): void { + parent::setUp(); + $this->config = new AppConfig([ 'foo' => false, 'bar' => 'Red herring', @@ -24,14 +26,14 @@ class ConfigTest extends TestCase public function test_it_can_retrieve_a_config_item(): void { - $config = new Config($this->config); + $config = new Config($this->container, $this->config); $this->assertEquals('Test value; please ignore', $config('foo')); } public function test_it_returns_a_default_value(): void { - $config = new Config($this->config); + $config = new Config($this->container, $this->config); $this->assertEquals('Default value', $config('bar', 'Default value')); } diff --git a/tests/ViewFunctions/IconTest.php b/tests/ViewFunctions/IconTest.php index 82ed9ba..96b32db 100644 --- a/tests/ViewFunctions/IconTest.php +++ b/tests/ViewFunctions/IconTest.php @@ -4,8 +4,8 @@ namespace Tests\ViewFunctions; use App\ViewFunctions\Icon; use PHLAK\Config\Config; -use PHPUnit\Framework\TestCase; use Symfony\Component\Finder\SplFileInfo; +use Tests\TestCase; class IconTest extends TestCase { @@ -14,6 +14,8 @@ class IconTest extends TestCase public function setUp(): void { + parent::setUp(); + $this->config = new Config([ 'php' => false, 'icons' => [ @@ -24,7 +26,7 @@ class IconTest extends TestCase public function test_it_can_return_icon_markup_for_a_file(): void { - $icon = new Icon($this->config); + $icon = new Icon($this->container, $this->config); $file = $this->createMock(SplFileInfo::class); $file->method('isDir')->willReturn(false); $file->method('getExtension')->willReturn('php'); @@ -34,7 +36,7 @@ class IconTest extends TestCase public function test_it_can_return_icon_markup_for_a_directory(): void { - $icon = new Icon($this->config); + $icon = new Icon($this->container, $this->config); $file = $this->createMock(SplFileInfo::class); $file->method('isDir')->willReturn(true); @@ -43,7 +45,7 @@ class IconTest extends TestCase public function test_it_can_return_the_default_icon_markup(): void { - $icon = new Icon($this->config); + $icon = new Icon($this->container, $this->config); $file = $this->createMock(SplFileInfo::class); $file->method('isDir')->willReturn(false); $file->method('getExtension')->willReturn('default'); diff --git a/tests/ViewFunctions/MarkdownTest.php b/tests/ViewFunctions/MarkdownTest.php new file mode 100644 index 0000000..d63758a --- /dev/null +++ b/tests/ViewFunctions/MarkdownTest.php @@ -0,0 +1,19 @@ +container, $this->config); + + $this->assertEquals( + '

Test markdown, please ignore

', + $markdown('**Test** `markdown`, ~~please~~ _ignore_') + ); + } +} diff --git a/tests/ViewFunctions/SizeForHumansTest.php b/tests/ViewFunctions/SizeForHumansTest.php index 30e3895..6508386 100644 --- a/tests/ViewFunctions/SizeForHumansTest.php +++ b/tests/ViewFunctions/SizeForHumansTest.php @@ -3,9 +3,8 @@ namespace Tests\ViewFunctions; use App\ViewFunctions\SizeForHumans; -use PHLAK\Config\Config; -use PHPUnit\Framework\TestCase; use Symfony\Component\Finder\SplFileInfo; +use Tests\TestCase; class SizeForHumansTest extends TestCase { @@ -14,7 +13,7 @@ class SizeForHumansTest extends TestCase $file = $this->createMock(SplFileInfo::class); $file->method('getSize')->willReturn(13); - $sizeForHumans = new SizeForHumans($this->createMock(Config::class)); + $sizeForHumans = new SizeForHumans($this->container, $this->config); $this->assertEquals('13.00B', $sizeForHumans($file)); } @@ -24,7 +23,7 @@ class SizeForHumansTest extends TestCase $file = $this->createMock(SplFileInfo::class); $file->method('getSize')->willReturn(13690); - $sizeForHumans = new SizeForHumans($this->createMock(Config::class)); + $sizeForHumans = new SizeForHumans($this->container, $this->config); $this->assertEquals('13.37KB', $sizeForHumans($file)); } @@ -34,7 +33,7 @@ class SizeForHumansTest extends TestCase $file = $this->createMock(SplFileInfo::class); $file->method('getSize')->willReturn(14019461); - $sizeForHumans = new SizeForHumans($this->createMock(Config::class)); + $sizeForHumans = new SizeForHumans($this->container, $this->config); $this->assertEquals('13.37MB', $sizeForHumans($file)); } @@ -44,7 +43,7 @@ class SizeForHumansTest extends TestCase $file = $this->createMock(SplFileInfo::class); $file->method('getSize')->willReturn(14355900000); - $sizeForHumans = new SizeForHumans($this->createMock(Config::class)); + $sizeForHumans = new SizeForHumans($this->container, $this->config); $this->assertEquals('13.37GB', $sizeForHumans($file)); } @@ -54,7 +53,7 @@ class SizeForHumansTest extends TestCase $file = $this->createMock(SplFileInfo::class); $file->method('getSize')->willReturn(14700500000000); - $sizeForHumans = new SizeForHumans($this->createMock(Config::class)); + $sizeForHumans = new SizeForHumans($this->container, $this->config); $this->assertEquals('13.37TB', $sizeForHumans($file)); } @@ -64,7 +63,7 @@ class SizeForHumansTest extends TestCase $file = $this->createMock(SplFileInfo::class); $file->method('getSize')->willReturn(15053300000000000); - $sizeForHumans = new SizeForHumans($this->createMock(Config::class)); + $sizeForHumans = new SizeForHumans($this->container, $this->config); $this->assertEquals('13.37PB', $sizeForHumans($file)); } @@ -74,7 +73,7 @@ class SizeForHumansTest extends TestCase $file = $this->createMock(SplFileInfo::class); $file->method('getSize')->willReturn(PHP_INT_MAX); - $sizeForHumans = new SizeForHumans($this->createMock(Config::class)); + $sizeForHumans = new SizeForHumans($this->container, $this->config); $this->assertEquals('8.00EB', $sizeForHumans($file)); } diff --git a/tests/_files/README.md b/tests/_files/README.md new file mode 100644 index 0000000..bb28c6b --- /dev/null +++ b/tests/_files/README.md @@ -0,0 +1 @@ +Test README.md; please ignore diff --git a/tests/_files/README.txt b/tests/_files/README.txt new file mode 100644 index 0000000..c083f83 --- /dev/null +++ b/tests/_files/README.txt @@ -0,0 +1 @@ +Test README.txt; please ignore diff --git a/tests/_files/subdir/foxtrot.json b/tests/_files/foxtrot.json similarity index 100% rename from tests/_files/subdir/foxtrot.json rename to tests/_files/foxtrot.json diff --git a/tests/_files/subdir/golf.txt b/tests/_files/golf.txt similarity index 100% rename from tests/_files/subdir/golf.txt rename to tests/_files/golf.txt diff --git a/tests/_files/subdir/hotel.md b/tests/_files/hotel.md similarity index 100% rename from tests/_files/subdir/hotel.md rename to tests/_files/hotel.md diff --git a/tests/_files/mix-manifest.json b/tests/_files/mix-manifest.json new file mode 100644 index 0000000..21db9e4 --- /dev/null +++ b/tests/_files/mix-manifest.json @@ -0,0 +1,4 @@ +{ + "/app/dist/app.js": "/app/dist/app.js?id=6753a7269276c7b52692", + "/app/dist/app.css": "/app/dist/app.css?id=417c7a9bc03852aafb27" +} diff --git a/tests/_files/alpha.scss b/tests/_files/subdir/alpha.scss similarity index 100% rename from tests/_files/alpha.scss rename to tests/_files/subdir/alpha.scss diff --git a/tests/_files/bravo.js b/tests/_files/subdir/bravo.js similarity index 100% rename from tests/_files/bravo.js rename to tests/_files/subdir/bravo.js diff --git a/tests/_files/charlie.bash b/tests/_files/subdir/charlie.bash similarity index 100% rename from tests/_files/charlie.bash rename to tests/_files/subdir/charlie.bash diff --git a/tests/_files/delta.html b/tests/_files/subdir/delta.html similarity index 100% rename from tests/_files/delta.html rename to tests/_files/subdir/delta.html diff --git a/tests/_files/echo.yaml b/tests/_files/subdir/echo.yaml similarity index 100% rename from tests/_files/echo.yaml rename to tests/_files/subdir/echo.yaml