/
ShapedArrayValuesMappingTest.php
189 lines (169 loc) · 7.47 KB
/
ShapedArrayValuesMappingTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
declare(strict_types=1);
namespace CuyZ\Valinor\Tests\Integration\Mapping\Object;
use CuyZ\Valinor\Mapper\MappingError;
use CuyZ\Valinor\MapperBuilder;
use CuyZ\Valinor\Tests\Integration\IntegrationTest;
use CuyZ\Valinor\Tests\Integration\Mapping\Fixture\SimpleObject;
use stdClass;
final class ShapedArrayValuesMappingTest extends IntegrationTest
{
public function test_values_are_mapped_properly(): void
{
$source = [
'basicShapedArrayWithStringKeys' => [
'foo' => 'fiz',
'bar' => 42,
],
'basicShapedArrayWithIntegerKeys' => [
0 => 'fiz',
1 => 42.404,
],
'shapedArrayWithObject' => ['foo' => 'bar'],
'shapedArrayWithOptionalValue' => [
'optionalString' => 'some value',
],
'shapedArrayOnSeveralLines' => [
'foo' => 'fiz',
'bar' => 42,
],
'shapedArrayOnSeveralLinesWithTrailingComma' => [
'foo' => 'fiz',
'bar' => 42,
],
'advancedShapedArray' => [
'mandatoryString' => 'bar',
1337,
42.404,
],
'shapedArrayWithClassNameAsKey' => [
'stdClass' => 'foo',
],
'shapedArrayWithLowercaseClassNameAsKey' => [
'stdclass' => 'foo',
],
'shapedArrayWithEnumNameAsKey' => [
'EnumAtRootNamespace' => 'foo',
],
'shapedArrayWithLowercaseEnumNameAsKey' => [
'enumatrootnamespace' => 'foo',
],
];
foreach ([ShapedArrayValues::class, ShapedArrayValuesWithConstructor::class] as $class) {
try {
$result = (new MapperBuilder())->mapper()->map($class, $source);
} catch (MappingError $error) {
$this->mappingFail($error);
}
self::assertSame($source['basicShapedArrayWithStringKeys'], $result->basicShapedArrayWithStringKeys);
self::assertSame($source['basicShapedArrayWithIntegerKeys'], $result->basicShapedArrayWithIntegerKeys);
self::assertInstanceOf(SimpleObject::class, $result->shapedArrayWithObject['foo']); // @phpstan-ignore-line
self::assertSame($source['shapedArrayWithOptionalValue'], $result->shapedArrayWithOptionalValue);
self::assertSame($source['shapedArrayOnSeveralLines'], $result->shapedArrayOnSeveralLines);
self::assertSame($source['shapedArrayOnSeveralLinesWithTrailingComma'], $result->shapedArrayOnSeveralLinesWithTrailingComma);
self::assertSame('bar', $result->advancedShapedArray['mandatoryString']);
self::assertSame(1337, $result->advancedShapedArray[0]);
self::assertSame(42.404, $result->advancedShapedArray[1]);
self::assertSame('foo', $result->shapedArrayWithClassNameAsKey['stdClass']);
self::assertSame('foo', $result->shapedArrayWithLowercaseClassNameAsKey['stdclass']);
self::assertSame('foo', $result->shapedArrayWithEnumNameAsKey['EnumAtRootNamespace']);
self::assertSame('foo', $result->shapedArrayWithLowercaseEnumNameAsKey['enumatrootnamespace']);
}
}
public function test_value_with_invalid_type_throws_exception(): void
{
try {
(new MapperBuilder())->mapper()->map(ShapedArrayValues::class, [
'basicShapedArrayWithStringKeys' => [
'foo' => new stdClass(),
'bar' => 42,
],
]);
} catch (MappingError $exception) {
$error = $exception->node()->children()['basicShapedArrayWithStringKeys']->children()['foo']->messages()[0];
self::assertSame('Value object(stdClass) is not a valid string.', (string)$error);
}
}
}
class ShapedArrayValues
{
/** @var array{foo: string, bar: int} */
public array $basicShapedArrayWithStringKeys;
/** @var array{0: string, 1: float} */
public array $basicShapedArrayWithIntegerKeys;
/** @var array{foo: SimpleObject} */
public array $shapedArrayWithObject;
/** @var array{optionalString?: string} */
public array $shapedArrayWithOptionalValue;
/**
* @var array{
* foo: string,
* bar: int
* }
*/
public array $shapedArrayOnSeveralLines;
/**
* @var array{
* foo: string,
* bar: int,
* }
*/
public array $shapedArrayOnSeveralLinesWithTrailingComma;
/** @var array{0: int, float, optionalString?: string, mandatoryString: string} */
public array $advancedShapedArray;
/** @var array{stdClass: string} */
public array $shapedArrayWithClassNameAsKey;
/** @var array{stdclass: string} */
public array $shapedArrayWithLowercaseClassNameAsKey;
/** @var array{EnumAtRootNamespace: string} */
public array $shapedArrayWithEnumNameAsKey;
/** @var array{enumatrootnamespace: string} */
public array $shapedArrayWithLowercaseEnumNameAsKey;
}
class ShapedArrayValuesWithConstructor extends ShapedArrayValues
{
/**
* @param array{foo: string, bar: int} $basicShapedArrayWithStringKeys
* @param array{0: string, 1: float} $basicShapedArrayWithIntegerKeys
* @param array{foo: SimpleObject} $shapedArrayWithObject
* @param array{optionalString?: string} $shapedArrayWithOptionalValue
* @param array{
* foo: string,
* bar: int
* } $shapedArrayOnSeveralLines
* @param array{
* foo: string,
* bar: int,
* } $shapedArrayOnSeveralLinesWithTrailingComma
* @param array{0: int, float, optionalString?: string, mandatoryString: string} $advancedShapedArray
* @param array{stdClass: string} $shapedArrayWithClassNameAsKey
* @param array{stdclass: string} $shapedArrayWithLowercaseClassNameAsKey
* @param array{EnumAtRootNamespace: string} $shapedArrayWithEnumNameAsKey
* @param array{enumatrootnamespace: string} $shapedArrayWithLowercaseEnumNameAsKey
*/
public function __construct(
array $basicShapedArrayWithStringKeys,
array $basicShapedArrayWithIntegerKeys,
array $shapedArrayWithObject,
array $shapedArrayWithOptionalValue,
array $shapedArrayOnSeveralLines,
array $shapedArrayOnSeveralLinesWithTrailingComma,
array $advancedShapedArray,
array $shapedArrayWithClassNameAsKey,
array $shapedArrayWithLowercaseClassNameAsKey,
array $shapedArrayWithEnumNameAsKey,
array $shapedArrayWithLowercaseEnumNameAsKey,
) {
$this->basicShapedArrayWithStringKeys = $basicShapedArrayWithStringKeys;
$this->basicShapedArrayWithIntegerKeys = $basicShapedArrayWithIntegerKeys;
$this->shapedArrayWithObject = $shapedArrayWithObject;
$this->shapedArrayWithOptionalValue = $shapedArrayWithOptionalValue;
$this->shapedArrayOnSeveralLines = $shapedArrayOnSeveralLines;
$this->shapedArrayOnSeveralLinesWithTrailingComma = $shapedArrayOnSeveralLinesWithTrailingComma;
$this->advancedShapedArray = $advancedShapedArray;
$this->shapedArrayWithClassNameAsKey = $shapedArrayWithClassNameAsKey;
$this->shapedArrayWithLowercaseClassNameAsKey = $shapedArrayWithLowercaseClassNameAsKey;
$this->shapedArrayWithEnumNameAsKey = $shapedArrayWithEnumNameAsKey;
$this->shapedArrayWithLowercaseEnumNameAsKey = $shapedArrayWithLowercaseEnumNameAsKey;
}
}