|
1
|
|
|
<?php // phpcs:ignore WordPress.Files.FileName |
|
2
|
|
|
/** |
|
3
|
|
|
* PHP autoloader test suite. |
|
4
|
|
|
* |
|
5
|
|
|
* @package automattic/jetpack-autoloader |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
// We live in the namespace of the test autoloader to avoid many use statements. |
|
9
|
|
|
namespace Automattic\Jetpack\Autoloader\jpCurrent; |
|
10
|
|
|
|
|
11
|
|
|
use Automattic\Jetpack\AutoloaderTesting\SharedTestClass; |
|
12
|
|
|
use PHPUnit\Framework\TestCase; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Test suite class for the management of the PHP autoloader. |
|
16
|
|
|
* |
|
17
|
|
|
* @runTestsInSeparateProcesses Ensure that each test loads class files new. |
|
18
|
|
|
* @preserveGlobalState disabled |
|
19
|
|
|
*/ |
|
20
|
|
|
class PHPAutoloaderTest extends TestCase { |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Tests that the autoloader can be registered correctly. |
|
24
|
|
|
*/ |
|
25
|
|
|
public function test_register_autoloader() { |
|
26
|
|
|
$removed_autoloader = 'Automattic\\Jetpack\\Autoloader\\jp123\\autoload'; |
|
27
|
|
|
spl_autoload_register( $removed_autoloader ); |
|
28
|
|
|
|
|
29
|
|
|
global $jetpack_autoloader_loader; |
|
30
|
|
|
|
|
31
|
|
|
$loader = $this->getMockBuilder( Version_Loader::class ) |
|
32
|
|
|
->disableOriginalConstructor() |
|
33
|
|
|
->getMock(); |
|
34
|
|
|
|
|
35
|
|
|
( new PHP_Autoloader() )->register_autoloader( $loader ); |
|
36
|
|
|
|
|
37
|
|
|
$autoloaders = spl_autoload_functions(); |
|
38
|
|
|
|
|
39
|
|
|
// Drop the autoloader so that PHPUnit does not throw errors. |
|
40
|
|
|
spl_autoload_unregister( PHP_Autoloader::class . '::load_class' ); |
|
41
|
|
|
|
|
42
|
|
|
$this->assertContains( array( PHP_Autoloader::class, 'load_class' ), $autoloaders ); |
|
43
|
|
|
$this->assertEquals( $loader, $jetpack_autoloader_loader ); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Tests that the autoloader can be unregistered. |
|
48
|
|
|
*/ |
|
49
|
|
|
public function test_unregister_autoloader() { |
|
50
|
|
|
// v2 Function Autoloader. |
|
51
|
|
|
$removed_autoloader = 'Automattic\\Jetpack\\Autoloader\\jp123\\autoload'; |
|
52
|
|
|
spl_autoload_register( $removed_autoloader ); |
|
53
|
|
|
|
|
54
|
|
|
( new PHP_Autoloader() )->unregister_autoloader(); |
|
55
|
|
|
|
|
56
|
|
|
$autoloaders = spl_autoload_functions(); |
|
57
|
|
|
$this->assertNotContains( $removed_autoloader, $autoloaders ); |
|
58
|
|
|
|
|
59
|
|
|
// v2 Class Autoloader. |
|
60
|
|
|
$removed_autoloader = array( \Automattic\Jetpack\Autoloader\jp123\PHP_Autoloader::class, 'load_class' ); |
|
61
|
|
|
spl_autoload_register( $removed_autoloader ); |
|
62
|
|
|
|
|
63
|
|
|
( new PHP_Autoloader() )->unregister_autoloader(); |
|
64
|
|
|
|
|
65
|
|
|
$autoloaders = spl_autoload_functions(); |
|
66
|
|
|
$this->assertNotContains( $removed_autoloader, $autoloaders ); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Tests that class files are loaded correctly. |
|
71
|
|
|
*/ |
|
72
|
|
View Code Duplication |
public function test_load_class() { |
|
73
|
|
|
$loader = $this->getMockBuilder( Version_Loader::class ) |
|
74
|
|
|
->disableOriginalConstructor() |
|
75
|
|
|
->setMethods( array( 'find_class_file' ) ) |
|
76
|
|
|
->getMock(); |
|
77
|
|
|
|
|
78
|
|
|
global $jetpack_autoloader_loader; |
|
79
|
|
|
$jetpack_autoloader_loader = $loader; |
|
80
|
|
|
$loader->expects( $this->once() ) |
|
81
|
|
|
->method( 'find_class_file' ) |
|
82
|
|
|
->with( SharedTestClass::class ) |
|
83
|
|
|
->willReturn( TEST_PLUGIN_DIR . '/src/SharedTestClass.php' ); |
|
84
|
|
|
|
|
85
|
|
|
$this->assertTrue( PHP_Autoloader::load_class( SharedTestClass::class ) ); |
|
86
|
|
|
$this->assertTrue( class_exists( SharedTestClass::class, false ) ); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Tests that nothing happens when a class file isn't found. |
|
91
|
|
|
*/ |
|
92
|
|
View Code Duplication |
public function test_load_class_does_nothing_without_class() { |
|
93
|
|
|
$loader = $this->getMockBuilder( Version_Loader::class ) |
|
94
|
|
|
->disableOriginalConstructor() |
|
95
|
|
|
->setMethods( array( 'find_class_file' ) ) |
|
96
|
|
|
->getMock(); |
|
97
|
|
|
|
|
98
|
|
|
global $jetpack_autoloader_loader; |
|
99
|
|
|
$jetpack_autoloader_loader = $loader; |
|
100
|
|
|
$loader->expects( $this->once() ) |
|
101
|
|
|
->method( 'find_class_file' ) |
|
102
|
|
|
->with( SharedTestClass::class ) |
|
103
|
|
|
->willReturn( null ); |
|
104
|
|
|
|
|
105
|
|
|
$this->assertFalse( PHP_Autoloader::load_class( SharedTestClass::class ) ); |
|
106
|
|
|
$this->assertFalse( class_exists( SharedTestClass::class, false ) ); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|