Reafactored and updated some tests

This commit is contained in:
Chris Kankiewicz
2020-01-07 21:05:35 -07:00
parent 8c9b160a56
commit cd89500314
22 changed files with 96 additions and 49 deletions

2
.gitignore vendored
View File

@@ -4,4 +4,4 @@
.env
.php_cs.cache
.phpunit.result.cache
mix-manifest.json
/mix-manifest.json

View File

@@ -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) {

View File

@@ -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) ?? []
);
}
}

View File

@@ -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)
);

View File

@@ -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());

View File

@@ -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));
}

View File

@@ -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'));
}
}

View File

@@ -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'));
}

View File

@@ -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');

View File

@@ -0,0 +1,19 @@
<?php
namespace Tests\ViewFunctions;
use App\ViewFunctions\Markdown;
use Tests\TestCase;
class MarkdownTest extends TestCase
{
public function test_it_can_parse_markdown_into_html(): void
{
$markdown = new Markdown($this->container, $this->config);
$this->assertEquals(
'<p><strong>Test</strong> <code>markdown</code>, <del>please</del> <em>ignore</em></p>',
$markdown('**Test** `markdown`, ~~please~~ _ignore_')
);
}
}

View File

@@ -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));
}

1
tests/_files/README.md Normal file
View File

@@ -0,0 +1 @@
Test README.md; please ignore

1
tests/_files/README.txt Normal file
View File

@@ -0,0 +1 @@
Test README.txt; please ignore

View File

@@ -0,0 +1,4 @@
{
"/app/dist/app.js": "/app/dist/app.js?id=6753a7269276c7b52692",
"/app/dist/app.css": "/app/dist/app.css?id=417c7a9bc03852aafb27"
}