diff --git a/CHANGELOG.md b/CHANGELOG.md index 7548917..b9a5d90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ... ### Changed -... +- Switched from PSR-4 to fully custom-built autoloader to support static code analysis tools. ### Fixed ... diff --git a/composer.json b/composer.json index 630f445..218e347 100644 --- a/composer.json +++ b/composer.json @@ -15,8 +15,10 @@ "aik099/coding-standard": "dev-master" }, "autoload": { + "files": [ + "phpunitcompat_autoloader.php" + ], "psr-4": { - "ConsoleHelpers\\PHPUnitCompat\\": "src/PHPUnitCompat/", "Tests\\ConsoleHelpers\\PHPUnitCompat\\": "tests/PHPUnitCompat/" } }, diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..fb3aff2 --- /dev/null +++ b/composer.lock @@ -0,0 +1,69 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "96284505c8924fa4a4b005dc03d5bbc6", + "packages": [], + "packages-dev": [ + { + "name": "aik099/coding-standard", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/aik099/CodingStandard.git", + "reference": "7f80da035830935c5bc1a44eb76302393ab70f0a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/aik099/CodingStandard/zipball/7f80da035830935c5bc1a44eb76302393ab70f0a", + "reference": "7f80da035830935c5bc1a44eb76302393ab70f0a", + "shasum": "" + }, + "require-dev": { + "squizlabs/php_codesniffer": "^3.0", + "yoast/phpunit-polyfills": "^1.0" + }, + "default-branch": true, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0.x-dev" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Alexander Obuhovich", + "email": "aik.bold@gmail.com" + } + ], + "description": "The PHP_CodeSniffer coding standard I'm using on all of my projects.", + "keywords": [ + "PHP_CodeSniffer", + "codesniffer" + ], + "support": { + "issues": "https://github.com/aik099/CodingStandard/issues", + "source": "https://github.com/aik099/CodingStandard/tree/master" + }, + "time": "2021-11-07T16:55:51+00:00" + } + ], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": { + "aik099/coding-standard": 20 + }, + "prefer-stable": false, + "prefer-lowest": false, + "platform": { + "php": ">=5.6.0" + }, + "platform-dev": [], + "plugin-api-version": "2.2.0" +} diff --git a/phpunitcompat_autoloader.php b/phpunitcompat_autoloader.php new file mode 100644 index 0000000..73896d2 --- /dev/null +++ b/phpunitcompat_autoloader.php @@ -0,0 +1,214 @@ + + * @link https://github.com/aik099/phpunit-mink + */ + +namespace ConsoleHelpers\PHPUnitCompat; + + +use PHPUnit\Runner\Version; + +if ( \class_exists('ConsoleHelpers\PHPUnitCompat\Autoload', false) === false ) { + + /** + * Custom autoloader. + */ + final class Autoload + { + + /** + * PHPUnit version. + * + * @var string + */ + protected static $phpUnitVersion; + + /** + * Loads a class. + * + * @param string $class_name The name of the class to load. + * + * @return boolean + */ + public static function load($class_name) + { + // Only load classes belonging to this library. + if ( \stripos($class_name, 'ConsoleHelpers\PHPUnitCompat') !== 0 ) { + return false; + } + + switch ( $class_name ) { + case 'ConsoleHelpers\PHPUnitCompat\AbstractTestCase': + self::defineAliases(); + + $phpunit_version = self::getPhpUnitVersion(); + + if ( version_compare($phpunit_version, '5.0.0', '<') ) { + require_once __DIR__ . '/src/PHPUnitCompat/AbstractTestCase4.php'; + } + elseif ( version_compare($phpunit_version, '6.0.0', '<') ) { + require_once __DIR__ . '/src/PHPUnitCompat/AbstractTestCase5.php'; + } + elseif ( version_compare($phpunit_version, '7.0.0', '<') ) { + require_once __DIR__ . '/src/PHPUnitCompat/AbstractTestCase6.php'; + } + elseif ( version_compare($phpunit_version, '8.0.0', '<') ) { + require_once __DIR__ . '/src/PHPUnitCompat/AbstractTestCase7.php'; + } + else { + require_once __DIR__ . '/src/PHPUnitCompat/AbstractTestCase8.php'; + } + + return true; + + case 'ConsoleHelpers\PHPUnitCompat\AbstractTestSuite': + $phpunit_version = self::getPhpUnitVersion(); + + if ( version_compare($phpunit_version, '5.0.0', '<') ) { + require_once __DIR__ . '/src/PHPUnitCompat/AbstractTestSuite4.php'; + } + elseif ( version_compare($phpunit_version, '6.0.0', '<') ) { + require_once __DIR__ . '/src/PHPUnitCompat/AbstractTestSuite5.php'; + } + elseif ( version_compare($phpunit_version, '7.0.0', '<') ) { + require_once __DIR__ . '/src/PHPUnitCompat/AbstractTestSuite6.php'; + } + elseif ( version_compare($phpunit_version, '8.0.0', '<') ) { + require_once __DIR__ . '/src/PHPUnitCompat/AbstractTestSuite7.php'; + } + else { + require_once __DIR__ . '/src/PHPUnitCompat/AbstractTestSuite8.php'; + } + + return true; + + /* + * Handles: + * - ConsoleHelpers\PHPUnitCompat\TAbstractTestCaseBody + * - ConsoleHelpers\PHPUnitCompat\TAbstractTestSuiteBody + */ + default: + $file = \realpath(__DIR__ . '/src/' . \strtr(\substr($class_name, 15), '\\', '/') . '.php'); + + if ( \is_string($file) && \file_exists($file) === true ) { + require_once $file; + + return true; + } + } + + return false; + } + + /** + * Get PHPUnit version. + * + * @return string + */ + protected static function getPhpUnitVersion() + { + if ( self::$phpUnitVersion === null ) { + if ( \class_exists('PHPUnit\Runner\Version') ) { + self::$phpUnitVersion = Version::id(); + } + else { + self::$phpUnitVersion = \PHPUnit_Runner_Version::id(); + } + } + + return self::$phpUnitVersion; + } + + /** + * Defines aliases. + * + * @return void + */ + protected static function defineAliases() + { + // PHPUnit Compat. + if ( \class_exists('\PHPUnit_Framework_IncompleteTestError') ) { + \class_alias( + '\PHPUnit_Framework_IncompleteTestError', + '\ConsoleHelpers\PHPUnitCompat\Framework\IncompleteTestError' + ); + } + else { + \class_alias( + '\PHPUnit\Framework\IncompleteTestError', + '\ConsoleHelpers\PHPUnitCompat\Framework\IncompleteTestError' + ); + } + + if ( class_exists('\PHPUnit_Framework_SkippedTestError') ) { + \class_alias('\PHPUnit_Framework_SkippedTestError', '\ConsoleHelpers\PHPUnitCompat\Framework\SkippedTestError'); + } + else { + \class_alias('\PHPUnit\Framework\SkippedTestError', '\ConsoleHelpers\PHPUnitCompat\Framework\SkippedTestError'); + } + + if ( class_exists('\PHPUnit_Framework_TestSuite_DataProvider') ) { + \class_alias( + '\PHPUnit_Framework_TestSuite_DataProvider', + '\ConsoleHelpers\PHPUnitCompat\Framework\DataProviderTestSuite' + ); + } + else { + \class_alias( + '\PHPUnit\Framework\DataProviderTestSuite', + '\ConsoleHelpers\PHPUnitCompat\Framework\DataProviderTestSuite' + ); + } + + if ( class_exists('\PHPUnit_Framework_TestResult') ) { + \class_alias('\PHPUnit_Framework_TestResult', '\ConsoleHelpers\PHPUnitCompat\Framework\TestResult'); + } + else { + \class_alias('\PHPUnit\Framework\TestResult', '\ConsoleHelpers\PHPUnitCompat\Framework\TestResult'); + } + + if ( class_exists('\PHPUnit_Framework_Test') ) { + \class_alias('\PHPUnit_Framework_Test', '\ConsoleHelpers\PHPUnitCompat\Framework\Test'); + } + else { + \class_alias('\PHPUnit\Framework\Test', '\ConsoleHelpers\PHPUnitCompat\Framework\Test'); + } + + // CodeCoverage Compat. + if ( class_exists('\PHP_CodeCoverage') ) { + \class_alias('\PHP_CodeCoverage', '\ConsoleHelpers\CodeCoverageCompat\CodeCoverage'); + } + else { + \class_alias( + '\SebastianBergmann\CodeCoverage\CodeCoverage', + '\ConsoleHelpers\CodeCoverageCompat\CodeCoverage' + ); + } + + if ( \interface_exists('\PHP_CodeCoverage_Driver') ) { + \class_alias('\PHP_CodeCoverage_Driver', '\ConsoleHelpers\CodeCoverageCompat\Driver\Driver'); + } + else { + \class_alias( + '\SebastianBergmann\CodeCoverage\Driver\Driver', + '\ConsoleHelpers\CodeCoverageCompat\Driver\Driver' + ); + } + + if ( class_exists('\PHP_CodeCoverage_Filter') ) { + \class_alias('\PHP_CodeCoverage_Filter', '\ConsoleHelpers\CodeCoverageCompat\Filter'); + } + else { + \class_alias('\SebastianBergmann\CodeCoverage\Filter', '\ConsoleHelpers\CodeCoverageCompat\Filter'); + } + } + + } + + \spl_autoload_register(__NAMESPACE__ . '\Autoload::load'); +} diff --git a/src/PHPUnitCompat/AbstractTestCase.php b/src/PHPUnitCompat/AbstractTestCase.php deleted file mode 100644 index 195d847..0000000 --- a/src/PHPUnitCompat/AbstractTestCase.php +++ /dev/null @@ -1,133 +0,0 @@ - - * @link https://github.com/aik099/phpunit-mink - */ - -namespace ConsoleHelpers\PHPUnitCompat; - - -use PHPUnit\Runner\Version; - -// PHPUnit Compat. -if ( \class_exists('\PHPUnit_Framework_IncompleteTestError') ) { - \class_alias( - '\PHPUnit_Framework_IncompleteTestError', - '\ConsoleHelpers\PHPUnitCompat\Framework\IncompleteTestError' - ); -} -else { - \class_alias( - '\PHPUnit\Framework\IncompleteTestError', - '\ConsoleHelpers\PHPUnitCompat\Framework\IncompleteTestError' - ); -} - -if ( class_exists('\PHPUnit_Framework_SkippedTestError') ) { - \class_alias('\PHPUnit_Framework_SkippedTestError', '\ConsoleHelpers\PHPUnitCompat\Framework\SkippedTestError'); -} -else { - \class_alias('\PHPUnit\Framework\SkippedTestError', '\ConsoleHelpers\PHPUnitCompat\Framework\SkippedTestError'); -} - -if ( class_exists('\PHPUnit_Framework_TestSuite_DataProvider') ) { - \class_alias( - '\PHPUnit_Framework_TestSuite_DataProvider', - '\ConsoleHelpers\PHPUnitCompat\Framework\DataProviderTestSuite' - ); -} -else { - \class_alias( - '\PHPUnit\Framework\DataProviderTestSuite', - '\ConsoleHelpers\PHPUnitCompat\Framework\DataProviderTestSuite' - ); -} - -if ( class_exists('\PHPUnit_Framework_TestResult') ) { - \class_alias('\PHPUnit_Framework_TestResult', '\ConsoleHelpers\PHPUnitCompat\Framework\TestResult'); -} -else { - \class_alias('\PHPUnit\Framework\TestResult', '\ConsoleHelpers\PHPUnitCompat\Framework\TestResult'); -} - -if ( class_exists('\PHPUnit_Framework_Test') ) { - \class_alias('\PHPUnit_Framework_Test', '\ConsoleHelpers\PHPUnitCompat\Framework\Test'); -} -else { - \class_alias('\PHPUnit\Framework\Test', '\ConsoleHelpers\PHPUnitCompat\Framework\Test'); -} - -if ( !\defined('PHPUNIT_COMPAT_RUNNER_VERSION') ) { - if ( \class_exists('PHPUnit\Runner\Version') ) { - \define('PHPUNIT_COMPAT_RUNNER_VERSION', Version::id()); - } - else { - \define('PHPUNIT_COMPAT_RUNNER_VERSION', \PHPUnit_Runner_Version::id()); - } -} - -// CodeCoverage Compat. -if ( class_exists('\PHP_CodeCoverage') ) { - \class_alias('\PHP_CodeCoverage', '\ConsoleHelpers\CodeCoverageCompat\CodeCoverage'); -} -else { - \class_alias( - '\SebastianBergmann\CodeCoverage\CodeCoverage', - '\ConsoleHelpers\CodeCoverageCompat\CodeCoverage' - ); -} - -if ( \interface_exists('\PHP_CodeCoverage_Driver') ) { - \class_alias('\PHP_CodeCoverage_Driver', '\ConsoleHelpers\CodeCoverageCompat\Driver\Driver'); -} -else { - \class_alias( - '\SebastianBergmann\CodeCoverage\Driver\Driver', - '\ConsoleHelpers\CodeCoverageCompat\Driver\Driver' - ); -} - -if ( class_exists('\PHP_CodeCoverage_Filter') ) { - \class_alias('\PHP_CodeCoverage_Filter', '\ConsoleHelpers\CodeCoverageCompat\Filter'); -} -else { - \class_alias('\SebastianBergmann\CodeCoverage\Filter', '\ConsoleHelpers\CodeCoverageCompat\Filter'); -} - - -trait TAbstractTestCaseBody -{ - - /** - * This method is called when a test method did not execute successfully. - * - * @param \Exception|\Throwable $e Exception. - * - * @return void - */ - protected function onNotSuccessfulTestCompat($e) - { - - } - -} - -if ( version_compare(\PHPUNIT_COMPAT_RUNNER_VERSION, '5.0.0', '<') ) { - require_once __DIR__ . '/AbstractTestCase4.php'; -} -elseif ( version_compare(\PHPUNIT_COMPAT_RUNNER_VERSION, '6.0.0', '<') ) { - require_once __DIR__ . '/AbstractTestCase5.php'; -} -elseif ( version_compare(\PHPUNIT_COMPAT_RUNNER_VERSION, '7.0.0', '<') ) { - require_once __DIR__ . '/AbstractTestCase6.php'; -} -elseif ( version_compare(\PHPUNIT_COMPAT_RUNNER_VERSION, '8.0.0', '<') ) { - require_once __DIR__ . '/AbstractTestCase7.php'; -} -else { - require_once __DIR__ . '/AbstractTestCase8.php'; -} diff --git a/src/PHPUnitCompat/AbstractTestSuite.php b/src/PHPUnitCompat/AbstractTestSuite.php deleted file mode 100644 index 9e50a97..0000000 --- a/src/PHPUnitCompat/AbstractTestSuite.php +++ /dev/null @@ -1,67 +0,0 @@ - - * @link https://github.com/aik099/phpunit-mink - */ - -namespace ConsoleHelpers\PHPUnitCompat; - - -use PHPUnit\Framework\TestResult; -use PHPUnit\Runner\Version; - -if ( !\defined('PHPUNIT_COMPAT_RUNNER_VERSION') ) { - if ( \class_exists('PHPUnit\Runner\Version') ) { - \define('PHPUNIT_COMPAT_RUNNER_VERSION', Version::id()); - } - else { - \define('PHPUNIT_COMPAT_RUNNER_VERSION', \PHPUnit_Runner_Version::id()); - } -} - - -trait TAbstractTestSuiteBody -{ - - /** - * Runs the tests and collects their result in a TestResult. - * - * @param TestResult $result Test result. - * - * @return TestResult - */ - public function runCompat($result = null) - { - return parent::run($result); - } - - /** - * Template Method that is called after the tests - * of this test suite have finished running. - */ - protected function tearDownCompat() - { - - } - -} - -if ( version_compare(\PHPUNIT_COMPAT_RUNNER_VERSION, '5.0.0', '<') ) { - require_once __DIR__ . '/AbstractTestSuite4.php'; -} -elseif ( version_compare(\PHPUNIT_COMPAT_RUNNER_VERSION, '6.0.0', '<') ) { - require_once __DIR__ . '/AbstractTestSuite5.php'; -} -elseif ( version_compare(\PHPUNIT_COMPAT_RUNNER_VERSION, '7.0.0', '<') ) { - require_once __DIR__ . '/AbstractTestSuite6.php'; -} -elseif ( version_compare(\PHPUNIT_COMPAT_RUNNER_VERSION, '8.0.0', '<') ) { - require_once __DIR__ . '/AbstractTestSuite7.php'; -} -else { - require_once __DIR__ . '/AbstractTestSuite8.php'; -} diff --git a/src/PHPUnitCompat/TAbstractTestCaseBody.php b/src/PHPUnitCompat/TAbstractTestCaseBody.php new file mode 100644 index 0000000..751c009 --- /dev/null +++ b/src/PHPUnitCompat/TAbstractTestCaseBody.php @@ -0,0 +1,29 @@ + + * @link https://github.com/aik099/phpunit-mink + */ + +namespace ConsoleHelpers\PHPUnitCompat; + + +trait TAbstractTestCaseBody +{ + + /** + * This method is called when a test method did not execute successfully. + * + * @param \Exception|\Throwable $e Exception. + * + * @return void + */ + protected function onNotSuccessfulTestCompat($e) + { + + } + +} diff --git a/src/PHPUnitCompat/TAbstractTestSuiteBody.php b/src/PHPUnitCompat/TAbstractTestSuiteBody.php new file mode 100644 index 0000000..1f654ac --- /dev/null +++ b/src/PHPUnitCompat/TAbstractTestSuiteBody.php @@ -0,0 +1,40 @@ + + * @link https://github.com/aik099/phpunit-mink + */ + +namespace ConsoleHelpers\PHPUnitCompat; + + +use PHPUnit\Framework\TestResult; + +trait TAbstractTestSuiteBody +{ + + /** + * Runs the tests and collects their result in a TestResult. + * + * @param TestResult $result Test result. + * + * @return TestResult + */ + public function runCompat($result = null) + { + return parent::run($result); + } + + /** + * Template Method that is called after the tests + * of this test suite have finished running. + */ + protected function tearDownCompat() + { + + } + +} diff --git a/tests/PHPUnitCompat/TestCaseOverridingTraitMethodTest.php b/tests/PHPUnitCompat/TestCaseOverridingTraitMethodTest.php index bb6361b..28dc470 100644 --- a/tests/PHPUnitCompat/TestCaseOverridingTraitMethodTest.php +++ b/tests/PHPUnitCompat/TestCaseOverridingTraitMethodTest.php @@ -12,17 +12,32 @@ use ConsoleHelpers\PHPUnitCompat\AbstractTestCase; +use PHPUnit\Runner\Version; class TestCaseOverridingTraitMethodTest extends AbstractTestCase { public function testPassing() { - echo '[runner version: ' . \PHPUNIT_COMPAT_RUNNER_VERSION . ']'; + echo '[runner version: ' . $this->getPhpUnitVersion() . ']'; $this->assertTrue(true); } + /** + * Get PHPUnit version. + * + * @return string + */ + protected function getPhpUnitVersion() + { + if ( \class_exists('PHPUnit\Runner\Version') ) { + return Version::id(); + } + + return \PHPUnit_Runner_Version::id(); + } + public function testFailing() { $this->assertFalse(