diff --git a/app/services.php b/app/services.php index b72874bd16..c79a6214cf 100644 --- a/app/services.php +++ b/app/services.php @@ -73,7 +73,10 @@ use Symfony\Component\ExpressionLanguage\ExpressionLanguage; return [ Advisor::class => ['class' => Advisor::class, 'arguments' => [DatabaseInterface::class, ExpressionLanguage::class]], - Application::class => ['class' => Application::class, 'arguments' => [ResponseFactory::class]], + Application::class => [ + 'class' => Application::class, + 'arguments' => [ResponseFactory::class, ResponseRenderer::class], + ], BrowseForeigners::class => [ 'class' => BrowseForeigners::class, 'arguments' => [Template::class, Config::class, ThemeManager::class], diff --git a/src/Application.php b/src/Application.php index ca99d9cd06..414f811707 100644 --- a/src/Application.php +++ b/src/Application.php @@ -55,7 +55,7 @@ use function sprintf; readonly class Application { - public function __construct(private ResponseFactory $responseFactory) + public function __construct(private ResponseFactory $responseFactory, private ResponseRenderer $responseRenderer) { } @@ -67,7 +67,7 @@ readonly class Application public function run(bool $isSetupPage = false): void { $container = ContainerBuilder::getContainer(); - $requestHandler = new QueueRequestHandler($container, new ApplicationHandler($this)); + $requestHandler = new QueueRequestHandler($container, new ApplicationHandler($this, $this->responseRenderer)); $requestHandler->add(ErrorHandling::class); $requestHandler->add(OutputBuffering::class); $requestHandler->add(PhpExtensionsChecking::class); diff --git a/src/Http/Handler/ApplicationHandler.php b/src/Http/Handler/ApplicationHandler.php index 98e82ce190..c773457bf5 100644 --- a/src/Http/Handler/ApplicationHandler.php +++ b/src/Http/Handler/ApplicationHandler.php @@ -14,9 +14,9 @@ use Psr\Http\Server\RequestHandlerInterface; use function assert; -final class ApplicationHandler implements RequestHandlerInterface +final readonly class ApplicationHandler implements RequestHandlerInterface { - public function __construct(private readonly Application $application) + public function __construct(private Application $application, private ResponseRenderer $responseRenderer) { } @@ -26,7 +26,7 @@ final class ApplicationHandler implements RequestHandlerInterface try { return $this->application->handle($request); } catch (ExitException) { - return ResponseRenderer::getInstance()->response(); + return $this->responseRenderer->response(); } } } diff --git a/tests/unit/ApplicationTest.php b/tests/unit/ApplicationTest.php index 0804f1e01b..d87a1580f6 100644 --- a/tests/unit/ApplicationTest.php +++ b/tests/unit/ApplicationTest.php @@ -9,6 +9,7 @@ use PhpMyAdmin\Config; use PhpMyAdmin\Container\ContainerBuilder; use PhpMyAdmin\Exceptions\ConfigException; use PhpMyAdmin\Http\Factory\ResponseFactory; +use PhpMyAdmin\ResponseRenderer; use PhpMyAdmin\Template; use PHPUnit\Framework\Attributes\BackupStaticProperties; use PHPUnit\Framework\Attributes\CoversClass; @@ -18,6 +19,7 @@ final class ApplicationTest extends AbstractTestCase { public function testInit(): void { + ContainerBuilder::$container = null; $application = ContainerBuilder::getContainer()->get(Application::class); self::assertSame($application, Application::init()); } @@ -39,7 +41,7 @@ final class ApplicationTest extends AbstractTestCase 'error_message' => 'Failed to load phpMyAdmin configuration.', ]); - $application = new Application(ResponseFactory::create()); + $application = new Application(ResponseFactory::create(), self::createStub(ResponseRenderer::class)); $application->run(); $output = $this->getActualOutputForAssertion(); diff --git a/tests/unit/Http/Handler/ApplicationHandlerTest.php b/tests/unit/Http/Handler/ApplicationHandlerTest.php index 22295b00ba..194e41c47e 100644 --- a/tests/unit/Http/Handler/ApplicationHandlerTest.php +++ b/tests/unit/Http/Handler/ApplicationHandlerTest.php @@ -13,7 +13,6 @@ use PhpMyAdmin\ResponseRenderer; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; use Psr\Http\Message\ResponseInterface; -use ReflectionProperty; #[CoversClass(ApplicationHandler::class)] final class ApplicationHandlerTest extends TestCase @@ -22,16 +21,13 @@ final class ApplicationHandlerTest extends TestCase { $responseRendererMock = self::createMock(ResponseRenderer::class); $responseRendererMock->expects(self::never())->method('response'); - $reflectionProperty = new ReflectionProperty(ResponseRenderer::class, 'instance'); - $reflectionProperty->setValue(null, $responseRendererMock); $request = self::createStub(ServerRequest::class); $responseStub = new Response(self::createStub(ResponseInterface::class)); $appMock = self::createMock(Application::class); $appMock->expects(self::once())->method('handle')->with($request)->willReturn($responseStub); - $handler = new ApplicationHandler($appMock); + $handler = new ApplicationHandler($appMock, $responseRendererMock); $response = $handler->handle($request); self::assertSame($response, $responseStub); - $reflectionProperty->setValue(null, null); } public function testHandleThrowsExit(): void @@ -39,14 +35,11 @@ final class ApplicationHandlerTest extends TestCase $responseStub = new Response(self::createStub(ResponseInterface::class)); $responseRendererMock = self::createMock(ResponseRenderer::class); $responseRendererMock->expects(self::once())->method('response')->willReturn($responseStub); - $reflectionProperty = new ReflectionProperty(ResponseRenderer::class, 'instance'); - $reflectionProperty->setValue(null, $responseRendererMock); $request = self::createStub(ServerRequest::class); $appMock = self::createMock(Application::class); $appMock->expects(self::once())->method('handle')->with($request)->willThrowException(new ExitException()); - $handler = new ApplicationHandler($appMock); + $handler = new ApplicationHandler($appMock, $responseRendererMock); $response = $handler->handle($request); self::assertSame($response, $responseStub); - $reflectionProperty->setValue(null, null); } }