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 Test_Version_Loading_From_Manifests extends TestCase { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* A manifest handler. |
19
|
|
|
* |
20
|
|
|
* @var Manifest_Reader |
21
|
|
|
*/ |
22
|
|
|
private $manifest_handler; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Setup runs before each test. |
26
|
|
|
* |
27
|
|
|
* @before |
28
|
|
|
*/ |
29
|
|
|
public function set_up() { |
30
|
|
|
$this->manifest_handler = new Manifest_Reader( new Version_Selector() ); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Tests that the classmap manifest from a single plugin can be handled correctly. |
35
|
|
|
*/ |
36
|
|
View Code Duplication |
public function test_classmap() { |
37
|
|
|
$path_map = array(); |
38
|
|
|
$this->manifest_handler->read_manifests( |
39
|
|
|
array( TEST_DATA_PATH . '/plugins/dummy_current' ), |
40
|
|
|
'vendor/composer/jetpack_autoload_classmap.php', |
41
|
|
|
$path_map |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
$loader = new Version_Loader( |
45
|
|
|
new Version_Selector(), |
46
|
|
|
$path_map, |
47
|
|
|
null, |
|
|
|
|
48
|
|
|
null |
|
|
|
|
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
$file = $loader->find_class_file( Test::class ); |
52
|
|
|
|
53
|
|
|
$this->assertEquals( TEST_DATA_PATH . '/plugins/dummy_current/includes/class-test.php', $file ); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Tests that the PSR-4 manifest from a single plugin can be handled correctly. |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
public function test_psr4() { |
60
|
|
|
$path_map = array(); |
61
|
|
|
$this->manifest_handler->read_manifests( |
62
|
|
|
array( TEST_DATA_PATH . '/plugins/dummy_current' ), |
63
|
|
|
'vendor/composer/jetpack_autoload_psr4.php', |
64
|
|
|
$path_map |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
$loader = new Version_Loader( |
68
|
|
|
new Version_Selector(), |
69
|
|
|
null, |
|
|
|
|
70
|
|
|
$path_map, |
71
|
|
|
null |
|
|
|
|
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
$file = $loader->find_class_file( Psr4Test::class ); |
75
|
|
|
|
76
|
|
|
$this->assertEquals( TEST_DATA_PATH . '/plugins/dummy_current/src/Psr4/Test.php', $file ); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Tests that the filemap manifest from a single plugin can be handled correctly. |
81
|
|
|
* |
82
|
|
|
* @preserveGlobalState disabled |
83
|
|
|
* @runInSeparateProcess |
84
|
|
|
*/ |
85
|
|
|
public function test_filemap() { |
86
|
|
|
$path_map = array(); |
87
|
|
|
$this->manifest_handler->read_manifests( |
88
|
|
|
array( TEST_DATA_PATH . '/plugins/dummy_current' ), |
89
|
|
|
'vendor/composer/jetpack_autoload_filemap.php', |
90
|
|
|
$path_map |
91
|
|
|
); |
92
|
|
|
|
93
|
|
|
$loader = new Version_Loader( |
94
|
|
|
new Version_Selector(), |
95
|
|
|
null, |
|
|
|
|
96
|
|
|
null, |
|
|
|
|
97
|
|
|
$path_map |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
$loader->load_filemap(); |
101
|
|
|
|
102
|
|
|
$this->assertTrue( $GLOBALS['__composer_autoload_files']['123456acbdefg'] ); |
103
|
|
|
$this->assertTrue( function_exists( '\\Jetpack\\AutoloaderTestData\\PluginCurrent\\if_i_exist_then_this_test_passed' ) ); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
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: