/
ScalarValuesMappingTest.php
262 lines (226 loc) · 10.5 KB
/
ScalarValuesMappingTest.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?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 DateTime;
use DateTimeImmutable;
use DateTimeInterface;
use stdClass;
use stdClass as ObjectAlias;
final class ScalarValuesMappingTest extends IntegrationTest
{
public function test_values_are_mapped_properly(): void
{
$source = [
'boolean' => true,
'float' => 42.404,
'floatWithInteger' => 42,
'positiveFloatValue' => 42.404,
'negativeFloatValue' => -42.404,
'integer' => 1337,
'positiveInteger' => 1337,
'negativeInteger' => -1337,
'nonPositiveInteger' => -1337,
'nonNegativeInteger' => 1337,
'integerRangeWithPositiveValue' => 1337,
'integerRangeWithNegativeValue' => -1337,
'integerRangeWithMinAndMax' => 42,
'positiveIntegerValue' => 42,
'negativeIntegerValue' => -42,
'string' => 'foo',
'nonEmptyString' => 'bar',
'numericString' => '1337',
'stringValueWithSingleQuote' => 'baz',
'stringValueContainingSpaceWithSingleQuote' => 'baz baz',
'stringValueContainingSpecialCharsWithSingleQuote' => 'baz & $ § % baz',
'stringValueWithDoubleQuote' => 'fiz',
'stringValueContainingSpaceWithDoubleQuote' => 'fiz fiz',
'stringValueContainingSpecialCharsWithDoubleQuote' => 'fiz & $ § % fiz',
'classString' => self::class,
'classStringOfDateTime' => DateTimeImmutable::class,
'classStringOfAlias' => stdClass::class,
];
foreach ([ScalarValues::class, ScalarValuesWithConstructor::class] as $class) {
try {
$result = (new MapperBuilder())->mapper()->map($class, $source);
} catch (MappingError $error) {
$this->mappingFail($error);
}
self::assertSame(true, $result->boolean);
self::assertSame(42.404, $result->float);
self::assertSame(42.0, $result->floatWithInteger);
self::assertSame(42.404, $result->positiveFloatValue); // @phpstan-ignore-line
self::assertSame(-42.404, $result->negativeFloatValue); // @phpstan-ignore-line
self::assertSame(1337, $result->integer);
self::assertSame(1337, $result->positiveInteger);
self::assertSame(-1337, $result->negativeInteger);
self::assertSame(-1337, $result->nonPositiveInteger);
self::assertSame(1337, $result->nonNegativeInteger);
self::assertSame(1337, $result->integerRangeWithPositiveValue);
self::assertSame(-1337, $result->integerRangeWithNegativeValue);
self::assertSame(42, $result->integerRangeWithMinAndMax);
self::assertSame(42, $result->positiveIntegerValue); // @phpstan-ignore-line
self::assertSame(-42, $result->negativeIntegerValue); // @phpstan-ignore-line
self::assertSame('foo', $result->string);
self::assertSame('bar', $result->nonEmptyString);
self::assertSame('1337', $result->numericString);
self::assertSame('baz', $result->stringValueWithSingleQuote); // @phpstan-ignore-line
self::assertSame('baz baz', $result->stringValueContainingSpaceWithSingleQuote); // @phpstan-ignore-line
self::assertSame('baz & $ § % baz', $result->stringValueContainingSpecialCharsWithSingleQuote); // @phpstan-ignore-line
self::assertSame('fiz', $result->stringValueWithDoubleQuote); // @phpstan-ignore-line
self::assertSame('fiz fiz', $result->stringValueContainingSpaceWithDoubleQuote); // @phpstan-ignore-line
self::assertSame('fiz & $ § % fiz', $result->stringValueContainingSpecialCharsWithDoubleQuote); // @phpstan-ignore-line
self::assertSame(self::class, $result->classString);
self::assertSame(DateTimeImmutable::class, $result->classStringOfDateTime);
self::assertSame(stdClass::class, $result->classStringOfAlias);
}
}
public function test_value_with_invalid_type_throws_exception(): void
{
try {
(new MapperBuilder())->mapper()->map(SimpleObject::class, new stdClass());
} catch (MappingError $exception) {
$error = $exception->node()->messages()[0];
self::assertSame('Value object(stdClass) is not a valid string.', (string)$error);
}
}
}
class ScalarValues
{
public bool $boolean = false;
public float $float = -1.0;
public float $floatWithInteger = -1.0;
/** @var 42.404 */
public float $positiveFloatValue;
/** @var -42.404 */
public float $negativeFloatValue;
public int $integer = -1;
/** @var positive-int */
public int $positiveInteger = 1;
/** @var negative-int */
public int $negativeInteger = -1;
/** @var non-positive-int */
public int $nonPositiveInteger = -1;
/** @var non-negative-int */
public int $nonNegativeInteger = 1;
/** @var int<-1337, 1337> */
public int $integerRangeWithPositiveValue = -1;
/** @var int<-1337, 1337> */
public int $integerRangeWithNegativeValue = -1;
/** @var int<min, max> */
public int $integerRangeWithMinAndMax = -1;
/** @var 42 */
public int $positiveIntegerValue;
/** @var -42 */
public int $negativeIntegerValue;
public string $string = 'Schwifty!';
/** @var non-empty-string */
public string $nonEmptyString = 'Schwifty!';
/** @var numeric-string */
public string $numericString = '42';
/** @var 'baz' */
public string $stringValueWithSingleQuote;
/** @var 'baz baz' */
public string $stringValueContainingSpaceWithSingleQuote;
/** @var 'baz & $ § % baz' */
public string $stringValueContainingSpecialCharsWithSingleQuote;
/** @var "fiz" */
public string $stringValueWithDoubleQuote;
/** @var "fiz fiz" */
public string $stringValueContainingSpaceWithDoubleQuote;
/** @var "fiz & $ § % fiz" */
public string $stringValueContainingSpecialCharsWithDoubleQuote;
/** @var class-string */
public string $classString = stdClass::class;
/** @var class-string<DateTimeInterface> */
public string $classStringOfDateTime = DateTime::class;
/** @var class-string<ObjectAlias> */
public string $classStringOfAlias;
}
class ScalarValuesWithConstructor extends ScalarValues
{
/**
* @param 42.404 $positiveFloatValue
* @param -42.404 $negativeFloatValue
* @param positive-int $positiveInteger
* @param negative-int $negativeInteger
* @param non-positive-int $nonPositiveInteger
* @param non-negative-int $nonNegativeInteger
* @param int<-1337, 1337> $integerRangeWithPositiveValue
* @param int<-1337, 1337> $integerRangeWithNegativeValue
* @param int<min, max> $integerRangeWithMinAndMax
* @param 42 $positiveIntegerValue
* @param -42 $negativeIntegerValue
* @param non-empty-string $nonEmptyString
* @param numeric-string $numericString
* @param 'baz' $stringValueWithSingleQuote
* @param 'baz baz' $stringValueContainingSpaceWithSingleQuote
* @param 'baz & $ § % baz' $stringValueContainingSpecialCharsWithSingleQuote
* @param "fiz" $stringValueWithDoubleQuote
* @param "fiz fiz" $stringValueContainingSpaceWithDoubleQuote
* @param "fiz & $ § % fiz" $stringValueContainingSpecialCharsWithDoubleQuote
* @param class-string $classString
* @param class-string<DateTimeInterface> $classStringOfDateTime
* @param class-string<ObjectAlias> $classStringOfAlias
*/
public function __construct(
bool $boolean,
float $float,
float $floatWithInteger,
float $positiveFloatValue,
float $negativeFloatValue,
int $integer,
int $positiveInteger,
int $negativeInteger,
int $nonPositiveInteger,
int $nonNegativeInteger,
int $integerRangeWithPositiveValue,
int $integerRangeWithNegativeValue,
int $integerRangeWithMinAndMax,
int $positiveIntegerValue,
int $negativeIntegerValue,
string $string,
string $nonEmptyString,
string $numericString,
string $stringValueWithSingleQuote,
string $stringValueContainingSpaceWithSingleQuote,
string $stringValueContainingSpecialCharsWithSingleQuote,
string $stringValueWithDoubleQuote,
string $stringValueContainingSpaceWithDoubleQuote,
string $stringValueContainingSpecialCharsWithDoubleQuote,
string $classString,
string $classStringOfDateTime,
string $classStringOfAlias
) {
$this->boolean = $boolean;
$this->float = $float;
$this->floatWithInteger = $floatWithInteger;
$this->positiveFloatValue = $positiveFloatValue;
$this->negativeFloatValue = $negativeFloatValue;
$this->integer = $integer;
$this->positiveInteger = $positiveInteger;
$this->negativeInteger = $negativeInteger;
$this->nonPositiveInteger = $nonPositiveInteger;
$this->nonNegativeInteger = $nonNegativeInteger;
$this->integerRangeWithPositiveValue = $integerRangeWithPositiveValue;
$this->integerRangeWithNegativeValue = $integerRangeWithNegativeValue;
$this->integerRangeWithMinAndMax = $integerRangeWithMinAndMax;
$this->positiveIntegerValue = $positiveIntegerValue;
$this->negativeIntegerValue = $negativeIntegerValue;
$this->string = $string;
$this->nonEmptyString = $nonEmptyString;
$this->numericString = $numericString;
$this->stringValueWithSingleQuote = $stringValueWithSingleQuote;
$this->stringValueContainingSpaceWithSingleQuote = $stringValueContainingSpaceWithSingleQuote;
$this->stringValueContainingSpecialCharsWithSingleQuote = $stringValueContainingSpecialCharsWithSingleQuote;
$this->stringValueWithDoubleQuote = $stringValueWithDoubleQuote;
$this->stringValueContainingSpaceWithDoubleQuote = $stringValueContainingSpaceWithDoubleQuote;
$this->stringValueContainingSpecialCharsWithDoubleQuote = $stringValueContainingSpecialCharsWithDoubleQuote;
$this->classString = $classString;
$this->classStringOfDateTime = $classStringOfDateTime;
$this->classStringOfAlias = $classStringOfAlias;
}
}