11<?php
2+
3+ declare (strict_types=1 );
4+
25use PHPUnit \Framework \TestCase ;
36
7+ use Piko \ModelTrait ;
48use Piko \Tests \lab \TestModel ;
59use Piko \Tests \lab \TypedTestModel ;
610
711class ModelTest extends TestCase
812{
9- public function testModelValidation ()
13+ public function testModelValidation (): void
1014 {
1115 $ model = new TestModel ();
1216
@@ -18,28 +22,28 @@ public function testModelValidation()
1822 $ this ->assertArrayHasKey ('lastName ' , $ errors );
1923
2024 $ model = new TestModel ();
21-
2225 $ model ->firstName = 'John ' ;
2326 $ model ->lastName = 'Lennon ' ;
2427
2528 $ this ->assertTrue ($ model ->isValid ());
2629 }
2730
28- public function testModelBind ()
31+ public function testModelBind (): void
2932 {
3033 $ model = new TestModel ();
3134 $ model ->bind (['firstName ' => 'John ' , 'lastName ' => 'Lennon ' ]);
32- $ this ->assertEquals ('John ' , $ model ->firstName );
33- $ this ->assertEquals ('Lennon ' , $ model ->lastName );
35+
36+ $ this ->assertSame ('John ' , $ model ->firstName );
37+ $ this ->assertSame ('Lennon ' , $ model ->lastName );
3438
3539 $ modelArray = $ model ->toArray ();
3640 $ this ->assertArrayHasKey ('firstName ' , $ modelArray );
3741 $ this ->assertArrayHasKey ('lastName ' , $ modelArray );
38- $ this ->assertEquals ('John ' , $ modelArray ['firstName ' ]);
39- $ this ->assertEquals ('Lennon ' , $ modelArray ['lastName ' ]);
42+ $ this ->assertSame ('John ' , $ modelArray ['firstName ' ]);
43+ $ this ->assertSame ('Lennon ' , $ modelArray ['lastName ' ]);
4044 }
4145
42- public function testModelBindWithTypedProperties ()
46+ public function testModelBindWithTypedProperties (): void
4347 {
4448 $ model = new TypedTestModel ();
4549 $ model ->bind ([
@@ -50,14 +54,71 @@ public function testModelBindWithTypedProperties()
5054 ]);
5155
5256 $ this ->assertSame (1 , $ model ->id );
53- $ this ->assertSame ( false , $ model ->active );
54- $ this ->assertEquals (20230.95 , $ model ->income );
57+ $ this ->assertFalse ( $ model ->active );
58+ $ this ->assertSame (20230.95 , $ model ->income );
5559 $ this ->assertNull ($ model ->nickname );
5660
5761 $ modelArray = $ model ->toArray ();
5862 $ this ->assertSame (1 , $ modelArray ['id ' ]);
59- $ this ->assertSame ( false , $ modelArray ['active ' ]);
60- $ this ->assertEquals (20230.95 , $ modelArray ['income ' ]);
63+ $ this ->assertFalse ( $ modelArray ['active ' ]);
64+ $ this ->assertSame (20230.95 , $ modelArray ['income ' ]);
6165 $ this ->assertNull ($ modelArray ['nickname ' ]);
6266 }
67+
68+ public function testModelBindConvertsEmptyStringToNullForNullableNonStringProperties (): void
69+ {
70+ $ model = new class () {
71+ use ModelTrait;
72+
73+ public ?int $ id = 42 ;
74+ };
75+
76+ $ model ->bind (['id ' => '' ]);
77+
78+ $ this ->assertNull ($ model ->id );
79+ $ this ->assertNull ($ model ->toArray ()['id ' ]);
80+ }
81+
82+ public function testModelBindKeepsBooleanValuesAndFallsBackForUnrecognizedStrings (): void
83+ {
84+ $ model = new class () {
85+ use ModelTrait;
86+
87+ public bool $ active = false ;
88+ };
89+
90+ $ model ->bind (['active ' => true ]);
91+ $ this ->assertTrue ($ model ->active );
92+
93+ $ model ->bind (['active ' => 'foo ' ]);
94+ $ this ->assertTrue ($ model ->active );
95+ }
96+
97+ public function testModelBindCastsStringProperties (): void
98+ {
99+ $ model = new class () {
100+ use ModelTrait;
101+
102+ public string $ name = '' ;
103+ };
104+
105+ $ model ->bind (['name ' => 123 ]);
106+
107+ $ this ->assertSame ('123 ' , $ model ->name );
108+ $ this ->assertSame ('123 ' , $ model ->toArray ()['name ' ]);
109+ }
110+
111+ public function testModelBindLeavesUnsupportedTypedValuesAsIs (): void
112+ {
113+ $ model = new class () {
114+ use ModelTrait;
115+
116+ public array $ tags = [];
117+ };
118+
119+ $ model ->bind (['tags ' => ['php ' , 'piko ' ]]);
120+
121+ $ this ->assertSame (['php ' , 'piko ' ], $ model ->tags );
122+ $ this ->assertSame (['php ' , 'piko ' ], $ model ->toArray ()['tags ' ]);
123+ }
63124}
0 commit comments