@@ -119,6 +119,96 @@ function ($arg) use ($genericFieldType) {
119119 );
120120 }
121121
122+ public function configureObjectManagerForOneToOneEntity ()
123+ {
124+ $ refl = new ReflectionClass ('DoctrineModuleTest\Stdlib\Hydrator\Asset\OneToOneEntity ' );
125+
126+ $ this
127+ ->metadata
128+ ->expects ($ this ->any ())
129+ ->method ('getFieldNames ' )
130+ ->will ($ this ->returnValue (['id ' ]));
131+
132+ $ this
133+ ->metadata
134+ ->expects ($ this ->any ())
135+ ->method ('getAssociationNames ' )
136+ ->will ($ this ->returnValue (['toOne ' ]));
137+
138+ $ this
139+ ->metadata
140+ ->expects ($ this ->any ())
141+ ->method ('getTypeOfField ' )
142+ ->with ($ this ->logicalOr ($ this ->equalTo ('id ' ), $ this ->equalTo ('toOne ' )))
143+ ->will (
144+ $ this ->returnCallback (
145+ function ($ arg ) {
146+ if ($ arg === 'id ' ) {
147+ return 'integer ' ;
148+ } elseif ($ arg === 'toOne ' ) {
149+ return 'DoctrineModuleTest\Stdlib\Hydrator\Asset\ByValueDifferentiatorEntity ' ;
150+ }
151+
152+ throw new \InvalidArgumentException ();
153+ }
154+ )
155+ );
156+
157+ $ this
158+ ->metadata
159+ ->expects ($ this ->any ())
160+ ->method ('hasAssociation ' )
161+ ->with ($ this ->logicalOr ($ this ->equalTo ('id ' ), $ this ->equalTo ('toOne ' )))
162+ ->will (
163+ $ this ->returnCallback (
164+ function ($ arg ) {
165+ if ($ arg === 'id ' ) {
166+ return false ;
167+ } elseif ($ arg === 'toOne ' ) {
168+ return true ;
169+ }
170+
171+ throw new \InvalidArgumentException ();
172+ }
173+ )
174+ );
175+
176+ $ this
177+ ->metadata
178+ ->expects ($ this ->any ())
179+ ->method ('isSingleValuedAssociation ' )
180+ ->with ('toOne ' )
181+ ->will ($ this ->returnValue (true ));
182+
183+ $ this
184+ ->metadata
185+ ->expects ($ this ->any ())
186+ ->method ('getAssociationTargetClass ' )
187+ ->with ('toOne ' )
188+ ->will ($ this ->returnValue ('DoctrineModuleTest\Stdlib\Hydrator\Asset\ByValueDifferentiatorEntity ' ));
189+
190+ $ this
191+ ->metadata
192+ ->expects ($ this ->any ())
193+ ->method ('getReflectionClass ' )
194+ ->will ($ this ->returnValue ($ refl ));
195+
196+ $ this
197+ ->metadata
198+ ->expects ($ this ->any ())
199+ ->method ('getIdentifier ' )
200+ ->will ($ this ->returnValue (["id " ]));
201+
202+ $ this ->hydratorByValue = new DoctrineObjectHydrator (
203+ $ this ->objectManager ,
204+ true
205+ );
206+ $ this ->hydratorByReference = new DoctrineObjectHydrator (
207+ $ this ->objectManager ,
208+ false
209+ );
210+ }
211+
122212 public function testHandleTypeConversionsDatetime ()
123213 {
124214 // When using hydration by value, it will use the public API of the entity to set values (setters)
@@ -621,4 +711,36 @@ public function testHandleTypeConversionsDecimal()
621711 $ this ->assertTrue (is_string ($ entity ->getGenericField ()));
622712 $ this ->assertEquals ('12345 ' , $ entity ->getGenericField ());
623713 }
714+
715+ public function testHandleTypeConversionsNullable ()
716+ {
717+ // When using hydration by value, it will use the public API of the entity to set values (setters)
718+ $ this ->configureObjectManagerForSimpleEntityWithGenericField (null );
719+
720+ $ entity = new Asset \SimpleEntityWithGenericField ();
721+ $ data = ['genericField ' => null ];
722+
723+ $ entity = $ this ->hydratorByValue ->hydrate ($ data , $ entity );
724+
725+ $ this ->assertNull ($ entity ->getGenericField ());
726+
727+ $ entity = new Asset \SimpleEntityWithGenericField ();
728+ $ data = ['genericField ' => null ];
729+
730+ $ entity = $ this ->hydratorByReference ->hydrate ($ data , $ entity );
731+
732+ $ this ->assertNull ($ entity ->getGenericField ());
733+ }
734+
735+ public function testHandleTypeConversionsNullableForAssociatedFields ()
736+ {
737+ $ this ->configureObjectManagerForOneToOneEntity ();
738+
739+ $ entity = new Asset \OneToOneEntity ();
740+ $ data = ['toOne ' => null ];
741+
742+ $ entity = $ this ->hydratorByReference ->hydrate ($ data , $ entity );
743+
744+ $ this ->assertNull ($ entity ->getToOne (false ));
745+ }
624746}
0 commit comments