diff --git a/app/src/Bootstrap/BootManager.php b/app/src/Bootstrap/BootManager.php index dd1069a..221ec6e 100644 --- a/app/src/Bootstrap/BootManager.php +++ b/app/src/Bootstrap/BootManager.php @@ -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(); diff --git a/index.php b/index.php index 1bacb91..7c11f10 100644 --- a/index.php +++ b/index.php @@ -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); diff --git a/tests/Bootstrap/BootManagerTest.php b/tests/Bootstrap/BootManagerTest.php new file mode 100644 index 0000000..ee2bd03 --- /dev/null +++ b/tests/Bootstrap/BootManagerTest.php @@ -0,0 +1,68 @@ +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')); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 0a5dfec..e708c79 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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); diff --git a/tests/_files/app/config/.gitignore b/tests/_files/app/config/.gitignore new file mode 100644 index 0000000..d6b7ef3 --- /dev/null +++ b/tests/_files/app/config/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore