Files
slim-blog/tests/Shared/BootstrapTest.php
2026-03-16 09:25:44 +01:00

79 lines
2.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Shared;
use App\Shared\Bootstrap;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use ReflectionProperty;
use Slim\Factory\AppFactory;
use Slim\App;
#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
final class BootstrapTest extends TestCase
{
private array $envBackup = [];
protected function setUp(): void
{
$this->envBackup = [
'APP_AUTO_PROVISION' => $_ENV['APP_AUTO_PROVISION'] ?? null,
];
}
protected function tearDown(): void
{
foreach ($this->envBackup as $key => $value) {
if ($value === null) {
unset($_ENV[$key]);
} else {
$_ENV[$key] = $value;
}
}
}
public function testInitializeInfrastructureReturnsPreloadedContainer(): void
{
$bootstrap = Bootstrap::create();
$container = $this->createStub(ContainerInterface::class);
$this->setPrivate($bootstrap, 'container', $container);
self::assertSame($container, $bootstrap->initializeInfrastructure());
self::assertSame($container, $bootstrap->getContainer());
}
public function testCreateHttpAppReturnsPreloadedApp(): void
{
$bootstrap = Bootstrap::create();
$app = AppFactory::create();
$this->setPrivate($bootstrap, 'app', $app);
self::assertSame($app, $bootstrap->createHttpApp());
}
public function testInitializeReturnsPreloadedAppWhenAutoProvisionIsDisabled(): void
{
$_ENV['APP_AUTO_PROVISION'] = '0';
$bootstrap = Bootstrap::create();
$container = $this->createStub(ContainerInterface::class);
$app = AppFactory::create();
$this->setPrivate($bootstrap, 'container', $container);
$this->setPrivate($bootstrap, 'app', $app);
self::assertSame($app, $bootstrap->initialize());
}
private function setPrivate(Bootstrap $bootstrap, string $property, mixed $value): void
{
$reflection = new ReflectionProperty($bootstrap, $property);
$reflection->setAccessible(true);
$reflection->setValue($bootstrap, $value);
}
}