@@ -102,6 +102,28 @@ public function it_hydrates_hasone_relationships_with_attributes()
102
102
$ this ->assertArrayHasKey ('hasone_relation ' , $ item ->toJsonApiArray ()['relationships ' ]);
103
103
}
104
104
105
+ /**
106
+ * @test
107
+ */
108
+ public function it_dissociates_hasone_relationships_when_null ()
109
+ {
110
+ $ data = [
111
+ 'hasone_relation ' => null ,
112
+ ];
113
+
114
+ $ item = new WithRelationshipItem ();
115
+ $ item = $ this ->getItemHydrator ()->hydrate ($ item , $ data );
116
+
117
+ /** @var \Swis\JsonApi\Client\Relations\HasOneRelation $hasOne */
118
+ $ hasOne = $ item ->getRelation ('hasone_relation ' );
119
+ $ this ->assertInstanceOf (HasOneRelation::class, $ hasOne );
120
+
121
+ $ this ->assertNull ($ hasOne ->getIncluded ());
122
+
123
+ $ this ->assertArrayHasKey ('hasone_relation ' , $ item ->toJsonApiArray ()['relationships ' ]);
124
+ $ this ->assertNull ($ item ->toJsonApiArray ()['relationships ' ]['hasone_relation ' ]['data ' ]);
125
+ }
126
+
105
127
/**
106
128
* @test
107
129
*/
@@ -174,6 +196,28 @@ public function it_hydrates_hasmany_relationships_with_attributes()
174
196
$ this ->assertArrayHasKey ('hasmany_relation ' , $ item ->toJsonApiArray ()['relationships ' ]);
175
197
}
176
198
199
+ /**
200
+ * @test
201
+ */
202
+ public function it_dissociates_hasmany_relationships_when_empty_array ()
203
+ {
204
+ $ data = [
205
+ 'hasmany_relation ' => [],
206
+ ];
207
+
208
+ $ item = new WithRelationshipItem ();
209
+ $ item = $ this ->getItemHydrator ()->hydrate ($ item , $ data );
210
+
211
+ /** @var \Swis\JsonApi\Client\Relations\HasManyRelation $hasMany */
212
+ $ hasMany = $ item ->getRelation ('hasmany_relation ' );
213
+ $ this ->assertInstanceOf (HasManyRelation::class, $ hasMany );
214
+
215
+ $ this ->assertTrue ($ hasMany ->getIncluded ()->isEmpty ());
216
+
217
+ $ this ->assertArrayHasKey ('hasmany_relation ' , $ item ->toJsonApiArray ()['relationships ' ]);
218
+ $ this ->assertSame ([], $ item ->toJsonApiArray ()['relationships ' ]['hasmany_relation ' ]['data ' ]);
219
+ }
220
+
177
221
/**
178
222
* @test
179
223
*/
@@ -228,6 +272,28 @@ public function it_hydrates_morphto_relationships_with_attributes()
228
272
$ this ->assertArrayHasKey ('morphto_relation ' , $ item ->toJsonApiArray ()['relationships ' ]);
229
273
}
230
274
275
+ /**
276
+ * @test
277
+ */
278
+ public function it_dissociates_morphto_relationships_when_null ()
279
+ {
280
+ $ data = [
281
+ 'morphto_relation ' => null ,
282
+ ];
283
+
284
+ $ item = new WithRelationshipItem ();
285
+ $ item = $ this ->getItemHydrator ()->hydrate ($ item , $ data );
286
+
287
+ /** @var \Swis\JsonApi\Client\Relations\MorphToRelation $morphTo */
288
+ $ morphTo = $ item ->getRelation ('morphto_relation ' );
289
+ $ this ->assertInstanceOf (MorphToRelation::class, $ morphTo );
290
+
291
+ $ this ->assertNull ($ morphTo ->getIncluded ());
292
+
293
+ $ this ->assertArrayHasKey ('morphto_relation ' , $ item ->toJsonApiArray ()['relationships ' ]);
294
+ $ this ->assertNull ($ item ->toJsonApiArray ()['relationships ' ]['morphto_relation ' ]['data ' ]);
295
+ }
296
+
231
297
/**
232
298
* @test
233
299
*/
@@ -350,6 +416,28 @@ public function it_hydrates_morphtomany_relationships_with_attributes()
350
416
$ this ->assertArrayHasKey ('morphtomany_relation ' , $ item ->toJsonApiArray ()['relationships ' ]);
351
417
}
352
418
419
+ /**
420
+ * @test
421
+ */
422
+ public function it_dissociates_morphtomany_relationships_when_empty_array ()
423
+ {
424
+ $ data = [
425
+ 'morphtomany_relation ' => [],
426
+ ];
427
+
428
+ $ item = new WithRelationshipItem ();
429
+ $ item = $ this ->getItemHydrator ()->hydrate ($ item , $ data );
430
+
431
+ /** @var \Swis\JsonApi\Client\Relations\MorphToManyRelation $morphToMany */
432
+ $ morphToMany = $ item ->getRelation ('morphtomany_relation ' );
433
+ $ this ->assertInstanceOf (MorphToManyRelation::class, $ morphToMany );
434
+
435
+ $ this ->assertTrue ($ morphToMany ->getIncluded ()->isEmpty ());
436
+
437
+ $ this ->assertArrayHasKey ('morphtomany_relation ' , $ item ->toJsonApiArray ()['relationships ' ]);
438
+ $ this ->assertSame ([], $ item ->toJsonApiArray ()['relationships ' ]['morphtomany_relation ' ]['data ' ]);
439
+ }
440
+
353
441
/**
354
442
* @test
355
443
*/
0 commit comments