11
11
12
12
namespace Tobyz \Tests \JsonApiServer \specification ;
13
13
14
+ use Tobyz \JsonApiServer \Exception \BadRequestException ;
15
+ use Tobyz \JsonApiServer \Exception \ConflictException ;
16
+ use Tobyz \JsonApiServer \Exception \ForbiddenException ;
17
+ use Tobyz \JsonApiServer \Exception \ResourceNotFoundException ;
14
18
use Tobyz \JsonApiServer \JsonApi ;
19
+ use Tobyz \JsonApiServer \Schema \Type ;
15
20
use Tobyz \Tests \JsonApiServer \AbstractTestCase ;
16
21
use Tobyz \Tests \JsonApiServer \MockAdapter ;
17
22
18
23
/**
19
- * @see https://jsonapi.org/format/1.0 /#crud-creating
24
+ * @see https://jsonapi.org/format/1.1 /#crud-creating
20
25
*/
21
26
class CreatingResourcesTest extends AbstractTestCase
22
27
{
@@ -25,55 +30,117 @@ class CreatingResourcesTest extends AbstractTestCase
25
30
*/
26
31
private $ api ;
27
32
28
- /**
29
- * @var MockAdapter
30
- */
31
- private $ adapter ;
32
-
33
33
public function setUp (): void
34
34
{
35
35
$ this ->api = new JsonApi ('http://example.com ' );
36
36
37
- $ this ->adapter = new MockAdapter ();
37
+ $ this ->api ->resourceType ('users ' , new MockAdapter (), function (Type $ type ) {
38
+ $ type ->creatable ();
39
+ $ type ->attribute ('name ' )->writable ();
40
+ $ type ->hasOne ('pet ' )->writable ();
41
+ });
38
42
}
39
43
40
44
public function test_bad_request_error_if_body_does_not_contain_data_type ()
41
45
{
42
- $ this ->markTestIncomplete ();
46
+ $ this ->expectException (BadRequestException::class);
47
+
48
+ $ this ->api ->handle (
49
+ $ this ->buildRequest ('POST ' , '/users ' )
50
+ ->withParsedBody ([
51
+ 'data ' => [],
52
+ ])
53
+ );
43
54
}
44
55
45
56
public function test_bad_request_error_if_relationship_does_not_contain_data ()
46
57
{
47
- $ this ->markTestIncomplete ();
58
+ $ this ->expectException (BadRequestException::class);
59
+
60
+ $ this ->api ->handle (
61
+ $ this ->buildRequest ('POST ' , '/users ' )
62
+ ->withParsedBody ([
63
+ 'data ' => [
64
+ 'type ' => 'users ' ,
65
+ 'relationships ' => [
66
+ 'pet ' => [],
67
+ ],
68
+ ],
69
+ ])
70
+ );
48
71
}
49
72
50
73
public function test_forbidden_error_if_client_generated_id_provided ()
51
74
{
52
- $ this ->markTestIncomplete ();
53
- }
75
+ $ this ->expectException (ForbiddenException::class);
54
76
55
- public function test_created_response_if_resource_successfully_created ()
56
- {
57
- $ this ->markTestIncomplete ();
77
+ $ this ->api ->handle (
78
+ $ this ->buildRequest ('POST ' , '/users ' )
79
+ ->withParsedBody ([
80
+ 'data ' => [
81
+ 'type ' => 'users ' ,
82
+ 'id ' => '1 ' ,
83
+ ],
84
+ ])
85
+ );
58
86
}
59
87
60
- public function test_created_response_includes_created_data ()
88
+ public function test_created_response_includes_created_data_and_location_header ()
61
89
{
62
- $ this ->markTestIncomplete ();
63
- }
90
+ $ response = $ this ->api ->handle (
91
+ $ this ->buildRequest ('POST ' , '/users ' )
92
+ ->withParsedBody ([
93
+ 'data ' => [
94
+ 'type ' => 'users ' ,
95
+ ],
96
+ ])
97
+ );
64
98
65
- public function test_created_response_includes_location_header_and_matches_self_link ()
66
- {
67
- $ this ->markTestIncomplete ();
99
+ $ this ->assertEquals (201 , $ response ->getStatusCode ());
100
+ $ this ->assertEquals ('http://example.com/users/1 ' , $ response ->getHeaderLine ('location ' ));
101
+
102
+ $ this ->assertJsonApiDocumentSubset ([
103
+ 'data ' => [
104
+ 'type ' => 'users ' ,
105
+ 'id ' => '1 ' ,
106
+ 'links ' => [
107
+ 'self ' => 'http://example.com/users/1 ' ,
108
+ ],
109
+ ],
110
+ ], $ response ->getBody ());
68
111
}
69
112
70
113
public function test_not_found_error_if_references_resource_that_does_not_exist ()
71
114
{
72
- $ this ->markTestIncomplete ();
115
+ $ this ->expectException (ResourceNotFoundException::class);
116
+
117
+ $ this ->api ->handle (
118
+ $ this ->buildRequest ('POST ' , '/users ' )
119
+ ->withParsedBody ([
120
+ 'data ' => [
121
+ 'type ' => 'users ' ,
122
+ 'relationships ' => [
123
+ 'pet ' => [
124
+ 'data ' => ['type ' => 'pets ' , 'id ' => '1 ' ],
125
+ ],
126
+ ],
127
+ ],
128
+ ])
129
+ );
73
130
}
74
131
75
132
public function test_conflict_error_if_type_does_not_match_endpoint ()
76
133
{
77
- $ this ->markTestIncomplete ();
134
+ $ this ->expectException (ConflictException::class);
135
+
136
+ $ this ->api ->handle (
137
+ $ this ->buildRequest ('POST ' , '/users ' )
138
+ ->withParsedBody ([
139
+ 'data ' => [
140
+ 'type ' => 'pets ' ,
141
+ 'id ' => '1 ' ,
142
+ ],
143
+ ])
144
+ );
78
145
}
79
146
}
0 commit comments