@@ -102,6 +102,28 @@ public function it_hydrates_hasone_relationships_with_attributes()
102102 $ this ->assertArrayHasKey ('hasone_relation ' , $ item ->toJsonApiArray ()['relationships ' ]);
103103 }
104104
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+
105127 /**
106128 * @test
107129 */
@@ -174,6 +196,28 @@ public function it_hydrates_hasmany_relationships_with_attributes()
174196 $ this ->assertArrayHasKey ('hasmany_relation ' , $ item ->toJsonApiArray ()['relationships ' ]);
175197 }
176198
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+
177221 /**
178222 * @test
179223 */
@@ -228,6 +272,28 @@ public function it_hydrates_morphto_relationships_with_attributes()
228272 $ this ->assertArrayHasKey ('morphto_relation ' , $ item ->toJsonApiArray ()['relationships ' ]);
229273 }
230274
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+
231297 /**
232298 * @test
233299 */
@@ -350,6 +416,28 @@ public function it_hydrates_morphtomany_relationships_with_attributes()
350416 $ this ->assertArrayHasKey ('morphtomany_relation ' , $ item ->toJsonApiArray ()['relationships ' ]);
351417 }
352418
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+
353441 /**
354442 * @test
355443 */
0 commit comments