@@ -311,4 +311,33 @@ public function testTypeof() {
311
311
self ::assertEquals ("null " , $ sut ->typeof ("nothing " ));
312
312
self ::assertNull ($ sut ->typeof ("address " ));
313
313
}
314
+
315
+ public function testJsonSerialize () {
316
+ $ obj = new StdClass ();
317
+ $ obj ->nestedKey = "nestedValue " ;
318
+ $ dateTime = new DateTime ();
319
+
320
+ $ sut = (new DataObject ())
321
+ ->with ("name " , "example " )
322
+ ->with ("id " , 123 )
323
+ ->with ("size " , 2_347.467 )
324
+ ->with ("isSecure " , true )
325
+ ->with ("container " , $ obj )
326
+ ->with ("dispatchDate " , $ dateTime )
327
+ ->with ("nothing " , null );
328
+ $ json = json_encode ($ sut );
329
+
330
+ self ::assertStringContainsString ('"name":"example" ' , $ json );
331
+ self ::assertStringContainsString ('"id":123 ' , $ json );
332
+ self ::assertStringContainsString ('"size":2347.467 ' , $ json );
333
+ self ::assertStringContainsString ('"isSecure":true ' , $ json );
334
+ self ::assertStringContainsString ('"container":{"nestedKey":"nestedValue"} ' , $ json );
335
+ self ::assertStringContainsString ('"nothing":null ' , $ json );
336
+
337
+ $ obj = json_decode ($ json );
338
+ self ::assertStringContainsString (
339
+ $ dateTime ->format ("Y-m-d H:i:s.u " ),
340
+ $ obj ->dispatchDate ->date
341
+ );
342
+ }
314
343
}
0 commit comments