Working state

This commit is contained in:
julien
2026-03-16 08:34:22 +01:00
parent ebc477877e
commit 81200f81cd
2 changed files with 159 additions and 0 deletions

View File

@@ -0,0 +1,89 @@
<?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);
}
}

View File

@@ -0,0 +1,70 @@
<?php
declare(strict_types=1);
namespace Tests\Shared;
use App\Shared\Mail\MailService;
use PHPMailer\PHPMailer\PHPMailer;
use PHPUnit\Framework\TestCase;
use Slim\Views\Twig;
use Twig\Loader\ArrayLoader;
#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
final class MailServiceTest extends TestCase
{
public function testCreateMailerUsesSslConfiguration(): void
{
$service = $this->createService('ssl', 465);
/** @var PHPMailer $mailer */
$mailer = $this->invokeCreateMailer($service);
self::assertSame('smtp', $mailer->Mailer);
self::assertSame('smtp.example.test', $mailer->Host);
self::assertTrue($mailer->SMTPAuth);
self::assertSame('mailer-user', $mailer->Username);
self::assertSame('mailer-pass', $mailer->Password);
self::assertSame(PHPMailer::ENCRYPTION_SMTPS, $mailer->SMTPSecure);
self::assertSame(465, $mailer->Port);
self::assertSame(PHPMailer::CHARSET_UTF8, $mailer->CharSet);
self::assertSame('noreply@example.test', $mailer->From);
self::assertSame('Slim Blog', $mailer->FromName);
}
public function testCreateMailerUsesStartTlsWhenEncryptionIsNotSsl(): void
{
$service = $this->createService('tls', 587);
/** @var PHPMailer $mailer */
$mailer = $this->invokeCreateMailer($service);
self::assertSame(PHPMailer::ENCRYPTION_STARTTLS, $mailer->SMTPSecure);
self::assertSame(587, $mailer->Port);
}
private function createService(string $encryption, int $port): MailService
{
$twig = new Twig(new ArrayLoader([
'emails/test.twig' => '<p>Hello {{ name }}</p>',
]));
return new MailService(
$twig,
'smtp.example.test',
$port,
'mailer-user',
'mailer-pass',
$encryption,
'noreply@example.test',
'Slim Blog',
);
}
private function invokeCreateMailer(MailService $service): mixed
{
$method = new \ReflectionMethod($service, 'createMailer');
$method->setAccessible(true);
return $method->invoke($service);
}
}