From fa196c596e22421004db3d77d63f6af3f58dee39 Mon Sep 17 00:00:00 2001 From: Michel Valdrighi Date: Fri, 15 Nov 2019 14:16:42 +0100 Subject: [PATCH] add support for the @group annotation at the class level --- phpunit.xml.dist | 1 + src/Queue/CreateTestsQueueFromPhpUnitXML.php | 52 ++++++++++++++++++- .../CreateTestsQueueFromPhpUnitXMLTest.php | 9 ++++ tests/Queue/Fixture/phpunit.xml.dist | 13 +++++ .../Queue/Fixture/tests/DummyExcludedTest.php | 14 +++++ .../Queue/Fixture/tests/DummyIncludedTest.php | 14 +++++ .../ReadFromInputAndPushIntoTheQueueTest.php | 9 ++++ 7 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 tests/Queue/Fixture/tests/DummyExcludedTest.php create mode 100644 tests/Queue/Fixture/tests/DummyIncludedTest.php diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 0d2ac83..b59fab8 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -6,6 +6,7 @@ tests/Queue/ tests/Process/ tests/Environment/ + tests/Queue/Fixture/ diff --git a/src/Queue/CreateTestsQueueFromPhpUnitXML.php b/src/Queue/CreateTestsQueueFromPhpUnitXML.php index c570262..3077da0 100644 --- a/src/Queue/CreateTestsQueueFromPhpUnitXML.php +++ b/src/Queue/CreateTestsQueueFromPhpUnitXML.php @@ -19,6 +19,18 @@ class_alias('\PHPUnit_Util_TestSuiteIterator', '\PHPUnit\Framework\TestSuiteIter class_alias('\PHPUnit_Util_Fileloader', '\PHPUnit\Util\Fileloader'); } +if (class_exists('\PHPUnit_Runner_Filter_Factory')) { + class_alias('\PHPUnit_Runner_Filter_Factory', '\PHPUnit\Runner\Filter\Factory'); +} + +if (class_exists('\PHPUnit_Runner_Filter_ExcludeGroupFilterIterator')) { + class_alias('\PHPUnit_Runner_Filter_ExcludeGroupFilterIterator', '\PHPUnit\Runner\Filter\ExcludeGroupFilterIterator'); +} + +if (class_exists('\PHPUnit_Runner_Filter_IncludeGroupFilterIterator')) { + class_alias('\PHPUnit_Runner_Filter_IncludeGroupFilterIterator', '\PHPUnit\Runner\Filter\IncludeGroupFilterIterator'); +} + /* * Trigger autoload for possible file loader versions. * This fixes the problem with PHP classes being case insensitive versus composer case sensitive autoloader. @@ -34,16 +46,52 @@ public static function execute($xmlFile) $testSuites = new TestsQueue(); self::handleBootstrap($configuration->getPHPUnitConfiguration()); - self::processTestSuite($testSuites, $configuration->getTestSuiteConfiguration()->getIterator()); + $testSuite = static::filterTestSuite($configuration); + self::processTestSuite($testSuites, $testSuite->getIterator()); return $testSuites; } + private static function filterTestSuite( + \PHPUnit\Util\Configuration $configuration + ) { + $testSuite = $configuration->getTestSuiteConfiguration(); + $groupConfiguration = $configuration->getGroupConfiguration(); + + if (empty($groupConfiguration['exclude']) && empty($groupConfiguration['include'])) { + return $testSuite; + } + + $filterFactory = new \PHPUnit\Runner\Filter\Factory(); + + if (!empty($groupConfiguration['exclude'])) { + $filterFactory->addFilter( + new \ReflectionClass(\PHPUnit\Runner\Filter\ExcludeGroupFilterIterator::class), + $groupConfiguration['exclude'] + ); + } + + if (!empty($groupConfiguration['include'])) { + $filterFactory->addFilter( + new \ReflectionClass(\PHPUnit\Runner\Filter\IncludeGroupFilterIterator::class), + $groupConfiguration['include'] + ); + } + + $testSuite->injectFilter($filterFactory); + + return $testSuite; + } + private static function processTestSuite( TestsQueue $testSuites, - \PHPUnit\Framework\TestSuiteIterator $testSuiteIterator + \RecursiveIterator $testSuiteIterator ) { foreach ($testSuiteIterator as $testSuite) { + if ($testSuite->count() === 0) { + continue; + } + self::addTestFile($testSuites, $testSuite); if ($testSuite instanceof \PHPUnit\Framework\TestSuite) { diff --git a/tests/Queue/CreateTestsQueueFromPhpUnitXMLTest.php b/tests/Queue/CreateTestsQueueFromPhpUnitXMLTest.php index fec9f13..7f5728b 100644 --- a/tests/Queue/CreateTestsQueueFromPhpUnitXMLTest.php +++ b/tests/Queue/CreateTestsQueueFromPhpUnitXMLTest.php @@ -25,6 +25,11 @@ public function shouldCreateAnArrayOfTestSuitesFromXML() 'ProcessorCounterTest.php', ]; + $dummyDir = __DIR__.'/Fixture/tests/'; + $dummyFiles = [ + 'DummyIncludedTest.php', + ]; + $queue = new TestsQueue(); foreach ($infrastructureFiles as $file) { @@ -35,6 +40,10 @@ public function shouldCreateAnArrayOfTestSuitesFromXML() $queue->add($processesDir.$file); } + foreach ($dummyFiles as $file) { + $queue->add($dummyDir.$file); + } + $this->assertEquals($queue, $output); } } diff --git a/tests/Queue/Fixture/phpunit.xml.dist b/tests/Queue/Fixture/phpunit.xml.dist index 23bb7fb..5169a90 100644 --- a/tests/Queue/Fixture/phpunit.xml.dist +++ b/tests/Queue/Fixture/phpunit.xml.dist @@ -8,5 +8,18 @@ ../../Process/ + + ./tests/ + + + + + default + dummy-included + + + dummy-excluded + + diff --git a/tests/Queue/Fixture/tests/DummyExcludedTest.php b/tests/Queue/Fixture/tests/DummyExcludedTest.php new file mode 100644 index 0000000..c4145f6 --- /dev/null +++ b/tests/Queue/Fixture/tests/DummyExcludedTest.php @@ -0,0 +1,14 @@ +assertTrue(true); + } +} diff --git a/tests/Queue/Fixture/tests/DummyIncludedTest.php b/tests/Queue/Fixture/tests/DummyIncludedTest.php new file mode 100644 index 0000000..1976fcf --- /dev/null +++ b/tests/Queue/Fixture/tests/DummyIncludedTest.php @@ -0,0 +1,14 @@ +assertTrue(true); + } +} diff --git a/tests/Queue/ReadFromInputAndPushIntoTheQueueTest.php b/tests/Queue/ReadFromInputAndPushIntoTheQueueTest.php index baa1936..796717d 100644 --- a/tests/Queue/ReadFromInputAndPushIntoTheQueueTest.php +++ b/tests/Queue/ReadFromInputAndPushIntoTheQueueTest.php @@ -20,6 +20,11 @@ public function shouldPushIntoTheQueueTheXMLFile() 'ProcessorCounterTest.php', ]; + $dummyDir = __DIR__ . '/Fixture/tests/'; + $dummyFiles = [ + 'DummyIncludedTest.php', + ]; + $assertion = new TestsQueue(); foreach ($files as $file) { @@ -30,6 +35,10 @@ public function shouldPushIntoTheQueueTheXMLFile() $assertion->add($processesDir.$file); } + foreach ($dummyFiles as $file) { + $assertion->add($dummyDir.$file); + } + $queue = $this->createMock('\Liuggio\Fastest\Queue\QueueInterface'); $queue->expects($this->once()) ->method('push')