has(BasicAttribute::class)); self::assertEmpty($annotations->ofType(BasicAttribute::class)); } } public function test_class_annotations_are_fetched_correctly(): void { $object = /** * @BasicAnnotation * @AnnotationWithArguments(foo="foo") */ new class () { }; $annotations = new DoctrineAnnotations(new ReflectionClass($object)); self::assertCount(2, $annotations); self::assertTrue($annotations->has(BasicAnnotation::class)); self::assertTrue($annotations->has(AnnotationWithArguments::class)); self::assertCount(1, $annotations->ofType(BasicAnnotation::class)); self::assertCount(1, $annotations->ofType(AnnotationWithArguments::class)); } public function test_property_annotations_are_fetched_correctly(): void { $object = new class () { /** * @BasicAnnotation * @AnnotationWithArguments(foo="foo") */ public string $property; }; $annotations = new DoctrineAnnotations(new ReflectionProperty($object, 'property')); self::assertCount(2, $annotations); self::assertTrue($annotations->has(BasicAnnotation::class)); self::assertTrue($annotations->has(AnnotationWithArguments::class)); self::assertCount(1, $annotations->ofType(BasicAnnotation::class)); self::assertCount(1, $annotations->ofType(AnnotationWithArguments::class)); } public function test_method_annotations_are_fetched_correctly(): void { $object = new class () { /** * @BasicAnnotation * @AnnotationWithArguments(foo="foo") */ public function method(): void { } }; $annotations = new DoctrineAnnotations(new ReflectionMethod($object, 'method')); self::assertCount(2, $annotations); self::assertTrue($annotations->has(BasicAnnotation::class)); self::assertTrue($annotations->has(AnnotationWithArguments::class)); self::assertCount(1, $annotations->ofType(BasicAnnotation::class)); self::assertCount(1, $annotations->ofType(AnnotationWithArguments::class)); } public function test_parameter_annotations_returns_empty_array(): void { $object = new class () { public function method(string $parameter): void { } }; $reflection = (new ReflectionParameter([$object, 'method'], 'parameter')); self::assertEmpty(new DoctrineAnnotations($reflection)); } public function test_invalid_reflection_throws_exception(): void { $wrongReflector = new class () implements Reflector { public static function export(): string { return 'foo'; } public function __toString(): string { return 'foo'; } }; $this->expectException(InvalidReflectionParameter::class); $this->expectExceptionCode(1534263918); $this->expectExceptionMessage('Invalid parameter given (type `' . get_class($wrongReflector) . '`), it must be an instance of `' . ReflectionClass::class . '`, `' . ReflectionProperty::class . '`, `' . ReflectionMethod::class . '`.'); new DoctrineAnnotations($wrongReflector); } }