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

Commit 68e0b0a

Browse files
authored
Merge pull request #14 from eerison/add_bad_request
Add bed request assert
2 parents 04471a0 + 4f6a022 commit 68e0b0a

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,13 @@ Functions
143143
- `delete()`
144144
- `findIriBy()`
145145
- `assertResponseIsSuccessful()`
146+
- `assertResourceIsBadRequest()`
146147
- `assertResourceIsNotFound()`
147148
- `assertResourceIsUnauthorized()`
148149
- `assertResourceIsForbidden()`
149150
- `assertMatchesResourceItemJsonSchema(Your::class)`
150151
- `assertMatchesResourceCollectionJsonSchema(Your::class)`
152+
- `assertResourceIsBadRequest(Response::HTTP_BAD_REQUEST)`
151153

152154
## Testing
153155

src/Autoload.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ function assertResourceIsForbidden(): void
9090
test()->assertResourceIsForbidden();
9191
}
9292

93+
function assertResourceIsBadRequest(): void
94+
{
95+
test()->assertResourceIsBadRequest();
96+
}
97+
98+
function assertResponseStatusCodeSame(int $statusCode): void
99+
{
100+
test()->assertResponseStatusCodeSame($statusCode);
101+
}
102+
93103
function assertMatchesResourceCollectionJsonSchema(string $object): void
94104
{
95105
ApiPlatform::assertMatchesResourceCollectionJsonSchema($object);

src/ResourceShortcuts.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,16 @@ public function assertResponseIsSuccessful(): TestCase
2424
return $this;
2525
}
2626

27-
public function assertResourceIsNotFound(): TestCase
27+
public function assertResourceIsBadRequest(): TestCase
2828
{
29-
test()->assertResponseStatusCodeSame(404);
29+
test()->assertResponseStatusCodeSame(400);
30+
31+
return $this;
32+
}
33+
34+
public function assertResourceIsUnauthorized(): TestCase
35+
{
36+
test()->assertResponseStatusCodeSame(401);
3037

3138
return $this;
3239
}
@@ -38,9 +45,9 @@ public function assertResourceIsForbidden(): TestCase
3845
return $this;
3946
}
4047

41-
public function assertResourceIsUnauthorized(): TestCase
48+
public function assertResourceIsNotFound(): TestCase
4249
{
43-
test()->assertResponseStatusCodeSame(401);
50+
test()->assertResponseStatusCodeSame(404);
4451

4552
return $this;
4653
}

tests/ResourceStatusCodeTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<?php
22

3+
use function Eerison\PestPluginApiPlatform\assertResourceIsBadRequest;
34
use function Eerison\PestPluginApiPlatform\assertResourceIsForbidden;
45
use function Eerison\PestPluginApiPlatform\assertResourceIsNotFound;
56
use function Eerison\PestPluginApiPlatform\assertResourceIsUnauthorized;
67
use function Eerison\PestPluginApiPlatform\assertResponseIsSuccessful;
8+
use function Eerison\PestPluginApiPlatform\assertResponseStatusCodeSame;
79
use function Eerison\PestPluginApiPlatform\get;
810

911
it('can get resource with status code 200.')
@@ -35,6 +37,14 @@
3537
->group('foo')
3638
;
3739

40+
test('assertResponseStatusCodeSame method')
41+
->get('/foo/response/400')
42+
->assertResponseStatusCodeSame(400);
43+
44+
test('assertResourceIsBadRequest method')
45+
->get('/foo/response/400')
46+
->assertResourceIsBadRequest();
47+
3848
it('can use assertResponseIsSuccessful as function', function () {
3949
get('/foo/response/204');
4050
assertResponseIsSuccessful();
@@ -72,3 +82,13 @@
7282
get('/foo/response/403');
7383
assertResourceIsForbidden();
7484
});
85+
86+
test('assertResponseStatusCodeSame method.', function () {
87+
get('/foo/response/403');
88+
assertResponseStatusCodeSame(403);
89+
});
90+
91+
test('bad request function.', function () {
92+
get('/foo/response/400');
93+
assertResourceIsBadRequest();
94+
});

0 commit comments

Comments
 (0)