1
|
|
|
<?php // phpcs:ignore WordPress.Files.FileName |
2
|
|
|
/** |
3
|
|
|
* 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 PHPUnit\Framework\TestCase; |
12
|
|
|
use Test_Plugin_Factory; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Test suite class for the Autoloader. |
16
|
|
|
* |
17
|
|
|
* @runTestsInSeparateProcesses Ensure that each test loads class files new. |
18
|
|
|
* @preserveGlobalState disabled |
19
|
|
|
*/ |
20
|
|
|
class AutoloaderTest extends TestCase { |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The older version of the autoloader that we want to use. Note that |
24
|
|
|
* the version should support PSR-4 since this one does. |
25
|
|
|
*/ |
26
|
|
|
const OLDER_VERSION = '2.4.0.0'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The directory of a plugin using the autoloader. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private static $older_plugin_dir; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Setup before class runs before the class. |
37
|
|
|
* |
38
|
|
|
* @beforeClass |
39
|
|
|
*/ |
40
|
|
|
public static function set_up_before_class() { |
41
|
|
|
self::$older_plugin_dir = Test_Plugin_Factory::create_test_plugin( false, self::OLDER_VERSION )->make(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Tests that the autoloader is initialized correctly and registers the correct hook. |
46
|
|
|
*/ |
47
|
|
|
public function test_init_autoloader() { |
48
|
|
|
$test_container = new Test_Container(); |
49
|
|
|
|
50
|
|
|
$plugin_locator = $this->getMockBuilder( Plugin_Locator::class ) |
51
|
|
|
->disableOriginalConstructor() |
52
|
|
|
->getMock(); |
53
|
|
|
$test_container->replace( Plugin_Locator::class, $plugin_locator ); |
54
|
|
|
|
55
|
|
|
$autoloader_handler = $this->getMockBuilder( Autoloader_Handler::class ) |
56
|
|
|
->disableOriginalConstructor() |
57
|
|
|
->getMock(); |
58
|
|
|
$test_container->replace( Autoloader_Handler::class, $autoloader_handler ); |
59
|
|
|
|
60
|
|
|
$plugins_handler = $this->getMockBuilder( Plugins_Handler::class ) |
61
|
|
|
->disableOriginalConstructor() |
62
|
|
|
->getMock(); |
63
|
|
|
$test_container->replace( Plugins_Handler::class, $plugins_handler ); |
64
|
|
|
|
65
|
|
|
$guard = $this->getMockBuilder( Latest_Autoloader_Guard::class ) |
66
|
|
|
->disableOriginalConstructor() |
67
|
|
|
->getMock(); |
68
|
|
|
$test_container->replace( Latest_Autoloader_Guard::class, $guard ); |
69
|
|
|
|
70
|
|
|
$plugin_locator->expects( $this->once() ) |
71
|
|
|
->method( 'find_current_plugin' ) |
72
|
|
|
->willReturn( TEST_PLUGIN_DIR ); |
73
|
|
|
$plugins_handler->expects( $this->once() ) |
74
|
|
|
->method( 'get_cached_plugins' ) |
75
|
|
|
->willReturn( array( self::$older_plugin_dir ) ); |
76
|
|
|
$autoloader_handler->expects( $this->once() ) |
77
|
|
|
->method( 'is_initializing' ) |
78
|
|
|
->willReturn( false ); |
79
|
|
|
$plugins_handler->expects( $this->once() ) |
80
|
|
|
->method( 'get_active_plugins' ) |
81
|
|
|
->willReturn( array( TEST_PLUGIN_DIR ) ); |
82
|
|
|
$guard->expects( $this->once() ) |
83
|
|
|
->method( 'should_stop_init' ) |
84
|
|
|
->with( |
85
|
|
|
TEST_PLUGIN_DIR, |
86
|
|
|
array( TEST_PLUGIN_DIR, self::$older_plugin_dir ), |
87
|
|
|
false |
88
|
|
|
) |
89
|
|
|
->willReturn( false ); |
90
|
|
|
$autoloader_handler->expects( $this->once() ) |
91
|
|
|
->method( 'activate_autoloader' ) |
92
|
|
|
->with( array( TEST_PLUGIN_DIR, self::$older_plugin_dir ) ); |
93
|
|
|
|
94
|
|
|
Autoloader::init( $test_container ); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Tests that the autoloader is stopped by the guard correctly. |
99
|
|
|
*/ |
100
|
|
|
public function test_init_autoloader_stopped_by_guard() { |
101
|
|
|
$test_container = new Test_Container(); |
102
|
|
|
|
103
|
|
|
$plugin_locator = $this->getMockBuilder( Plugin_Locator::class ) |
104
|
|
|
->disableOriginalConstructor() |
105
|
|
|
->getMock(); |
106
|
|
|
$test_container->replace( Plugin_Locator::class, $plugin_locator ); |
107
|
|
|
|
108
|
|
|
$autoloader_handler = $this->getMockBuilder( Autoloader_Handler::class ) |
109
|
|
|
->disableOriginalConstructor() |
110
|
|
|
->getMock(); |
111
|
|
|
$test_container->replace( Autoloader_Handler::class, $autoloader_handler ); |
112
|
|
|
|
113
|
|
|
$plugins_handler = $this->getMockBuilder( Plugins_Handler::class ) |
114
|
|
|
->disableOriginalConstructor() |
115
|
|
|
->getMock(); |
116
|
|
|
$test_container->replace( Plugins_Handler::class, $plugins_handler ); |
117
|
|
|
|
118
|
|
|
$guard = $this->getMockBuilder( Latest_Autoloader_Guard::class ) |
119
|
|
|
->disableOriginalConstructor() |
120
|
|
|
->getMock(); |
121
|
|
|
$test_container->replace( Latest_Autoloader_Guard::class, $guard ); |
122
|
|
|
|
123
|
|
|
$plugin_locator->expects( $this->once() ) |
124
|
|
|
->method( 'find_current_plugin' ) |
125
|
|
|
->willReturn( TEST_PLUGIN_DIR ); |
126
|
|
|
$plugins_handler->expects( $this->once() ) |
127
|
|
|
->method( 'get_cached_plugins' ) |
128
|
|
|
->willReturn( array( self::$older_plugin_dir ) ); |
129
|
|
|
$autoloader_handler->expects( $this->once() ) |
130
|
|
|
->method( 'is_initializing' ) |
131
|
|
|
->willReturn( false ); |
132
|
|
|
$plugins_handler->expects( $this->once() ) |
133
|
|
|
->method( 'get_active_plugins' ) |
134
|
|
|
->willReturn( array( TEST_PLUGIN_DIR ) ); |
135
|
|
|
$guard->expects( $this->once() ) |
136
|
|
|
->method( 'should_stop_init' ) |
137
|
|
|
->with( |
138
|
|
|
TEST_PLUGIN_DIR, |
139
|
|
|
array( TEST_PLUGIN_DIR, self::$older_plugin_dir ), |
140
|
|
|
false |
141
|
|
|
) |
142
|
|
|
->willReturn( true ); |
143
|
|
|
$autoloader_handler->expects( $this->never() )->method( 'activate_autoloader' ); |
144
|
|
|
|
145
|
|
|
Autoloader::init( $test_container ); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|