Refactored tests

This commit is contained in:
Chris Kankiewicz
2019-12-27 23:59:00 -07:00
parent 73fcf9c995
commit 53051aeb43
7 changed files with 70 additions and 68 deletions

41
tests/TestCase.php Normal file
View File

@@ -0,0 +1,41 @@
<?php
namespace Tests;
use DI\Container;
use PHLAK\Config\Config;
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
class TestCase extends PHPUnitTestCase
{
/** @var Container The test container */
protected $container;
/** @var Config The test config */
protected $config;
/** @var string Path to test files directory */
protected $testFilesPath = __DIR__ . '/files';
/**
* This method is called before each test.
*
* @return void
*/
public function setUp(): void
{
$this->container = new Container();
$this->container->set('app.root', $this->testFilesPath);
$this->config = new Config([
'app' => [
'sort_order' => 'type',
'reverse_sort' => false,
'hidden_files' => [],
'hide_app_files' => true,
'hide_vcs_files' => false,
'max_hash_size' => 1000000000,
],
]);
}
}

View File

@@ -3,18 +3,16 @@
namespace Tests\Unit\Bootstrap;
use App\Bootstrap\ConfigComposer;
use DI\Container;
use PHLAK\Config\Config;
use PHPUnit\Framework\TestCase;
use Tests\TestCase;
class ConfigComposerTest extends TestCase
{
public function test_it_can_compose_the_config_component()
public function test_it_can_compose_the_config_component(): void
{
$container = new Container();
(new ConfigComposer($container))();
(new ConfigComposer($this->container))();
$config = $container->get(Config::class);
$config = $this->container->get(Config::class);
$this->assertInstanceOf(Config::class, $config);
$this->assertTrue($config->has('app'));

View File

@@ -3,37 +3,13 @@
namespace Tests\Unit\Bootstrap;
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;
use Tests\TestCase;
class FinderComposerTest extends TestCase
{
/** @var Container The application container */
protected $container;
/** @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))();
@@ -67,7 +43,7 @@ class FinderComposerTest extends TestCase
], $this->getFilesArray($finder));
}
public function test_it_can_reverse_the_sort_order()
public function test_it_can_reverse_the_sort_order(): void
{
$this->config->set('app.reverse_sort', true);

View File

@@ -4,19 +4,17 @@ namespace Tests\Unit\Bootstrap;
use App\Bootstrap\ViewComposer;
use App\Bootstrap\ViewFunctions;
use DI\Container;
use PHLAK\Config\Config;
use PHPUnit\Framework\TestCase;
use Slim\Views\Twig;
use Tests\TestCase;
class ViewComposerTest extends TestCase
{
public function test_it_can_compose_the_view_component()
public function test_it_can_compose_the_view_component(): void
{
$container = new Container();
(new ViewComposer($container, new Config))();
(new ViewComposer($this->container, new Config))();
$twig = $container->get(Twig::class);
$twig = $this->container->get(Twig::class);
$this->assertInstanceOf(Twig::class, $twig);
$this->assertEquals('app/cache/views', $twig->getEnvironment()->getCache());

View File

@@ -2,31 +2,33 @@
namespace Tests\Controllers;
use App\Bootstrap\ViewComposer;
use App\Controllers\DirectoryController;
use DI\Container;
use PHLAK\Config\Config;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Slim\Psr7\Response;
use Slim\Views\Twig;
use Symfony\Component\Finder\Finder;
use Tests\TestCase;
class DirectoryControllerTest extends TestCase
{
public function test_it_returns_a_response()
public function test_it_returns_a_response(): void
{
$this->container->call(ViewComposer::class);
$controller = new DirectoryController(
$this->createMock(Container::class),
$this->createMock(Config::class),
$this->createMock(Twig::class)
$this->container,
$this->config,
$this->container->get(Twig::class)
);
$response = $controller(
$this->createMock(Finder::class),
new Finder(),
new Response(),
'tests/files'
);
$this->assertInstanceOf(ResponseInterface::class, $response);
$this->assertEquals(200, $response->getStatusCode());
}
}

View File

@@ -3,26 +3,13 @@
namespace Tests\Unit\Controllers;
use App\Controllers\FileInfoController;
use PHLAK\Config\Config;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Slim\Psr7\Response;
use Tests\TestCase;
class FileInfoControllerTest extends TestCase
{
/** @var Config The application config */
protected $config;
public function setUp(): void
{
$this->config = new Config([
'app' => [
'max_hash_size' => 1000000000
],
]);
}
public function test_it_can_return_a_successful_response()
public function test_it_can_return_a_successful_response(): void
{
$controller = new FileInfoController($this->config);
@@ -32,7 +19,7 @@ class FileInfoControllerTest extends TestCase
$this->assertEquals(200, $response->getStatusCode());
}
public function test_it_can_return_a_not_found_response()
public function test_it_can_return_a_not_found_response(): void
{
$controller = new FileInfoController($this->config);
@@ -42,7 +29,7 @@ class FileInfoControllerTest extends TestCase
$this->assertEquals(404, $response->getStatusCode());
}
public function test_it_returns_an_error_when_file_size_is_too_large()
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);

View File

@@ -7,7 +7,7 @@ use PHPUnit\Framework\TestCase;
class HelpersTest extends TestCase
{
public function test_it_can_get_an_environment_variable()
public function test_it_can_get_an_environment_variable(): void
{
putenv('TEST_STRING=Test string; please ignore');
@@ -16,14 +16,14 @@ class HelpersTest extends TestCase
$this->assertEquals('Test string; please ignore', $env);
}
public function test_it_can_return_a_default_value()
public function test_it_can_return_a_default_value(): void
{
$env = Helpers::env('DEFAULT_TEST', 'Test default; please ignore');
$this->assertEquals('Test default; please ignore', $env);
}
public function test_it_can_a_retrieve_boolean_value()
public function test_it_can_a_retrieve_boolean_value(): void
{
putenv('TRUE_TEST=true');
putenv('FALSE_TEST=false');
@@ -32,14 +32,14 @@ class HelpersTest extends TestCase
$this->assertFalse(Helpers::env('FALSE_TEST'));
}
public function test_it_can_retrieve_a_null_value()
public function test_it_can_retrieve_a_null_value(): void
{
putenv('NULL_TEST=null');
$this->assertNull(Helpers::env('NULL_TEST'));
}
public function test_it_can_be_surrounded_bys_quotation_marks()
public function test_it_can_be_surrounded_bys_quotation_marks(): void
{
putenv('QUOTES_TEST="Test charlie; please ignore"');