diff --git a/docs/content/en/changelog.md b/docs/content/en/changelog.md index 81ef4123..f80b9d59 100644 --- a/docs/content/en/changelog.md +++ b/docs/content/en/changelog.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). **Added** +- `toHaveLocation()` expectation - `toConfirmCreation()` expectation - `toBeRedirectToSignedRoute()` expectation - `toHaveJson()` expectation diff --git a/docs/content/en/expectations/responses.md b/docs/content/en/expectations/responses.md index bee83541..63688e69 100644 --- a/docs/content/en/expectations/responses.md +++ b/docs/content/en/expectations/responses.md @@ -86,7 +86,7 @@ Assert that the response offers a file download. ```php expect(get('/reports/last.pdf'))->toBeDownload(); ``` -< + ### `toHaveStatus()` Assert that the response has the given status code. @@ -95,6 +95,14 @@ Assert that the response has the given status code. expect(post('/comment'))->toHaveStatus(201); ``` +### `toHaveLocation()` + +Assert that the current location header matches the given URI. + +```php +expect(get('/secret'))->toHaveLocation('/login'); + ``` + ### `toHaveValid()` Assert that the response doesn't have the given validation error keys. diff --git a/src/Expectations/Response.php b/src/Expectations/Response.php index 130b4cc2..f943e6d5 100644 --- a/src/Expectations/Response.php +++ b/src/Expectations/Response.php @@ -395,3 +395,17 @@ function ($keys = null, string $errorBag = 'default', string $responseKey = 'err return $this; }, ); + +expect()->extend( + 'toHaveLocation', + /** + * Assert that the current location header matches the given URI. + */ + function (string $uri): Expectation { + /** @var TestResponse $response */ + $response = $this->value; + $response->assertLocation($uri); + + return $this; + }, +); diff --git a/tests/Expect/Response/toHaveLocation.php b/tests/Expect/Response/toHaveLocation.php new file mode 100644 index 00000000..6d66dc9c --- /dev/null +++ b/tests/Expect/Response/toHaveLocation.php @@ -0,0 +1,36 @@ +header('Location', '/foo'); + }); + + expect($response)->toHaveLocation('/foo'); +}); + +test('fails', function () { + $response = build_response(function (Response $response) { + $response->header('Location', '/foo'); + }); + + expect($response)->toHaveLocation('/bar'); +})->throws(ExpectationFailedException::class, 'Failed asserting that two strings are equal'); + +test('pass with negation', function () { + $response = build_response(function (Response $response) { + $response->header('Location', '/foo'); + }); + + expect($response)->not->toHaveLocation('/bar'); +}); + +test('fails with negation', function () { + $response = build_response(function (Response $response) { + $response->header('Location', '/foo'); + }); + + expect($response)->not->toHaveLocation('/foo'); +})->throws(ExpectationFailedException::class);