Failed Conditions
Pull Request — new-parser-ast-metadata (#2)
by
unknown
01:43
created

TargetValidatorTest::invalidExamples()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 19
rs 9.8666
c 0
b 0
f 0
cc 3
nc 4
nop 0
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\Annotation\Parser\ScopeMother;
13
use Doctrine\Tests\Annotations\Metadata\AnnotationMetadataMother;
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
class TargetValidatorTest extends TestCase
23
{
24
    /** @var TargetValidator */
25
    private $validator;
26
27
    public function setUp() : void
28
    {
29
        $this->validator = new TargetValidator();
30
    }
31
32
    /**
33
     * @dataProvider validExamples
34
     */
35
    public function testValidatesValidExamples(AnnotationMetadata $metadata, Scope $scope) : void
36
    {
37
        $this->validator->validate($metadata, $scope);
38
39
        $this->assertTrue(true);
40
    }
41
42
    public function validExamples() : iterable
43
    {
44
        /** @var Reflector[] $reflectors */
45
        $reflectors = [
46
            new ReflectionClass(self::class),
47
            new ReflectionProperty(self::class, 'validator'),
48
            new ReflectionMethod(self::class, 'setUp'),
49
        ];
50
51
        foreach ($reflectors as $reflector) {
52
            yield 'TargetAll for reflector ' . get_class($reflector) => [
53
                AnnotationMetadataMother::withTarget(new AnnotationTarget(AnnotationTarget::TARGET_ALL)),
54
                ScopeMother::withSubject($reflector),
55
            ];
56
        }
57
58
        foreach ([AnnotationTarget::TARGET_ALL, AnnotationTarget::TARGET_ANNOTATION] as $target) {
59
            yield sprintf('Target of value %d for nested scope', $target) => [
60
                AnnotationMetadataMother::withTarget(new AnnotationTarget($target)),
61
                ScopeMother::withNestingLevel(2),
62
            ];
63
        }
64
65
        $matchingReflectors = [
66
            AnnotationTarget::TARGET_CLASS => $reflectors[0],
67
            AnnotationTarget::TARGET_PROPERTY => $reflectors[1],
68
            AnnotationTarget::TARGET_METHOD => $reflectors[2],
69
        ];
70
71
        foreach ($matchingReflectors as $target => $reflector) {
72
            yield sprintf('Target of value %d for reflector %s', $target, get_class($reflector)) => [
73
                AnnotationMetadataMother::withTarget(new AnnotationTarget($target)),
74
                ScopeMother::withSubject($reflector),
75
            ];
76
77
            yield sprintf('Target of value %d for reflector %s', AnnotationTarget::TARGET_ALL, get_class($reflector)) => [
78
                AnnotationMetadataMother::withTarget(new AnnotationTarget(AnnotationTarget::TARGET_ALL)),
79
                ScopeMother::withSubject($reflector),
80
            ];
81
        }
82
    }
83
84
    /**
85
     * @dataProvider invalidExamples
86
     */
87
    public function testValidatesInvalidExamplesAndThrows(AnnotationMetadata $metadata, Scope $scope) : void
88
    {
89
        $this->expectException(InvalidTarget::class);
90
91
        $this->validator->validate($metadata, $scope);
92
    }
93
94
    public function invalidExamples() : 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) => [
98
                AnnotationMetadataMother::withTarget(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(self::class, 'validator'),
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
                AnnotationMetadataMother::withTarget(new AnnotationTarget($target)),
112
                ScopeMother::withSubject($reflector),
113
            ];
114
        }
115
    }
116
}
117