diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..789c4ed --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,41 @@ +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, + ], + ]); + } +} diff --git a/tests/Unit/Bootstrap/ConfigComposerTest.php b/tests/Unit/Bootstrap/ConfigComposerTest.php index a106d6c..3ef2ca4 100644 --- a/tests/Unit/Bootstrap/ConfigComposerTest.php +++ b/tests/Unit/Bootstrap/ConfigComposerTest.php @@ -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')); diff --git a/tests/Unit/Bootstrap/FinderComposerTest.php b/tests/Unit/Bootstrap/FinderComposerTest.php index 7a8b50c..6e212f5 100644 --- a/tests/Unit/Bootstrap/FinderComposerTest.php +++ b/tests/Unit/Bootstrap/FinderComposerTest.php @@ -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); diff --git a/tests/Unit/Bootstrap/ViewComposerTest.php b/tests/Unit/Bootstrap/ViewComposerTest.php index f413a97..0c496ba 100644 --- a/tests/Unit/Bootstrap/ViewComposerTest.php +++ b/tests/Unit/Bootstrap/ViewComposerTest.php @@ -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()); diff --git a/tests/Unit/Controllers/DirectoryControllerTest.php b/tests/Unit/Controllers/DirectoryControllerTest.php index 38f36e0..bdcde64 100644 --- a/tests/Unit/Controllers/DirectoryControllerTest.php +++ b/tests/Unit/Controllers/DirectoryControllerTest.php @@ -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()); } } diff --git a/tests/Unit/Controllers/FileInfoControllerTest.php b/tests/Unit/Controllers/FileInfoControllerTest.php index 828b948..1e2f23e 100644 --- a/tests/Unit/Controllers/FileInfoControllerTest.php +++ b/tests/Unit/Controllers/FileInfoControllerTest.php @@ -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); diff --git a/tests/Unit/Support/HelpersTest.php b/tests/Unit/Support/HelpersTest.php index d395722..a3ae2ec 100644 --- a/tests/Unit/Support/HelpersTest.php +++ b/tests/Unit/Support/HelpersTest.php @@ -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"');