Skip to content

Commit

Permalink
[10.x] Adds testing helpers for Precognition (#48151)
Browse files Browse the repository at this point in the history
* adds testing helpers for Precognition

* Review changes

* Fix comment

* fix comment

* Add functional test to precog test

* Add assertNoContent to response assertion

* Fixes

* Update MakesHttpRequests.php

* formatting

---------

Co-authored-by: Tim MacDonald <hello@timacdonald.me>
Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
3 people committed Aug 30, 2023
1 parent 60c1330 commit 47767f6
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,16 @@ public function from(string $url)
return $this->withHeader('referer', $url);
}

/**
* Set the Precognition header to "true".
*
* @return $this
*/
public function withPrecognition()
{
return $this->withHeader('Precognition', 'true');
}

/**
* Visit the given URI with a GET request.
*
Expand Down
23 changes: 23 additions & 0 deletions src/Illuminate/Testing/TestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,29 @@ public function assertSuccessful()
return $this;
}

/**
* Assert that the Precognition request was successful.
*
* @return $this
*/
public function assertSuccessfulPrecognition()
{
$this->assertNoContent();

PHPUnit::assertTrue(
$this->headers->has('Precognition-Success'),
'Header [Precognition-Success] not present on response.'
);

PHPUnit::assertSame(
'true',
$this->headers->get('Precognition-Success'),
'The Precognition-Success header was found, but the value is not `true`.'
);

return $this;
}

/**
* Assert that the response is a server error.
*
Expand Down
14 changes: 14 additions & 0 deletions tests/Foundation/Testing/Concerns/MakesHttpRequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Contracts\Routing\Registrar;
use Illuminate\Contracts\Routing\UrlGenerator;
use Illuminate\Foundation\Http\Middleware\HandlePrecognitiveRequests;
use Illuminate\Http\RedirectResponse;
use Orchestra\Testbench\TestCase;

Expand Down Expand Up @@ -186,6 +187,19 @@ public function testFollowingRedirectsTerminatesInExpectedOrder()

$this->assertEquals(['from', 'to'], $callOrder);
}

public function testWithPrecognition()
{
$this->withPrecognition();
$this->assertSame('true', $this->defaultHeaders['Precognition']);

$this->app->make(Registrar::class)
->get('test-route', fn () => 'ok')->middleware(HandlePrecognitiveRequests::class);
$this->get('test-route')
->assertStatus(204)
->assertHeader('Precognition', 'true')
->assertHeader('Precognition-Success', 'true');
}
}

class MyMiddleware
Expand Down
26 changes: 26 additions & 0 deletions tests/Testing/TestResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,32 @@ public function testAssertHeaderMissing()
$response->assertHeaderMissing('Location');
}

public function testAssertPrecognitionSuccessfulWithMissingHeader()
{
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Header [Precognition-Success] not present on response.');

$baseResponse = new Response('', 204);

$response = TestResponse::fromBaseResponse($baseResponse);

$response->assertSuccessfulPrecognition();
}

public function testAssertPrecognitionSuccessfulWithIncorrectValue()
{
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('The Precognition-Success header was found, but the value is not `true`.');

$baseResponse = tap(new Response('', 204), function ($response) {
$response->header('Precognition-Success', '');
});

$response = TestResponse::fromBaseResponse($baseResponse);

$response->assertSuccessfulPrecognition();
}

public function testAssertJsonWithArray()
{
$response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceStub));
Expand Down

0 comments on commit 47767f6

Please sign in to comment.