Skip to content

Commit

Permalink
Merge pull request #38 from def-studio/toHaveLocation
Browse files Browse the repository at this point in the history
adds toHaveLocation() expectation
  • Loading branch information
fabio-ivona authored Oct 20, 2021
2 parents 67db2f9 + 49ac508 commit 93f8df7
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/content/en/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

**Added**

- `toHaveLocation()` expectation
- `toConfirmCreation()` expectation
- `toBeRedirectToSignedRoute()` expectation
- `toHaveJson()` expectation
Expand Down
10 changes: 9 additions & 1 deletion docs/content/en/expectations/responses.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
14 changes: 14 additions & 0 deletions src/Expectations/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
);
36 changes: 36 additions & 0 deletions tests/Expect/Response/toHaveLocation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use Illuminate\Http\Response;
use PHPUnit\Framework\ExpectationFailedException;

test('pass', function () {
$response = build_response(function (Response $response) {
$response->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);

0 comments on commit 93f8df7

Please sign in to comment.