From 249dec784b3a5ffff1a57cacf67a33c99ce7d3f0 Mon Sep 17 00:00:00 2001 From: Quynh Nguyen Date: Thu, 13 Jun 2024 15:13:17 +0700 Subject: [PATCH 1/2] Test abort behavior --- .../Foundation/FoundationApplicationTest.php | 31 +++++++++++ tests/Foundation/FoundationHelpersTest.php | 54 +++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/tests/Foundation/FoundationApplicationTest.php b/tests/Foundation/FoundationApplicationTest.php index f23dc801f33d..af50b2d82e4f 100755 --- a/tests/Foundation/FoundationApplicationTest.php +++ b/tests/Foundation/FoundationApplicationTest.php @@ -11,6 +11,8 @@ use Mockery as m; use PHPUnit\Framework\TestCase; use stdClass; +use Symfony\Component\HttpKernel\Exception\HttpException; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; class FoundationApplicationTest extends TestCase { @@ -524,6 +526,35 @@ public function testUseConfigPath(): void $this->assertSame('bar', $app->make('config')->get('app.foo')); } + + public function testAbortThrowsNotFoundHttpException() + { + $this->expectException(NotFoundHttpException::class); + $this->expectExceptionMessage('Page was not found'); + + $app = new Application(); + $app->abort(404, 'Page was not found'); + } + + public function testAbortThrowsHttpException() + { + $this->expectException(HttpException::class); + $this->expectExceptionMessage('Request is bad'); + + $app = new Application(); + $app->abort(400, 'Request is bad'); + } + + public function testAbortAcceptsHeaders() + { + try { + $app = new Application(); + $app->abort(400, 'Bad request', ['X-FOO' => 'BAR']); + $this->fail(sprintf('abort must throw an %s.', HttpException::class)); + } catch (HttpException $exception) { + $this->assertSame(['X-FOO' => 'BAR'], $exception->getHeaders()); + } + } } class ApplicationBasicServiceProviderStub extends ServiceProvider diff --git a/tests/Foundation/FoundationHelpersTest.php b/tests/Foundation/FoundationHelpersTest.php index 79b7531a2f38..138ef5c8f7f0 100644 --- a/tests/Foundation/FoundationHelpersTest.php +++ b/tests/Foundation/FoundationHelpersTest.php @@ -3,13 +3,18 @@ namespace Illuminate\Tests\Foundation; use Exception; +use Illuminate\Container\Container; use Illuminate\Contracts\Config\Repository; +use Illuminate\Contracts\Support\Responsable; use Illuminate\Foundation\Application; use Illuminate\Foundation\Mix; +use Illuminate\Http\Exceptions\HttpResponseException; +use Illuminate\Http\Request; use Illuminate\Support\Str; use Mockery as m; use PHPUnit\Framework\TestCase; use stdClass; +use Symfony\Component\HttpFoundation\Response as SymfonyResponse; class FoundationHelpersTest extends TestCase { @@ -240,4 +245,53 @@ public function testMixIsSwappableForTests() $this->assertSame('expected', mix('asset.png')); } + + public function testAbortReceivesCodeAsSymfonyResponseInstance() + { + try { + abort($code = new SymfonyResponse()); + + $this->fail( + sprintf('abort function must throw %s when receiving code as Symfony Response instance.', HttpResponseException::class) + ); + } catch (HttpResponseException $ex) { + $this->assertSame($code, $ex->getResponse()); + } + } + + public function testAbortReceivesCodeAsResponableImplementation() + { + app()->instance('request', $request = Request::create('/')); + + try { + abort($code = new class implements Responsable { + public $request; + + public function toResponse($request) + { + $this->request = $request; + + return new SymfonyResponse(); + } + }); + + $this->fail( + sprintf('abort function must throw %s when receiving code as Responable implementation.', HttpResponseException::class) + ); + } catch (HttpResponseException $ex) { + $this->assertSame($request, $code->request); + } + } + + public function testAbortReceivesCodeAsInteger() + { + $app = m::mock(Application::class); + $app->shouldReceive('abort') + ->with($code = 400, $message = 'Bad request', $headers = ['X-FOO' => 'BAR']) + ->once(); + + Container::setInstance($app); + + abort($code, $message, $headers); + } } From 528166ba75e0b86486c3c01b4d7550691e2576b5 Mon Sep 17 00:00:00 2001 From: Quynh Nguyen Date: Thu, 13 Jun 2024 15:46:23 +0700 Subject: [PATCH 2/2] Fix code style --- tests/Foundation/FoundationHelpersTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Foundation/FoundationHelpersTest.php b/tests/Foundation/FoundationHelpersTest.php index 138ef5c8f7f0..90b56692715d 100644 --- a/tests/Foundation/FoundationHelpersTest.php +++ b/tests/Foundation/FoundationHelpersTest.php @@ -264,7 +264,8 @@ public function testAbortReceivesCodeAsResponableImplementation() app()->instance('request', $request = Request::create('/')); try { - abort($code = new class implements Responsable { + abort($code = new class implements Responsable + { public $request; public function toResponse($request)