Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Test abort behavior #51800

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions tests/Foundation/FoundationApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
Expand Down
55 changes: 55 additions & 0 deletions tests/Foundation/FoundationHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}
}