File: AnnotationRegistryTest.php

package info (click to toggle)
php-doctrine-annotations 1.11.2-1%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,028 kB
  • sloc: php: 8,480; makefile: 19
file content (231 lines) | stat: -rw-r--r-- 6,925 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php

namespace Doctrine\Tests\Common\Annotations;

use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\Tests\Common\Annotations\Fixtures\Annotation\CanBeAutoLoaded;
use Doctrine\Tests\Common\Annotations\Fixtures\Annotation\LoadedUsingRegisterFile;
use Doctrine\Tests\Common\Annotations\Fixtures\Annotation\ShouldNeverBeLoaded;
use PHPUnit\Framework\TestCase;
use ReflectionProperty;
use TypeError;

use function random_int;

class AnnotationRegistryTest extends TestCase
{
    /** @var string */
    protected $class = AnnotationRegistry::class;

    /**
     * @runInSeparateProcess
     */
    public function testReset(): void
    {
        $data = ['foo' => 'bar'];

        $this->setStaticField($this->class, 'autoloadNamespaces', $data);
        $this->setStaticField($this->class, 'loaders', $data);

        self::assertSame($data, $this->getStaticField($this->class, 'autoloadNamespaces'));
        self::assertSame($data, $this->getStaticField($this->class, 'loaders'));

        AnnotationRegistry::reset();

        self::assertEmpty($this->getStaticField($this->class, 'autoloadNamespaces'));
        self::assertEmpty($this->getStaticField($this->class, 'loaders'));
    }

    /**
     * @runInSeparateProcess
     */
    public function testRegisterAutoloadNamespaces(): void
    {
        $this->setStaticField($this->class, 'autoloadNamespaces', ['foo' => 'bar']);

        AnnotationRegistry::registerAutoloadNamespaces(['test' => 'bar']);
        self::assertSame(['foo' => 'bar', 'test' => 'bar'], $this->getStaticField($this->class, 'autoloadNamespaces'));
    }

    /**
     * @runInSeparateProcess
     */
    public function testRegisterLoaderNoCallable(): void
    {
        $this->expectException(TypeError::class);

        AnnotationRegistry::registerLoader('test' . random_int(10, 10000));
    }

    /**
     * @param mixed[] $value
     */
    protected function setStaticField(string $class, string $field, array $value): void
    {
        $reflection = new ReflectionProperty($class, $field);

        $reflection->setAccessible(true);
        $reflection->setValue(null, $value);
    }

    /**
     * @return mixed
     */
    protected function getStaticField(string $class, string $field)
    {
        $reflection = new ReflectionProperty($class, $field);

        $reflection->setAccessible(true);

        return $reflection->getValue();
    }

    /**
     * @runInSeparateProcess
     */
    public function testStopCallingLoadersIfClassIsNotFound(): void
    {
        AnnotationRegistry::reset();
        $i          = 0;
        $autoLoader = static function () use (&$i): bool {
            $i += 1;

            return false;
        };
        AnnotationRegistry::registerLoader($autoLoader);
        AnnotationRegistry::loadAnnotationClass('unloadableClass');
        AnnotationRegistry::loadAnnotationClass('unloadableClass');
        AnnotationRegistry::loadAnnotationClass('unloadableClass');
        self::assertSame(1, $i, 'Autoloader should only be called once');
    }

    /**
     * @runInSeparateProcess
     */
    public function testStopCallingLoadersAfterClassIsFound(): void
    {
        $className = 'autoloadedClass' . random_int(10, 100000);
        AnnotationRegistry::reset();
        $i          = 0;
        $autoLoader = static function () use (&$i, $className): bool {
            eval('class ' . $className . ' {}');
            $i += 1;

            return true;
        };
        AnnotationRegistry::registerLoader($autoLoader);
        AnnotationRegistry::loadAnnotationClass($className);
        AnnotationRegistry::loadAnnotationClass($className);
        AnnotationRegistry::loadAnnotationClass($className);
        self::assertSame(1, $i, 'Autoloader should only be called once');
    }

    /**
     * @runInSeparateProcess
     */
    public function testAddingANewLoaderClearsTheCache(): void
    {
        $failures      = 0;
        $failingLoader = static function () use (&$failures): bool {
            $failures += 1;

            return false;
        };

        AnnotationRegistry::reset();
        AnnotationRegistry::registerLoader($failingLoader);

        self::assertSame(0, $failures);

        AnnotationRegistry::loadAnnotationClass('unloadableClass');

        self::assertSame(1, $failures);

        AnnotationRegistry::loadAnnotationClass('unloadableClass');

        self::assertSame(1, $failures);

        AnnotationRegistry::registerLoader(static function (): bool {
            return false;
        });
        AnnotationRegistry::loadAnnotationClass('unloadableClass');

        self::assertSame(2, $failures);
    }

    /**
     * @runInSeparateProcess
     */
    public function testResetClearsRegisteredAutoloaderFailures(): void
    {
        $failures      = 0;
        $failingLoader = static function () use (&$failures): bool {
            $failures += 1;

            return false;
        };

        AnnotationRegistry::reset();
        AnnotationRegistry::registerLoader($failingLoader);

        self::assertSame(0, $failures);

        AnnotationRegistry::loadAnnotationClass('unloadableClass');

        self::assertSame(1, $failures);

        AnnotationRegistry::loadAnnotationClass('unloadableClass');

        self::assertSame(1, $failures);

        AnnotationRegistry::reset();
        AnnotationRegistry::registerLoader($failingLoader);
        AnnotationRegistry::loadAnnotationClass('unloadableClass');

        self::assertSame(2, $failures);
    }

    /**
     * @runInSeparateProcess
     */
    public function testRegisterLoaderIfNotExistsOnlyRegisteresSameLoaderOnce(): void
    {
        $className = 'autoloadedClassThatDoesNotExist';
        AnnotationRegistry::reset();
        $autoLoader = self::createPartialMock(Autoloader::class, ['__invoke']);
        $autoLoader->expects($this->once())->method('__invoke');
        AnnotationRegistry::registerUniqueLoader($autoLoader);
        AnnotationRegistry::registerUniqueLoader($autoLoader);
        AnnotationRegistry::loadAnnotationClass($className);
        AnnotationRegistry::loadAnnotationClass($className);
    }

    /**
     * @runInSeparateProcess
     */
    public function testClassExistsFallback(): void
    {
        AnnotationRegistry::reset();

        self::assertTrue(AnnotationRegistry::loadAnnotationClass(CanBeAutoLoaded::class));
    }

    /**
     * @runInSeparateProcess
     */
    public function testClassExistsFallbackNotUsedWhenRegisterFileUsed(): void
    {
        AnnotationRegistry::reset();
        AnnotationRegistry::registerFile(__DIR__ . '/Fixtures/Annotation/LoadedUsingRegisterFile.php');

        self::assertTrue(AnnotationRegistry::loadAnnotationClass(LoadedUsingRegisterFile::class));
        self::assertFalse(AnnotationRegistry::loadAnnotationClass(ShouldNeverBeLoaded::class));
    }
}

class Autoloader
{
    public function __invoke(): void
    {
    }
}