22
33namespace Tobyz \Tests \JsonApiServer \feature ;
44
5- use stdClass ;
65use Tobyz \JsonApiServer \Endpoint \Create ;
76use Tobyz \JsonApiServer \Endpoint \Show ;
87use Tobyz \JsonApiServer \Exception \BadRequestException ;
98use Tobyz \JsonApiServer \Exception \UnprocessableEntityException ;
109use Tobyz \JsonApiServer \JsonApi ;
1110use Tobyz \JsonApiServer \Schema \Field \ToOne ;
1211use Tobyz \Tests \JsonApiServer \AbstractTestCase ;
12+ use Tobyz \Tests \JsonApiServer \MockCollection ;
1313use Tobyz \Tests \JsonApiServer \MockResource ;
1414
1515class RelationshipToOneTest extends AbstractTestCase
@@ -213,16 +213,18 @@ public function test_to_one_create_invalid_type()
213213
214214 public function test_to_one_create_polymorphic ()
215215 {
216- $ this ->api ->resource (new MockResource ('animals ' , models: [(object ) ['id ' => '1 ' ]]));
216+ $ this ->api ->resource (
217+ new MockResource ('animals ' , models: [($ friend = (object ) ['id ' => '1 ' ])]),
218+ );
217219
218220 $ this ->api ->resource (
219- new MockResource (
221+ $ resource = new MockResource (
220222 'users ' ,
221223 models: [(object ) ['id ' => '1 ' ]],
222224 endpoints: [Create::make ()],
223225 fields: [
224226 ToOne::make ('friend ' )
225- ->type (['users ' , stdClass::class => 'animals ' ])
227+ ->type (['users ' , 'animals ' ])
226228 ->writable (),
227229 ],
228230 ),
@@ -238,6 +240,44 @@ public function test_to_one_create_polymorphic()
238240 );
239241
240242 $ this ->assertEquals (201 , $ response ->getStatusCode ());
243+ $ this ->assertSame ($ friend , $ resource ->models [1 ]->friend );
244+ }
245+
246+ public function test_to_one_create_collection ()
247+ {
248+ $ this ->api ->collection (
249+ new MockCollection ('animals ' , [
250+ 'dogs ' => [(object ) ['id ' => '1 ' ]],
251+ 'cats ' => [($ cat = (object ) ['id ' => '1 ' ])],
252+ ]),
253+ );
254+
255+ $ this ->api ->resource (new MockResource ('cats ' , models: [$ cat ]));
256+
257+ $ this ->api ->resource (
258+ $ resource = new MockResource (
259+ 'users ' ,
260+ models: [(object ) ['id ' => '1 ' ]],
261+ endpoints: [Create::make ()],
262+ fields: [
263+ ToOne::make ('pet ' )
264+ ->collection ('animals ' )
265+ ->writable (),
266+ ],
267+ ),
268+ );
269+
270+ $ response = $ this ->api ->handle (
271+ $ this ->buildRequest ('POST ' , '/users ' )->withParsedBody ([
272+ 'data ' => [
273+ 'type ' => 'users ' ,
274+ 'relationships ' => ['pet ' => ['data ' => ['type ' => 'cats ' , 'id ' => '1 ' ]]],
275+ ],
276+ ]),
277+ );
278+
279+ $ this ->assertEquals (201 , $ response ->getStatusCode ());
280+ $ this ->assertSame ($ cat , $ resource ->models [1 ]->pet );
241281 }
242282
243283 public function test_to_one_create_null_not_nullable ()
0 commit comments