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
|
|
|
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
|
|
|
/** |
43
|
|
|
* @return mixed[] |
44
|
|
|
*/ |
45
|
|
|
public function validExamples() : iterable |
46
|
|
|
{ |
47
|
|
|
/** @var Reflector[] $reflectors */ |
48
|
|
|
$reflectors = [ |
49
|
|
|
new ReflectionClass(self::class), |
50
|
|
|
new ReflectionProperty(self::class, 'validator'), |
51
|
|
|
new ReflectionMethod(self::class, 'setUp'), |
52
|
|
|
]; |
53
|
|
|
|
54
|
|
|
foreach ($reflectors as $reflector) { |
55
|
|
|
yield 'TargetAll for reflector ' . get_class($reflector) => [ |
|
|
|
|
56
|
|
|
AnnotationMetadataMother::withTarget(new AnnotationTarget(AnnotationTarget::TARGET_ALL)), |
57
|
|
|
ScopeMother::withSubject($reflector), |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
foreach ([AnnotationTarget::TARGET_ALL, AnnotationTarget::TARGET_ANNOTATION] as $target) { |
62
|
|
|
yield sprintf('Target of value %d for nested scope', $target) => [ |
63
|
|
|
AnnotationMetadataMother::withTarget(new AnnotationTarget($target)), |
64
|
|
|
ScopeMother::withNestingLevel(2), |
65
|
|
|
]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$matchingReflectors = [ |
69
|
|
|
AnnotationTarget::TARGET_CLASS => $reflectors[0], |
70
|
|
|
AnnotationTarget::TARGET_PROPERTY => $reflectors[1], |
71
|
|
|
AnnotationTarget::TARGET_METHOD => $reflectors[2], |
72
|
|
|
]; |
73
|
|
|
|
74
|
|
|
foreach ($matchingReflectors as $target => $reflector) { |
75
|
|
|
yield sprintf('Target of value %d for reflector %s', $target, get_class($reflector)) => [ |
76
|
|
|
AnnotationMetadataMother::withTarget(new AnnotationTarget($target)), |
77
|
|
|
ScopeMother::withSubject($reflector), |
78
|
|
|
]; |
79
|
|
|
|
80
|
|
|
yield sprintf('Target of value %d for reflector %s', AnnotationTarget::TARGET_ALL, get_class($reflector)) => [ |
81
|
|
|
AnnotationMetadataMother::withTarget(new AnnotationTarget(AnnotationTarget::TARGET_ALL)), |
82
|
|
|
ScopeMother::withSubject($reflector), |
83
|
|
|
]; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @dataProvider invalidExamples |
89
|
|
|
*/ |
90
|
|
|
public function testValidatesInvalidExamplesAndThrows(AnnotationMetadata $metadata, Scope $scope) : void |
91
|
|
|
{ |
92
|
|
|
$this->expectException(InvalidTarget::class); |
93
|
|
|
|
94
|
|
|
$this->validator->validate($metadata, $scope); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return mixed[] |
99
|
|
|
*/ |
100
|
|
|
public function invalidExamples() : iterable |
101
|
|
|
{ |
102
|
|
|
foreach ([AnnotationTarget::TARGET_CLASS, AnnotationTarget::TARGET_METHOD, AnnotationTarget::TARGET_PROPERTY] as $target) { |
103
|
|
|
yield sprintf('Target of value %d for nested scope', $target) => [ |
|
|
|
|
104
|
|
|
AnnotationMetadataMother::withTarget(new AnnotationTarget($target)), |
105
|
|
|
ScopeMother::withNestingLevel(2), |
106
|
|
|
]; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$notMatchingReflectors = [ |
110
|
|
|
AnnotationTarget::TARGET_CLASS => new ReflectionMethod(self::class, 'setUp'), |
111
|
|
|
AnnotationTarget::TARGET_METHOD => new ReflectionProperty(self::class, 'validator'), |
112
|
|
|
AnnotationTarget::TARGET_PROPERTY => new ReflectionClass(self::class), |
113
|
|
|
]; |
114
|
|
|
|
115
|
|
|
foreach ($notMatchingReflectors as $target => $reflector) { |
116
|
|
|
yield sprintf('Target of value %d for reflector %s', $target, get_class($reflector)) => [ |
117
|
|
|
AnnotationMetadataMother::withTarget(new AnnotationTarget($target)), |
118
|
|
|
ScopeMother::withSubject($reflector), |
119
|
|
|
]; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|