2
2
3
3
namespace Tobyz \Tests \JsonApiServer \feature ;
4
4
5
- use stdClass ;
6
5
use Tobyz \JsonApiServer \Endpoint \Create ;
7
6
use Tobyz \JsonApiServer \Endpoint \Show ;
8
7
use Tobyz \JsonApiServer \Exception \BadRequestException ;
9
8
use Tobyz \JsonApiServer \Exception \UnprocessableEntityException ;
10
9
use Tobyz \JsonApiServer \JsonApi ;
11
10
use Tobyz \JsonApiServer \Schema \Field \ToOne ;
12
11
use Tobyz \Tests \JsonApiServer \AbstractTestCase ;
12
+ use Tobyz \Tests \JsonApiServer \MockCollection ;
13
13
use Tobyz \Tests \JsonApiServer \MockResource ;
14
14
15
15
class RelationshipToOneTest extends AbstractTestCase
@@ -213,16 +213,18 @@ public function test_to_one_create_invalid_type()
213
213
214
214
public function test_to_one_create_polymorphic ()
215
215
{
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
+ );
217
219
218
220
$ this ->api ->resource (
219
- new MockResource (
221
+ $ resource = new MockResource (
220
222
'users ' ,
221
223
models: [(object ) ['id ' => '1 ' ]],
222
224
endpoints: [Create::make ()],
223
225
fields: [
224
226
ToOne::make ('friend ' )
225
- ->type (['users ' , stdClass::class => 'animals ' ])
227
+ ->type (['users ' , 'animals ' ])
226
228
->writable (),
227
229
],
228
230
),
@@ -238,6 +240,44 @@ public function test_to_one_create_polymorphic()
238
240
);
239
241
240
242
$ 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 );
241
281
}
242
282
243
283
public function test_to_one_create_null_not_nullable ()
0 commit comments