first commit
This commit is contained in:
155
tests/Kernel/InfrastructureBootstrapperTest.php
Normal file
155
tests/Kernel/InfrastructureBootstrapperTest.php
Normal file
@@ -0,0 +1,155 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Kernel;
|
||||
|
||||
use Netig\Netslim\Kernel\Runtime\Startup\InfrastructureBootstrapper;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
final class InfrastructureBootstrapperTest extends TestCase
|
||||
{
|
||||
private string $workspace;
|
||||
|
||||
/** @var array<string, string|null> */
|
||||
private array $envBackup = [];
|
||||
|
||||
/** @var array<string, string|false> */
|
||||
private array $putenvBackup = [];
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->workspace = sys_get_temp_dir() . '/netslim-bootstrapper-' . bin2hex(random_bytes(8));
|
||||
mkdir($this->workspace, 0777, true);
|
||||
|
||||
foreach (['APP_ENV', 'APP_URL', 'TIMEZONE'] as $key) {
|
||||
$this->envBackup[$key] = $_ENV[$key] ?? null;
|
||||
$this->putenvBackup[$key] = getenv($key);
|
||||
unset($_ENV[$key], $_SERVER[$key]);
|
||||
putenv($key);
|
||||
}
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
foreach ($this->envBackup as $key => $value) {
|
||||
if ($value === null) {
|
||||
unset($_ENV[$key], $_SERVER[$key]);
|
||||
} else {
|
||||
$_ENV[$key] = $value;
|
||||
$_SERVER[$key] = $value;
|
||||
}
|
||||
|
||||
$previous = $this->putenvBackup[$key] ?? false;
|
||||
if ($previous === false) {
|
||||
putenv($key);
|
||||
} else {
|
||||
putenv($key . '=' . $previous);
|
||||
}
|
||||
}
|
||||
|
||||
$this->removeDirectory($this->workspace);
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function testBootstrapCreatesRuntimeDirectoriesLoadsEnvironmentAndMemoizesContainer(): void
|
||||
{
|
||||
$this->writeEnvFile([
|
||||
'APP_ENV=development',
|
||||
'APP_URL=http://localhost',
|
||||
'ADMIN_USERNAME=admin',
|
||||
'ADMIN_EMAIL=admin@example.test',
|
||||
'ADMIN_PASSWORD=secret123456',
|
||||
'TIMEZONE=Europe/Paris',
|
||||
]);
|
||||
$definitionsPath = $this->writeDefinitionsFile();
|
||||
$bootstrapper = new InfrastructureBootstrapper($this->workspace, $definitionsPath);
|
||||
|
||||
$container = $bootstrapper->bootstrap();
|
||||
$sameContainer = $bootstrapper->bootstrap();
|
||||
|
||||
self::assertInstanceOf(ContainerInterface::class, $container);
|
||||
self::assertSame($container, $sameContainer);
|
||||
self::assertSame('from-test-definitions', $container->get('bootstrap.flag'));
|
||||
self::assertSame('Europe/Paris', date_default_timezone_get());
|
||||
|
||||
foreach ([
|
||||
'var/cache/twig',
|
||||
'var/cache/htmlpurifier',
|
||||
'var/cache/di',
|
||||
'var/logs',
|
||||
'database',
|
||||
'public/media',
|
||||
] as $directory) {
|
||||
self::assertDirectoryExists($this->workspace . '/' . $directory);
|
||||
}
|
||||
}
|
||||
|
||||
public function testBootstrapFailsWhenEnvironmentFileIsMissing(): void
|
||||
{
|
||||
$bootstrapper = new InfrastructureBootstrapper($this->workspace, $this->writeDefinitionsFile());
|
||||
|
||||
$this->expectException(\RuntimeException::class);
|
||||
$this->expectExceptionMessage('Fichier .env introuvable');
|
||||
|
||||
$bootstrapper->bootstrap();
|
||||
}
|
||||
|
||||
public function testBootstrapNoLongerRequiresIdentityProvisioningVariables(): void
|
||||
{
|
||||
$this->writeEnvFile([
|
||||
'APP_ENV=production',
|
||||
'APP_URL=https://example.test',
|
||||
]);
|
||||
$bootstrapper = new InfrastructureBootstrapper($this->workspace, $this->writeDefinitionsFile());
|
||||
|
||||
$container = $bootstrapper->bootstrap();
|
||||
|
||||
self::assertInstanceOf(ContainerInterface::class, $container);
|
||||
}
|
||||
|
||||
private function writeEnvFile(array $lines): void
|
||||
{
|
||||
file_put_contents($this->workspace . '/.env', implode(PHP_EOL, $lines) . PHP_EOL);
|
||||
}
|
||||
|
||||
private function writeDefinitionsFile(): string
|
||||
{
|
||||
$definitionsPath = $this->workspace . '/definitions.php';
|
||||
file_put_contents($definitionsPath, <<<'PHP'
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
return [
|
||||
'bootstrap.flag' => 'from-test-definitions',
|
||||
];
|
||||
PHP);
|
||||
|
||||
return $definitionsPath;
|
||||
}
|
||||
|
||||
private function removeDirectory(string $directory): void
|
||||
{
|
||||
if (!is_dir($directory)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$iterator = new \RecursiveIteratorIterator(
|
||||
new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS),
|
||||
\RecursiveIteratorIterator::CHILD_FIRST,
|
||||
);
|
||||
|
||||
foreach ($iterator as $fileInfo) {
|
||||
if ($fileInfo->isDir()) {
|
||||
rmdir($fileInfo->getPathname());
|
||||
continue;
|
||||
}
|
||||
|
||||
unlink($fileInfo->getPathname());
|
||||
}
|
||||
|
||||
rmdir($directory);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user