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

VersionLoaderTest::set_up_before_class()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php // phpcs:ignore WordPress.Files.FileName
2
/**
3
 * Class loader 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\Current\UniqueTestClass;
12
use Automattic\Jetpack\AutoloaderTesting\SharedTestClass;
13
use PHPUnit\Framework\TestCase;
14
use Test_Plugin_Factory;
15
16
/**
17
 * Test suite class for the Autoloader part that handles file loading.
18
 */
19
class VersionLoaderTest extends TestCase {
20
21
	/**
22
	 * The older version of the autoloader that we want to use. Note that
23
	 * the version should support PSR-4 since this one does.
24
	 */
25
	const OLDER_VERSION = '2.4.0.0';
26
27
	/**
28
	 * The directory of a plugin using the autoloader.
29
	 *
30
	 * @var string
31
	 */
32
	private static $older_plugin_dir;
33
34
	/**
35
	 * Setup before class runs before the class.
36
	 *
37
	 * @beforeClass
38
	 */
39
	public static function set_up_before_class() {
40
		self::$older_plugin_dir = Test_Plugin_Factory::create_test_plugin( false, self::OLDER_VERSION )->make();
41
	}
42
43
	/**
44
	 * Tests that `find_class_file` returns null when the given class is not known.
45
	 */
46
	public function test_find_class_file_returns_null_for_unknown_class() {
47
		$version_loader = new Version_Loader( new Version_Selector(), null, null, null );
48
49
		$file_path = $version_loader->find_class_file( UniqueTestClass::class );
50
51
		$this->assertNull( $file_path );
52
	}
53
54
	/**
55
	 * Tests that `find_class_file` returns the path to the class when present in the classmap.
56
	 */
57 View Code Duplication
	public function test_find_class_file_returns_path_for_classmap() {
58
		$version_loader = new Version_Loader(
59
			new Version_Selector(),
60
			array(
61
				SharedTestClass::class => array(
62
					'version' => '1.0.0.0',
63
					'path'    => TEST_PLUGIN_DIR . '/src/SharedTestClass.php',
64
				),
65
			),
66
			null,
67
			null
68
		);
69
70
		$file_path = $version_loader->find_class_file( SharedTestClass::class );
71
72
		$this->assertEquals( TEST_PLUGIN_DIR . '/src/SharedTestClass.php', $file_path );
73
	}
74
75
	/**
76
	 * Test that `find_class_file` returns the path to the class when present in the PSR-4 map.
77
	 */
78 View Code Duplication
	public function test_find_class_file_returns_path_for_psr4() {
79
		$version_loader = new Version_Loader(
80
			new Version_Selector(),
81
			null,
82
			array(
83
				Test_Plugin_Factory::TESTING_NAMESPACE => array(
84
					'version' => '1.0.0.0',
85
					'path'    => array( TEST_PLUGIN_DIR . '/src' ),
86
				),
87
			),
88
			null
89
		);
90
91
		$file_path = $version_loader->find_class_file( SharedTestClass::class );
92
93
		$this->assertEquals( TEST_PLUGIN_DIR . '/src/SharedTestClass.php', $file_path );
94
	}
95
96
	/**
97
	 * Tests that `find_class_file` returns the path to the class when presented
98
	 * with less-specific namespaces first in the PSR-4 map.
99
	 */
100 View Code Duplication
	public function test_find_class_file_checks_returns_path_for_psr4_with_less_specific_namespace() {
101
		$version_loader = new Version_Loader(
102
			new Version_Selector(),
103
			null,
104
			array(
105
				Test_Plugin_Factory::TESTING_NAMESPACE => array(
106
					'version' => '1.0.0.0',
107
					'path'    => array( TEST_PLUGIN_DIR . '/src' ),
108
				),
109
				Test_Plugin_Factory::TESTING_NAMESPACE . 'Current\\' => array(
110
					'version' => '1.0.0.0',
111
					'path'    => array( TEST_PLUGIN_DIR . '/src/Current' ),
112
				),
113
			),
114
			null
115
		);
116
117
		$file_path = $version_loader->find_class_file( UniqueTestClass::class );
118
119
		$this->assertEquals( TEST_PLUGIN_DIR . '/src/Current/UniqueTestClass.php', $file_path );
120
	}
121
122
	/**
123
	 * Test that `find_class_file` returns the classmap version when newer.
124
	 */
125 View Code Duplication
	public function test_find_class_file_returns_newer_classmap() {
126
		$version_loader = new Version_Loader(
127
			new Version_Selector(),
128
			array(
129
				SharedTestClass::class => array(
130
					'version' => '2.0.0.0',
131
					'path'    => TEST_PLUGIN_DIR . '/src/SharedTestClass.php',
132
				),
133
			),
134
			array(
135
				Test_Plugin_Factory::TESTING_NAMESPACE => array(
136
					'version' => '1.0.0.0',
137
					'path'    => array( self::$older_plugin_dir . '/src' ),
138
				),
139
			),
140
			null
141
		);
142
143
		$file_path = $version_loader->find_class_file( SharedTestClass::class );
144
145
		$this->assertEquals( TEST_PLUGIN_DIR . '/src/SharedTestClass.php', $file_path );
146
	}
147
148
	/**
149
	 * Test that `find_class_file` returns the PSR-4 version when newer.
150
	 */
151 View Code Duplication
	public function test_find_class_file_returns_newer_psr4() {
152
		$version_loader = new Version_Loader(
153
			new Version_Selector(),
154
			array(
155
				SharedTestClass::class => array(
156
					'version' => '1.0.0.0',
157
					'path'    => self::$older_plugin_dir . '/src/SharedTestClass.php',
158
				),
159
			),
160
			array(
161
				Test_Plugin_Factory::TESTING_NAMESPACE => array(
162
					'version' => '2.0.0.0',
163
					'path'    => array( TEST_PLUGIN_DIR . '/src' ),
164
				),
165
			),
166
			null
167
		);
168
169
		$file_path = $version_loader->find_class_file( SharedTestClass::class );
170
171
		$this->assertEquals( TEST_PLUGIN_DIR . '/src/SharedTestClass.php', $file_path );
172
	}
173
174
	/**
175
	 * Tests that `load_filemap` correctly loads all of the files.
176
	 *
177
	 * @preserveGlobalState disabled
178
	 * @runInSeparateProcess
179
	 */
180
	public function test_loads_filemap() {
181
		$file_hash = md5( 'test-file' );
182
183
		$version_loader = new Version_Loader(
184
			new Version_Selector(),
185
			null,
186
			null,
187
			array(
188
				$file_hash => array(
189
					'version' => '1.0.0.0',
190
					'path'    => TEST_PLUGIN_DIR . '/functions.php',
191
				),
192
			)
193
		);
194
195
		$version_loader->load_filemap();
196
197
		$this->assertTrue( $GLOBALS['__composer_autoload_files'][ $file_hash ] );
198
		global $jetpack_autoloader_testing_loaded_files;
199
		$this->assertContains( Test_Plugin_Factory::VERSION_CURRENT, $jetpack_autoloader_testing_loaded_files );
200
	}
201
202
	/**
203
	 * Tests that `load_filemap` does not load files that have already been loaded.
204
	 *
205
	 * @preserveGlobalState disabled
206
	 * @runInSeparateProcess
207
	 */
208
	public function test_loads_filemap_skips_existing_files() {
209
		$file_hash = md5( 'test-file' );
210
211
		$version_loader = new Version_Loader(
212
			new Version_Selector(),
213
			null,
214
			null,
215
			array(
216
				$file_hash => array(
217
					'version' => '1.0.0.0',
218
					'path'    => TEST_PLUGIN_DIR . '/functions.php',
219
				),
220
			)
221
		);
222
223
		// Pretend it was already loaded!
224
		$GLOBALS['__composer_autoload_files'][ $file_hash ] = true;
225
226
		$version_loader->load_filemap();
227
228
		$this->assertTrue( $GLOBALS['__composer_autoload_files'][ $file_hash ] );
229
		global $jetpack_autoloader_testing_loaded_files;
230
		$this->assertNotContains( Test_Plugin_Factory::VERSION_CURRENT, $jetpack_autoloader_testing_loaded_files );
231
	}
232
}
233