1
|
|
|
<?php // phpcs:ignore WordPress.Files.FileName |
2
|
|
|
/** |
3
|
|
|
* Autoloader test suite. |
4
|
|
|
* |
5
|
|
|
* @package automattic/jetpack-autoloader |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Test suite class for the Autoloader. |
12
|
|
|
* |
13
|
|
|
* @runTestsInSeparateProcesses Ensure that each test loads class files new. |
14
|
|
|
* @preserveGlobalState disabled |
15
|
|
|
*/ |
16
|
|
|
class Test_Autoloader extends TestCase { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Tests that the autoloader is initialized correctly and registers the correct hook. |
20
|
|
|
*/ |
21
|
|
|
public function test_init_autoloader() { |
22
|
|
|
global $test_container; |
23
|
|
|
|
24
|
|
|
$plugin_locator = $this->getMockBuilder( Plugin_Locator::class ) |
25
|
|
|
->disableOriginalConstructor() |
26
|
|
|
->getMock(); |
27
|
|
|
$test_container->replace( Plugin_Locator::class, $plugin_locator ); |
28
|
|
|
|
29
|
|
|
$autoloader_handler = $this->getMockBuilder( Autoloader_Handler::class ) |
30
|
|
|
->disableOriginalConstructor() |
31
|
|
|
->getMock(); |
32
|
|
|
$test_container->replace( Autoloader_Handler::class, $autoloader_handler ); |
33
|
|
|
|
34
|
|
|
$plugins_handler = $this->getMockBuilder( Plugins_Handler::class ) |
35
|
|
|
->disableOriginalConstructor() |
36
|
|
|
->getMock(); |
37
|
|
|
$test_container->replace( Plugins_Handler::class, $plugins_handler ); |
38
|
|
|
|
39
|
|
|
$guard = $this->getMockBuilder( Latest_Autoloader_Guard::class ) |
40
|
|
|
->disableOriginalConstructor() |
41
|
|
|
->getMock(); |
42
|
|
|
$test_container->replace( Latest_Autoloader_Guard::class, $guard ); |
43
|
|
|
|
44
|
|
|
$plugin_locator->expects( $this->once() ) |
45
|
|
|
->method( 'find_current_plugin' ) |
46
|
|
|
->willReturn( TEST_DATA_PATH . '/plugins/dummy_current' ); |
47
|
|
|
$plugins_handler->expects( $this->once() ) |
48
|
|
|
->method( 'get_cached_plugins' ) |
49
|
|
|
->willReturn( array( TEST_DATA_PATH . '/plugins/dummy_newer' ) ); |
50
|
|
|
$autoloader_handler->expects( $this->once() ) |
51
|
|
|
->method( 'is_initializing' ) |
52
|
|
|
->willReturn( false ); |
53
|
|
|
$plugins_handler->expects( $this->once() ) |
54
|
|
|
->method( 'get_active_plugins' ) |
55
|
|
|
->willReturn( array( TEST_DATA_PATH . '/plugins/dummy_current' ) ); |
56
|
|
|
$guard->expects( $this->once() ) |
57
|
|
|
->method( 'should_stop_init' ) |
58
|
|
|
->with( |
59
|
|
|
TEST_DATA_PATH . '/plugins/dummy_current', |
60
|
|
|
array( TEST_DATA_PATH . '/plugins/dummy_current', TEST_DATA_PATH . '/plugins/dummy_newer' ), |
61
|
|
|
false |
62
|
|
|
) |
63
|
|
|
->willReturn( false ); |
64
|
|
|
$autoloader_handler->expects( $this->once() ) |
65
|
|
|
->method( 'activate_autoloader' ) |
66
|
|
|
->with( array( TEST_DATA_PATH . '/plugins/dummy_current', TEST_DATA_PATH . '/plugins/dummy_newer' ) ); |
67
|
|
|
|
68
|
|
|
Autoloader::init( $test_container ); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Tests that the autoloader is stopped by the guard correctly. |
73
|
|
|
*/ |
74
|
|
|
public function test_init_autoloader_stopped_by_guard() { |
75
|
|
|
global $test_container; |
76
|
|
|
|
77
|
|
|
$plugin_locator = $this->getMockBuilder( Plugin_Locator::class ) |
78
|
|
|
->disableOriginalConstructor() |
79
|
|
|
->getMock(); |
80
|
|
|
$test_container->replace( Plugin_Locator::class, $plugin_locator ); |
81
|
|
|
|
82
|
|
|
$autoloader_handler = $this->getMockBuilder( Autoloader_Handler::class ) |
83
|
|
|
->disableOriginalConstructor() |
84
|
|
|
->getMock(); |
85
|
|
|
$test_container->replace( Autoloader_Handler::class, $autoloader_handler ); |
86
|
|
|
|
87
|
|
|
$plugins_handler = $this->getMockBuilder( Plugins_Handler::class ) |
88
|
|
|
->disableOriginalConstructor() |
89
|
|
|
->getMock(); |
90
|
|
|
$test_container->replace( Plugins_Handler::class, $plugins_handler ); |
91
|
|
|
|
92
|
|
|
$guard = $this->getMockBuilder( Latest_Autoloader_Guard::class ) |
93
|
|
|
->disableOriginalConstructor() |
94
|
|
|
->getMock(); |
95
|
|
|
$test_container->replace( Latest_Autoloader_Guard::class, $guard ); |
96
|
|
|
|
97
|
|
|
$plugin_locator->expects( $this->once() ) |
98
|
|
|
->method( 'find_current_plugin' ) |
99
|
|
|
->willReturn( TEST_DATA_PATH . '/plugins/dummy_current' ); |
100
|
|
|
$plugins_handler->expects( $this->once() ) |
101
|
|
|
->method( 'get_cached_plugins' ) |
102
|
|
|
->willReturn( array( TEST_DATA_PATH . '/plugins/dummy_newer' ) ); |
103
|
|
|
$autoloader_handler->expects( $this->once() ) |
104
|
|
|
->method( 'is_initializing' ) |
105
|
|
|
->willReturn( false ); |
106
|
|
|
$plugins_handler->expects( $this->once() ) |
107
|
|
|
->method( 'get_active_plugins' ) |
108
|
|
|
->willReturn( array( TEST_DATA_PATH . '/plugins/dummy_current' ) ); |
109
|
|
|
$guard->expects( $this->once() ) |
110
|
|
|
->method( 'should_stop_init' ) |
111
|
|
|
->with( |
112
|
|
|
TEST_DATA_PATH . '/plugins/dummy_current', |
113
|
|
|
array( TEST_DATA_PATH . '/plugins/dummy_current', TEST_DATA_PATH . '/plugins/dummy_newer' ), |
114
|
|
|
false |
115
|
|
|
) |
116
|
|
|
->willReturn( true ); |
117
|
|
|
|
118
|
|
|
Autoloader::init( $test_container ); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|