diff --git a/tests/NativeTypeReflectorTest.php b/tests/NativeTypeReflectorTest.php deleted file mode 100644 index e47aa7d..0000000 --- a/tests/NativeTypeReflectorTest.php +++ /dev/null @@ -1,543 +0,0 @@ -remove(self::STUBS_DIR); - } - - public static function tearDownAfterClass(): void - { - (new Filesystem())->remove(self::STUBS_DIR); - } - - /** - * @return callable-string - */ - private static function generateFunctionName(): string - { - /** @var callable-string */ - return uniqid('function_'); - } - - /** - * @return class-string - */ - private static function generateClassName(): string - { - /** @var class-string */ - return uniqid('class_'); - } - - private static function require(string $code): void - { - $file = self::STUBS_DIR.'/'.uniqid(more_entropy: true).'.php'; - (new Filesystem())->dumpFile($file, "reflectTypeFromString($type); - - assertEquals($expectedType, $reflectedType); - } - - public function testItReflectsSelfTypeFromString(): void - { - $reflector = new TypeReflector(); - - $reflectedType = $reflector->reflectTypeFromString('self', self::class); - - assertEquals(new NamedObjectT(self::class), $reflectedType); - } - - public function testItReflectsParentTypeFromString(): void - { - $reflector = new TypeReflector(); - - $reflectedType = $reflector->reflectTypeFromString('parent', self::class); - - assertEquals(new NamedObjectT(parent::class), $reflectedType); - } - - public function testItReflectsStaticTypeFromString(): void - { - $reflector = new TypeReflector(); - - $reflectedType = $reflector->reflectTypeFromString('static', self::class); - - assertEquals(new NamedObjectT(self::class), $reflectedType); - } - - /** - * @dataProvider types - * @dataProvider callableType - */ - public function testItReflectsFunctionParameterType(string $type, ?Type $expectedType): void - { - $function = self::generateFunctionName(); - self::require( - <<reflectFunctionParameterType($function, 'test'); - - assertEquals($expectedType, $reflectedType); - } - - /** - * @dataProvider types - * @dataProvider callableType - * @dataProvider voidType - * @dataProvider neverType - */ - public function testItReflectsFunctionReturnType(string $type, ?Type $expectedType): void - { - $typeDeclaration = $type === '' ? '' : ': '.$type; - $function = self::generateFunctionName(); - self::require( - <<reflectFunctionReturnType($function); - - assertEquals($expectedType, $reflectedType); - } - - /** - * @dataProvider types - */ - public function testItReflectsPropertyType(string $type, ?Type $expectedType): void - { - $class = self::generateClassName(); - self::require( - <<reflectPropertyType($class, 'test'); - - assertEquals($expectedType, $reflectedType); - } - - public function testItReflectsPropertySelfType(): void - { - $class = self::generateClassName(); - self::require( - <<reflectPropertyType($class, 'test'); - - assertEquals(new NamedObjectT($class), $reflectedType); - } - - public function testItReflectsPropertyParentType(): void - { - $class = self::generateClassName(); - self::require( - <<reflectPropertyType($class, 'test'); - - assertEquals(new NamedObjectT(\ArrayObject::class), $reflectedType); - } - - public function testItReflectsPropertyInheritedParentType(): void - { - $baseClass = self::generateClassName(); - $class = self::generateClassName(); - self::require( - <<reflectPropertyType($class, 'test'); - - assertEquals(new NamedObjectT(\ArrayObject::class), $reflectedType); - } - - /** - * @dataProvider types - */ - public function testItReflectsPromotedPropertyType(string $type, ?Type $expectedType): void - { - $class = self::generateClassName(); - self::require( - <<reflectPropertyType($class, 'test'); - - assertEquals($expectedType, $reflectedType); - } - - public function testItReflectsPromotedPropertySelfType(): void - { - $class = self::generateClassName(); - self::require( - <<reflectPropertyType($class, 'test'); - - assertEquals(new NamedObjectT($class), $reflectedType); - } - - public function testItReflectsPromotedPropertyParentType(): void - { - $class = self::generateClassName(); - self::require( - <<reflectPropertyType($class, 'test'); - - assertEquals(new NamedObjectT(\ArrayObject::class), $reflectedType); - } - - public function testItReflectsPromotedPropertyInheritedParentType(): void - { - $baseClass = self::generateClassName(); - $class = self::generateClassName(); - self::require( - <<reflectPropertyType($class, 'test'); - - assertEquals(new NamedObjectT(\ArrayObject::class), $reflectedType); - } - - /** - * @dataProvider types - * @dataProvider callableType - */ - public function testItReflectsMethodParameterType(string $type, ?Type $expectedType): void - { - $class = self::generateClassName(); - self::require( - <<reflectMethodParameterType($class, 'test', 'test'); - - assertEquals($expectedType, $reflectedType); - } - - public function testItReflectsMethodSelfParameterType(): void - { - $class = self::generateClassName(); - self::require( - <<reflectMethodParameterType($class, 'test', 'test'); - - assertEquals(new NamedObjectT($class), $reflectedType); - } - - public function testItReflectsMethodParentParameterType(): void - { - $class = self::generateClassName(); - self::require( - <<reflectMethodParameterType($class, 'test', 'test'); - - assertEquals(new NamedObjectT(\ArrayObject::class), $reflectedType); - } - - public function testItReflectsMethodInheritedParentParameterType(): void - { - $baseClass = self::generateClassName(); - $class = self::generateClassName(); - self::require( - <<reflectMethodParameterType($class, 'test', 'test'); - - assertEquals(new NamedObjectT(\ArrayObject::class), $reflectedType); - } - - /** - * @dataProvider types - * @dataProvider callableType - */ - public function testItReflectsMethodReturnType(string $type, ?Type $expectedType): void - { - $typeDeclaration = $type === '' ? '' : ': '.$type; - $class = self::generateClassName(); - self::require( - <<reflectMethodReturnType($class, 'test'); - - assertEquals($expectedType, $reflectedType); - } - - public function testItReflectsMethodSelfReturnType(): void - { - $class = self::generateClassName(); - self::require( - <<reflectMethodReturnType($class, 'test'); - - assertEquals(new NamedObjectT($class), $reflectedType); - } - - public function testItReflectsMethodParentReturnType(): void - { - $class = self::generateClassName(); - self::require( - <<reflectMethodReturnType($class, 'test'); - - assertEquals(new NamedObjectT(\ArrayObject::class), $reflectedType); - } - - public function testItReflectsMethodInheritedParentReturnType(): void - { - $baseClass = self::generateClassName(); - $class = self::generateClassName(); - self::require( - <<reflectMethodReturnType($class, 'test'); - - assertEquals(new NamedObjectT(\ArrayObject::class), $reflectedType); - } - - public function testItReflectsMethodStaticReturnType(): void - { - $class = self::generateClassName(); - self::require( - <<reflectMethodReturnType($class, 'test'); - - assertEquals(new StaticT($class), $reflectedType); - } - - public function testItReflectsMethodInheritedStaticReturnType(): void - { - $baseClass = self::generateClassName(); - $class = self::generateClassName(); - self::require( - <<reflectMethodReturnType($class, 'test'); - - assertEquals(new StaticT($baseClass), $reflectedType); - } - - /** - * @return \Generator - */ - public function types(): \Generator - { - yield 'no type' => ['', null]; - yield 'bool' => ['bool', new BoolT()]; - yield 'int' => ['int', new IntT()]; - yield 'float' => ['float', new FloatT()]; - yield 'string' => ['string', new StringT()]; - yield 'array' => ['array', new ArrayT()]; - yield 'iterable' => ['iterable', new IterableT()]; - yield 'object' => ['object', new ObjectT()]; - yield 'mixed' => ['mixed', new MixedT()]; - yield '\Closure' => ['\Closure', new NamedObjectT(\Closure::class)]; - yield 'string|int|null' => ['string|int|null', new UnionT(new StringT(), new IntT(), new NullT())]; - yield 'string|false' => ['string|false', new UnionT(new StringT(), new FalseT())]; - yield '\Countable&\Traversable' => ['\Countable&\Traversable', new IntersectionT(new NamedObjectT(\Countable::class), new NamedObjectT(\Traversable::class))]; - yield '?int' => ['?int', new NullableT(new IntT())]; - - if (\PHP_VERSION_ID >= 80200) { - yield 'null' => ['null', new NullT()]; - yield 'true' => ['true', new TrueT()]; - yield 'false' => ['false', new FalseT()]; - yield '(\Countable&\Traversable)|string' => ['(\Countable&\Traversable)|string', new UnionT( - new IntersectionT(new NamedObjectT(\Countable::class), new NamedObjectT(\Traversable::class)), - new StringT(), - )]; - } - } - - /** - * @return \Generator - */ - public function callableType(): \Generator - { - yield 'callable' => ['callable', new CallableT()]; - } - - /** - * @return \Generator - */ - public function voidType(): \Generator - { - yield 'void' => ['void', new VoidT()]; - } - - /** - * @return \Generator - */ - public function neverType(): \Generator - { - yield 'never' => ['never', new NeverT()]; - } -} diff --git a/tests/PHPDocTypeReflectorTest.php b/tests/TypeReflectorTest.php similarity index 60% rename from tests/PHPDocTypeReflectorTest.php rename to tests/TypeReflectorTest.php index 8e1fb6a..31f7e69 100644 --- a/tests/PHPDocTypeReflectorTest.php +++ b/tests/TypeReflectorTest.php @@ -43,9 +43,9 @@ * @covers \ExtendedTypeSystem\TypeReflector * @group unit */ -final class PHPDocTypeReflectorTest extends TestCase +final class TypeReflectorTest extends TestCase { - private const STUBS_DIR = __DIR__.'/../../var/test_stubs/PHPDocTypeReflectorTest'; + private const STUBS_DIR = __DIR__.'/../var/test_stubs/PHPDocTypeReflectorTest'; public static function setUpBeforeClass(): void { @@ -84,6 +84,382 @@ private static function require(string $code): void require_once $file; } + /** + * @dataProvider types + * @dataProvider callableType + */ + public function testItReflectsNativeFunctionParameterType(string $type, ?Type $expectedType): void + { + $function = self::generateFunctionName(); + self::require( + <<reflectFunctionParameterType($function, 'test'); + + assertEquals($expectedType, $reflectedType); + } + + /** + * @dataProvider types + * @dataProvider callableType + * @dataProvider voidType + * @dataProvider neverType + */ + public function testItReflectsNativeFunctionReturnType(string $type, ?Type $expectedType): void + { + $typeDeclaration = $type === '' ? '' : ': '.$type; + $function = self::generateFunctionName(); + self::require( + <<reflectFunctionReturnType($function); + + assertEquals($expectedType, $reflectedType); + } + + /** + * @dataProvider types + */ + public function testItReflectsNativePropertyType(string $type, ?Type $expectedType): void + { + $class = self::generateClassName(); + self::require( + <<reflectPropertyType($class, 'test'); + + assertEquals($expectedType, $reflectedType); + } + + public function testItReflectsNativePropertySelfType(): void + { + $class = self::generateClassName(); + self::require( + <<reflectPropertyType($class, 'test'); + + assertEquals(new NamedObjectT($class), $reflectedType); + } + + public function testItReflectsNativePropertyParentType(): void + { + $class = self::generateClassName(); + self::require( + <<reflectPropertyType($class, 'test'); + + assertEquals(new NamedObjectT(\ArrayObject::class), $reflectedType); + } + + public function testItReflectsNativePropertyInheritedParentType(): void + { + $baseClass = self::generateClassName(); + $class = self::generateClassName(); + self::require( + <<reflectPropertyType($class, 'test'); + + assertEquals(new NamedObjectT(\ArrayObject::class), $reflectedType); + } + + /** + * @dataProvider types + */ + public function testItReflectsNativePromotedPropertyType(string $type, ?Type $expectedType): void + { + $class = self::generateClassName(); + self::require( + <<reflectPropertyType($class, 'test'); + + assertEquals($expectedType, $reflectedType); + } + + public function testItReflectsNativePromotedPropertySelfType(): void + { + $class = self::generateClassName(); + self::require( + <<reflectPropertyType($class, 'test'); + + assertEquals(new NamedObjectT($class), $reflectedType); + } + + public function testItReflectsNativePromotedPropertyParentType(): void + { + $class = self::generateClassName(); + self::require( + <<reflectPropertyType($class, 'test'); + + assertEquals(new NamedObjectT(\ArrayObject::class), $reflectedType); + } + + public function testItReflectsNativePromotedPropertyInheritedParentType(): void + { + $baseClass = self::generateClassName(); + $class = self::generateClassName(); + self::require( + <<reflectPropertyType($class, 'test'); + + assertEquals(new NamedObjectT(\ArrayObject::class), $reflectedType); + } + + /** + * @dataProvider types + * @dataProvider callableType + */ + public function testItReflectsNativeMethodParameterType(string $type, ?Type $expectedType): void + { + $class = self::generateClassName(); + self::require( + <<reflectMethodParameterType($class, 'test', 'test'); + + assertEquals($expectedType, $reflectedType); + } + + public function testItReflectsNativeMethodSelfParameterType(): void + { + $class = self::generateClassName(); + self::require( + <<reflectMethodParameterType($class, 'test', 'test'); + + assertEquals(new NamedObjectT($class), $reflectedType); + } + + public function testItReflectsNativeMethodParentParameterType(): void + { + $class = self::generateClassName(); + self::require( + <<reflectMethodParameterType($class, 'test', 'test'); + + assertEquals(new NamedObjectT(\ArrayObject::class), $reflectedType); + } + + public function testItReflectsNativeMethodInheritedParentParameterType(): void + { + $baseClass = self::generateClassName(); + $class = self::generateClassName(); + self::require( + <<reflectMethodParameterType($class, 'test', 'test'); + + assertEquals(new NamedObjectT(\ArrayObject::class), $reflectedType); + } + + /** + * @dataProvider types + * @dataProvider callableType + */ + public function testItReflectsNativeMethodReturnType(string $type, ?Type $expectedType): void + { + $typeDeclaration = $type === '' ? '' : ': '.$type; + $class = self::generateClassName(); + self::require( + <<reflectMethodReturnType($class, 'test'); + + assertEquals($expectedType, $reflectedType); + } + + public function testItReflectsNativeMethodSelfReturnType(): void + { + $class = self::generateClassName(); + self::require( + <<reflectMethodReturnType($class, 'test'); + + assertEquals(new NamedObjectT($class), $reflectedType); + } + + public function testItReflectsNativeMethodParentReturnType(): void + { + $class = self::generateClassName(); + self::require( + <<reflectMethodReturnType($class, 'test'); + + assertEquals(new NamedObjectT(\ArrayObject::class), $reflectedType); + } + + public function testItReflectsNativeMethodInheritedParentReturnType(): void + { + $baseClass = self::generateClassName(); + $class = self::generateClassName(); + self::require( + <<reflectMethodReturnType($class, 'test'); + + assertEquals(new NamedObjectT(\ArrayObject::class), $reflectedType); + } + + public function testItReflectsNativeMethodStaticReturnType(): void + { + $class = self::generateClassName(); + self::require( + <<reflectMethodReturnType($class, 'test'); + + assertEquals(new StaticT($class), $reflectedType); + } + + public function testItReflectsNativeMethodInheritedStaticReturnType(): void + { + $baseClass = self::generateClassName(); + $class = self::generateClassName(); + self::require( + <<reflectMethodReturnType($class, 'test'); + + assertEquals(new StaticT($baseClass), $reflectedType); + } + /** * @dataProvider types * @dataProvider callableType