Skip to content
This repository was archived by the owner on Aug 26, 2022. It is now read-only.

Commit a2a2a63

Browse files
authored
Merge pull request #16 from eerison/add-UnprocessableEntity
Add assertResourceIsUnprocessableEntity
2 parents 68e0b0a + bff59fc commit a2a2a63

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,14 @@ Functions
149149
- `assertResourceIsForbidden()`
150150
- `assertMatchesResourceItemJsonSchema(Your::class)`
151151
- `assertMatchesResourceCollectionJsonSchema(Your::class)`
152-
- `assertResourceIsBadRequest(Response::HTTP_BAD_REQUEST)`
152+
- `assertResourceIsBadRequest()`
153+
- `assertResourceIsUnprocessableEntity()`
154+
- `expectResponseContent()`
155+
156+
> if you want to test `expectResponseContent` and not return an exception pass `false` as parameter, example: `expectResponseContent(false)`
157+
158+
159+
153160

154161
## Testing
155162

src/Autoload.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ function assertResourceIsBadRequest(): void
9595
test()->assertResourceIsBadRequest();
9696
}
9797

98+
function assertResourceIsUnprocessableEntity(): void
99+
{
100+
test()->assertResourceIsUnprocessableEntity();
101+
}
102+
98103
function assertResponseStatusCodeSame(int $statusCode): void
99104
{
100105
test()->assertResponseStatusCodeSame($statusCode);

src/ResourceShortcuts.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ public function assertResourceIsUnauthorized(): TestCase
3838
return $this;
3939
}
4040

41+
public function assertResourceIsUnprocessableEntity(): TestCase
42+
{
43+
test()->assertResponseStatusCodeSame(422);
44+
45+
return $this;
46+
}
47+
4148
public function assertResourceIsForbidden(): TestCase
4249
{
4350
test()->assertResponseStatusCodeSame(403);
@@ -55,8 +62,8 @@ public function assertResourceIsNotFound(): TestCase
5562
/**
5663
* @return Expectation|Extendable
5764
*/
58-
public function expectResponseContent()
65+
public function expectResponseContent(bool $throw = true)
5966
{
60-
return expect($this->response()->getContent());
67+
return expect($this->response()->getContent($throw));
6168
}
6269
}

tests/ResourceStatusCodeTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use function Eerison\PestPluginApiPlatform\assertResourceIsForbidden;
55
use function Eerison\PestPluginApiPlatform\assertResourceIsNotFound;
66
use function Eerison\PestPluginApiPlatform\assertResourceIsUnauthorized;
7+
use function Eerison\PestPluginApiPlatform\assertResourceIsUnprocessableEntity;
78
use function Eerison\PestPluginApiPlatform\assertResponseIsSuccessful;
89
use function Eerison\PestPluginApiPlatform\assertResponseStatusCodeSame;
910
use function Eerison\PestPluginApiPlatform\get;
@@ -45,6 +46,10 @@
4546
->get('/foo/response/400')
4647
->assertResourceIsBadRequest();
4748

49+
test('assertResourceIsUnprocessableEntity method')
50+
->get('/foo/response/422')
51+
->assertResourceIsUnprocessableEntity();
52+
4853
it('can use assertResponseIsSuccessful as function', function () {
4954
get('/foo/response/204');
5055
assertResponseIsSuccessful();
@@ -92,3 +97,8 @@
9297
get('/foo/response/400');
9398
assertResourceIsBadRequest();
9499
});
100+
101+
test('Unprocessable entity function.', function () {
102+
get('/foo/response/422');
103+
assertResourceIsUnprocessableEntity();
104+
});

0 commit comments

Comments
 (0)