1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ProxyManagerTest\Autoloader; |
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use ProxyManager\Autoloader\Autoloader; |
10
|
|
|
use ProxyManager\FileLocator\FileLocatorInterface; |
11
|
|
|
use ProxyManager\Generator\Util\UniqueIdentifierGenerator; |
12
|
|
|
use ProxyManager\Inflector\ClassNameInflectorInterface; |
13
|
|
|
use function class_exists; |
14
|
|
|
use function file_put_contents; |
15
|
|
|
use function spl_autoload_register; |
16
|
|
|
use function spl_autoload_unregister; |
17
|
|
|
use function sprintf; |
18
|
|
|
use function sys_get_temp_dir; |
19
|
|
|
use function uniqid; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Tests for {@see \ProxyManager\Autoloader\Autoloader} |
23
|
|
|
* |
24
|
|
|
* @covers \ProxyManager\Autoloader\Autoloader |
25
|
|
|
* @group Coverage |
26
|
|
|
*/ |
27
|
|
|
final class AutoloaderTest extends TestCase |
28
|
|
|
{ |
29
|
|
|
private Autoloader $autoloader; |
|
|
|
|
30
|
|
|
|
31
|
|
|
/** @var FileLocatorInterface&MockObject */ |
32
|
|
|
private FileLocatorInterface $fileLocator; |
33
|
|
|
|
34
|
|
|
/** @var ClassNameInflectorInterface&MockObject */ |
35
|
|
|
private ClassNameInflectorInterface $classNameInflector; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @covers \ProxyManager\Autoloader\Autoloader::__construct |
39
|
|
|
*/ |
40
|
|
|
protected function setUp() : void |
41
|
|
|
{ |
42
|
|
|
$this->fileLocator = $this->createMock(FileLocatorInterface::class); |
43
|
|
|
$this->classNameInflector = $this->createMock(ClassNameInflectorInterface::class); |
44
|
|
|
$this->autoloader = new Autoloader($this->fileLocator, $this->classNameInflector); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @covers \ProxyManager\Autoloader\Autoloader::__invoke |
49
|
|
|
*/ |
50
|
|
|
public function testWillNotAutoloadUserClasses() : void |
51
|
|
|
{ |
52
|
|
|
/** @var class-string $className */ |
53
|
|
|
$className = 'Foo\\' . UniqueIdentifierGenerator::getIdentifier('Bar'); |
54
|
|
|
$this |
55
|
|
|
->classNameInflector |
56
|
|
|
->expects(self::once()) |
57
|
|
|
->method('isProxyClassName') |
58
|
|
|
->with($className) |
59
|
|
|
->willReturn(false); |
60
|
|
|
|
61
|
|
|
self::assertFalse($this->autoloadWithoutFurtherAutoloaders($className)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @covers \ProxyManager\Autoloader\Autoloader::__invoke |
66
|
|
|
*/ |
67
|
|
|
public function testWillNotAutoloadNonExistingClass() : void |
68
|
|
|
{ |
69
|
|
|
/** @var class-string $className */ |
70
|
|
|
$className = 'Foo\\' . UniqueIdentifierGenerator::getIdentifier('Bar'); |
71
|
|
|
$this |
72
|
|
|
->classNameInflector |
73
|
|
|
->expects(self::once()) |
74
|
|
|
->method('isProxyClassName') |
75
|
|
|
->with($className) |
76
|
|
|
->willReturn(true); |
77
|
|
|
$this |
78
|
|
|
->fileLocator |
79
|
|
|
->expects(self::once()) |
80
|
|
|
->method('getProxyFileName') |
81
|
|
|
->willReturn(__DIR__ . '/non-existing'); |
82
|
|
|
|
83
|
|
|
self::assertFalse($this->autoloadWithoutFurtherAutoloaders($className)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @covers \ProxyManager\Autoloader\Autoloader::__invoke |
88
|
|
|
*/ |
89
|
|
|
public function testWillNotAutoloadExistingClass() : void |
90
|
|
|
{ |
91
|
|
|
self::assertFalse($this->autoloadWithoutFurtherAutoloaders(self::class)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @covers \ProxyManager\Autoloader\Autoloader::__invoke |
96
|
|
|
*/ |
97
|
|
|
public function testWillAutoloadExistingFile() : void |
98
|
|
|
{ |
99
|
|
|
$namespace = 'Foo'; |
100
|
|
|
$className = UniqueIdentifierGenerator::getIdentifier('Bar'); |
101
|
|
|
/** @var class-string $fqcn */ |
102
|
|
|
$fqcn = $namespace . '\\' . $className; |
103
|
|
|
$fileName = sys_get_temp_dir() . '/foo_' . uniqid('file', true) . '.php'; |
104
|
|
|
|
105
|
|
|
file_put_contents($fileName, '<?php namespace ' . $namespace . '; class ' . $className . '{}'); |
106
|
|
|
|
107
|
|
|
$this |
108
|
|
|
->classNameInflector |
109
|
|
|
->expects(self::once()) |
110
|
|
|
->method('isProxyClassName') |
111
|
|
|
->with($fqcn) |
112
|
|
|
->willReturn(true); |
113
|
|
|
$this |
114
|
|
|
->fileLocator |
115
|
|
|
->expects(self::once()) |
116
|
|
|
->method('getProxyFileName') |
117
|
|
|
->willReturn($fileName); |
118
|
|
|
|
119
|
|
|
self::assertTrue($this->autoloadWithoutFurtherAutoloaders($fqcn)); |
120
|
|
|
self::assertTrue(class_exists($fqcn, false)); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** @psalm-param class-string $className */ |
124
|
|
|
private function autoloadWithoutFurtherAutoloaders(string $className) : bool |
125
|
|
|
{ |
126
|
|
|
$failingAutoloader = null; |
127
|
|
|
$failingAutoloader = function (string $className) use (& $failingAutoloader) : void { |
128
|
|
|
spl_autoload_unregister($failingAutoloader); |
129
|
|
|
|
130
|
|
|
$this->fail(sprintf('Fallback autoloading was triggered to load "%s"', $className)); |
131
|
|
|
}; |
132
|
|
|
|
133
|
|
|
spl_autoload_register($failingAutoloader); |
134
|
|
|
|
135
|
|
|
$result = $this->autoloader->__invoke($className); |
136
|
|
|
|
137
|
|
|
spl_autoload_unregister($failingAutoloader); |
138
|
|
|
|
139
|
|
|
return $result; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|