Added additional unit tests to bring coverate to 100%

This commit is contained in:
Chris Kankiewicz
2019-12-27 23:26:22 -07:00
parent 83cc516906
commit 73fcf9c995
10 changed files with 155 additions and 13 deletions

View File

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

View File

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

View File

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

3
tests/files/alpha.scss Normal file
View File

@@ -0,0 +1,3 @@
body {
background-color: #AC3;
}

1
tests/files/bravo.js Normal file
View File

@@ -0,0 +1 @@
console.log('Test file; please ignore');

4
tests/files/charlie.bash Normal file
View File

@@ -0,0 +1,4 @@
#!/usr/bin/env bash
set -o errexit
echo "Test file; please ignore"

12
tests/files/delta.html Normal file
View File

@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>Test file; please ignore</h1>
</body>
</html>

2
tests/files/echo.yaml Normal file
View File

@@ -0,0 +1,2 @@
file:
contents: 'Test file; please ignore'

3
tests/files/index.php Normal file
View File

@@ -0,0 +1,3 @@
<?php
echo 'Test file; please ignore';

View File

@@ -1 +0,0 @@
Test file; please ignore