generated from spawnia/php-package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSafeCastTest.php
More file actions
337 lines (309 loc) · 11.1 KB
/
SafeCastTest.php
File metadata and controls
337 lines (309 loc) · 11.1 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
<?php declare(strict_types=1);
namespace MLL\Utils\Tests;
use MLL\Utils\SafeCast;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
final class SafeCastTest extends TestCase
{
/**
* @dataProvider validIntProvider
*
* @param mixed $input can be anything
*/
#[DataProvider('validIntProvider')]
public function testToIntWithValidInput(int $expected, $input): void
{
self::assertSame($expected, SafeCast::toInt($input));
}
/** @return iterable<array{int, mixed}> */
public static function validIntProvider(): iterable
{
yield [42, 42];
yield [-123, -123];
yield [0, 0];
yield [42, '42'];
yield [-123, '-123'];
yield [0, '0'];
yield [999, ' 999 '];
yield [5, 5.0];
yield [-10, -10.0];
yield [0, 0.0];
yield [42, '042'];
yield [0, '000'];
yield [42, '+42'];
}
/**
* @dataProvider invalidIntProvider
*
* @param mixed $input can be anything
*/
#[DataProvider('invalidIntProvider')]
public function testToIntThrowsExceptionForInvalidInput(string $expectedMessage, $input): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage($expectedMessage);
SafeCast::toInt($input);
}
/** @return iterable<array{string, mixed}> */
public static function invalidIntProvider(): iterable
{
yield ['String value "hello" is not a valid integer format', 'hello'];
yield ['String value "123abc" is not a valid integer format', '123abc'];
yield ['String value "12.34" is not a valid integer format', '12.34'];
yield ['Empty string cannot be cast to int', ''];
yield ['Float value "5.5" cannot be safely cast to int', 5.5];
yield ['cannot be safely cast to int (not a whole number or not finite)', INF];
yield ['Cannot cast value of type "array" to int', []];
yield ['Cannot cast value of type "boolean" to int', true];
yield ['Cannot cast value of type "boolean" to int', false];
yield ['cannot be safely cast to int', PHP_INT_MAX + 1.0];
yield ['cannot be safely cast to int', (float) PHP_INT_MIN * 2];
yield ['is not a valid integer format', '99999999999999999999'];
}
/**
* @dataProvider validFloatProvider
*
* @param mixed $input can be anything
*/
#[DataProvider('validFloatProvider')]
public function testToFloatWithValidInput(float $expected, $input): void
{
self::assertSame($expected, SafeCast::toFloat($input));
}
/** @return iterable<array{float, mixed}> */
public static function validFloatProvider(): iterable
{
yield [3.14, 3.14];
yield [-2.5, -2.5];
yield [0.0, 0.0];
yield [42.0, 42];
yield [-123.0, -123];
yield [0.0, 0];
yield [3.14, '3.14'];
yield [-2.5, '-2.5'];
yield [42.0, '42'];
yield [1.23, ' 1.23 '];
yield [1.5e3, '1.5e3'];
yield [2.5e-2, '2.5e-2'];
}
/**
* @dataProvider invalidFloatProvider
*
* @param mixed $input can be anything
*/
#[DataProvider('invalidFloatProvider')]
public function testToFloatThrowsExceptionForInvalidInput(string $expectedMessage, $input): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage($expectedMessage);
SafeCast::toFloat($input);
}
/** @return iterable<array{string, mixed}> */
public static function invalidFloatProvider(): iterable
{
yield ['String value "hello" is not a valid numeric format', 'hello'];
yield ['String value "3.14abc" is not a valid numeric format', '3.14abc'];
yield ['String value "1.2.3" is not a valid numeric format', '1.2.3'];
yield ['Empty string cannot be cast to float', ''];
yield ['Cannot cast value of type "array" to float', []];
yield ['is not a valid numeric format', '0x1A'];
yield ['is not a valid numeric format', '0b1010'];
yield ['is not a valid numeric format', '-0x1A'];
yield ['is not a valid numeric format', '+0b1010'];
yield ['Cannot cast value of type "boolean" to float', true];
yield ['Cannot cast value of type "boolean" to float', false];
yield ['Cannot cast value of type "double" to float', INF];
yield ['Cannot cast value of type "double" to float', -INF];
yield ['Cannot cast value of type "double" to float', NAN];
yield ['is not a valid numeric format', '1e999'];
}
/**
* @dataProvider validStringProvider
*
* @param mixed $input can be anything
*/
#[DataProvider('validStringProvider')]
public function testToStringWithValidInput(string $expected, $input): void
{
self::assertSame($expected, SafeCast::toString($input));
}
/** @return iterable<array{string, mixed}> */
public static function validStringProvider(): iterable
{
yield ['hello', 'hello'];
yield ['', ''];
yield ['42', 42];
yield ['-123', -123];
yield ['0', 0];
yield ['3.14', 3.14];
yield ['-2.5', -2.5];
yield ['object-string-with-stringable-interface', new class() implements \Stringable {
public function __toString(): string
{
return 'object-string-with-stringable-interface';
}
}];
yield ['object-string-without-stringable-interface', new class() {
public function __toString(): string
{
return 'object-string-without-stringable-interface';
}
}];
}
public function testToStringWithNull(): void
{
self::assertSame('', SafeCast::toString(null));
}
public function testTryStringWithNullReturnsNull(): void
{
self::assertNull(SafeCast::tryString(null));
}
/**
* @dataProvider invalidStringProvider
*
* @param mixed $input can be anything
*/
#[DataProvider('invalidStringProvider')]
public function testToStringThrowsExceptionForInvalidInput(string $expectedMessage, $input): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage($expectedMessage);
SafeCast::toString($input);
}
/** @return iterable<array{string, mixed}> */
public static function invalidStringProvider(): iterable
{
yield ['Cannot cast value of type "object" to string', new \stdClass()];
yield ['Cannot cast value of type "array" to string', []];
yield ['Cannot cast value of type "boolean" to string', true];
yield ['Cannot cast value of type "boolean" to string', false];
}
/**
* @dataProvider validIntProvider
*
* @param mixed $input can be anything
*/
#[DataProvider('validIntProvider')]
public function testTryIntWithValidInput(int $expected, $input): void
{
self::assertSame($expected, SafeCast::tryInt($input));
}
/**
* @dataProvider invalidIntProvider
*
* @param mixed $input can be anything
*/
#[DataProvider('invalidIntProvider')]
public function testTryIntReturnsNullForInvalidInput(string $expectedMessage, $input): void
{
self::assertNull(SafeCast::tryInt($input));
}
/**
* @dataProvider validFloatProvider
*
* @param mixed $input can be anything
*/
#[DataProvider('validFloatProvider')]
public function testTryFloatWithValidInput(float $expected, $input): void
{
self::assertSame($expected, SafeCast::tryFloat($input));
}
/**
* @dataProvider invalidFloatProvider
*
* @param mixed $input can be anything
*/
#[DataProvider('invalidFloatProvider')]
public function testTryFloatReturnsNullForInvalidInput(string $expectedMessage, $input): void
{
self::assertNull(SafeCast::tryFloat($input));
}
/**
* @dataProvider validStringProvider
*
* @param mixed $input can be anything
*/
#[DataProvider('validStringProvider')]
public function testTryStringWithValidInput(string $expected, $input): void
{
self::assertSame($expected, SafeCast::tryString($input));
}
/**
* @dataProvider invalidStringProvider
*
* @param mixed $input can be anything
*/
#[DataProvider('invalidStringProvider')]
public function testTryStringReturnsNullForInvalidInput(string $expectedMessage, $input): void
{
self::assertNull(SafeCast::tryString($input));
}
/**
* @dataProvider validBoolProvider
*
* @param mixed $input can be anything
*/
#[DataProvider('validBoolProvider')]
public function testTryBoolWithValidInput(bool $expected, $input): void
{
self::assertSame($expected, SafeCast::tryBool($input));
}
/**
* @dataProvider invalidBoolProvider
*
* @param mixed $input can be anything
*/
#[DataProvider('invalidBoolProvider')]
public function testTryBoolReturnsNullForInvalidInput(string $expectedMessage, $input): void
{
self::assertNull(SafeCast::tryBool($input));
}
/**
* @dataProvider validBoolProvider
*
* @param mixed $input can be anything
*/
#[DataProvider('validBoolProvider')]
public function testToBoolWithValidInput(bool $expected, $input): void
{
self::assertSame($expected, SafeCast::toBool($input));
}
/** @return iterable<array{bool, mixed}> */
public static function validBoolProvider(): iterable
{
yield [true, true];
yield [false, false];
yield [true, 1];
yield [false, 0];
yield [true, '1'];
yield [false, '0'];
}
/**
* @dataProvider invalidBoolProvider
*
* @param mixed $input can be anything
*/
#[DataProvider('invalidBoolProvider')]
public function testToBoolThrowsExceptionForInvalidInput(string $expectedMessage, $input): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage($expectedMessage);
SafeCast::toBool($input);
}
/** @return iterable<array{string, mixed}> */
public static function invalidBoolProvider(): iterable
{
yield ['Cannot safely cast value of type "string" to bool', 'true'];
yield ['Cannot safely cast value of type "string" to bool', 'false'];
yield ['Cannot safely cast value of type "string" to bool', 'yes'];
yield ['Cannot safely cast value of type "string" to bool', 'no'];
yield ['Cannot safely cast value of type "string" to bool', ''];
yield ['Cannot safely cast value of type "NULL" to bool', null];
yield ['Cannot safely cast value of type "integer" to bool', 2];
yield ['Cannot safely cast value of type "integer" to bool', -1];
yield ['Cannot safely cast value of type "array" to bool', []];
yield ['Cannot safely cast value of type "object" to bool', new \stdClass()];
yield ['Cannot safely cast value of type "double" to bool', 1.0];
yield ['Cannot safely cast value of type "double" to bool', 0.0];
}
}