Skip to content

Commit

Permalink
Reset ResponseRenderer::$instance after test in ApplicationHandlerTest
Browse files Browse the repository at this point in the history
Not resetting this value causes other tests to fail when running then in
a specific order.

Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed Mar 29, 2024
1 parent d0be67b commit 52b39e0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
16 changes: 9 additions & 7 deletions tests/unit/Http/Handler/ApplicationHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use PhpMyAdmin\Http\Response;
use PhpMyAdmin\Http\ServerRequest;
use PhpMyAdmin\ResponseRenderer;
use PHPUnit\Framework\Attributes\BackupStaticProperties;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -19,48 +18,51 @@
#[CoversClass(ApplicationHandler::class)]
final class ApplicationHandlerTest extends TestCase
{
#[BackupStaticProperties(true)]
public function testHandleReturnsResponse(): void
{
$responseRendererMock = self::createMock(ResponseRenderer::class);
$responseRendererMock->expects(self::never())->method('response');
(new ReflectionProperty(ResponseRenderer::class, 'instance'))->setValue(null, $responseRendererMock);
$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);
$response = $handler->handle($request);
self::assertSame($response, $responseStub);
$reflectionProperty->setValue(null, null);
}

#[BackupStaticProperties(true)]
public function testHandleThrowsExit(): void
{
$responseStub = new Response(self::createStub(ResponseInterface::class));
$responseRendererMock = self::createMock(ResponseRenderer::class);
$responseRendererMock->expects(self::once())->method('response')->willReturn($responseStub);
(new ReflectionProperty(ResponseRenderer::class, 'instance'))->setValue(null, $responseRendererMock);
$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);
$response = $handler->handle($request);
self::assertSame($response, $responseStub);
$reflectionProperty->setValue(null, null);
}

#[BackupStaticProperties(true)]
public function testHandleReturnsNull(): void
{
$responseStub = new Response(self::createStub(ResponseInterface::class));
$responseRendererMock = self::createMock(ResponseRenderer::class);
$responseRendererMock->expects(self::once())->method('response')->willReturn($responseStub);
(new ReflectionProperty(ResponseRenderer::class, 'instance'))->setValue(null, $responseRendererMock);
$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)->willReturn(null);
$handler = new ApplicationHandler($appMock);
$response = $handler->handle($request);
self::assertSame($response, $responseStub);
$reflectionProperty->setValue(null, null);
}
}
6 changes: 0 additions & 6 deletions tests/unit/Stubs/ResponseRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use PhpMyAdmin\ConfigStorage\Relation;
use PhpMyAdmin\Console;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Exceptions\ExitException;
use PhpMyAdmin\Footer;
use PhpMyAdmin\Header;
use PhpMyAdmin\Http\Factory\ResponseFactory;
Expand Down Expand Up @@ -177,11 +176,6 @@ public function isDisabled(): bool
return $this->isDisabled;
}

public function callExit(string $message = ''): never
{
throw new ExitException($message);
}

public function getResponse(): Response
{
return $this->response;
Expand Down

0 comments on commit 52b39e0

Please sign in to comment.