|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace TypeLang\Reader\Tests\Unit\Reader; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 8 | +use PHPUnit\Framework\Attributes\Group; |
| 9 | +use PHPUnit\Framework\Attributes\RequiresPhp; |
| 10 | +use TypeLang\Parser\Node\FullQualifiedName; |
| 11 | +use TypeLang\Parser\Node\Identifier; |
| 12 | +use TypeLang\Parser\Node\Stmt\IntersectionTypeNode; |
| 13 | +use TypeLang\Parser\Node\Stmt\NamedTypeNode; |
| 14 | +use TypeLang\Parser\Node\Stmt\UnionTypeNode; |
| 15 | +use TypeLang\Reader\ConstantReaderInterface; |
| 16 | +use TypeLang\Reader\Tests\Unit\Reader\Stub\__ConstantReaderEnum; |
| 17 | +use TypeLang\Reader\Tests\Unit\Reader\Stub\ConstantReaderStub; |
| 18 | + |
| 19 | +#[Group('unit'), Group('type-lang/reader')] |
| 20 | +class ConstantReaderTest extends ReaderTestCase |
| 21 | +{ |
| 22 | + #[RequiresPhp('>=8.3')] |
| 23 | + #[DataProvider('readersDataProvider')] |
| 24 | + public function testSimpleType(ConstantReaderInterface $reader): void |
| 25 | + { |
| 26 | + $type = $reader->findConstantType( |
| 27 | + constant: new \ReflectionClassConstant(ConstantReaderStub::class, 'SINGLE'), |
| 28 | + ); |
| 29 | + |
| 30 | + self::assertEquals(new NamedTypeNode( |
| 31 | + name: new Identifier('int'), |
| 32 | + ), $type); |
| 33 | + } |
| 34 | + |
| 35 | + #[RequiresPhp('>=8.3')] |
| 36 | + #[DataProvider('readersDataProvider')] |
| 37 | + public function testUnionType(ConstantReaderInterface $reader): void |
| 38 | + { |
| 39 | + $type = $reader->findConstantType( |
| 40 | + constant: new \ReflectionClassConstant(ConstantReaderStub::class, 'UNION'), |
| 41 | + ); |
| 42 | + |
| 43 | + self::assertEquals(new UnionTypeNode( |
| 44 | + a: new NamedTypeNode('string'), |
| 45 | + b: new NamedTypeNode('int'), |
| 46 | + ), $type); |
| 47 | + } |
| 48 | + |
| 49 | + #[RequiresPhp('>=8.3')] |
| 50 | + #[DataProvider('readersDataProvider')] |
| 51 | + public function testIntersectionType(ConstantReaderInterface $reader): void |
| 52 | + { |
| 53 | + $type = $reader->findConstantType( |
| 54 | + constant: new \ReflectionClassConstant(ConstantReaderStub::class, 'INTERSECTION'), |
| 55 | + ); |
| 56 | + |
| 57 | + self::assertEquals(new IntersectionTypeNode( |
| 58 | + a: new NamedTypeNode(new FullQualifiedName(__ConstantReaderEnum::class)), |
| 59 | + b: new NamedTypeNode(new FullQualifiedName(\BackedEnum::class)), |
| 60 | + ), $type); |
| 61 | + } |
| 62 | + |
| 63 | + #[RequiresPhp('>=8.3')] |
| 64 | + #[DataProvider('readersDataProvider')] |
| 65 | + public function testCompositeType(ConstantReaderInterface $reader): void |
| 66 | + { |
| 67 | + $type = $reader->findConstantType( |
| 68 | + constant: new \ReflectionClassConstant(ConstantReaderStub::class, 'COMPOSITE'), |
| 69 | + ); |
| 70 | + |
| 71 | + self::assertEquals(new UnionTypeNode( |
| 72 | + a: new IntersectionTypeNode( |
| 73 | + a: new NamedTypeNode(new FullQualifiedName(__ConstantReaderEnum::class)), |
| 74 | + b: new NamedTypeNode(new FullQualifiedName(\BackedEnum::class)), |
| 75 | + ), |
| 76 | + b: new NamedTypeNode('array'), |
| 77 | + ), $type); |
| 78 | + } |
| 79 | +} |
0 commit comments