diff --git a/tests/Foundation/FoundationApplicationTest.php b/tests/Foundation/FoundationApplicationTest.php index d5aba462c2b..d403e4a0702 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 { @@ -578,6 +580,35 @@ public function testMergingConfig(): void $this->assertSame(['overwrite' => true], $config->get('queue.connections.database')); $this->assertSame(['merge' => true], $config->get('queue.connections.new')); } + + 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 79b7531a2f3..90b56692715 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,54 @@ 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); + } }