From 99d2c5eccec2f86db60598dbd6bf3825bc51876d Mon Sep 17 00:00:00 2001 From: Artem Lopata Date: Fri, 10 Jul 2020 15:01:09 +0200 Subject: [PATCH] Support for new PHPUnit 9 value object configuration --- .travis.yml | 10 ++- .../features/bootstrap/FeatureContext.php | 2 +- .../features/bootstrap/FeatureContext.php | 2 +- phpstan.neon | 21 +++++- src/Queue/CreateTestsQueueFromPhpUnitXML.php | 69 +++++++++++++++++++ tests/Process/ProcessesManagerTest.php | 3 +- 6 files changed, 99 insertions(+), 8 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9576dfd..ba2e162 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,16 +6,20 @@ php: env: matrix: - - PREFER_LOWEST="--prefer-lowest" - - PREFER_LOWEST="" + - PREFER_LOWEST="--prefer-lowest" PHPUNIT_VERSION="^8.2" + - PREFER_LOWEST="" PHPUNIT_VERSION="^8.2" + - PREFER_LOWEST="" PHPUNIT_VERSION="~9.0" + - PREFER_LOWEST="" PHPUNIT_VERSION="~9.3" + - PREFER_LOWEST="" PHPUNIT_VERSION="~9.5" cache: directories: - $HOME/.composer/cache before_script: + - phpenv config-rm xdebug.ini - composer selfupdate - - composer require "phpunit/phpunit:^8.2" --no-update; + - composer require "phpunit/phpunit:$PHPUNIT_VERSION" --no-update; - composer update $PREFER_LOWEST script: diff --git a/adapters/Behat/features/bootstrap/FeatureContext.php b/adapters/Behat/features/bootstrap/FeatureContext.php index 070c0d9..5f8cbbc 100644 --- a/adapters/Behat/features/bootstrap/FeatureContext.php +++ b/adapters/Behat/features/bootstrap/FeatureContext.php @@ -55,7 +55,7 @@ public function theConsoleOutputShouldHaveLinesEndingIn(PyStringNode $string) */ public function theConsoleOutputShouldContain($string) { - \PHPUnit\Framework\Assert::assertContains($string, $this->lastBehatStdOut); + \PHPUnit\Framework\Assert::assertStringContainsString($string, $this->lastBehatStdOut); } /** diff --git a/adapters/Behat2/features/bootstrap/FeatureContext.php b/adapters/Behat2/features/bootstrap/FeatureContext.php index fd90a1b..c25c493 100644 --- a/adapters/Behat2/features/bootstrap/FeatureContext.php +++ b/adapters/Behat2/features/bootstrap/FeatureContext.php @@ -66,7 +66,7 @@ public function theConsoleOutputShouldHaveLinesEndingIn(PyStringNode $string) */ public function theConsoleOutputShouldContain($string) { - \PHPUnit\Framework\Assert::assertContains($string, $this->lastBehatStdOut); + \PHPUnit\Framework\Assert::assertStringContainsString($string, $this->lastBehatStdOut); } /** diff --git a/phpstan.neon b/phpstan.neon index 71b898c..501b3d1 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -15,4 +15,23 @@ parameters: - phpstan-stub/Symfony/Component/Process/Exception/LogicException.stub reportUnmatchedIgnoredErrors: false ignoreErrors: #see https://github.com/phpstan/phpstan/issues/1267#issuecomment-552874947 -> @todo remove when sf 3.4 support ends - - "#^Call to an undefined static method #" \ No newline at end of file + - "#^Call to an undefined static method #" + # @todo remove when phpunit 8.5 support ends + - message: '#^Call to static method getInstance\(\) on an unknown class PHPUnit\\TextUI\\(:?|Xml)Configuration\\Registry.$#' + path: src/Queue/CreateTestsQueueFromPhpUnitXML.php + - message: '#has invalid typehint type PHPUnit\\TextUI\\(:?|Xml)Configuration\\TestSuiteCollection.$#' + path: src/Queue/CreateTestsQueueFromPhpUnitXML.php + - message: '#^Call to method map\(\) on an unknown class PHPUnit\\TextUI\\(:?|Xml)Configuration\\TestSuiteMapper.$#' + path: src/Queue/CreateTestsQueueFromPhpUnitXML.php + - message: '#has invalid typehint type PHPUnit\\TextUI\\(:?|Xml)Configuration\\PHPUnit.$#' + path: src/Queue/CreateTestsQueueFromPhpUnitXML.php + - message: '#on an unknown class PHPUnit\\TextUI\\(:?|Xml)Configuration\\PHPUnit.$#' + path: src/Queue/CreateTestsQueueFromPhpUnitXML.php + - message: '#^Instantiated class PHPUnit\\TextUI\\(:?|Xml)Configuration\\TestSuiteMapper not found.$#' + path: src/Queue/CreateTestsQueueFromPhpUnitXML.php + - message: '#^Call to method load\(\) on an unknown class PHPUnit\\TextUI\\XmlConfiguration\\Loader.$#' + path: src/Queue/CreateTestsQueueFromPhpUnitXML.php + - message: '#^Instantiated class PHPUnit\\TextUI\\XmlConfiguration\\Loader not found.$#' + path: src/Queue/CreateTestsQueueFromPhpUnitXML.php + - message: '#^Call to static method getInstance\(\) on an unknown class PHPUnit\\Util\\Configuration.$#' + path: src/Queue/CreateTestsQueueFromPhpUnitXML.php \ No newline at end of file diff --git a/src/Queue/CreateTestsQueueFromPhpUnitXML.php b/src/Queue/CreateTestsQueueFromPhpUnitXML.php index 8ee2778..51e15a4 100644 --- a/src/Queue/CreateTestsQueueFromPhpUnitXML.php +++ b/src/Queue/CreateTestsQueueFromPhpUnitXML.php @@ -6,6 +6,30 @@ class CreateTestsQueueFromPhpUnitXML { public static function execute(string $xmlFile): TestsQueue { + // phpunit 9.0 compatibility + if (class_exists('\PHPUnit\TextUI\Configuration\Configuration')) { + $configuration = \PHPUnit\TextUI\Configuration\Registry::getInstance()->get($xmlFile); + + $testSuites = new TestsQueue(); + + self::handlePhpUnitBootstrapV90($configuration->phpunit()); + self::processTestSuiteCollectionV90($testSuites, $configuration->testSuite()); + + return $testSuites; + } + + // phpunit 9.3 compatibility + if (class_exists('\PHPUnit\TextUI\XmlConfiguration\Configuration')) { + $configuration = (new \PHPUnit\TextUI\XmlConfiguration\Loader)->load($xmlFile); + + $testSuites = new TestsQueue(); + + self::handlePhpUnitBootstrapV93($configuration->phpunit()); + self::processTestSuiteCollectionV93($testSuites, $configuration->testSuite()); + + return $testSuites; + } + $configuration = \PHPUnit\Util\Configuration::getInstance($xmlFile); $testSuites = new TestsQueue(); @@ -34,6 +58,27 @@ private static function processTestSuite( } } + private static function processTestSuiteCollectionV90( + TestsQueue $testSuites, + \PHPUnit\TextUI\Configuration\TestSuiteCollection $testSuiteCollection + ): void { + $testSuite = (new \PHPUnit\TextUI\Configuration\TestSuiteMapper)->map($testSuiteCollection, ''); + self::processTestSuite($testSuites, $testSuite->getIterator()); + } + + private static function processTestSuiteCollectionV93( + TestsQueue $testSuites, + \PHPUnit\TextUI\XmlConfiguration\TestSuiteCollection $testSuiteCollection + ): void { + // phpunit 9.5 compatibility + if (class_exists('\PHPUnit\TextUI\TestSuiteMapper')) { + $testSuite = (new \PHPUnit\TextUI\TestSuiteMapper)->map($testSuiteCollection, ''); + } else { + $testSuite = (new \PHPUnit\TextUI\XmlConfiguration\TestSuiteMapper)->map($testSuiteCollection, ''); + } + self::processTestSuite($testSuites, $testSuite->getIterator()); + } + /** * @param TestsQueue $testSuites * @param \PHPUnit\Framework\TestSuite<\PHPUnit\Framework\Test>|\PHPUnit\Framework\TestCase $testSuite @@ -65,4 +110,28 @@ private static function handleBootstrap(array $config): void \PHPUnit\Util\FileLoader::checkAndLoad($filename); } + + /** + * Loads a bootstrap file. + * + * @param \PHPUnit\TextUI\Configuration\PHPUnit $PHPUnit The Phpunit config + */ + private static function handlePhpUnitBootstrapV90(\PHPUnit\TextUI\Configuration\PHPUnit $PHPUnit): void + { + $filename = $PHPUnit->hasBootstrap() ? $PHPUnit->bootstrap() : 'vendor/autoload.php'; + + \PHPUnit\Util\FileLoader::checkAndLoad($filename); + } + + /** + * Loads a bootstrap file. + * + * @param \PHPUnit\TextUI\XmlConfiguration\PHPUnit $PHPUnit The Phpunit config + */ + private static function handlePhpUnitBootstrapV93(\PHPUnit\TextUI\XmlConfiguration\PHPUnit $PHPUnit): void + { + $filename = $PHPUnit->hasBootstrap() ? $PHPUnit->bootstrap() : 'vendor/autoload.php'; + + \PHPUnit\Util\FileLoader::checkAndLoad($filename); + } } diff --git a/tests/Process/ProcessesManagerTest.php b/tests/Process/ProcessesManagerTest.php index f4a4eab..8810b94 100644 --- a/tests/Process/ProcessesManagerTest.php +++ b/tests/Process/ProcessesManagerTest.php @@ -84,8 +84,7 @@ public function shouldCreate6ProcessesGivingThemTheCorrectEnvParameters(): void ->method('getIndexesOfCompletedChannel') ->willReturn(range(1, 3)); $processes->expects($this->any()) - ->method('add') - ->willReturn(true); + ->method('add'); $processes->expects($this->any()) ->method('start') ->willReturn(true);