Completed
Push — renovate/major-react-monorepo ( 880c2b...a6f86c )
by
unknown
355:09 queued 345:32
created

test_should_allow_init_when_latest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php // phpcs:ignore WordPress.Files.FileName
2
/**
3
 * Autoloader guard 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 part responsible for ensuring only the latest autoloader is ever executed.
16
 */
17
class LatestAutoloaderGuardTest extends TestCase {
18
19
	/**
20
	 * The mock Plugins_Handler instance.
21
	 *
22
	 * @var Plugins_Handler|\PHPUnit\Framework\MockObject\MockObject
23
	 */
24
	private $plugins_handler;
25
26
	/**
27
	 * The mock Autoloader_Handler instance.
28
	 *
29
	 * @var Autoloader_Handler|\PHPUnit\Framework\MockObject\MockObject
30
	 */
31
	private $autoloader_handler;
32
33
	/**
34
	 * The mock Autoloader_Locator instance.
35
	 *
36
	 * @var Autoloader_Locator|\PHPUnit\Framework\MockObject\MockObject
37
	 */
38
	private $autoloader_locator;
39
40
	/**
41
	 * The class we're testing.
42
	 *
43
	 * @var Latest_Autoloader_Guard
44
	 */
45
	private $guard;
46
47
	/**
48
	 * Setup runs before each test.
49
	 *
50
	 * @before
51
	 */
52
	public function set_up() {
53
		$this->plugins_handler    = $this->getMockBuilder( Plugins_Handler::class )
54
			->disableOriginalConstructor()
55
			->getMock();
56
		$this->autoloader_handler = $this->getMockBuilder( Autoloader_Handler::class )
57
			->disableOriginalConstructor()
58
			->getMock();
59
		$this->autoloader_locator = $this->getMockBuilder( Autoloader_Locator::class )
60
			->disableOriginalConstructor()
61
			->getMock();
62
63
		$this->guard = new Latest_Autoloader_Guard(
64
			$this->plugins_handler,
65
			$this->autoloader_handler,
66
			$this->autoloader_locator
67
		);
68
	}
69
70
	/**
71
	 * Tests that the guard stops initialization when the autoloader has already initialized.
72
	 *
73
	 * @runInSeparateProcess
74
	 * @preserveGlobalState disabled
75
	 */
76 View Code Duplication
	public function test_should_stop_init_when_autoloader_already_initialized() {
77
		global $jetpack_autoloader_latest_version;
78
		$jetpack_autoloader_latest_version = Test_Plugin_Factory::VERSION_CURRENT;
79
80
		$this->assertTrue(
81
			$this->guard->should_stop_init(
82
				TEST_PLUGIN_DIR,
83
				array(),
84
				false
85
			)
86
		);
87
	}
88
89
	/**
90
	 * Tests that the guard allows initialization when the autoloader has been initialized but we've been deliberately included by it.
91
	 *
92
	 * @preserveGlobalState disabled
93
	 */
94 View Code Duplication
	public function test_should_allow_init_when_including_latest() {
95
		// Mark it as already initialized so we can make sure it overrides it.
96
		global $jetpack_autoloader_latest_version;
97
		$jetpack_autoloader_latest_version = Test_Plugin_Factory::VERSION_CURRENT;
98
99
		$this->assertFalse(
100
			$this->guard->should_stop_init(
101
				TEST_PLUGIN_DIR,
102
				array(),
103
				true
104
			)
105
		);
106
	}
107
108
	/**
109
	 * Tests that the guard stops initialization when not the latest autoloader.
110
	 *
111
	 * @preserveGlobalState disabled
112
	 */
113
	public function test_should_stop_init_when_not_latest_autoloader() {
114
		$this->plugins_handler->method( 'have_plugins_changed' )
115
			->with( array() )
116
			->willReturn( true );
117
		$this->autoloader_locator->method( 'find_latest_autoloader' )
118
			->willReturn( 'new-latest' );
119
		$this->autoloader_locator->method( 'get_autoloader_path' )
120
			->with( 'new-latest' )
121
			->willReturn( TEST_PLUGIN_DIR . '/functions.php' );
122
123
		$this->assertTrue(
124
			$this->guard->should_stop_init(
125
				TEST_PLUGIN_DIR,
126
				array(),
127
				false
128
			)
129
		);
130
131
		// Make sure we loaded the newer autoloader.
132
		global $jetpack_autoloader_testing_loaded_files;
133
		$this->assertContains( Test_Plugin_Factory::VERSION_CURRENT, $jetpack_autoloader_testing_loaded_files );
134
	}
135
136
	/**
137
	 * Tests that the guard allows initialization when the latest.
138
	 */
139
	public function test_should_allow_init_when_latest() {
140
		$this->plugins_handler->method( 'have_plugins_changed' )
141
			->with( array() )
142
			->willReturn( true );
143
		$this->autoloader_locator->method( 'find_latest_autoloader' )
144
			->willReturn( null );
145
146
		$this->assertFalse(
147
			$this->guard->should_stop_init(
148
				TEST_PLUGIN_DIR,
149
				array(),
150
				false
151
			)
152
		);
153
	}
154
155
	/**
156
	 * Tests that the guard resets when plugins have changed.
157
	 *
158
	 * @preserveGlobalState disabled
159
	 */
160
	public function test_should_stop_init_should_reset_when_plugins_change() {
161
		$this->plugins_handler->method( 'have_plugins_changed' )
162
			->with( array() )
163
			->willReturn( true );
164
		$this->autoloader_handler->expects( $this->once() )->method( 'reset_autoloader' );
165
		$this->autoloader_locator->method( 'find_latest_autoloader' )
166
			->willReturn( null );
167
168
		$this->assertFalse(
169
			$this->guard->should_stop_init(
170
				TEST_PLUGIN_DIR,
171
				array(),
172
				false
173
			)
174
		);
175
	}
176
}
177