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

[10.x] Adds testing helpers for Precognition #48151

Merged
merged 9 commits into from Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php
Expand Up @@ -309,6 +309,16 @@ public function from(string $url)
return $this->withHeader('referer', $url);
}

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

/**
* Visit the given URI with a GET request.
*
Expand Down
21 changes: 21 additions & 0 deletions src/Illuminate/Testing/TestResponse.php
Expand Up @@ -1417,6 +1417,27 @@ public function assertSessionMissing($key)
return $this;
}

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

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

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

return $this;
}

/**
* Get the current session store.
*
Expand Down
14 changes: 14 additions & 0 deletions tests/Foundation/Testing/Concerns/MakesHttpRequestsTest.php
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
Expand Up @@ -1035,6 +1035,32 @@ public function testAssertHeaderMissing()
$response->assertHeaderMissing('Location');
}

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

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

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

$response->assertPrecognitionSuccess();
}

public function testAssertPrecognitionSuccessfulWithIncorrectValue()
{
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('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->assertPrecognitionSuccess();
}

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