testClassAnnotationOptimizedParsing()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 2
b 0
f 0
nc 1
nop 2
dl 0
loc 13
rs 10
1
<?php
2
3
namespace Doctrine\Tests\Common\Reflection;
4
5
use Doctrine\Common\Reflection\Psr0FindFile;
6
use Doctrine\Common\Reflection\StaticReflectionParser;
7
use Doctrine\Tests\Common\Reflection\Dummies\NoParent as NoParentDummy;
8
use Doctrine\Tests\DoctrineTestCase;
9
use ReflectionException;
10
use function strlen;
11
use function substr;
12
13
class StaticReflectionParserTest extends DoctrineTestCase
14
{
15
    /**
16
     * @param bool   $classAnnotationOptimize
17
     * @param string $parsedClassName
18
     * @param string $expectedClassName
19
     *
20
     * @return void
21
     *
22
     * @dataProvider parentClassData
23
     */
24
    public function testParentClass($classAnnotationOptimize, $parsedClassName, $expectedClassName)
25
    {
26
        // If classed annotation optimization is enabled the properties tested
27
        // below cannot be found.
28
        if ($classAnnotationOptimize) {
29
            $this->expectException(ReflectionException::class);
30
        }
31
32
        $testsRoot              = substr(__DIR__, 0, -strlen(__NAMESPACE__) - 1);
33
        $paths                  = [
34
            'Doctrine\\Tests' => [$testsRoot],
35
        ];
36
        $staticReflectionParser = new StaticReflectionParser($parsedClassName, new Psr0FindFile($paths), $classAnnotationOptimize);
37
        $declaringClassName     = $staticReflectionParser->getStaticReflectionParserForDeclaringClass('property', 'test')->getClassName();
38
        self::assertSame($expectedClassName, $declaringClassName);
39
    }
40
41
    /**
42
     * @return mixed[]
43
     */
44
    public function parentClassData()
45
    {
46
        $data                 = [];
47
        $noParentClassName    = NoParent::class;
48
        $dummyParentClassName = NoParentDummy::class;
49
50
        foreach ([false, true] as $classAnnotationOptimize) {
51
            $data[] = [
52
                $classAnnotationOptimize,
53
                $noParentClassName,
54
                $noParentClassName,
55
            ];
56
57
            $data[] = [
58
                $classAnnotationOptimize,
59
                FullyClassifiedParent::class,
60
                $noParentClassName,
61
            ];
62
63
            $data[] = [
64
                $classAnnotationOptimize,
65
                SameNamespaceParent::class,
66
                $noParentClassName,
67
            ];
68
69
            $data[] = [
70
                $classAnnotationOptimize,
71
                DeeperNamespaceParent::class,
72
                $dummyParentClassName,
73
            ];
74
75
            $data[] = [
76
                $classAnnotationOptimize,
77
                UseParent::class,
78
                $dummyParentClassName,
79
            ];
80
        }
81
82
        return $data;
83
    }
84
85
    /**
86
     * @dataProvider classAnnotationOptimize
87
     */
88
    public function testClassAnnotationOptimizedParsing($class, $classAnnotationOptimize)
89
    {
90
        $testsRoot              = substr(__DIR__, 0, -strlen(__NAMESPACE__) - 1);
91
        $paths                  = [
92
            'Doctrine\\Tests' => [$testsRoot],
93
        ];
94
        $staticReflectionParser = new StaticReflectionParser($class, new Psr0FindFile($paths), $classAnnotationOptimize);
95
        $expectedDocComment     = '/**
96
 * @Annotation(
97
 *   key = "value"
98
 * )
99
 */';
100
        self::assertSame($expectedDocComment, $staticReflectionParser->getDocComment('class'));
101
    }
102
103
    /**
104
     * @return array<array<bool|string>>
105
     */
106
    public function classAnnotationOptimize()
107
    {
108
        return [
109
            [ExampleAnnotationClass::class, false],
110
            [ExampleAnnotationClass::class, true],
111
            [AnnotationClassWithScopeResolution::class, false],
112
            [AnnotationClassWithScopeResolution::class, true],
113
            [AnnotationClassWithAnonymousClass::class, false],
114
            [AnnotationClassWithAnonymousClass::class, true],
115
        ];
116
    }
117
}
118