|
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\Assembler\AnnotationMetadataAssembler; |
|
10
|
|
|
use Doctrine\Annotations\Metadata\MetadataCollection; |
|
11
|
|
|
use Doctrine\Annotations\Metadata\MetadataCollector; |
|
12
|
|
|
use Doctrine\Annotations\Parser\Ast\Annotation; |
|
13
|
|
|
use Doctrine\Annotations\Parser\Ast\Annotations; |
|
14
|
|
|
use Doctrine\Annotations\Parser\Ast\Parameter\UnnamedParameter; |
|
15
|
|
|
use Doctrine\Annotations\Parser\Ast\Parameters; |
|
16
|
|
|
use Doctrine\Annotations\Parser\Ast\Reference; |
|
17
|
|
|
use Doctrine\Annotations\Parser\Scope; |
|
18
|
|
|
use Doctrine\Tests\Annotations\Assembler\Acceptor\AlwaysAcceptingAcceptor; |
|
19
|
|
|
use Doctrine\Tests\Annotations\Parser\Reference\IdentifierPassingReferenceResolver; |
|
20
|
|
|
use Doctrine\Tests\Annotations\Parser\ScopeMother; |
|
21
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
22
|
|
|
use PHPUnit\Framework\TestCase; |
|
23
|
|
|
|
|
24
|
|
|
final class MetadataCollectorTest extends TestCase |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var AnnotationMetadataAssembler|MockObject */ |
|
27
|
|
|
private $assembler; |
|
28
|
|
|
|
|
29
|
|
|
/** @var MetadataCollector */ |
|
30
|
|
|
private $collector; |
|
31
|
|
|
|
|
32
|
|
|
protected function setUp() : void |
|
33
|
|
|
{ |
|
34
|
|
|
$this->assembler = $this->createMock(AnnotationMetadataAssembler::class); |
|
35
|
|
|
$this->collector = new MetadataCollector( |
|
36
|
|
|
$this->assembler, |
|
37
|
|
|
new AlwaysAcceptingAcceptor(), |
|
38
|
|
|
new IdentifierPassingReferenceResolver() |
|
39
|
|
|
); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param callable(AnnotationMetadataAssembler) : void $initializer |
|
44
|
|
|
* @param callable(AnnotationMetadata[]) : void $asserter |
|
45
|
|
|
* |
|
46
|
|
|
* @dataProvider docBlocksProvider() |
|
47
|
|
|
*/ |
|
48
|
|
|
public function testMetadata(Annotations $annotations, callable $initializer, callable $asserter) : void |
|
49
|
|
|
{ |
|
50
|
|
|
$initializer($this->assembler); |
|
51
|
|
|
|
|
52
|
|
|
$collection = new MetadataCollection(); |
|
53
|
|
|
|
|
54
|
|
|
$this->collector->collect($annotations, ScopeMother::example(), $collection); |
|
55
|
|
|
|
|
56
|
|
|
$asserter($collection); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return string[][]|AnnotationMetadata[][][] |
|
61
|
|
|
*/ |
|
62
|
|
|
public function docBlocksProvider() : iterable |
|
63
|
|
|
{ |
|
64
|
|
|
yield 'single without parameters' => [ |
|
|
|
|
|
|
65
|
|
|
new Annotations( |
|
66
|
|
|
new Annotation( |
|
67
|
|
|
new Reference('Foo', true), |
|
68
|
|
|
new Parameters() |
|
69
|
|
|
) |
|
70
|
|
|
), |
|
71
|
|
|
static function (AnnotationMetadataAssembler $assembler) : void { |
|
72
|
|
|
/** @var AnnotationMetadataAssembler|MockObject $assembler */ |
|
73
|
|
|
$assembler->method('assemble') |
|
|
|
|
|
|
74
|
|
|
->with( |
|
75
|
|
|
self::callback(static function (Reference $reference) : bool { |
|
76
|
|
|
return $reference->getIdentifier() === 'Foo' && $reference->isFullyQualified() === true; |
|
77
|
|
|
}), |
|
78
|
|
|
self::isInstanceOf(Scope::class) |
|
79
|
|
|
) |
|
80
|
|
|
->willReturn(new AnnotationMetadata( |
|
81
|
|
|
'Foo', |
|
82
|
|
|
new AnnotationTarget(AnnotationTarget::TARGET_ALL), |
|
83
|
|
|
false, |
|
84
|
|
|
[] |
|
85
|
|
|
)); |
|
86
|
|
|
}, |
|
87
|
|
|
static function (MetadataCollection $collection) : void { |
|
88
|
|
|
self::assertCount(1, $collection); |
|
89
|
|
|
self::assertSame('Foo', $collection['Foo']->getName()); |
|
90
|
|
|
}, |
|
91
|
|
|
]; |
|
92
|
|
|
yield 'nested' => [ |
|
93
|
|
|
new Annotations( |
|
94
|
|
|
new Annotation( |
|
95
|
|
|
new Reference('Foo', true), |
|
96
|
|
|
new Parameters( |
|
97
|
|
|
new UnnamedParameter( |
|
98
|
|
|
new Annotation( |
|
99
|
|
|
new Reference('Bar', false), |
|
100
|
|
|
new Parameters() |
|
101
|
|
|
) |
|
102
|
|
|
) |
|
103
|
|
|
) |
|
104
|
|
|
) |
|
105
|
|
|
), |
|
106
|
|
|
static function (AnnotationMetadataAssembler $assembler) : void { |
|
107
|
|
|
/** @var AnnotationMetadataAssembler|MockObject $assembler */ |
|
108
|
|
|
$assembler->method('assemble') |
|
109
|
|
|
->withConsecutive( |
|
110
|
|
|
[ |
|
111
|
|
|
self::callback(static function (Reference $reference) : bool { |
|
112
|
|
|
return $reference->getIdentifier() === 'Bar' && $reference->isFullyQualified() === false; |
|
113
|
|
|
}), |
|
114
|
|
|
self::isInstanceOf(Scope::class), |
|
115
|
|
|
], |
|
116
|
|
|
[ |
|
117
|
|
|
self::callback(static function (Reference $reference) : bool { |
|
118
|
|
|
return $reference->getIdentifier() === 'Foo' && $reference->isFullyQualified() === true; |
|
119
|
|
|
}), |
|
120
|
|
|
self::isInstanceOf(Scope::class), |
|
121
|
|
|
] |
|
122
|
|
|
) |
|
123
|
|
|
->willReturnOnConsecutiveCalls( |
|
124
|
|
|
new AnnotationMetadata( |
|
125
|
|
|
'Bar', |
|
126
|
|
|
new AnnotationTarget(AnnotationTarget::TARGET_ALL), |
|
127
|
|
|
false, |
|
128
|
|
|
[] |
|
129
|
|
|
), |
|
130
|
|
|
new AnnotationMetadata( |
|
131
|
|
|
'Foo', |
|
132
|
|
|
new AnnotationTarget(AnnotationTarget::TARGET_ALL), |
|
133
|
|
|
false, |
|
134
|
|
|
[] |
|
135
|
|
|
) |
|
136
|
|
|
); |
|
137
|
|
|
}, |
|
138
|
|
|
static function (MetadataCollection $collection) : void { |
|
139
|
|
|
self::assertCount(2, $collection); |
|
140
|
|
|
self::assertSame('Bar', $collection['Bar']->getName()); |
|
141
|
|
|
self::assertSame('Foo', $collection['Foo']->getName()); |
|
142
|
|
|
}, |
|
143
|
|
|
]; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|