77use Tests \Fixtures \Foo ;
88use Tests \Fixtures \NestedData ;
99use Tests \Fixtures \SampleData ;
10+ use Tests \Fixtures \UntypedData ;
1011
1112class DataTransferObjectTest extends TestCase
1213{
@@ -17,17 +18,26 @@ public function testSimpleProperty(): void
1718 $ data ->nullable_prop = 'bar ' ;
1819
1920 self ::assertEquals ([
21+ 'initialized_prop ' => 'Initialized ' ,
2022 'simple_prop ' => 'foo ' ,
2123 'nullable_prop ' => 'bar ' ,
2224 ], $ data ->toArray ());
2325 }
2426
27+ public function testInitializedProperty (): void
28+ {
29+ self ::assertEquals (['initialized_prop ' => 'Initialized ' ], SampleData::make ()->toArray ());
30+ }
31+
2532 public function testArrayProperty (): void
2633 {
2734 $ data = SampleData::make ();
2835 $ data ->array_prop = ['foo ' => 'bar ' ];
2936
30- self ::assertEquals (['array_prop ' => ['foo ' => 'bar ' ]], $ data ->toArray ());
37+ self ::assertEquals ([
38+ 'initialized_prop ' => 'Initialized ' ,
39+ 'array_prop ' => ['foo ' => 'bar ' ],
40+ ], $ data ->toArray ());
3141 }
3242
3343 public function testObjectProperty (): void
@@ -36,7 +46,10 @@ public function testObjectProperty(): void
3646 $ data = SampleData::make ();
3747 $ data ->object_prop = $ foo ;
3848
39- self ::assertEquals (['object_prop ' => $ foo ], $ data ->toArray ());
49+ self ::assertEquals ([
50+ 'initialized_prop ' => 'Initialized ' ,
51+ 'object_prop ' => $ foo ,
52+ ], $ data ->toArray ());
4053 }
4154
4255 public function testSettingNonExistentPropertyThrows (): void
@@ -52,7 +65,10 @@ public function testSet(): void
5265 $ data = SampleData::make ();
5366 $ data ->set ('simple_prop ' , 'foo ' );
5467
55- self ::assertSame (['simple_prop ' => 'foo ' ], $ data ->toArray ());
68+ self ::assertSame ([
69+ 'initialized_prop ' => 'Initialized ' ,
70+ 'simple_prop ' => 'foo ' ,
71+ ], $ data ->toArray ());
5672 }
5773
5874 public function testSetArray (): void
@@ -64,6 +80,7 @@ public function testSetArray(): void
6480 ]);
6581
6682 self ::assertSame ([
83+ 'initialized_prop ' => 'Initialized ' ,
6784 'simple_prop ' => 'foo ' ,
6885 'nullable_prop ' => 'bar ' ,
6986 ], $ data ->toArray ());
@@ -78,11 +95,15 @@ public function testBuiltinUnset(): void
7895 self ::assertEquals ([
7996 'simple_prop ' => 'foo ' ,
8097 'nullable_prop ' => 'bar ' ,
98+ 'initialized_prop ' => 'Initialized ' ,
8199 ], $ data ->toArray ());
82100
83101 unset($ data ->nullable_prop );
84102
85- self ::assertEquals (['simple_prop ' => 'foo ' ], $ data ->toArray ());
103+ self ::assertEquals ([
104+ 'simple_prop ' => 'foo ' ,
105+ 'initialized_prop ' => 'Initialized ' ,
106+ ], $ data ->toArray ());
86107 }
87108
88109 public function testUnset (): void
@@ -92,13 +113,17 @@ public function testUnset(): void
92113 $ data ->nullable_prop = 'bar ' ;
93114
94115 self ::assertEquals ([
116+ 'initialized_prop ' => 'Initialized ' ,
95117 'simple_prop ' => 'foo ' ,
96118 'nullable_prop ' => 'bar ' ,
97119 ], $ data ->toArray ());
98120
99121 $ data ->unset ('nullable_prop ' );
100122
101- self ::assertEquals (['simple_prop ' => 'foo ' ], $ data ->toArray ());
123+ self ::assertEquals ([
124+ 'simple_prop ' => 'foo ' ,
125+ 'initialized_prop ' => 'Initialized ' ,
126+ ], $ data ->toArray ());
102127 }
103128
104129 public function testUnsetSpread (): void
@@ -110,9 +135,10 @@ public function testUnsetSpread(): void
110135 self ::assertEquals ([
111136 'simple_prop ' => 'foo ' ,
112137 'nullable_prop ' => 'bar ' ,
138+ 'initialized_prop ' => 'Initialized ' ,
113139 ], $ data ->toArray ());
114140
115- $ data ->unset ('nullable_prop ' , 'simple_prop ' );
141+ $ data ->unset ('nullable_prop ' , 'simple_prop ' , ' initialized_prop ' );
116142
117143 self ::assertEquals ([], $ data ->toArray ());
118144 }
@@ -133,6 +159,7 @@ public function testMake(): void
133159 ]);
134160
135161 self ::assertEquals ([
162+ 'initialized_prop ' => 'Initialized ' ,
136163 'simple_prop ' => 'foo ' ,
137164 'nullable_prop ' => 'bar ' ,
138165 ], $ data ->toArray ());
@@ -144,7 +171,10 @@ public function testCompact(): void
144171 $ data ->simple_prop = 'foo ' ;
145172 $ data ->nullable_prop = null ;
146173
147- self ::assertEquals (['simple_prop ' => 'foo ' ], $ data ->compact ()->toArray ());
174+ self ::assertEquals ([
175+ 'initialized_prop ' => 'Initialized ' ,
176+ 'simple_prop ' => 'foo ' ,
177+ ], $ data ->compact ()->toArray ());
148178 }
149179
150180 public function testOnly (): void
@@ -162,54 +192,61 @@ public function testOnlySpread(): void
162192 $ data = SampleData::make ([
163193 'simple_prop ' => 'foo ' ,
164194 'nullable_prop ' => 'bar ' ,
165- 'mixed_prop ' => 'baz ' ,
166195 ]);
167196
168197 self ::assertEquals ([
169198 'simple_prop ' => 'foo ' ,
170- 'mixed_prop ' => 'baz ' ,
171- ], $ data ->only ('simple_prop ' , 'mixed_prop ' )->toArray ());
199+ 'initialized_prop ' => 'Initialized ' ,
200+ ], $ data ->only ('simple_prop ' , 'initialized_prop ' )->toArray ());
172201 }
173202
174203 public function testExcept (): void
175204 {
176205 $ data = SampleData::make ([
177206 'simple_prop ' => 'foo ' ,
178207 'nullable_prop ' => 'bar ' ,
179- 'mixed_prop ' => 'baz ' ,
180208 ]);
181209
182210 self ::assertEquals ([
183211 'simple_prop ' => 'foo ' ,
184212 'nullable_prop ' => 'bar ' ,
185- ], $ data ->except ('mixed_prop ' )->toArray ());
213+ ], $ data ->except ('initialized_prop ' )->toArray ());
186214 }
187215
188216 public function testExceptSpread (): void
189217 {
190218 $ data = SampleData::make ([
191219 'simple_prop ' => 'foo ' ,
192220 'nullable_prop ' => 'bar ' ,
193- 'mixed_prop ' => 'baz ' ,
194221 ]);
195222
196- self ::assertEquals (['simple_prop ' => 'foo ' ], $ data ->except ('mixed_prop ' , 'nullable_prop ' )->toArray ());
223+ self ::assertEquals (
224+ ['initialized_prop ' => 'Initialized ' ],
225+ $ data ->except ('simple_prop ' , 'nullable_prop ' )->toArray ()
226+ );
197227 }
198228
199229 public function testNestedDTO (): void
200230 {
201231 $ data = SampleData::make ();
202232 $ data ->nested = NestedData::make (['sample_prop ' => 'sample ' ]);
203233
204- self ::assertEquals (['nested ' => ['sample_prop ' => 'sample ' ]], $ data ->compact ()->toArray ());
234+ self ::assertEquals ([
235+ 'nested ' => ['sample_prop ' => 'sample ' ],
236+ 'initialized_prop ' => 'Initialized ' ,
237+ ], $ data ->compact ()->toArray ());
205238 }
206239
207- public function testPropertyAccess (): void
240+ public function testPropertyAccessViaGet (): void
208241 {
209242 $ data = SampleData::make (['simple_prop ' => 'foo ' ]);
210-
243+
211244 self ::assertSame ('foo ' , $ data ->get ('simple_prop ' ));
212- self ::assertSame ('foo ' , $ data ->simple_prop );
245+ }
246+
247+ public function testPropertyAccessViaGetWithDefault (): void
248+ {
249+ self ::assertSame ('foo ' , SampleData::make ()->get ('simple_prop ' , 'foo ' ));
213250 }
214251
215252 public function testAccessingNonExistentPropertyWillThrow (): void
@@ -219,4 +256,38 @@ public function testAccessingNonExistentPropertyWillThrow(): void
219256
220257 echo SampleData::make ()->nope ;
221258 }
259+
260+ public function testDirectPropertyAccess (): void
261+ {
262+ $ data = SampleData::make (['simple_prop ' => 'foo ' ]);
263+
264+ self ::assertSame ('foo ' , $ data ->simple_prop );
265+ }
266+
267+ public function testAccessingNonInitializedAccessThrows (): void
268+ {
269+ self ::expectException (DataTransferObjectException::class);
270+ self ::expectExceptionMessage (
271+ 'Tests\Fixtures\SampleData::$simple_prop must not be accessed before initialization. '
272+ );
273+
274+ echo SampleData::make ()->simple_prop ;
275+ }
276+
277+ public function testUntypedData (): void
278+ {
279+ $ data = UntypedData::make ();
280+
281+ self ::assertSame ([
282+ 'foo_prop ' => 'Foo ' ,
283+ 'null_prop ' => null ,
284+ ], $ data ->toArray ());
285+
286+ $ data = UntypedData::make (['null_prop ' => 'Not so null ' ]);
287+
288+ self ::assertSame ([
289+ 'foo_prop ' => 'Foo ' ,
290+ 'null_prop ' => 'Not so null ' ,
291+ ], $ data ->toArray ());
292+ }
222293}
0 commit comments