|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is part of the phpunit-mink library. |
| 4 | + * For the full copyright and license information, please view |
| 5 | + * the LICENSE file that was distributed with this source code. |
| 6 | + * |
| 7 | + * @copyright Alexander Obuhovich <[email protected]> |
| 8 | + * @link https://github.com/aik099/phpunit-mink |
| 9 | + */ |
| 10 | + |
| 11 | +namespace ConsoleHelpers\PHPUnitCompat; |
| 12 | + |
| 13 | + |
| 14 | +use PHPUnit\Runner\Version; |
| 15 | + |
| 16 | +if ( \class_exists('ConsoleHelpers\PHPUnitCompat\Autoload', false) === false ) { |
| 17 | + |
| 18 | + /** |
| 19 | + * Custom autoloader. |
| 20 | + */ |
| 21 | + final class Autoload |
| 22 | + { |
| 23 | + |
| 24 | + /** |
| 25 | + * PHPUnit version. |
| 26 | + * |
| 27 | + * @var string |
| 28 | + */ |
| 29 | + protected static $phpUnitVersion; |
| 30 | + |
| 31 | + /** |
| 32 | + * Loads a class. |
| 33 | + * |
| 34 | + * @param string $class_name The name of the class to load. |
| 35 | + * |
| 36 | + * @return boolean |
| 37 | + */ |
| 38 | + public static function load($class_name) |
| 39 | + { |
| 40 | + // Only load classes belonging to this library. |
| 41 | + if ( \stripos($class_name, 'ConsoleHelpers\PHPUnitCompat') !== 0 ) { |
| 42 | + return false; |
| 43 | + } |
| 44 | + |
| 45 | + switch ( $class_name ) { |
| 46 | + case 'ConsoleHelpers\PHPUnitCompat\AbstractTestCase': |
| 47 | + self::defineAliases(); |
| 48 | + |
| 49 | + $phpunit_version = self::getPhpUnitVersion(); |
| 50 | + |
| 51 | + if ( version_compare($phpunit_version, '5.0.0', '<') ) { |
| 52 | + require_once __DIR__ . '/src/PHPUnitCompat/AbstractTestCase4.php'; |
| 53 | + } |
| 54 | + elseif ( version_compare($phpunit_version, '6.0.0', '<') ) { |
| 55 | + require_once __DIR__ . '/src/PHPUnitCompat/AbstractTestCase5.php'; |
| 56 | + } |
| 57 | + elseif ( version_compare($phpunit_version, '7.0.0', '<') ) { |
| 58 | + require_once __DIR__ . '/src/PHPUnitCompat/AbstractTestCase6.php'; |
| 59 | + } |
| 60 | + elseif ( version_compare($phpunit_version, '8.0.0', '<') ) { |
| 61 | + require_once __DIR__ . '/src/PHPUnitCompat/AbstractTestCase7.php'; |
| 62 | + } |
| 63 | + else { |
| 64 | + require_once __DIR__ . '/src/PHPUnitCompat/AbstractTestCase8.php'; |
| 65 | + } |
| 66 | + |
| 67 | + return true; |
| 68 | + |
| 69 | + case 'ConsoleHelpers\PHPUnitCompat\AbstractTestSuite': |
| 70 | + $phpunit_version = self::getPhpUnitVersion(); |
| 71 | + |
| 72 | + if ( version_compare($phpunit_version, '5.0.0', '<') ) { |
| 73 | + require_once __DIR__ . '/src/PHPUnitCompat/AbstractTestSuite4.php'; |
| 74 | + } |
| 75 | + elseif ( version_compare($phpunit_version, '6.0.0', '<') ) { |
| 76 | + require_once __DIR__ . '/src/PHPUnitCompat/AbstractTestSuite5.php'; |
| 77 | + } |
| 78 | + elseif ( version_compare($phpunit_version, '7.0.0', '<') ) { |
| 79 | + require_once __DIR__ . '/src/PHPUnitCompat/AbstractTestSuite6.php'; |
| 80 | + } |
| 81 | + elseif ( version_compare($phpunit_version, '8.0.0', '<') ) { |
| 82 | + require_once __DIR__ . '/src/PHPUnitCompat/AbstractTestSuite7.php'; |
| 83 | + } |
| 84 | + else { |
| 85 | + require_once __DIR__ . '/src/PHPUnitCompat/AbstractTestSuite8.php'; |
| 86 | + } |
| 87 | + |
| 88 | + return true; |
| 89 | + |
| 90 | + /* |
| 91 | + * Handles: |
| 92 | + * - ConsoleHelpers\PHPUnitCompat\TAbstractTestCaseBody |
| 93 | + * - ConsoleHelpers\PHPUnitCompat\TAbstractTestSuiteBody |
| 94 | + */ |
| 95 | + default: |
| 96 | + $file = \realpath(__DIR__ . '/src/' . \strtr(\substr($class_name, 15), '\\', '/') . '.php'); |
| 97 | + |
| 98 | + if ( \is_string($file) && \file_exists($file) === true ) { |
| 99 | + require_once $file; |
| 100 | + |
| 101 | + return true; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return false; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Get PHPUnit version. |
| 110 | + * |
| 111 | + * @return string |
| 112 | + */ |
| 113 | + protected static function getPhpUnitVersion() |
| 114 | + { |
| 115 | + if ( self::$phpUnitVersion === null ) { |
| 116 | + if ( \class_exists('PHPUnit\Runner\Version') ) { |
| 117 | + self::$phpUnitVersion = Version::id(); |
| 118 | + } |
| 119 | + else { |
| 120 | + self::$phpUnitVersion = \PHPUnit_Runner_Version::id(); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return self::$phpUnitVersion; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Defines aliases. |
| 129 | + * |
| 130 | + * @return void |
| 131 | + */ |
| 132 | + protected static function defineAliases() |
| 133 | + { |
| 134 | + // PHPUnit Compat. |
| 135 | + if ( \class_exists('\PHPUnit_Framework_IncompleteTestError') ) { |
| 136 | + \class_alias( |
| 137 | + '\PHPUnit_Framework_IncompleteTestError', |
| 138 | + '\ConsoleHelpers\PHPUnitCompat\Framework\IncompleteTestError' |
| 139 | + ); |
| 140 | + } |
| 141 | + else { |
| 142 | + \class_alias( |
| 143 | + '\PHPUnit\Framework\IncompleteTestError', |
| 144 | + '\ConsoleHelpers\PHPUnitCompat\Framework\IncompleteTestError' |
| 145 | + ); |
| 146 | + } |
| 147 | + |
| 148 | + if ( class_exists('\PHPUnit_Framework_SkippedTestError') ) { |
| 149 | + \class_alias('\PHPUnit_Framework_SkippedTestError', '\ConsoleHelpers\PHPUnitCompat\Framework\SkippedTestError'); |
| 150 | + } |
| 151 | + else { |
| 152 | + \class_alias('\PHPUnit\Framework\SkippedTestError', '\ConsoleHelpers\PHPUnitCompat\Framework\SkippedTestError'); |
| 153 | + } |
| 154 | + |
| 155 | + if ( class_exists('\PHPUnit_Framework_TestSuite_DataProvider') ) { |
| 156 | + \class_alias( |
| 157 | + '\PHPUnit_Framework_TestSuite_DataProvider', |
| 158 | + '\ConsoleHelpers\PHPUnitCompat\Framework\DataProviderTestSuite' |
| 159 | + ); |
| 160 | + } |
| 161 | + else { |
| 162 | + \class_alias( |
| 163 | + '\PHPUnit\Framework\DataProviderTestSuite', |
| 164 | + '\ConsoleHelpers\PHPUnitCompat\Framework\DataProviderTestSuite' |
| 165 | + ); |
| 166 | + } |
| 167 | + |
| 168 | + if ( class_exists('\PHPUnit_Framework_TestResult') ) { |
| 169 | + \class_alias('\PHPUnit_Framework_TestResult', '\ConsoleHelpers\PHPUnitCompat\Framework\TestResult'); |
| 170 | + } |
| 171 | + else { |
| 172 | + \class_alias('\PHPUnit\Framework\TestResult', '\ConsoleHelpers\PHPUnitCompat\Framework\TestResult'); |
| 173 | + } |
| 174 | + |
| 175 | + if ( class_exists('\PHPUnit_Framework_Test') ) { |
| 176 | + \class_alias('\PHPUnit_Framework_Test', '\ConsoleHelpers\PHPUnitCompat\Framework\Test'); |
| 177 | + } |
| 178 | + else { |
| 179 | + \class_alias('\PHPUnit\Framework\Test', '\ConsoleHelpers\PHPUnitCompat\Framework\Test'); |
| 180 | + } |
| 181 | + |
| 182 | + // CodeCoverage Compat. |
| 183 | + if ( class_exists('\PHP_CodeCoverage') ) { |
| 184 | + \class_alias('\PHP_CodeCoverage', '\ConsoleHelpers\CodeCoverageCompat\CodeCoverage'); |
| 185 | + } |
| 186 | + else { |
| 187 | + \class_alias( |
| 188 | + '\SebastianBergmann\CodeCoverage\CodeCoverage', |
| 189 | + '\ConsoleHelpers\CodeCoverageCompat\CodeCoverage' |
| 190 | + ); |
| 191 | + } |
| 192 | + |
| 193 | + if ( \interface_exists('\PHP_CodeCoverage_Driver') ) { |
| 194 | + \class_alias('\PHP_CodeCoverage_Driver', '\ConsoleHelpers\CodeCoverageCompat\Driver\Driver'); |
| 195 | + } |
| 196 | + else { |
| 197 | + \class_alias( |
| 198 | + '\SebastianBergmann\CodeCoverage\Driver\Driver', |
| 199 | + '\ConsoleHelpers\CodeCoverageCompat\Driver\Driver' |
| 200 | + ); |
| 201 | + } |
| 202 | + |
| 203 | + if ( class_exists('\PHP_CodeCoverage_Filter') ) { |
| 204 | + \class_alias('\PHP_CodeCoverage_Filter', '\ConsoleHelpers\CodeCoverageCompat\Filter'); |
| 205 | + } |
| 206 | + else { |
| 207 | + \class_alias('\SebastianBergmann\CodeCoverage\Filter', '\ConsoleHelpers\CodeCoverageCompat\Filter'); |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + } |
| 212 | + |
| 213 | + \spl_autoload_register(__NAMESPACE__ . '\Autoload::load'); |
| 214 | +} |
0 commit comments