testOmitNotRegisteredAnnotation()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\Annotations;
4
5
use Doctrine\Annotations\AnnotationException;
6
use Doctrine\Annotations\AnnotationReader;
7
use Doctrine\Annotations\DocParser;
8
use Doctrine\Tests\Annotations\Fixtures\Annotation\SingleUseAnnotation;
9
use Doctrine\Tests\Annotations\Fixtures\ClassWithFullPathUseStatement;
10
use Doctrine\Tests\Annotations\Fixtures\IgnoredNamespaces\AnnotatedAtClassLevel;
11
use Doctrine\Tests\Annotations\Fixtures\IgnoredNamespaces\AnnotatedAtMethodLevel;
12
use Doctrine\Tests\Annotations\Fixtures\IgnoredNamespaces\AnnotatedAtPropertyLevel;
13
14
class AnnotationReaderTest extends AbstractReaderTest
15
{
16
    /**
17
     * @param DocParser|null $parser
18
     * @return AnnotationReader
19
     */
20
    protected function getReader(DocParser $parser = null)
21
    {
22
        return new AnnotationReader($parser);
23
    }
24
25
    public function testMethodAnnotationFromTrait()
26
    {
27
        $reader = $this->getReader();
28
        $ref = new \ReflectionClass(Fixtures\ClassUsesTrait::class);
29
30
        $annotations = $reader->getMethodAnnotations($ref->getMethod('someMethod'));
31
        self::assertInstanceOf(Bar\Autoload::class, $annotations[0]);
32
33
        $annotations = $reader->getMethodAnnotations($ref->getMethod('traitMethod'));
34
        self::assertInstanceOf(Fixtures\Annotation\Autoload::class, $annotations[0]);
35
    }
36
37
    public function testMethodAnnotationFromOverwrittenTrait()
38
    {
39
        $reader = $this->getReader();
40
        $ref = new \ReflectionClass(Fixtures\ClassOverwritesTrait::class);
41
42
        $annotations = $reader->getMethodAnnotations($ref->getMethod('traitMethod'));
43
        self::assertInstanceOf(Bar2\Autoload::class, $annotations[0]);
44
    }
45
46
    public function testPropertyAnnotationFromTrait()
47
    {
48
        $reader = $this->getReader();
49
        $ref = new \ReflectionClass(Fixtures\ClassUsesTrait::class);
50
51
        $annotations = $reader->getPropertyAnnotations($ref->getProperty('aProperty'));
52
        self::assertInstanceOf(Bar\Autoload::class, $annotations[0]);
53
54
        $annotations = $reader->getPropertyAnnotations($ref->getProperty('traitProperty'));
55
        self::assertInstanceOf(Fixtures\Annotation\Autoload::class, $annotations[0]);
56
    }
57
58
    public function testOmitNotRegisteredAnnotation()
59
    {
60
        $parser = new DocParser();
61
        $parser->setIgnoreNotImportedAnnotations(true);
62
63
        $reader = $this->getReader($parser);
64
        $ref = new \ReflectionClass(Fixtures\ClassWithNotRegisteredAnnotationUsed::class);
65
66
        $annotations = $reader->getMethodAnnotations($ref->getMethod('methodWithNotRegisteredAnnotation'));
67
        self::assertEquals([], $annotations);
68
    }
69
70
    /**
71
     * @group 45
72
     *
73
     * @runInSeparateProcess
74
     */
75
    public function testClassAnnotationIsIgnored()
76
    {
77
        $reader = $this->getReader();
78
        $ref = new \ReflectionClass(AnnotatedAtClassLevel::class);
79
80
        $reader::addGlobalIgnoredNamespace('SomeClassAnnotationNamespace');
81
82
        self::assertEmpty($reader->getClassAnnotations($ref));
83
    }
84
85
    /**
86
     * @group 45
87
     *
88
     * @runInSeparateProcess
89
     */
90
    public function testMethodAnnotationIsIgnored()
91
    {
92
        $reader = $this->getReader();
93
        $ref = new \ReflectionClass(AnnotatedAtMethodLevel::class);
94
95
        $reader::addGlobalIgnoredNamespace('SomeMethodAnnotationNamespace');
96
97
        self::assertEmpty($reader->getMethodAnnotations($ref->getMethod('test')));
98
    }
99
100
    /**
101
     * @group 45
102
     *
103
     * @runInSeparateProcess
104
     */
105
    public function testPropertyAnnotationIsIgnored()
106
    {
107
        $reader = $this->getReader();
108
        $ref = new \ReflectionClass(AnnotatedAtPropertyLevel::class);
109
110
        $reader::addGlobalIgnoredNamespace('SomePropertyAnnotationNamespace');
111
112
        self::assertEmpty($reader->getPropertyAnnotations($ref->getProperty('property')));
113
    }
114
115
    public function testClassWithFullPathUseStatement()
116
    {
117
        if (class_exists(SingleUseAnnotation::class, false)) {
118
            throw new \LogicException(
119
                'The SingleUseAnnotation must not be used in other tests for this test to be useful.' .
120
                'If the class is already loaded then the code path that finds the class to load is not used and ' .
121
                'this test becomes useless.'
122
            );
123
        }
124
125
        $reader = $this->getReader();
126
        $ref = new \ReflectionClass(ClassWithFullPathUseStatement::class);
127
128
        $annotations = $reader->getClassAnnotations($ref);
129
130
        self::assertInstanceOf(SingleUseAnnotation::class,$annotations[0]);
131
    }
132
}
133