33
44use DateTime ;
55use DateTimeInterface ;
6+ use Error ;
67use Gt \DataObject \DataObject ;
78use PHPUnit \Framework \TestCase ;
89use stdClass ;
10+ use Throwable ;
911use TypeError ;
1012
1113class DataObjectTest extends TestCase {
@@ -105,6 +107,21 @@ public function testGetStringNull() {
105107 self ::assertNull ($ sut ->getString ("nothing " ));
106108 }
107109
110+ public function testGetStringFromDateTime () {
111+ $ sut = (new DataObject ())
112+ ->with ("dt " , new DateTime ());
113+
114+ $ exception = null ;
115+
116+ try {
117+ $ data = $ sut ->getString ("dt " );
118+ }
119+ catch (Throwable $ exception ) {}
120+
121+ self ::assertInstanceOf (Error::class, $ exception );
122+ self ::assertSame ("Object of class DateTime could not be converted to string " , $ exception ->getMessage ());
123+ }
124+
108125 public function testGetIntFromString () {
109126 $ sut = (new DataObject ())
110127 ->with ("one " , "1 " )
@@ -374,15 +391,20 @@ public function testGetArrayFixedTypeInt_typeError() {
374391 49997 ,
375392 50000 ,
376393 49999 ,
377- "PLOP! " ,
394+ "50003 " ,
378395 50001 ,
379396 ];
380397 $ sut = (new DataObject ())
381398 ->with ("timestamps " , $ timestampArray );
382399
383- self ::expectException (TypeError::class);
384- self ::expectExceptionMessage ("Array index 3 must be of type int, string given " );
385- $ sut ->getArray ("timestamps " , "int " );
400+ $ array = $ sut ->getArray ("timestamps " , "int " );
401+ foreach ($ array as $ i => $ value ) {
402+ if ($ i === 3 ) {
403+ self ::assertSame (50003 , $ value );
404+ }
405+
406+ self ::assertIsInt ($ value );
407+ }
386408 }
387409
388410 public function testGetArrayFixedTypeFloat () {
@@ -433,7 +455,7 @@ public function testGetArrayFixedTypeString() {
433455 }
434456 }
435457
436- public function testGetArrayFixedTypeString_typeErrorWithObject () {
458+ public function testGetArrayFixedTypeString_typeErrorWithDateTime () {
437459 $ wordsArray = [
438460 "one " ,
439461 "two " ,
@@ -443,7 +465,7 @@ public function testGetArrayFixedTypeString_typeErrorWithObject() {
443465 ];
444466 $ sut = (new DataObject ())
445467 ->with ("words " , $ wordsArray );
446- self ::expectExceptionMessage ("Array index 3 must be of type string, DateTime given " );
468+ self ::expectExceptionMessage ("Object of class DateTime could not be converted to string " );
447469 $ sut ->getArray ("words " , "string " );
448470 }
449471
@@ -456,7 +478,7 @@ public function testGetArrayFixedTypeDateTime() {
456478 ->with ("dates " , $ dateArray );
457479 $ array = $ sut ->getArray ("dates " , DateTimeInterface::class);
458480 foreach ($ array as $ i => $ value ) {
459- self ::assertInstanceOf (DateTimeInterface ::class, $ value );
481+ self ::assertInstanceOf (DateTime ::class, $ value );
460482 }
461483 }
462484
@@ -470,9 +492,8 @@ public function testGetArrayFixedTypesMismatch() {
470492 ];
471493 $ sut = (new DataObject ())
472494 ->with ("timestamps " , $ timestampArray );
473- self ::expectException (TypeError::class);
474- self ::expectExceptionMessage ("Array index 2 must be of type int, double given " );
475- $ sut ->getArray ("timestamps " , "int " );
495+ $ array = $ sut ->getArray ("timestamps " , "int " );
496+ self ::assertSame (49999 , $ array [2 ]);
476497 }
477498
478499 public function testGetArray_nullableType ():void {
0 commit comments