1
|
|
|
<?php // phpcs:ignore WordPress.Files.FileName |
2
|
|
|
/** |
3
|
|
|
* Integration test suite for the loader population. |
4
|
|
|
* |
5
|
|
|
* @package automattic/jetpack-autoloader |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
use Jetpack\AutoloaderTestData\Plugin\Psr4\Test as Psr4Test; |
9
|
|
|
use Jetpack\AutoloaderTestData\Plugin\Test as Test; |
|
|
|
|
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Test suite class for verifying that parsed manifests can be put into the loader and used. |
14
|
|
|
*/ |
15
|
|
|
class WP_Test_Integration_Loader extends TestCase { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* A manifest handler configured for a single plugin. |
19
|
|
|
* |
20
|
|
|
* @var Manifest_Handler |
21
|
|
|
*/ |
22
|
|
|
private $single_manifest_handler; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* A manifest handler configured for multiple plugins. |
26
|
|
|
* |
27
|
|
|
* @var Manifest_Handler |
28
|
|
|
*/ |
29
|
|
|
private $multiple_manifest_handler; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Setup runs before each test. |
33
|
|
|
*/ |
34
|
|
|
public function setUp() { |
35
|
|
|
parent::setUp(); |
36
|
|
|
|
37
|
|
|
$this->single_manifest_handler = new Manifest_Handler( |
38
|
|
|
array( |
39
|
|
|
TEST_DATA_PATH . '/plugins/plugin_current', |
40
|
|
|
), |
41
|
|
|
new Version_Selector() |
42
|
|
|
); |
43
|
|
|
$this->multiple_manifest_handler = new Manifest_Handler( |
44
|
|
|
array( |
45
|
|
|
TEST_DATA_PATH . '/plugins/plugin_current', |
46
|
|
|
TEST_DATA_PATH . '/plugins/plugin_newer', |
47
|
|
|
), |
48
|
|
|
new Version_Selector() |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Tests that the classmap manifest from a single plugin can be handled correctly. |
54
|
|
|
*/ |
55
|
|
View Code Duplication |
public function test_single_plugin_classmap() { |
56
|
|
|
$path_map = array(); |
57
|
|
|
$this->single_manifest_handler->register_plugin_manifests( |
58
|
|
|
'vendor/composer/jetpack_autoload_classmap.php', |
59
|
|
|
$path_map |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
$loader = new Version_Loader( |
63
|
|
|
new Version_Selector(), |
64
|
|
|
$path_map, |
65
|
|
|
null, |
|
|
|
|
66
|
|
|
null |
|
|
|
|
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
$file = $loader->find_class_file( Test::class ); |
70
|
|
|
|
71
|
|
|
$this->assertEquals( TEST_DATA_PATH . '/plugins/plugin_current/includes/class-test.php', $file ); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Tests that the classmap manifest from multiple plugins can be handled correctly. |
76
|
|
|
*/ |
77
|
|
View Code Duplication |
public function test_multiple_plugin_classmap() { |
78
|
|
|
$path_map = array(); |
79
|
|
|
$this->multiple_manifest_handler->register_plugin_manifests( |
80
|
|
|
'vendor/composer/jetpack_autoload_classmap.php', |
81
|
|
|
$path_map |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
$loader = new Version_Loader( |
85
|
|
|
new Version_Selector(), |
86
|
|
|
$path_map, |
87
|
|
|
null, |
|
|
|
|
88
|
|
|
null |
|
|
|
|
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
$file = $loader->find_class_file( Test::class ); |
92
|
|
|
|
93
|
|
|
$this->assertEquals( TEST_DATA_PATH . '/plugins/plugin_newer/includes/class-test.php', $file ); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Tests that the PSR-4 manifest from a single plugin can be handled correctly. |
98
|
|
|
*/ |
99
|
|
View Code Duplication |
public function test_single_plugin_psr4() { |
100
|
|
|
$path_map = array(); |
101
|
|
|
$this->single_manifest_handler->register_plugin_manifests( |
102
|
|
|
'vendor/composer/jetpack_autoload_psr4.php', |
103
|
|
|
$path_map |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
$loader = new Version_Loader( |
107
|
|
|
new Version_Selector(), |
108
|
|
|
null, |
|
|
|
|
109
|
|
|
$path_map, |
110
|
|
|
null |
|
|
|
|
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
$file = $loader->find_class_file( Psr4Test::class ); |
114
|
|
|
|
115
|
|
|
$this->assertEquals( TEST_DATA_PATH . '/plugins/plugin_current/src/Psr4/Test.php', $file ); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Tests that the PSR-4 manifest from multiple plugins can be handled correctly. |
120
|
|
|
*/ |
121
|
|
View Code Duplication |
public function test_multiple_plugin_psr4() { |
122
|
|
|
$path_map = array(); |
123
|
|
|
$this->multiple_manifest_handler->register_plugin_manifests( |
124
|
|
|
'vendor/composer/jetpack_autoload_psr4.php', |
125
|
|
|
$path_map |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
$loader = new Version_Loader( |
129
|
|
|
new Version_Selector(), |
130
|
|
|
null, |
|
|
|
|
131
|
|
|
$path_map, |
132
|
|
|
null |
|
|
|
|
133
|
|
|
); |
134
|
|
|
|
135
|
|
|
$file = $loader->find_class_file( Psr4Test::class ); |
136
|
|
|
|
137
|
|
|
$this->assertEquals( TEST_DATA_PATH . '/plugins/plugin_newer/src/Psr4/Test.php', $file ); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Tests that the filemap manifest from a single plugin can be handled correctly. |
142
|
|
|
* |
143
|
|
|
* @preserveGlobalState disabled |
144
|
|
|
* @runInSeparateProcess |
145
|
|
|
*/ |
146
|
|
View Code Duplication |
public function test_single_plugin_filemap() { |
147
|
|
|
$path_map = array(); |
148
|
|
|
$this->single_manifest_handler->register_plugin_manifests( |
149
|
|
|
'vendor/composer/jetpack_autoload_filemap.php', |
150
|
|
|
$path_map |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
$loader = new Version_Loader( |
154
|
|
|
new Version_Selector(), |
155
|
|
|
null, |
|
|
|
|
156
|
|
|
null, |
|
|
|
|
157
|
|
|
$path_map |
158
|
|
|
); |
159
|
|
|
|
160
|
|
|
$loader->load_filemap(); |
161
|
|
|
|
162
|
|
|
$this->assertTrue( $GLOBALS['__composer_autoload_files']['123456acbdefg'] ); |
163
|
|
|
$this->assertTrue( function_exists( '\\Jetpack\\AutoloaderTestData\\PluginCurrent\\if_i_exist_then_this_test_passed' ) ); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Tests that the filemap manifest from multiple plugins can be handled correctly. |
168
|
|
|
* |
169
|
|
|
* @preserveGlobalState disabled |
170
|
|
|
* @runInSeparateProcess |
171
|
|
|
*/ |
172
|
|
View Code Duplication |
public function test_multiple_plugin_filemap() { |
173
|
|
|
$path_map = array(); |
174
|
|
|
$this->multiple_manifest_handler->register_plugin_manifests( |
175
|
|
|
'vendor/composer/jetpack_autoload_filemap.php', |
176
|
|
|
$path_map |
177
|
|
|
); |
178
|
|
|
|
179
|
|
|
$loader = new Version_Loader( |
180
|
|
|
new Version_Selector(), |
181
|
|
|
null, |
|
|
|
|
182
|
|
|
null, |
|
|
|
|
183
|
|
|
$path_map |
184
|
|
|
); |
185
|
|
|
|
186
|
|
|
$loader->load_filemap(); |
187
|
|
|
|
188
|
|
|
$this->assertTrue( $GLOBALS['__composer_autoload_files']['123456acbdefg'] ); |
189
|
|
|
$this->assertTrue( function_exists( '\\Jetpack\\AutoloaderTestData\\PluginNewer\\if_i_exist_then_this_test_passed' ) ); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are 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.php
However, as
OtherDir/Foo.php
does 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: