|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Tests\Innmind\Immutable; |
|
5
|
|
|
|
|
6
|
|
|
use Innmind\Immutable\{ |
|
7
|
|
|
Type, |
|
8
|
|
|
ValidateArgument\PrimitiveType, |
|
9
|
|
|
ValidateArgument\VariableType, |
|
10
|
|
|
ValidateArgument\MixedType, |
|
11
|
|
|
ValidateArgument\ClassType, |
|
12
|
|
|
ValidateArgument\NullableType, |
|
13
|
|
|
ValidateArgument\UnionType |
|
14
|
|
|
}; |
|
15
|
|
|
use PHPUnit\Framework\TestCase; |
|
16
|
|
|
|
|
17
|
|
|
class TypeTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
public function testGetSpecificationFor() |
|
20
|
|
|
{ |
|
21
|
|
|
$this->assertInstanceOf(PrimitiveType::class, Type::of('null')); |
|
22
|
|
|
$this->assertInstanceOf(PrimitiveType::class, Type::of('string')); |
|
23
|
|
|
$this->assertInstanceOf(PrimitiveType::class, Type::of('int')); |
|
24
|
|
|
$this->assertInstanceOf(PrimitiveType::class, Type::of('float')); |
|
25
|
|
|
$this->assertInstanceOf(PrimitiveType::class, Type::of('array')); |
|
26
|
|
|
$this->assertInstanceOf(PrimitiveType::class, Type::of('bool')); |
|
27
|
|
|
$this->assertInstanceOf(PrimitiveType::class, Type::of('object')); |
|
28
|
|
|
$this->assertInstanceOf(VariableType::class, Type::of('variable')); |
|
29
|
|
|
$this->assertInstanceOf(MixedType::class, Type::of('mixed')); |
|
30
|
|
|
$this->assertInstanceOf(ClassType::class, Type::of('stdClass')); |
|
31
|
|
|
$this->assertInstanceOf(NullableType::class, Type::of('?string')); |
|
32
|
|
|
$this->assertInstanceOf(NullableType::class, Type::of('?int')); |
|
33
|
|
|
$this->assertInstanceOf(NullableType::class, Type::of('?float')); |
|
34
|
|
|
$this->assertInstanceOf(NullableType::class, Type::of('?array')); |
|
35
|
|
|
$this->assertInstanceOf(NullableType::class, Type::of('?bool')); |
|
36
|
|
|
$this->assertInstanceOf(NullableType::class, Type::of('?object')); |
|
37
|
|
|
$this->assertInstanceOf(NullableType::class, Type::of('?variable')); |
|
38
|
|
|
$this->assertInstanceOf(NullableType::class, Type::of('?stdClass')); |
|
39
|
|
|
$this->assertInstanceOf(UnionType::class, Type::of('int|stdClass')); |
|
40
|
|
|
|
|
41
|
|
|
$this->assertNull(Type::of('?string')('foo', 1)); |
|
42
|
|
|
$this->assertNull(Type::of('?int')(42, 1)); |
|
43
|
|
|
$this->assertNull(Type::of('?float')(2.4, 1)); |
|
44
|
|
|
$this->assertNull(Type::of('?array')([], 1)); |
|
45
|
|
|
$this->assertNull(Type::of('?bool')(true, 1)); |
|
46
|
|
|
$this->assertNull(Type::of('?object')(new \stdClass, 1)); |
|
47
|
|
|
$this->assertNull(Type::of('?variable')(false, 1)); |
|
48
|
|
|
$this->assertNull(Type::of('?stdClass')(new \stdClass, 1)); |
|
49
|
|
|
$this->assertNull(Type::of('int|stdClass')(new \stdClass, 1)); |
|
50
|
|
|
$this->assertNull(Type::of('int|stdClass')(42, 1)); |
|
51
|
|
|
$this->assertNull(Type::of('int|stdClass|bool')(true, 1)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function testTypeOfNullableNullIsNotAccepted() |
|
55
|
|
|
{ |
|
56
|
|
|
$this->expectException(\ParseError::class); |
|
57
|
|
|
$this->expectExceptionMessage('\'null\' type is already nullable'); |
|
58
|
|
|
|
|
59
|
|
|
Type::of('?null'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function testTypeOfNullableMixedIsNotAccepted() |
|
63
|
|
|
{ |
|
64
|
|
|
$this->expectException(\ParseError::class); |
|
65
|
|
|
$this->expectExceptionMessage('\'mixed\' type already accepts \'null\' values'); |
|
66
|
|
|
|
|
67
|
|
|
Type::of('?mixed'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function testTypeOfNullableUnionIsNotAccepted() |
|
71
|
|
|
{ |
|
72
|
|
|
$this->expectException(\ParseError::class); |
|
73
|
|
|
$this->expectExceptionMessage('Nullable expression is not allowed in a union type'); |
|
74
|
|
|
|
|
75
|
|
|
Type::of('int|?float'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function testDetermineType() |
|
79
|
|
|
{ |
|
80
|
|
|
$this->assertSame('stdClass', Type::determine(new \stdClass)); |
|
81
|
|
|
$this->assertSame('null', Type::determine(null)); |
|
82
|
|
|
$this->assertSame('string', Type::determine('')); |
|
83
|
|
|
$this->assertSame('int', Type::determine(1)); |
|
84
|
|
|
$this->assertSame('float', Type::determine(1.1)); |
|
85
|
|
|
$this->assertSame('array', Type::determine([])); |
|
86
|
|
|
$this->assertSame('bool', Type::determine(true)); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|