Added BootManangerTests and tweaked the BootManager a bit

This commit is contained in:
Chris Kankiewicz
2021-04-25 16:36:11 -07:00
parent 4c8448d0a5
commit 5382bfe866
5 changed files with 79 additions and 5 deletions

View File

@@ -8,14 +8,14 @@ use DI\ContainerBuilder;
class BootManager
{
/** Create the application service container. */
public static function createContainer(string $configDirectory): Container
public static function createContainer(string $configPath, string $cachePath): Container
{
$container = (new ContainerBuilder)->addDefinitions(
...glob($configDirectory . '/*.php')
...glob($configPath . '/*.php')
);
if (self::enableContainerCompilation()) {
$container->enableCompilation(dirname(__DIR__, 2) . '/cache');
$container->enableCompilation($cachePath);
}
return $container->build();

View File

@@ -14,7 +14,10 @@ ini_set('open_basedir', __DIR__);
Dotenv::createUnsafeImmutable(__DIR__)->safeLoad();
// Initialize the container
$container = BootManager::createContainer(__DIR__ . '/app/config');
$container = BootManager::createContainer(
__DIR__ . '/app/config',
__DIR__ . '/app/cache'
);
// Initialize the application
$app = $container->call(AppManager::class);

View File

@@ -0,0 +1,68 @@
<?php
namespace Tests\Bootstrap;
use App\Bootstrap\BootManager;
use DI\Container;
use Tests\TestCase;
/** @covers \App\Bootstrap\BootManager */
class BootManagerTest extends TestCase
{
/** Path to the compiled container. */
private const COMPILED_CONTAINER_PATH = 'app/cache/CompiledContainer.php';
protected function tearDown(): void
{
if (file_exists($this->filePath(self::COMPILED_CONTAINER_PATH))) {
unlink($this->filePath(self::COMPILED_CONTAINER_PATH));
}
parent::tearDown();
}
/** @test */
public function it_caches_the_container_when_compile_container_is_true(): void
{
putenv('APP_DEBUG=false');
putenv('COMPILE_CONTAINER=true');
$container = BootManager::createContainer(
$this->filePath('app/config'),
$this->filePath('app/cache')
);
$this->assertInstanceOf(Container::class, $container);
$this->assertFileExists($this->filePath(self::COMPILED_CONTAINER_PATH));
}
/** @test */
public function it_does_not_cache_the_container_when_compilation_is_explicitly_disabled(): void
{
putenv('APP_DEBUG=false');
putenv('COMPILE_CONTAINER=false');
$container = BootManager::createContainer(
$this->filePath('app/config'),
$this->filePath('app/cache')
);
$this->assertInstanceOf(Container::class, $container);
$this->assertFileDoesNotExist($this->filePath('app/cache/CompiledContainer.php'));
}
/** @test */
public function it_does_not_cache_the_container_when_debug_is_enabled(): void
{
putenv('APP_DEBUG=true');
putenv('COMPILE_CONTAINER=true');
$container = BootManager::createContainer(
$this->filePath('app/config'),
$this->filePath('app/cache')
);
$this->assertInstanceOf(Container::class, $container);
$this->assertFileDoesNotExist($this->filePath('app/cache/CompiledContainer.php'));
}
}

View File

@@ -27,7 +27,8 @@ class TestCase extends BaseTestCase
Dotenv::createUnsafeImmutable(__DIR__)->safeLoad();
$this->container = BootManager::createContainer(
dirname(__DIR__) . '/app/config'
dirname(__DIR__) . '/app/config',
dirname(__DIR__) . '/app/cache'
);
$this->config = new Config($this->container);

2
tests/_files/app/config/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
*
!.gitignore