@@ -30,6 +30,34 @@ public function __construct(
3030 $ this ->type_null = $ abc ;
3131 }
3232 }
33+
34+ class TestConstructFromSerialize extends Serialize
35+ {
36+ public function __construct (
37+ public string $ type_string ,
38+ public object $ type_object ,
39+ public int $ type_int ,
40+ public int $ type_null ,
41+ public float $ type_float ,
42+ ) {
43+ }
44+ }
45+
46+ class TestConstructValidationFromSerialize extends Serialize
47+ {
48+
49+ public string $ type_string ;
50+
51+ public function validate ():void
52+ {
53+ if ($ this ->type_string !== '123 ' ){
54+ throw new Exception ('type_string is not 123 ' );
55+ }
56+
57+ $ this ->type_string = '234 ' ;
58+
59+ }
60+ }
3361});
3462
3563it ('test parse Serialize class ' , function () {
@@ -64,25 +92,32 @@ public function __construct(
6492
6593it ('test parse construct Serialize class ' , function () {
6694
67- $ object = TestFromSerialize::from (
68- [
69- 'input_name ' => [ fn () => new stdClass ()],
70- 'type_string ' => 123 ,
71- 'type_int ' => '11 ' ,
72- 'type_float ' => '0.01 ' ,
73- 'type_object ' => new StdClass (),
74- 'withoutType ' => 'hhh ' ,
75- ],
76- input_name:null ,
77- type_object:null ,
78- type_mixed_other: ['abc ' => ['bbb ' => ['ccc ' => 'dddd ' ],['abc ' ]],'aaa ' ,'bbb ' ,'ccc ' ,'' ],
79- abc:123
95+ $ object = TestConstructFromSerialize::from (
96+ type_string: 123 ,
97+ type_object: null ,
98+ type_int: '123 ' ,
99+ type_null: null ,
100+ type_float: '0.01 ' ,
80101 );
81102
82- expect ($ object )->toBeInstanceOf (TestFromSerialize ::class)
103+ expect ($ object )->toBeInstanceOf (TestConstructFromSerialize ::class)
83104 ->and ($ object ->type_string )->toBe ('123 ' )
84105 ->and ($ object ->type_object )->toBeInstanceOf (StdClass::class)
85- ->and ($ object ->type_int )->toBe (11 )
86- ->and ($ object ->type_null )->toBe (123 )
106+ ->and ($ object ->type_int )->toBe (123 )
107+ ->and ($ object ->type_null )->toBe (0 )
87108 ->and ($ object ->type_float )->toBe (0.01 );
88109});
110+
111+ it ('throws exception when type_string is not 123 ' , function () {
112+ expect (function () {
113+ TestConstructValidationFromSerialize::from (type_string: 111 );
114+ })->toThrow (Exception::class, 'type_string is not 123 ' );
115+ });
116+
117+ it ('creates object successfully when type_string is 123 ' , function () {
118+ $ object = TestConstructValidationFromSerialize::from (type_string: 123 );
119+
120+ expect ($ object )
121+ ->toBeInstanceOf (TestConstructValidationFromSerialize::class)
122+ ->and ($ object ->type_string )->toBe ('234 ' );
123+ });
0 commit comments