-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
TypeTest.php
63 lines (53 loc) · 2.55 KB
/
TypeTest.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
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\TypeInfo\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\TypeInfo\Exception\LogicException;
use Symfony\Component\TypeInfo\Type;
use Symfony\Component\TypeInfo\TypeIdentifier;
class TypeTest extends TestCase
{
public function testIs()
{
$isInt = fn (Type $t) => TypeIdentifier::INT === $t->getBaseType()->getTypeIdentifier();
$this->assertTrue(Type::int()->is($isInt));
$this->assertTrue(Type::union(Type::string(), Type::int())->is($isInt));
$this->assertTrue(Type::generic(Type::int(), Type::string())->is($isInt));
$this->assertFalse(Type::string()->is($isInt));
$this->assertFalse(Type::union(Type::string(), Type::float())->is($isInt));
$this->assertFalse(Type::generic(Type::string(), Type::int())->is($isInt));
}
public function testIsNullable()
{
$this->assertTrue(Type::null()->isNullable());
$this->assertTrue(Type::mixed()->isNullable());
$this->assertTrue(Type::nullable(Type::int())->isNullable());
$this->assertTrue(Type::union(Type::int(), Type::null())->isNullable());
$this->assertTrue(Type::union(Type::int(), Type::mixed())->isNullable());
$this->assertTrue(Type::generic(Type::null(), Type::string())->isNullable());
$this->assertFalse(Type::int()->isNullable());
$this->assertFalse(Type::union(Type::int(), Type::string())->isNullable());
$this->assertFalse(Type::generic(Type::int(), Type::nullable(Type::string()))->isNullable());
$this->assertFalse(Type::generic(Type::int(), Type::mixed())->isNullable());
}
public function testGetBaseType()
{
$this->assertEquals(Type::string(), Type::string()->getBaseType());
$this->assertEquals(Type::object(self::class), Type::object(self::class)->getBaseType());
$this->assertEquals(Type::object(), Type::generic(Type::object(), Type::int())->getBaseType());
$this->assertEquals(Type::builtin(TypeIdentifier::ARRAY), Type::list()->getBaseType());
$this->assertEquals(Type::int(), Type::collection(Type::generic(Type::int(), Type::string()))->getBaseType());
}
public function testCannotGetBaseTypeOnCompoundType()
{
$this->expectException(LogicException::class);
Type::union(Type::int(), Type::string())->getBaseType();
}
}