From fcd29aae664e978115c612cd930cc56708b48135 Mon Sep 17 00:00:00 2001 From: rmp-up Date: Thu, 14 Jan 2021 21:54:17 +0100 Subject: [PATCH] v10: assertMatchesRegularExpression forward compatible The assertion ::assertNotRegExp() will be renamed to ::assertMatchesRegularExpression() in v10. We backport ::assertMatchesRegularExpression() and offer a forward-compatibility from v6 on. * marking assertRegExp as deprecated to show up the upcoming breaking change * marking assertNotRegExp as deprecated to show up the upcoming breaking change --- .../v6/Assert/MatchesRegularExpression.php | 43 +++++++++++++++++ lib/Compat/v6/TestCase.php | 44 +++++++++++++++++- .../v7/Assert/MatchesRegularExpression.php | 43 +++++++++++++++++ lib/Compat/v7/TestCase.php | 15 ++++-- lib/Compat/v8/TestCase.php | 25 ++++++++++ lib/Compat/v9/TestCase.php | 46 ++++++++++++++++++- lib/Versions.php | 40 ++++++++++------ .../v10/MatchesRegularExpressionTest.php | 24 ++++++++++ 8 files changed, 260 insertions(+), 20 deletions(-) create mode 100644 lib/Compat/v6/Assert/MatchesRegularExpression.php create mode 100644 lib/Compat/v7/Assert/MatchesRegularExpression.php create mode 100644 opt/doc/TestCase/v10/MatchesRegularExpressionTest.php diff --git a/lib/Compat/v6/Assert/MatchesRegularExpression.php b/lib/Compat/v6/Assert/MatchesRegularExpression.php new file mode 100644 index 0000000..c09edc5 --- /dev/null +++ b/lib/Compat/v6/Assert/MatchesRegularExpression.php @@ -0,0 +1,43 @@ +compatSetUp(); + } + + /** + * @deprecated Please use ::compatSetUpBeforeClass() + */ + public static function setUpBeforeClass() + { + static::compatSetUpBeforeClass(); + } + + /** + * @deprecated Please use ::compatTearDown() + */ + protected function tearDown() + { + $this->compatTearDown(); + } + + /** + * @deprecated Please use ::compatTearDownAfterClass() + */ + public static function tearDownAfterClass() + { + static::compatTearDownAfterClass(); + } } diff --git a/lib/Compat/v7/Assert/MatchesRegularExpression.php b/lib/Compat/v7/Assert/MatchesRegularExpression.php new file mode 100644 index 0000000..8d4c9f7 --- /dev/null +++ b/lib/Compat/v7/Assert/MatchesRegularExpression.php @@ -0,0 +1,43 @@ +compatTearDown(); } + /** + * @deprecated Please use ::compatTearDownAfterClass() + */ public static function tearDownAfterClass() { static::compatTearDownAfterClass(); diff --git a/lib/Compat/v8/TestCase.php b/lib/Compat/v8/TestCase.php index 953078d..90fa160 100644 --- a/lib/Compat/v8/TestCase.php +++ b/lib/Compat/v8/TestCase.php @@ -24,6 +24,7 @@ use PHPUnit\Util\InvalidArgumentHelper; use RmpUp\PHPUnitCompat\Compat\TestCaseTrait; +use RmpUp\PHPUnitCompat\Compat\v7\Assert\MatchesRegularExpression; use RmpUp\PHPUnitCompat\Compat\v8\Assert\ArraySubset; /** @@ -35,23 +36,47 @@ class TestCase extends \PHPUnit\Framework\TestCase { use TestCaseTrait; + /** + * Backward-compatibility + * + * Note: This may become heavily opinionated some day + * and very old methods may be dropped. + * Fingers crossed that we can keep BC up for all of them. + */ use ArraySubset; + /** + * Forward-compatibility + */ + use MatchesRegularExpression; + + /** + * @deprecated Please use ::compatSetUp() instead + */ protected function setUp(): void { $this->compatSetUp(); } + /** + * @deprecated Please use ::compatSetUpBeforeClass() instead. + */ public static function setUpBeforeClass(): void { static::compatSetUpBeforeClass(); } + /** + * @deprecated Please use ::compatTearDown() instead. + */ protected function tearDown(): void { $this->compatTearDown(); } + /** + * @deprecated Please use compatTearDownAfterClass() instead. + */ public static function tearDownAfterClass(): void { static::compatTearDownAfterClass(); diff --git a/lib/Compat/v9/TestCase.php b/lib/Compat/v9/TestCase.php index 034a330..91bbd74 100644 --- a/lib/Compat/v9/TestCase.php +++ b/lib/Compat/v9/TestCase.php @@ -22,13 +22,57 @@ namespace RmpUp\PHPUnitCompat\Compat\v9; +use RmpUp\PHPUnitCompat\Compat\TestCaseTrait; use RmpUp\PHPUnitCompat\Compat\v8\Assert\ArraySubset; /** * TestCase */ -class TestCase extends \RmpUp\PHPUnitCompat\Compat\v8\TestCase +class TestCase extends \PHPUnit\Framework\TestCase { + use TestCaseTrait; + + /** + * Backward-compatibility + * + * Note: This may become heavily opinionated some day + * and very old methods may be dropped. + * Fingers crossed that we can keep BC up for all of them. + */ + use ArraySubset; + + /** + * @deprecated Please use ::compatSetUp() instead + */ + protected function setUp(): void + { + $this->compatSetUp(); + } + + /** + * @deprecated Please use ::compatSetUpBeforeClass() instead. + */ + public static function setUpBeforeClass(): void + { + static::compatSetUpBeforeClass(); + } + + /** + * @deprecated Please use ::compatTearDown() instead. + */ + protected function tearDown(): void + { + $this->compatTearDown(); + } + + /** + * @deprecated Please use compatTearDownAfterClass() instead. + */ + public static function tearDownAfterClass(): void + { + static::compatTearDownAfterClass(); + } + /** * @deprecated Use expectExceptionMessageMatches() instead */ diff --git a/lib/Versions.php b/lib/Versions.php index 8ab2012..2238972 100644 --- a/lib/Versions.php +++ b/lib/Versions.php @@ -29,24 +29,34 @@ */ class Versions { + private static $version; + + /** + * List of classes last seen in the mapped PHPUnit version + * + * Those classes are all deprecated. + * Their last occurrence tells us something about the PHPUnit version. + * + * @var int[] + */ + private static $classToVersion = [ + \PHPUnit\Framework\BaseTestListener::class => 6, + \PHPUnit\Util\TestDox\TestResult::class => 7, + \PHPUnit\Util\Configuration::class => 8, + \PHPUnit\Util\Blacklist::class => 9 + ]; + public static function getPhpUnitVersion(): int { - if (class_exists(\PHPUnit\Framework\BaseTestListener::class)) { - return 6; - } - - if (class_exists(\PHPUnit\Util\TestDox\TestResult::class)) { - return 7; - } - - if (class_exists(\PHPUnit\Util\Configuration::class)) { - return 8; - } - - if (class_exists(\PHPUnit\Util\Blacklist::class)) { - return 9; + if (null === self::$version) { + foreach (self::$classToVersion as $className => $phpUnitVersion) { + if (class_exists($className)) { + self::$version = $phpUnitVersion; + break; + } + } } - return 0; + return self::$version; } } diff --git a/opt/doc/TestCase/v10/MatchesRegularExpressionTest.php b/opt/doc/TestCase/v10/MatchesRegularExpressionTest.php new file mode 100644 index 0000000..5688802 --- /dev/null +++ b/opt/doc/TestCase/v10/MatchesRegularExpressionTest.php @@ -0,0 +1,24 @@ +