summaryrefslogtreecommitdiffstatshomepage
path: root/tests/phpunit/includes/class-mockobject-autoload.php
blob: b8f46e8a2e3891c7f614e3184cd4085dd747318a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php

/**
 * Custom autoloader for the PHPUnit 9.x MockObject classes.
 *
 * Hack around PHPUnit < 9.3 mocking not being compatible with PHP 8.
 *
 * This is a temporary solution until the PHPUnit version constraints are widened.
 *
 * @since 5.9.0
 */
final class MockObject_Autoload {

	/**
	 * A list of the classes this autoloader handles.
	 *
	 * @var string[] => true
	 */
	private static $supported_classes = array(
		'PHPUnit\Framework\MockObject\Builder\NamespaceMatch' => '/phpunit7/MockObject/Builder/NamespaceMatch.php',
		'PHPUnit\Framework\MockObject\Builder\ParametersMatch' => '/phpunit7/MockObject/Builder/ParametersMatch.php',
		'PHPUnit\Framework\MockObject\InvocationMocker' => '/phpunit7/MockObject/InvocationMocker.php',
		'PHPUnit\Framework\MockObject\MockMethod'       => '/phpunit7/MockObject/MockMethod.php',
	);

	/**
	 * Loads a class.
	 *
	 * @param string $class_name The name of the class to load.
	 * @return bool
	 */
	public static function load( $class_name ) {

		if ( isset( self::$supported_classes[ $class_name ] ) === false ) {
			// Bow out, not a class this autoloader handles.
			return false;
		}

		if ( PHP_VERSION_ID < 80000 ) {
			// This autoloader is only needed when the tests are being run on PHP >= 8.0.
			// Let the standard Composer autoloader handle things otherwise.
			return false;
		}

		if ( class_exists( 'PHPUnit\Runner\Version' ) === false // PHPUnit < 6.0.
			|| ( version_compare( \PHPUnit\Runner\Version::id(), '7.0.0', '<' )
			&& version_compare( \PHPUnit\Runner\Version::id(), '8.0.0', '>=' ) )
		) {
			// This autoloader is only needed when the tests are being run on PHPUnit 7.
			return false;
		}

		$relative_path = self::$supported_classes[ $class_name ];
		$file          = \realpath( __DIR__ . $relative_path );

		if ( false === $file || @\file_exists( $file ) === false ) {
			return false;
		}

		require_once $file;
		return true;
	}
}