|
1
|
|
|
<?php // phpcs:ignore WordPress.Files.FileName |
|
2
|
|
|
/** |
|
3
|
|
|
* Class loader test suite. |
|
4
|
|
|
* |
|
5
|
|
|
* @package automattic/jetpack-autoloader |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
use \Jetpack\AutoloaderTestData\Plugin\Test; |
|
|
|
|
|
|
10
|
|
|
use \Jetpack\AutoloaderTestData\Plugin\Psr4\Test as Psr4Test; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Test suite class for the Autoloader part that handles file loading. |
|
14
|
|
|
*/ |
|
15
|
|
|
class WP_Test_Version_Loader extends TestCase { |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Tests that `find_class_file` returns null when the given class is not known. |
|
19
|
|
|
*/ |
|
20
|
|
|
public function test_find_class_file_returns_null_for_unknown_class() { |
|
21
|
|
|
$version_loader = new Version_Loader( new Version_Selector(), null, null, null ); |
|
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
$file_path = $version_loader->find_class_file( Test::class ); |
|
24
|
|
|
|
|
25
|
|
|
$this->assertNull( $file_path ); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Tests that `find_class_file` returns the path to the class when present in the classmap. |
|
30
|
|
|
*/ |
|
31
|
|
View Code Duplication |
public function test_find_class_file_returns_path_for_classmap() { |
|
32
|
|
|
$version_loader = new Version_Loader( |
|
33
|
|
|
new Version_Selector(), |
|
34
|
|
|
array( |
|
35
|
|
|
Test::class => array( |
|
36
|
|
|
'version' => '1.0.0.0', |
|
37
|
|
|
'path' => TEST_DATA_PATH . '/plugins/plugin_current/includes/class-test.php', |
|
38
|
|
|
), |
|
39
|
|
|
), |
|
40
|
|
|
null, |
|
|
|
|
|
|
41
|
|
|
null |
|
|
|
|
|
|
42
|
|
|
); |
|
43
|
|
|
|
|
44
|
|
|
$file_path = $version_loader->find_class_file( Test::class ); |
|
45
|
|
|
|
|
46
|
|
|
$this->assertEquals( TEST_DATA_PATH . '/plugins/plugin_current/includes/class-test.php', $file_path ); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Test that `find_class_file` returns the path to the class when present in the PSR-4 map. |
|
51
|
|
|
*/ |
|
52
|
|
View Code Duplication |
public function test_find_class_file_returns_path_for_psr4() { |
|
53
|
|
|
$version_loader = new Version_Loader( |
|
54
|
|
|
new Version_Selector(), |
|
55
|
|
|
null, |
|
|
|
|
|
|
56
|
|
|
array( |
|
57
|
|
|
'Jetpack\\AutoloaderTestData\\Plugin\\' => array( |
|
58
|
|
|
'version' => '1.0.0.0', |
|
59
|
|
|
'path' => array( TEST_DATA_PATH . '/plugins/plugin_current/src' ), |
|
60
|
|
|
), |
|
61
|
|
|
), |
|
62
|
|
|
null |
|
|
|
|
|
|
63
|
|
|
); |
|
64
|
|
|
|
|
65
|
|
|
$file_path = $version_loader->find_class_file( Psr4Test::class ); |
|
66
|
|
|
|
|
67
|
|
|
$this->assertEquals( TEST_DATA_PATH . '/plugins/plugin_current/src/Psr4/Test.php', $file_path ); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Tests that `find_class_file` returns the path to the class when presented |
|
72
|
|
|
* with less-specific namespaces first in the PSR-4 map. |
|
73
|
|
|
*/ |
|
74
|
|
View Code Duplication |
public function test_find_class_file_checks_returns_path_for_psr4_with_less_specific_namespace() { |
|
75
|
|
|
$version_loader = new Version_Loader( |
|
76
|
|
|
new Version_Selector(), |
|
77
|
|
|
null, |
|
|
|
|
|
|
78
|
|
|
array( |
|
79
|
|
|
'Jetpack\\AutoloaderTestData\\' => array( |
|
80
|
|
|
'version' => '1.0.0.0', |
|
81
|
|
|
'path' => array( TEST_DATA_PATH . '/plugins/plugin_current' ), |
|
82
|
|
|
), |
|
83
|
|
|
'Jetpack\\AutoloaderTestData\\Plugin\\' => array( |
|
84
|
|
|
'version' => '1.0.0.0', |
|
85
|
|
|
'path' => array( TEST_DATA_PATH . '/plugins/plugin_current/src' ), |
|
86
|
|
|
), |
|
87
|
|
|
), |
|
88
|
|
|
null |
|
|
|
|
|
|
89
|
|
|
); |
|
90
|
|
|
|
|
91
|
|
|
$file_path = $version_loader->find_class_file( Psr4Test::class ); |
|
92
|
|
|
|
|
93
|
|
|
$this->assertEquals( TEST_DATA_PATH . '/plugins/plugin_current/src/Psr4/Test.php', $file_path ); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Test that `find_class_file` returns the classmap version when newer. |
|
98
|
|
|
*/ |
|
99
|
|
View Code Duplication |
public function test_find_class_file_returns_newer_classmap() { |
|
100
|
|
|
$version_loader = new Version_Loader( |
|
101
|
|
|
new Version_Selector(), |
|
102
|
|
|
array( |
|
103
|
|
|
Psr4Test::class => array( |
|
104
|
|
|
'version' => '2.0.0.0', |
|
105
|
|
|
'path' => TEST_DATA_PATH . '/plugins/plugin_newer/src/Psr4/Test.php', |
|
106
|
|
|
), |
|
107
|
|
|
), |
|
108
|
|
|
array( |
|
109
|
|
|
'Jetpack\\AutoloaderTestData\\Plugin\\' => array( |
|
110
|
|
|
'version' => '1.0.0.0', |
|
111
|
|
|
'path' => array( TEST_DATA_PATH . '/plugins/plugin_current/src' ), |
|
112
|
|
|
), |
|
113
|
|
|
), |
|
114
|
|
|
null |
|
|
|
|
|
|
115
|
|
|
); |
|
116
|
|
|
|
|
117
|
|
|
$file_path = $version_loader->find_class_file( Psr4Test::class ); |
|
118
|
|
|
|
|
119
|
|
|
$this->assertEquals( TEST_DATA_PATH . '/plugins/plugin_newer/src/Psr4/Test.php', $file_path ); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Test that `find_class_file` returns the PSR-4 version when newer. |
|
124
|
|
|
*/ |
|
125
|
|
View Code Duplication |
public function test_find_class_file_returns_newer_psr4() { |
|
126
|
|
|
$version_loader = new Version_Loader( |
|
127
|
|
|
new Version_Selector(), |
|
128
|
|
|
array( |
|
129
|
|
|
Psr4Test::class => array( |
|
130
|
|
|
'version' => '1.0.0.0', |
|
131
|
|
|
'path' => TEST_DATA_PATH . '/plugins/plugin_current/src/Psr4/Test.php', |
|
132
|
|
|
), |
|
133
|
|
|
), |
|
134
|
|
|
array( |
|
135
|
|
|
'Jetpack\\AutoloaderTestData\\Plugin\\' => array( |
|
136
|
|
|
'version' => '2.0.0.0', |
|
137
|
|
|
'path' => array( TEST_DATA_PATH . '/plugins/plugin_newer/src' ), |
|
138
|
|
|
), |
|
139
|
|
|
), |
|
140
|
|
|
null |
|
|
|
|
|
|
141
|
|
|
); |
|
142
|
|
|
|
|
143
|
|
|
$file_path = $version_loader->find_class_file( Psr4Test::class ); |
|
144
|
|
|
|
|
145
|
|
|
$this->assertEquals( TEST_DATA_PATH . '/plugins/plugin_newer/src/Psr4/Test.php', $file_path ); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Tests that `load_filemap` correctly loads all of the files. |
|
150
|
|
|
* |
|
151
|
|
|
* @preserveGlobalState disabled |
|
152
|
|
|
* @runInSeparateProcess |
|
153
|
|
|
*/ |
|
154
|
|
|
public function test_loads_filemap() { |
|
155
|
|
|
$version_loader = new Version_Loader( |
|
156
|
|
|
new Version_Selector(), |
|
157
|
|
|
null, |
|
|
|
|
|
|
158
|
|
|
null, |
|
|
|
|
|
|
159
|
|
|
array( |
|
160
|
|
|
'123456acbdefg' => array( |
|
161
|
|
|
'version' => '1.0.0.0', |
|
162
|
|
|
'path' => TEST_DATA_PATH . '/plugins/plugin_current/includes/functions.php', |
|
163
|
|
|
), |
|
164
|
|
|
) |
|
165
|
|
|
); |
|
166
|
|
|
|
|
167
|
|
|
$version_loader->load_filemap(); |
|
168
|
|
|
|
|
169
|
|
|
$this->assertTrue( $GLOBALS['__composer_autoload_files']['123456acbdefg'] ); |
|
170
|
|
|
$this->assertTrue( function_exists( '\\Jetpack\\AutoloaderTestData\\PluginCurrent\\if_i_exist_then_this_test_passed' ) ); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Tests that `load_filemap` does not load files that have already been loaded. |
|
175
|
|
|
*/ |
|
176
|
|
|
public function test_loads_filemap_skips_existing_files() { |
|
177
|
|
|
$version_loader = new Version_Loader( |
|
178
|
|
|
new Version_Selector(), |
|
179
|
|
|
null, |
|
|
|
|
|
|
180
|
|
|
null, |
|
|
|
|
|
|
181
|
|
|
array( |
|
182
|
|
|
'123456acbdefg' => array( |
|
183
|
|
|
'version' => '1.0.0.0', |
|
184
|
|
|
'path' => TEST_DATA_PATH . '/plugins/plugin_current/includes/functions.php', |
|
185
|
|
|
), |
|
186
|
|
|
) |
|
187
|
|
|
); |
|
188
|
|
|
|
|
189
|
|
|
// Pretend it was already loaded! |
|
190
|
|
|
$GLOBALS['__composer_autoload_files']['123456acbdefg'] = true; |
|
191
|
|
|
|
|
192
|
|
|
$version_loader->load_filemap(); |
|
193
|
|
|
|
|
194
|
|
|
$this->assertTrue( $GLOBALS['__composer_autoload_files']['123456acbdefg'] ); |
|
195
|
|
|
$this->assertFalse( function_exists( '\\Jetpack\\AutoloaderTestData\\PluginCurrent\\if_i_exist_then_this_test_passed' ) ); |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: