|
1 | 1 | <?php |
2 | 2 |
|
3 | | -namespace StrvalTest; |
| 3 | +namespace StrvalFamilyTest; |
4 | 4 |
|
5 | 5 | use function PHPStan\Testing\assertType; |
6 | 6 |
|
7 | 7 | /** |
8 | 8 | * @param class-string<\stdClass> $class |
9 | 9 | */ |
10 | | -function test(string $class) |
| 10 | +function strvalTest(string $string, string $class): void |
11 | 11 | { |
| 12 | +assertType('null', strval()); |
12 | 13 | assertType('\'foo\'', strval('foo')); |
| 14 | +assertType('string', strval($string)); |
13 | 15 | assertType('\'\'', strval(null)); |
| 16 | +assertType('\'\'', strval(false)); |
| 17 | +assertType('\'1\'', strval(true)); |
14 | 18 | assertType('\'\'|\'1\'', strval(rand(0, 1) === 0)); |
| 19 | +assertType('\'42\'', strval(42)); |
15 | 20 | assertType('string&numeric', strval(rand())); |
16 | 21 | assertType('string&numeric', strval(rand() * 0.1)); |
17 | 22 | assertType('string&numeric', strval(strval(rand()))); |
18 | 23 | assertType('class-string<stdClass>', strval($class)); |
| 24 | +assertType('string', strval(new \Exception())); |
| 25 | +assertType('*ERROR*', strval(new \stdClass())); |
| 26 | +} |
| 27 | + |
| 28 | +function intvalTest(string $string): void |
| 29 | +{ |
| 30 | +assertType('null', intval()); |
| 31 | +assertType('42', intval('42')); |
| 32 | +assertType('0', intval('foo')); |
| 33 | +assertType('int', intval($string)); |
| 34 | +assertType('0', intval(null)); |
| 35 | +assertType('0', intval(false)); |
| 36 | +assertType('1', intval(true)); |
| 37 | +assertType('0|1', intval(rand(0, 1) === 0)); |
| 38 | +assertType('42', intval(42)); |
| 39 | +assertType('int', intval(rand())); |
| 40 | +assertType('int', intval(rand() * 0.1)); |
| 41 | +assertType('0', intval([])); |
| 42 | +assertType('1', intval([null])); |
| 43 | +} |
| 44 | + |
| 45 | +function floatvalTest(string $string): void |
| 46 | +{ |
| 47 | +assertType('null', floatval()); |
| 48 | +assertType('3.14', floatval('3.14')); |
| 49 | +assertType('0.0', floatval('foo')); |
| 50 | +assertType('float', floatval($string)); |
| 51 | +assertType('0.0', floatval(null)); |
| 52 | +assertType('0.0', floatval(false)); |
| 53 | +assertType('1.0', floatval(true)); |
| 54 | +assertType('0.0|1.0', floatval(rand(0, 1) === 0)); |
| 55 | +assertType('42.0', floatval(42)); |
| 56 | +assertType('float', floatval(rand())); |
| 57 | +assertType('float', floatval(rand() * 0.1)); |
| 58 | +assertType('0.0', floatval([])); |
| 59 | +assertType('1.0', floatval([null])); |
| 60 | +} |
| 61 | + |
| 62 | +function boolvalTest(string $string): void |
| 63 | +{ |
| 64 | +assertType('null', boolval()); |
| 65 | +assertType('false', boolval('')); |
| 66 | +assertType('true', boolval('foo')); |
| 67 | +assertType('bool', boolval($string)); |
| 68 | +assertType('false', boolval(null)); |
| 69 | +assertType('false', boolval(false)); |
| 70 | +assertType('true', boolval(true)); |
| 71 | +assertType('bool', boolval(rand(0, 1) === 0)); |
| 72 | +assertType('true', boolval(42)); |
| 73 | +assertType('bool', boolval(rand())); |
| 74 | +assertType('bool', boolval(rand() * 0.1)); |
| 75 | +assertType('false', boolval([])); |
| 76 | +assertType('true', boolval([null])); |
| 77 | +assertType('true', boolval(new \stdClass())); |
19 | 78 | } |
0 commit comments