1
|
|
|
<?php // phpcs:ignore WordPress.Files.FileName |
2
|
|
|
/** |
3
|
|
|
* File loader test suite. |
4
|
|
|
* |
5
|
|
|
* @package automattic/jetpack-autoloader |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
use Jetpack\AutoloaderTestData\Plugin\Test; |
|
|
|
|
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Test suite class for the Autoloader part that handles file loading. |
13
|
|
|
*/ |
14
|
|
|
class WP_Test_Manifest_Handler extends TestCase { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Tests whether registering a manifest file registers the individual class file. |
18
|
|
|
*/ |
19
|
|
View Code Duplication |
public function test_registering_adds_to_the_input_array() { |
20
|
|
|
$input_array = array(); |
21
|
|
|
$manifest_handler = new Manifest_Handler( |
22
|
|
|
array( |
23
|
|
|
TEST_DATA_PATH . '/plugins/plugin_current', |
24
|
|
|
), |
25
|
|
|
new Version_Selector() |
26
|
|
|
); |
27
|
|
|
|
28
|
|
|
$manifest_handler->register_plugin_manifests( 'vendor/composer/jetpack_autoload_classmap.php', $input_array ); |
29
|
|
|
|
30
|
|
|
$this->assertArrayHasKey( Test::class, $input_array ); |
31
|
|
|
$this->assertEquals( '1.0.0.0', $input_array[ Test::class ]['version'] ); |
32
|
|
|
$this->assertEquals( $input_array[ Test::class ]['path'], TEST_DATA_PATH . '/plugins/plugin_current/includes/class-test.php' ); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Tests whether registering a manifest file will override already registered paths with newer ones. |
37
|
|
|
*/ |
38
|
|
View Code Duplication |
public function test_registering_adds_latest_version_to_the_input_array() { |
39
|
|
|
$input_array = array(); |
40
|
|
|
$manifest_handler = new Manifest_Handler( |
41
|
|
|
array( |
42
|
|
|
TEST_DATA_PATH . '/plugins/plugin_newer', |
43
|
|
|
TEST_DATA_PATH . '/plugins/plugin_current', |
44
|
|
|
), |
45
|
|
|
new Version_Selector() |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
$manifest_handler->register_plugin_manifests( 'vendor/composer/jetpack_autoload_classmap.php', $input_array ); |
49
|
|
|
|
50
|
|
|
$this->assertArrayHasKey( Test::class, $input_array ); |
51
|
|
|
$this->assertEquals( '2.0.0.0', $input_array[ Test::class ]['version'] ); |
52
|
|
|
$this->assertEquals( $input_array[ Test::class ]['path'], TEST_DATA_PATH . '/plugins/plugin_newer/includes/class-test.php' ); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Tests whether registering a manifest file ignores the dev version of the file when |
57
|
|
|
* JETPACK_AUTOLOAD_DEV is not set. |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
public function test_registering_does_not_add_dev_versions_to_the_input_array() { |
60
|
|
|
$input_array = array(); |
61
|
|
|
$manifest_handler = new Manifest_Handler( |
62
|
|
|
array( |
63
|
|
|
TEST_DATA_PATH . '/plugins/plugin_dev', |
64
|
|
|
TEST_DATA_PATH . '/plugins/plugin_current', |
65
|
|
|
), |
66
|
|
|
new Version_Selector() |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
$manifest_handler->register_plugin_manifests( 'vendor/composer/jetpack_autoload_classmap.php', $input_array ); |
70
|
|
|
|
71
|
|
|
$this->assertArrayHasKey( Test::class, $input_array ); |
72
|
|
|
$this->assertEquals( '1.0.0.0', $input_array[ Test::class ]['version'] ); |
73
|
|
|
$this->assertEquals( $input_array[ Test::class ]['path'], TEST_DATA_PATH . '/plugins/plugin_current/includes/class-test.php' ); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Tests whether registering a manifest file prioritizes the dev version of the file when |
78
|
|
|
* JETPACK_AUTOLOAD_DEV is set to true. |
79
|
|
|
* |
80
|
|
|
* @runInSeparateProcess |
81
|
|
|
* @preserveGlobalState disabled |
82
|
|
|
*/ |
83
|
|
|
public function test_registering_adds_the_dev_version_to_the_input_array_with_constant() { |
84
|
|
|
$input_array = array(); |
85
|
|
|
$manifest_handler = new Manifest_Handler( |
86
|
|
|
array( |
87
|
|
|
TEST_DATA_PATH . '/plugins/plugin_dev', |
88
|
|
|
TEST_DATA_PATH . '/plugins/plugin_current', |
89
|
|
|
), |
90
|
|
|
new Version_Selector() |
91
|
|
|
); |
92
|
|
|
defined( 'JETPACK_AUTOLOAD_DEV' ) || define( 'JETPACK_AUTOLOAD_DEV', true ); |
93
|
|
|
|
94
|
|
|
$manifest_handler->register_plugin_manifests( 'vendor/composer/jetpack_autoload_classmap.php', $input_array ); |
95
|
|
|
|
96
|
|
|
$this->assertArrayHasKey( Test::class, $input_array ); |
97
|
|
|
$this->assertEquals( 'dev-main', $input_array[ Test::class ]['version'] ); |
98
|
|
|
$this->assertEquals( $input_array[ Test::class ]['path'], TEST_DATA_PATH . '/plugins/plugin_dev/includes/class-test.php' ); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
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: