Passed
Pull Request — master (#439)
by Kirill
06:35
created

LocatorTest::getAttributesLocator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Tests\Annotations;
13
14
use Doctrine\Common\Annotations\AnnotationRegistry;
15
use PHPUnit\Framework\TestCase;
16
use Spiral\Annotations\AnnotationLocator;
17
use Spiral\Attributes\AnnotationReader;
18
use Spiral\Attributes\AttributeReader;
19
use Spiral\Tests\Annotations\Fixtures\Annotation\PropertyAnnotation;
20
use Spiral\Tests\Annotations\Fixtures\Annotation\MethodAnnotation;
21
use Spiral\Tests\Annotations\Fixtures\Annotation\ClassAnnotation;
22
use Spiral\Tests\Annotations\Fixtures\AttributeTestClass;
23
use Spiral\Tests\Annotations\Fixtures\TestClass;
24
use Spiral\Tokenizer\ClassLocator;
25
use Symfony\Component\Finder\Finder;
26
27
class LocatorTest extends TestCase
28
{
29
    public function testLocateClasses()
30
    {
31
        // Annotations.
32
        $classes = $this->getAnnotationsLocator(__DIR__ . '/Fixtures')->findClasses(ClassAnnotation::class);
33
        $classes = iterator_to_array($classes);
34
35
        $this->assertCount(1, $classes);
36
37
        foreach ($classes as $class) {
38
            $this->assertSame(TestClass::class, $class->getClass()->getName());
39
            $this->assertSame('abc', $class->getAnnotation()->value);
40
        }
41
42
        // Attributes.
43
        $classes = $this->getAttributesLocator(__DIR__ . '/Fixtures')->findClasses(ClassAnnotation::class);
44
        $classes = iterator_to_array($classes);
45
46
        $this->assertCount(1, $classes);
47
48
        foreach ($classes as $class) {
49
            $this->assertSame(AttributeTestClass::class, $class->getClass()->getName());
50
            $this->assertSame('abc', $class->getAnnotation()->value);
51
        }
52
    }
53
54
    public function testLocateProperties()
55
    {
56
        // Annotations.
57
        $props = $this->getAnnotationsLocator(__DIR__ . '/Fixtures')->findProperties(PropertyAnnotation::class);
58
        $props = iterator_to_array($props);
59
60
        $this->assertCount(1, $props);
61
62
        foreach ($props as $prop) {
63
            $this->assertSame(TestClass::class, $prop->getClass()->getName());
64
            $this->assertSame('name', $prop->getProperty()->getName());
65
            $this->assertSame('123', $prop->getAnnotation()->id);
66
        }
67
68
        // Attributes.
69
        $props = $this->getAttributesLocator(__DIR__ . '/Fixtures')->findProperties(PropertyAnnotation::class);
70
        $props = iterator_to_array($props);
71
72
        $this->assertCount(1, $props);
73
74
        foreach ($props as $prop) {
75
            $this->assertSame(AttributeTestClass::class, $prop->getClass()->getName());
76
            $this->assertSame('name', $prop->getProperty()->getName());
77
            $this->assertSame('123', $prop->getAnnotation()->id);
78
        }
79
    }
80
81
    public function testLocateMethods()
82
    {
83
        // Annotations.
84
        $methods = $this->getAnnotationsLocator(__DIR__ . '/Fixtures')->findMethods(MethodAnnotation::class);
85
        $methods = iterator_to_array($methods);
86
87
        $this->assertCount(1, $methods);
88
89
        foreach ($methods as $m) {
90
            $this->assertSame(TestClass::class, $m->getClass()->getName());
91
            $this->assertSame('testMethod', $m->getMethod()->getName());
92
            $this->assertSame('/', $m->getAnnotation()->path);
93
        }
94
95
        // Attributes.
96
        $methods = $this->getAttributesLocator(__DIR__ . '/Fixtures')->findMethods(MethodAnnotation::class);
97
        $methods = iterator_to_array($methods);
98
99
        $this->assertCount(1, $methods);
100
101
        foreach ($methods as $m) {
102
            $this->assertSame(AttributeTestClass::class, $m->getClass()->getName());
103
            $this->assertSame('testMethod', $m->getMethod()->getName());
104
            $this->assertSame('/', $m->getAnnotation()->path);
105
        }
106
    }
107
108
    private function getAnnotationsLocator(string $directory): AnnotationLocator
109
    {
110
        AnnotationRegistry::registerLoader('class_exists');
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\Common\Annotati...istry::registerLoader() has been deprecated: This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

110
        /** @scrutinizer ignore-deprecated */ AnnotationRegistry::registerLoader('class_exists');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
111
112
        return new AnnotationLocator(
113
            (new ClassLocator((new Finder())->files()->in([$directory]))),
114
            new AnnotationReader()
115
        );
116
    }
117
118
    private function getAttributesLocator(string $directory): AnnotationLocator
119
    {
120
        return new AnnotationLocator(
121
            (new ClassLocator((new Finder())->files()->in([$directory]))),
122
            new AttributeReader()
123
        );
124
    }
125
}
126