66 * For the full copyright and license information, please view the LICENSE
77 * file that was distributed with this source code.
88 */
9+
910declare (strict_types=1 );
1011
1112namespace EventEngineTest \Data ;
1213
1314use EventEngineTest \Data \Stub \ImmutableItem ;
1415use EventEngineTest \Data \Stub \ImmutableRecordWithNoTypes ;
1516use EventEngineTest \Data \Stub \ImmutableRecordWithTypedGetters ;
17+ use EventEngineTest \Data \Stub \RecordWithSpecialKey ;
1618use EventEngineTest \Data \Stub \RecordWithStringList ;
1719use EventEngineTest \Data \Stub \TypeHintedImmutableRecord ;
1820use EventEngineTest \Data \Stub \TypeHintedImmutableRecordWithValueObjects ;
2325use EventEngineTest \Data \Stub \ValueObject \Type ;
2426use EventEngineTest \Data \Stub \ValueObject \Version ;
2527use PHPUnit \Framework \TestCase ;
26- use function sprintf ;
2728
2829final class ImmutableRecordLogicTest extends TestCase
2930{
@@ -121,7 +122,7 @@ public function it_returns_new_record_with_changed_properties()
121122
122123 $ changedValueObjects = $ valueObjects ->with ([
123124 'version ' => Version::fromInt (2 ),
124- 'percentage ' => Percentage::fromFloat (0.9 )
125+ 'percentage ' => Percentage::fromFloat (0.9 ),
125126 ]);
126127
127128 $ this ->data ['type ' ] = null ;
@@ -152,6 +153,28 @@ public function it_equals_other_record_with_same_values()
152153 $ this ->assertTrue ($ valueObjects ->equals ($ other ));
153154 }
154155
156+ /**
157+ * @test
158+ */
159+ public function it_supports_special_keys (): void
160+ {
161+ // emulates snake_case
162+ $ recordArray = [
163+ RecordWithSpecialKey::BANK_ACCOUNT => '12324434 ' ,
164+ RecordWithSpecialKey::SUCCESS_RATE => 33.33 ,
165+ RecordWithSpecialKey::ITEM_LIST => [['name ' => 'Awesome tester ' ], ['name ' => 'John Smith ' ]],
166+ ];
167+ $ specialKey = RecordWithSpecialKey::fromArray ($ recordArray );
168+ $ this ->assertSame ($ recordArray , $ specialKey ->toArray ());
169+
170+ $ specialKey = RecordWithSpecialKey::fromRecordData ([
171+ RecordWithSpecialKey::BANK_ACCOUNT => $ recordArray [RecordWithSpecialKey::BANK_ACCOUNT ],
172+ RecordWithSpecialKey::SUCCESS_RATE => Percentage::fromFloat ($ recordArray [RecordWithSpecialKey::SUCCESS_RATE ]),
173+ RecordWithSpecialKey::ITEM_LIST => ItemList::fromArray ($ recordArray [RecordWithSpecialKey::ITEM_LIST ]),
174+ ]);
175+ $ this ->assertSame ($ recordArray , $ specialKey ->toArray ());
176+ }
177+
155178 /**
156179 * @test
157180 */
@@ -183,7 +206,7 @@ public function it_throws_exception_if_non_nullable_prop_should_be_set_to_null()
183206 {
184207 $ this ->data ['version ' ] = null ;
185208
186- $ this ->expectExceptionMessage (" Got null for non nullable property version of Record " . TypeHintedImmutableRecord::class);
209+ $ this ->expectExceptionMessage (' Got null for non nullable property version of Record ' . TypeHintedImmutableRecord::class);
187210
188211 TypeHintedImmutableRecord::fromArray ($ this ->data );
189212 }
@@ -195,8 +218,8 @@ public function it_throws_exception_if_property_value_has_wrong_type()
195218 {
196219 $ this ->data ['version ' ] = 'v1 ' ;
197220
198- $ this ->expectExceptionMessage (sprintf (
199- " Record %s data contains invalid value for property version. Expected type is int. Got type string. " ,
221+ $ this ->expectExceptionMessage (\ sprintf (
222+ ' Record %s data contains invalid value for property version. Expected type is int. Got type string. ' ,
200223 TypeHintedImmutableRecord::class
201224 ));
202225
@@ -208,10 +231,10 @@ public function it_throws_exception_if_property_value_has_wrong_type()
208231 */
209232 public function it_throws_exception_if_array_property_contains_invalid_value ()
210233 {
211- $ stringList = [" abc " , 123 , " def " ];
234+ $ stringList = [' abc ' , 123 , ' def ' ];
212235
213- $ this ->expectExceptionMessage (sprintf (
214- " Record %s data contains invalid value for property stringList. Value should be an array of string, but at least one item of the array has the wrong type. " ,
236+ $ this ->expectExceptionMessage (\ sprintf (
237+ ' Record %s data contains invalid value for property stringList. Value should be an array of string, but at least one item of the array has the wrong type. ' ,
215238 RecordWithStringList::class
216239 ));
217240
0 commit comments