@@ -347,23 +347,31 @@ public function testCastIntBool(): void
347
347
];
348
348
};
349
349
350
- $ entity ->setAttributes ([ ' active ' => '1 ' ]) ;
350
+ $ entity ->active = '1 ' ;
351
351
352
352
$ this ->assertTrue ($ entity ->active );
353
353
354
- $ entity ->setAttributes (['active ' => '0 ' ]);
354
+ $ entity ->active = '0 ' ;
355
+
356
+ $ this ->assertFalse ($ entity ->active );
357
+
358
+ $ entity ->active = 1 ;
359
+
360
+ $ this ->assertTrue ($ entity ->active );
361
+
362
+ $ entity ->active = 0 ;
355
363
356
364
$ this ->assertFalse ($ entity ->active );
357
365
358
366
$ entity ->active = true ;
359
367
360
368
$ this ->assertTrue ($ entity ->active );
361
- $ this ->assertSame (['active ' => 1 ], $ entity ->toRawArray ());
369
+ $ this ->assertSame (['active ' => 1 ], $ entity ->toDatabase ());
362
370
363
371
$ entity ->active = false ;
364
372
365
373
$ this ->assertFalse ($ entity ->active );
366
- $ this ->assertSame (['active ' => 0 ], $ entity ->toRawArray ());
374
+ $ this ->assertSame (['active ' => 0 ], $ entity ->toDatabase ());
367
375
}
368
376
369
377
public function testCastFloat (): void
@@ -423,11 +431,12 @@ public function testCastBoolean(): void
423
431
424
432
public function testCastCSV (): void
425
433
{
426
- $ entity = $ this ->getCastEntity ();
434
+ $ entity = $ this ->getCastEntity ();
435
+
427
436
$ data = ['foo ' , 'bar ' , 'bam ' ];
428
437
$ entity ->twelfth = $ data ;
429
438
430
- $ result = $ entity ->toRawArray ();
439
+ $ result = $ entity ->toDatabase ();
431
440
432
441
$ this ->assertIsString ($ result ['twelfth ' ]);
433
442
$ this ->assertSame ('foo,bar,bam ' , $ result ['twelfth ' ]);
@@ -486,8 +495,9 @@ public function testCastArray(): void
486
495
487
496
$ entity ->seventh = ['foo ' => 'bar ' ];
488
497
489
- $ check = $ this ->getPrivateProperty ($ entity , 'attributes ' )['seventh ' ];
490
- $ this ->assertSame (serialize (['foo ' => 'bar ' ]), $ check );
498
+ $ result = $ entity ->toDatabase ();
499
+
500
+ $ this ->assertSame (serialize (['foo ' => 'bar ' ]), $ result ['seventh ' ]);
491
501
$ this ->assertSame (['foo ' => 'bar ' ], $ entity ->seventh );
492
502
}
493
503
@@ -497,11 +507,12 @@ public function testCastArrayByStringSerialize(): void
497
507
498
508
$ entity ->seventh = 'foobar ' ;
499
509
510
+ $ result = $ entity ->toDatabase ();
511
+
500
512
// Should be a serialized string now...
501
- $ check = $ this ->getPrivateProperty ($ entity , 'attributes ' )['seventh ' ];
502
- $ this ->assertSame (serialize ('foobar ' ), $ check );
513
+ $ this ->assertSame (serialize ('foobar ' ), $ result ['seventh ' ]);
503
514
504
- $ this ->assertSame ([ 'foobar ' ] , $ entity ->seventh );
515
+ $ this ->assertSame ('foobar ' , $ entity ->seventh );
505
516
}
506
517
507
518
public function testCastArrayByArraySerialize (): void
@@ -510,9 +521,10 @@ public function testCastArrayByArraySerialize(): void
510
521
511
522
$ entity ->seventh = ['foo ' => 'bar ' ];
512
523
524
+ $ result = $ entity ->toDatabase ();
525
+
513
526
// Should be a serialized string now...
514
- $ check = $ this ->getPrivateProperty ($ entity , 'attributes ' )['seventh ' ];
515
- $ this ->assertSame (serialize (['foo ' => 'bar ' ]), $ check );
527
+ $ this ->assertSame (serialize (['foo ' => 'bar ' ]), $ result ['seventh ' ]);
516
528
517
529
$ this ->assertSame (['foo ' => 'bar ' ], $ entity ->seventh );
518
530
}
@@ -524,9 +536,10 @@ public function testCastArrayByFill(): void
524
536
$ data = ['seventh ' => [1 , 2 , 3 ]];
525
537
$ entity ->fill ($ data );
526
538
539
+ $ result = $ entity ->toDatabase ();
540
+
527
541
// Check if serialiazed
528
- $ check = $ this ->getPrivateProperty ($ entity , 'attributes ' )['seventh ' ];
529
- $ this ->assertSame (serialize ([1 , 2 , 3 ]), $ check );
542
+ $ this ->assertSame (serialize ([1 , 2 , 3 ]), $ result ['seventh ' ]);
530
543
// Check if unserialized
531
544
$ this ->assertSame ([1 , 2 , 3 ], $ entity ->seventh );
532
545
}
@@ -536,9 +549,10 @@ public function testCastArrayByConstructor(): void
536
549
$ data = ['seventh ' => [1 , 2 , 3 ]];
537
550
$ entity = $ this ->getCastEntity ($ data );
538
551
552
+ $ result = $ entity ->toDatabase ();
553
+
539
554
// Check if serialiazed
540
- $ check = $ this ->getPrivateProperty ($ entity , 'attributes ' )['seventh ' ];
541
- $ this ->assertSame (serialize ([1 , 2 , 3 ]), $ check );
555
+ $ this ->assertSame (serialize ([1 , 2 , 3 ]), $ result ['seventh ' ]);
542
556
// Check if unserialized
543
557
$ this ->assertSame ([1 , 2 , 3 ], $ entity ->seventh );
544
558
}
@@ -584,12 +598,12 @@ public function testCastAsJSON(): void
584
598
585
599
$ entity ->tenth = ['foo ' => 'bar ' ];
586
600
601
+ $ result = $ entity ->toDatabase ();
602
+
587
603
// Should be a JSON-encoded string now...
588
- $ check = $ this ->getPrivateProperty ($ entity , 'attributes ' )['tenth ' ];
589
- $ this ->assertSame ('{"foo":"bar"} ' , $ check );
604
+ $ this ->assertSame ('{"foo":"bar"} ' , $ result ['tenth ' ]);
590
605
591
- $ this ->assertInstanceOf ('stdClass ' , $ entity ->tenth );
592
- $ this ->assertSame (['foo ' => 'bar ' ], (array ) $ entity ->tenth );
606
+ $ this ->assertSame (['foo ' => 'bar ' ], $ entity ->tenth );
593
607
}
594
608
595
609
public function testCastAsJSONArray (): void
@@ -599,9 +613,10 @@ public function testCastAsJSONArray(): void
599
613
$ data = ['Sun ' , 'Mon ' , 'Tue ' ];
600
614
$ entity ->eleventh = $ data ;
601
615
616
+ $ result = $ entity ->toDatabase ();
617
+
602
618
// Should be a JSON-encoded string now...
603
- $ check = $ this ->getPrivateProperty ($ entity , 'attributes ' )['eleventh ' ];
604
- $ this ->assertSame ('["Sun","Mon","Tue"] ' , $ check );
619
+ $ this ->assertSame ('["Sun","Mon","Tue"] ' , $ result ['eleventh ' ]);
605
620
606
621
$ this ->assertSame ($ data , $ entity ->eleventh );
607
622
}
@@ -613,9 +628,10 @@ public function testCastAsJsonByFill(): void
613
628
$ data = ['eleventh ' => [1 , 2 , 3 ]];
614
629
$ entity ->fill ($ data );
615
630
631
+ $ result = $ entity ->toDatabase ();
632
+
616
633
// Check if serialiazed
617
- $ check = $ this ->getPrivateProperty ($ entity , 'attributes ' )['eleventh ' ];
618
- $ this ->assertSame (json_encode ([1 , 2 , 3 ]), $ check );
634
+ $ this ->assertSame (json_encode ([1 , 2 , 3 ]), $ result ['eleventh ' ]);
619
635
// Check if unserialized
620
636
$ this ->assertSame ([1 , 2 , 3 ], $ entity ->eleventh );
621
637
}
@@ -625,9 +641,10 @@ public function testCastAsJsonByConstructor(): void
625
641
$ data = ['eleventh ' => [1 , 2 , 3 ]];
626
642
$ entity = $ this ->getCastEntity ($ data );
627
643
644
+ $ result = $ entity ->toDatabase ();
645
+
628
646
// Check if serialiazed
629
- $ check = $ this ->getPrivateProperty ($ entity , 'attributes ' )['eleventh ' ];
630
- $ this ->assertSame (json_encode ([1 , 2 , 3 ]), $ check );
647
+ $ this ->assertSame (json_encode ([1 , 2 , 3 ]), $ result ['eleventh ' ]);
631
648
// Check if unserialized
632
649
$ this ->assertSame ([1 , 2 , 3 ], $ entity ->eleventh );
633
650
}
@@ -651,6 +668,8 @@ public function testCastAsJSONErrorDepth(): void
651
668
}
652
669
$ current = $ value ;
653
670
$ entity ->tenth = $ array ;
671
+
672
+ $ entity ->toDatabase ();
654
673
}
655
674
656
675
public function testCastAsJSONErrorUTF8 (): void
@@ -661,6 +680,8 @@ public function testCastAsJSONErrorUTF8(): void
661
680
$ entity = $ this ->getCastEntity ();
662
681
663
682
$ entity ->tenth = "\xB1\x31" ;
683
+
684
+ $ entity ->toDatabase ();
664
685
}
665
686
666
687
/**
@@ -675,7 +696,7 @@ public function testCastAsJSONSyntaxError(): void
675
696
$ entity = new Entity ();
676
697
$ entity ->casts ['dummy ' ] = 'json[array] ' ;
677
698
678
- return $ entity ->castAs ($ value , 'dummy ' );
699
+ return $ entity ->castAs ($ value , 'dummy ' , ' fromDatabase ' );
679
700
}, null , Entity::class))('{ this is bad string ' );
680
701
}
681
702
@@ -692,7 +713,7 @@ public function testCastAsJSONAnotherErrorDepth(): void
692
713
$ entity = new Entity ();
693
714
$ entity ->casts ['dummy ' ] = 'json[array] ' ;
694
715
695
- return $ entity ->castAs ($ value , 'dummy ' );
716
+ return $ entity ->castAs ($ value , 'dummy ' , ' fromDatabase ' );
696
717
}, null , Entity::class))($ string );
697
718
}
698
719
@@ -709,7 +730,7 @@ public function testCastAsJSONControlCharCheck(): void
709
730
$ entity = new Entity ();
710
731
$ entity ->casts ['dummy ' ] = 'json[array] ' ;
711
732
712
- return $ entity ->castAs ($ value , 'dummy ' );
733
+ return $ entity ->castAs ($ value , 'dummy ' , ' fromDatabase ' );
713
734
}, null , Entity::class))($ string );
714
735
}
715
736
@@ -726,22 +747,24 @@ public function testCastAsJSONStateMismatch(): void
726
747
$ entity = new Entity ();
727
748
$ entity ->casts ['dummy ' ] = 'json[array] ' ;
728
749
729
- return $ entity ->castAs ($ value , 'dummy ' );
750
+ return $ entity ->castAs ($ value , 'dummy ' , ' fromDatabase ' );
730
751
}, null , Entity::class))($ string );
731
752
}
732
753
733
754
public function testCastSetter (): void
734
755
{
735
- $ string = '321 String with numbers 123 ' ;
736
- $ entity = $ this ->getCastEntity ();
737
- $ entity ->first = $ string ;
756
+ $ entity = $ this ->getCastEntity ();
738
757
739
758
$ entity ->cast (false );
759
+ $ string = '321 String with numbers 123 ' ;
760
+ $ entity ->first = $ string ;
740
761
741
762
$ this ->assertIsString ($ entity ->first );
742
763
$ this ->assertSame ($ string , $ entity ->first );
743
764
744
765
$ entity ->cast (true );
766
+ $ string = '321 String with numbers 123 ' ;
767
+ $ entity ->first = $ string ;
745
768
746
769
$ this ->assertIsInt ($ entity ->first );
747
770
$ this ->assertSame ((int ) $ string , $ entity ->first );
@@ -1285,16 +1308,16 @@ protected function getCastNullableEntity()
1285
1308
return new class () extends Entity {
1286
1309
protected $ attributes = [
1287
1310
'string_null ' => null ,
1288
- 'string_empty ' => null ,
1311
+ 'string_empty ' => '' ,
1289
1312
'integer_null ' => null ,
1290
- 'integer_0 ' => null ,
1313
+ 'integer_0 ' => 0 ,
1291
1314
'string_value_not_null ' => 'value ' ,
1292
1315
];
1293
1316
protected $ _original = [
1294
1317
'string_null ' => null ,
1295
- 'string_empty ' => null ,
1318
+ 'string_empty ' => '' ,
1296
1319
'integer_null ' => null ,
1297
- 'integer_0 ' => null ,
1320
+ 'integer_0 ' => 0 ,
1298
1321
'string_value_not_null ' => 'value ' ,
1299
1322
];
1300
1323
0 commit comments