Failed Conditions
Pull Request — new-parser-ast-metadata (#3)
by
unknown
02:22
created

TargetValidatorTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 43
dl 0
loc 101
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 3 1
A targetsWithMatchingScopeProvider() 0 38 4
A testValidatesTargetForNotMatchingScopeAndThrows() 0 7 1
A targetsWithNotMatchingScopeProvider() 0 19 3
A testValidatesTargetForMatchingScope() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\Annotations\Assembler\Validator;
6
7
use Doctrine\Annotations\Assembler\Validator\Exception\InvalidTarget;
8
use Doctrine\Annotations\Assembler\Validator\TargetValidator;
9
use Doctrine\Annotations\Metadata\AnnotationMetadata;
10
use Doctrine\Annotations\Metadata\AnnotationTarget;
11
use Doctrine\Annotations\Parser\Scope;
12
use Doctrine\Tests\Annotations\Metadata\AnnotationMetadataMother;
13
use Doctrine\Tests\Annotations\Parser\ScopeMother;
14
use PHPUnit\Framework\TestCase;
15
use ReflectionClass;
16
use ReflectionMethod;
17
use ReflectionProperty;
18
use Reflector;
19
use function get_class;
20
use function sprintf;
21
22
final class TargetValidatorTest extends TestCase
23
{
24
    /** @var TargetValidator */
25
    private $validator;
26
27
    protected function setUp() : void
28
    {
29
        $this->validator = new TargetValidator();
30
    }
31
32
    /**
33
     * @dataProvider targetsWithMatchingScopeProvider
34
     */
35
    public function testValidatesTargetForMatchingScope(AnnotationTarget $target, Scope $scope) : void
36
    {
37
        $metadata = AnnotationMetadataMother::withTarget($target);
38
39
        $this->validator->validate($metadata, $scope);
40
41
        self::assertTrue(true);
42
    }
43
44
    /**
45
     * @return mixed[]
46
     */
47
    public function targetsWithMatchingScopeProvider() : iterable
48
    {
49
        /** @var Reflector[] $reflectors */
50
        $reflectors = [
51
            new ReflectionClass(self::class),
52
            new ReflectionProperty(AnnotationMetadata::class, 'name'),
53
            new ReflectionMethod(self::class, 'setUp'),
54
        ];
55
56
        foreach ($reflectors as $reflector) {
57
            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...
58
                new AnnotationTarget(AnnotationTarget::TARGET_ALL),
59
                ScopeMother::withSubject($reflector),
60
            ];
61
        }
62
63
        foreach ([AnnotationTarget::TARGET_ALL, AnnotationTarget::TARGET_ANNOTATION] as $target) {
64
            yield sprintf('Target of value %d for nested scope', $target) => [
65
                new AnnotationTarget($target),
66
                ScopeMother::withNestingLevel(2),
67
            ];
68
        }
69
70
        $matchingReflectors = [
71
            AnnotationTarget::TARGET_CLASS => $reflectors[0],
72
            AnnotationTarget::TARGET_PROPERTY => $reflectors[1],
73
            AnnotationTarget::TARGET_METHOD => $reflectors[2],
74
        ];
75
76
        foreach ($matchingReflectors as $target => $reflector) {
77
            yield sprintf('Target of value %d for reflector %s', $target, get_class($reflector)) => [
78
                new AnnotationTarget($target),
79
                ScopeMother::withSubject($reflector),
80
            ];
81
82
            yield sprintf('Target of value %d for reflector %s', AnnotationTarget::TARGET_ALL, get_class($reflector)) => [
83
                new AnnotationTarget(AnnotationTarget::TARGET_ALL),
84
                ScopeMother::withSubject($reflector),
85
            ];
86
        }
87
    }
88
89
    /**
90
     * @dataProvider targetsWithNotMatchingScopeProvider
91
     */
92
    public function testValidatesTargetForNotMatchingScopeAndThrows(AnnotationTarget $target, Scope $scope) : void
93
    {
94
        $metadata = AnnotationMetadataMother::withTarget($target);
95
96
        $this->expectException(InvalidTarget::class);
97
98
        $this->validator->validate($metadata, $scope);
99
    }
100
101
    /**
102
     * @return mixed[]
103
     */
104
    public function targetsWithNotMatchingScopeProvider() : iterable
105
    {
106
        foreach ([AnnotationTarget::TARGET_CLASS, AnnotationTarget::TARGET_METHOD, AnnotationTarget::TARGET_PROPERTY] as $target) {
107
            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...
108
                new AnnotationTarget($target),
109
                ScopeMother::withNestingLevel(2),
110
            ];
111
        }
112
113
        $notMatchingReflectors = [
114
            AnnotationTarget::TARGET_CLASS => new ReflectionMethod(self::class, 'setUp'),
115
            AnnotationTarget::TARGET_METHOD => new ReflectionProperty(AnnotationMetadata::class, 'name'),
116
            AnnotationTarget::TARGET_PROPERTY => new ReflectionClass(self::class),
117
        ];
118
119
        foreach ($notMatchingReflectors as $target => $reflector) {
120
            yield sprintf('Target of value %d for reflector %s', $target, get_class($reflector)) => [
121
                new AnnotationTarget($target),
122
                ScopeMother::withSubject($reflector),
123
            ];
124
        }
125
    }
126
}
127