Passed
Pull Request — new-parser-ast-metadata (#3)
by
unknown
03:27
created

testValidatesTargetForMatchingScope()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\Annotations\Metadata;
6
7
use Doctrine\Annotations\Metadata\AnnotationMetadata;
8
use Doctrine\Annotations\Metadata\AnnotationTarget;
9
use Doctrine\Annotations\Metadata\InvalidTarget;
10
use Doctrine\Annotations\Parser\Scope;
11
use Doctrine\Tests\Annotations\Parser\ScopeMother;
12
use PHPUnit\Framework\TestCase;
13
use ReflectionClass;
14
use ReflectionMethod;
15
use ReflectionProperty;
16
use Reflector;
17
use function get_class;
18
use function sprintf;
19
20
final class AnnotationMetadataTest extends TestCase
21
{
22
    /**
23
     * @dataProvider targetsWithMatchingScopeProvider
24
     */
25
    public function testValidatesTargetForMatchingScope(AnnotationTarget $target, Scope $scope) : void
26
    {
27
        $metadata = AnnotationMetadataMother::withTarget($target);
28
29
        $metadata->validateTarget($scope);
30
31
        self::assertTrue(true);
32
    }
33
34
    /**
35
     * @return mixed[]
36
     */
37
    public function targetsWithMatchingScopeProvider() : iterable
38
    {
39
        /** @var Reflector[] $reflectors */
40
        $reflectors = [
41
            new ReflectionClass(self::class),
42
            new ReflectionProperty(AnnotationMetadata::class, 'name'),
43
            new ReflectionMethod(self::class, 'setUp'),
44
        ];
45
46
        foreach ($reflectors as $reflector) {
47
            yield 'TargetAll for reflector ' . get_class($reflector) => [
0 ignored issues
show
Bug Best Practice introduced by
The expression yield 'TargetAll for ref...ithSubject($reflector)) returns the type Generator which is incompatible with the documented return type array<mixed,mixed>.
Loading history...
48
                new AnnotationTarget(AnnotationTarget::TARGET_ALL),
49
                ScopeMother::withSubject($reflector),
50
            ];
51
        }
52
53
        foreach ([AnnotationTarget::TARGET_ALL, AnnotationTarget::TARGET_ANNOTATION] as $target) {
54
            yield sprintf('Target of value %d for nested scope', $target) => [
55
                new AnnotationTarget($target),
56
                ScopeMother::withNestingLevel(2),
57
            ];
58
        }
59
60
        $matchingReflectors = [
61
            AnnotationTarget::TARGET_CLASS => $reflectors[0],
62
            AnnotationTarget::TARGET_PROPERTY => $reflectors[1],
63
            AnnotationTarget::TARGET_METHOD => $reflectors[2],
64
        ];
65
66
        foreach ($matchingReflectors as $target => $reflector) {
67
            yield sprintf('Target of value %d for reflector %s', $target, get_class($reflector)) => [
68
                new AnnotationTarget($target),
69
                ScopeMother::withSubject($reflector),
70
            ];
71
72
            yield sprintf('Target of value %d for reflector %s', AnnotationTarget::TARGET_ALL, get_class($reflector)) => [
73
                new AnnotationTarget(AnnotationTarget::TARGET_ALL),
74
                ScopeMother::withSubject($reflector),
75
            ];
76
        }
77
    }
78
79
    /**
80
     * @dataProvider targetsWithNotMatchingScopeProvider
81
     */
82
    public function testValidatesTargetForNotMatchingScopeAndThrows(AnnotationTarget $target, Scope $scope) : void
83
    {
84
        $metadata = AnnotationMetadataMother::withTarget($target);
85
86
        $this->expectException(InvalidTarget::class);
87
88
        $metadata->validateTarget($scope);
89
    }
90
91
    /**
92
     * @return mixed[]
93
     */
94
    public function targetsWithNotMatchingScopeProvider() : iterable
95
    {
96
        foreach ([AnnotationTarget::TARGET_CLASS, AnnotationTarget::TARGET_METHOD, AnnotationTarget::TARGET_PROPERTY] as $target) {
97
            yield sprintf('Target of value %d for nested scope', $target) => [
0 ignored issues
show
Bug Best Practice introduced by
The expression yield sprintf('Target of...r::withNestingLevel(2)) returns the type Generator which is incompatible with the documented return type array<mixed,mixed>.
Loading history...
98
                new AnnotationTarget($target),
99
                ScopeMother::withNestingLevel(2),
100
            ];
101
        }
102
103
        $notMatchingReflectors = [
104
            AnnotationTarget::TARGET_CLASS => new ReflectionMethod(self::class, 'setUp'),
105
            AnnotationTarget::TARGET_METHOD => new ReflectionProperty(AnnotationMetadata::class, 'name'),
106
            AnnotationTarget::TARGET_PROPERTY => new ReflectionClass(self::class),
107
        ];
108
109
        foreach ($notMatchingReflectors as $target => $reflector) {
110
            yield sprintf('Target of value %d for reflector %s', $target, get_class($reflector)) => [
111
                new AnnotationTarget($target),
112
                ScopeMother::withSubject($reflector),
113
            ];
114
        }
115
    }
116
}
117