Files
slim-blog/tests/Shared/BootstrapTest.php
2026-03-16 08:34:22 +01:00

90 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Shared;
use App\Shared\Bootstrap;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Slim\Factory\AppFactory;
#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
final class BootstrapTest extends TestCase
{
/** @var array<string, string> */
private array $envBackup = [];
protected function setUp(): void
{
$this->envBackup = $_ENV;
}
protected function tearDown(): void
{
$_ENV = $this->envBackup;
}
public function testGetContainerReturnsPreloadedContainer(): void
{
$bootstrap = Bootstrap::create();
$container = new class implements ContainerInterface {
public function get(string $id): mixed
{
throw new \RuntimeException('Not expected');
}
public function has(string $id): bool
{
return false;
}
};
$this->setPrivateProperty($bootstrap, 'container', $container);
self::assertSame($container, $bootstrap->getContainer());
self::assertSame($container, $bootstrap->initializeInfrastructure());
}
public function testCreateHttpAppReturnsPreloadedApp(): void
{
$bootstrap = Bootstrap::create();
$app = AppFactory::create();
$this->setPrivateProperty($bootstrap, 'app', $app);
self::assertSame($app, $bootstrap->createHttpApp());
}
public function testInitializeReturnsPreloadedAppWhenAutoProvisioningDisabled(): void
{
$_ENV['APP_ENV'] = 'production';
$_ENV['APP_AUTO_PROVISION'] = '0';
$bootstrap = Bootstrap::create();
$container = new class implements ContainerInterface {
public function get(string $id): mixed
{
throw new \RuntimeException('Not expected');
}
public function has(string $id): bool
{
return false;
}
};
$app = AppFactory::create();
$this->setPrivateProperty($bootstrap, 'container', $container);
$this->setPrivateProperty($bootstrap, 'app', $app);
self::assertSame($app, $bootstrap->initialize());
}
private function setPrivateProperty(object $object, string $property, mixed $value): void
{
$reflection = new \ReflectionProperty($object, $property);
$reflection->setAccessible(true);
$reflection->setValue($object, $value);
}
}