1
|
|
|
<?php // phpcs:ignore WordPress.Files.FileName |
2
|
|
|
/** |
3
|
|
|
* Autoloader guard test suite. |
4
|
|
|
* |
5
|
|
|
* @package automattic/jetpack-autoloader |
6
|
|
|
*/ |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Test suite class for the Autoloader part responsible for ensuring only the latest autoloader is ever executed. |
11
|
|
|
*/ |
12
|
|
|
class Test_Latest_Autoloader_Guard extends TestCase { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The mock Plugins_Handler instance. |
16
|
|
|
* |
17
|
|
|
* @var Plugins_Handler|\PHPUnit\Framework\MockObject\MockObject |
18
|
|
|
*/ |
19
|
|
|
private $plugins_handler; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The mock Autoloader_Handler instance. |
23
|
|
|
* |
24
|
|
|
* @var Autoloader_Handler|\PHPUnit\Framework\MockObject\MockObject |
25
|
|
|
*/ |
26
|
|
|
private $autoloader_handler; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The mock Autoloader_Locator instance. |
30
|
|
|
* |
31
|
|
|
* @var Autoloader_Locator|\PHPUnit\Framework\MockObject\MockObject |
32
|
|
|
*/ |
33
|
|
|
private $autoloader_locator; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The class we're testing. |
37
|
|
|
* |
38
|
|
|
* @var Latest_Autoloader_Guard |
39
|
|
|
*/ |
40
|
|
|
private $guard; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Setup runs before each test. |
44
|
|
|
* |
45
|
|
|
* @before |
46
|
|
|
*/ |
47
|
|
|
public function set_up() { |
48
|
|
|
$this->plugins_handler = $this->getMockBuilder( Plugins_Handler::class ) |
49
|
|
|
->disableOriginalConstructor() |
50
|
|
|
->getMock(); |
51
|
|
|
$this->autoloader_handler = $this->getMockBuilder( Autoloader_Handler::class ) |
52
|
|
|
->disableOriginalConstructor() |
53
|
|
|
->getMock(); |
54
|
|
|
$this->autoloader_locator = $this->getMockBuilder( Autoloader_Locator::class ) |
55
|
|
|
->disableOriginalConstructor() |
56
|
|
|
->getMock(); |
57
|
|
|
|
58
|
|
|
$this->guard = new Latest_Autoloader_Guard( |
59
|
|
|
$this->plugins_handler, |
60
|
|
|
$this->autoloader_handler, |
61
|
|
|
$this->autoloader_locator |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Tests that the guard stops initialization when the autoloader has already initialized. |
67
|
|
|
* |
68
|
|
|
* @runInSeparateProcess |
69
|
|
|
* @preserveGlobalState disabled |
70
|
|
|
*/ |
71
|
|
|
public function test_should_stop_init_when_autoloader_already_initialized() { |
72
|
|
|
global $jetpack_autoloader_latest_version; |
73
|
|
|
$jetpack_autoloader_latest_version = '2.0.0.0'; |
74
|
|
|
|
75
|
|
|
$this->assertTrue( |
76
|
|
|
$this->guard->should_stop_init( |
77
|
|
|
TEST_DATA_PATH . '/plugins/plugin_current', |
78
|
|
|
array() |
79
|
|
|
) |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Tests that the guard stops initialization when not the latest autoloader. |
85
|
|
|
*/ |
86
|
|
|
public function test_should_stop_init_when_not_latest_autoloader() { |
87
|
|
|
$this->plugins_handler->method( 'have_plugins_changed' ) |
|
|
|
|
88
|
|
|
->with( array() ) |
89
|
|
|
->willReturn( true ); |
90
|
|
|
$this->autoloader_locator->method( 'find_latest_autoloader' ) |
|
|
|
|
91
|
|
|
->willReturn( TEST_DATA_PATH . '/plugins/dummy_current' ); |
92
|
|
|
$this->autoloader_locator->method( 'get_autoloader_path' ) |
|
|
|
|
93
|
|
|
->willReturn( TEST_DATA_PATH . '/plugins/dummy_current/dummy_current.php' ); |
94
|
|
|
|
95
|
|
|
$this->assertTrue( |
96
|
|
|
$this->guard->should_stop_init( |
97
|
|
|
TEST_DATA_PATH . '/plugins/dummy_newer', |
98
|
|
|
array() |
99
|
|
|
) |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Tests that the guard allows initialization when the latest. |
105
|
|
|
*/ |
106
|
|
|
public function test_should_allow_init_when_latest() { |
107
|
|
|
$this->plugins_handler->method( 'have_plugins_changed' ) |
|
|
|
|
108
|
|
|
->with( array() ) |
109
|
|
|
->willReturn( true ); |
110
|
|
|
$this->autoloader_locator->method( 'find_latest_autoloader' ) |
|
|
|
|
111
|
|
|
->willReturn( null ); |
112
|
|
|
|
113
|
|
|
$this->assertFalse( |
114
|
|
|
$this->guard->should_stop_init( |
115
|
|
|
TEST_DATA_PATH . '/plugins/plugin_current', |
116
|
|
|
array() |
117
|
|
|
) |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Tests that the guard resets when plugins have changed. |
123
|
|
|
*/ |
124
|
|
|
public function test_should_stop_init_should_reset_when_plugins_change() { |
125
|
|
|
$this->plugins_handler->method( 'have_plugins_changed' ) |
|
|
|
|
126
|
|
|
->with( array() ) |
127
|
|
|
->willReturn( true ); |
128
|
|
|
$this->autoloader_handler->expects( $this->once() )->method( 'reset_autoloader' ); |
|
|
|
|
129
|
|
|
$this->autoloader_locator->method( 'find_latest_autoloader' ) |
|
|
|
|
130
|
|
|
->willReturn( null ); |
131
|
|
|
|
132
|
|
|
$this->assertFalse( |
133
|
|
|
$this->guard->should_stop_init( |
134
|
|
|
TEST_DATA_PATH . '/plugins/plugin_current', |
135
|
|
|
array() |
136
|
|
|
) |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.