/
IntersectionTypeTest.php
88 lines (76 loc) 路 2.51 KB
/
IntersectionTypeTest.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
<?php
declare(strict_types=1);
namespace Psl\Tests\Unit\Type;
use Iterator;
use Psl\Collection\CollectionInterface;
use Psl\Collection\IndexAccessInterface;
use Psl\Type;
final class IntersectionTypeTest extends TypeTest
{
public function testIntersectionLeft(): void
{
$intersection = Type\intersection(Type\array_key(), Type\int(), Type\positive_int());
static::assertSame(1, $intersection->coerce('1'));
}
public function getType(): Type\TypeInterface
{
return Type\intersection(Type\int(), Type\array_key());
}
public function getValidCoercions(): iterable
{
yield [1, 1];
yield ['1', 1];
yield ['123', 123];
yield [$this->stringable('123'), 123];
yield [$this->stringable('000'), 0];
yield [$this->stringable('0007'), 7];
}
public function getInvalidCoercions(): iterable
{
yield [null];
yield [STDIN];
yield ['hello'];
yield [$this->stringable('foo')];
yield [new class {
}];
}
public function getToStringExamples(): iterable
{
yield [
Type\intersection(
Type\object(IndexAccessInterface::class),
Type\object(CollectionInterface::class)
),
'Psl\Collection\IndexAccessInterface&Psl\Collection\CollectionInterface'
];
yield [
Type\intersection(
Type\object(IndexAccessInterface::class),
Type\union(
Type\object(CollectionInterface::class),
Type\object(Iterator::class)
)
),
'Psl\Collection\IndexAccessInterface&(Psl\Collection\CollectionInterface|Iterator)'
];
yield [
Type\intersection(
Type\union(
Type\object(CollectionInterface::class),
Type\object(Iterator::class)
),
Type\object(IndexAccessInterface::class)
),
'(Psl\Collection\CollectionInterface|Iterator)&Psl\Collection\IndexAccessInterface'
];
yield [
Type\intersection(
Type\object(IndexAccessInterface::class),
Type\object(CollectionInterface::class),
Type\object(Iterator::class),
Type\shape(['id' => Type\string()]),
),
'Psl\Collection\IndexAccessInterface&Psl\Collection\CollectionInterface&Iterator&array{\'id\': string}'
];
}
}