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 1 commit
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
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
*
* @return $this
*/
public function usingPrecognition()
{
return $this->withHeader('Precognition', 'true');
}

timacdonald marked this conversation as resolved.
Show resolved Hide resolved
/**
* Visit the given URI with a GET request.
*
Expand Down
19 changes: 19 additions & 0 deletions src/Illuminate/Testing/TestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -1417,6 +1417,25 @@ public function assertSessionMissing($key)
return $this;
}

/**
* Assert that the Precognition request was successful
*
* @return $this
*/
public function assertPrecognitionSuccess()
{
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`.'
);
peterfox marked this conversation as resolved.
Show resolved Hide resolved

return $this;
}

/**
* Get the current session store.
*
Expand Down
7 changes: 7 additions & 0 deletions tests/Foundation/Testing/Concerns/MakesHttpRequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@ public function testFollowingRedirectsTerminatesInExpectedOrder()

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

public function testUsingPrecognition()
{
$this->usingPrecognition();

$this->assertSame('true', $this->defaultHeaders['Precognition']);
timacdonald marked this conversation as resolved.
Show resolved Hide resolved
}
}

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('Precognition-Success Header not present on response.');

$baseResponse = new Response();

$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`.');
timacdonald marked this conversation as resolved.
Show resolved Hide resolved

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

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

$response->assertPrecognitionSuccess();
}

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