diff --git a/tests/Shared/BootstrapTest.php b/tests/Shared/BootstrapTest.php new file mode 100644 index 0000000..d3e15a7 --- /dev/null +++ b/tests/Shared/BootstrapTest.php @@ -0,0 +1,89 @@ + */ + 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); + } +} diff --git a/tests/Shared/MailServiceTest.php b/tests/Shared/MailServiceTest.php new file mode 100644 index 0000000..1114b7f --- /dev/null +++ b/tests/Shared/MailServiceTest.php @@ -0,0 +1,70 @@ +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' => '

Hello {{ name }}

', + ])); + + 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); + } +}