Skip to content

Commit

Permalink
add support for the @group annotation at the class level
Browse files Browse the repository at this point in the history
  • Loading branch information
Michel Valdrighi authored and Michel VALDRIGHI committed Nov 15, 2019
1 parent 07a5ca1 commit fa196c5
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 2 deletions.
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<directory>tests/Queue/</directory>
<directory>tests/Process/</directory>
<directory>tests/Environment/</directory>
<exclude>tests/Queue/Fixture/</exclude>
</testsuite>
</testsuites>

Expand Down
52 changes: 50 additions & 2 deletions src/Queue/CreateTestsQueueFromPhpUnitXML.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions tests/Queue/CreateTestsQueueFromPhpUnitXMLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public function shouldCreateAnArrayOfTestSuitesFromXML()
'ProcessorCounterTest.php',
];

$dummyDir = __DIR__.'/Fixture/tests/';
$dummyFiles = [
'DummyIncludedTest.php',
];

$queue = new TestsQueue();

foreach ($infrastructureFiles as $file) {
Expand All @@ -35,6 +40,10 @@ public function shouldCreateAnArrayOfTestSuitesFromXML()
$queue->add($processesDir.$file);
}

foreach ($dummyFiles as $file) {
$queue->add($dummyDir.$file);
}

$this->assertEquals($queue, $output);
}
}
13 changes: 13 additions & 0 deletions tests/Queue/Fixture/phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,18 @@
<testsuite name="Processes Test Suite">
<directory>../../Process/</directory>
</testsuite>
<testsuite name="Dummy Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>

<groups>
<include>
<group>default</group>
<group>dummy-included</group>
</include>
<exclude>
<group>dummy-excluded</group>
</exclude>
</groups>
</phpunit>
14 changes: 14 additions & 0 deletions tests/Queue/Fixture/tests/DummyExcludedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Liuggio\Fastest\Queue\Fixture\tests;

/**
* @group dummy-excluded
*/
class DummyExcludedTest extends \PHPUnit\Framework\TestCase
{
public function testDummyAssert()
{
$this->assertTrue(true);
}
}
14 changes: 14 additions & 0 deletions tests/Queue/Fixture/tests/DummyIncludedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Liuggio\Fastest\Queue\Fixture\tests;

/**
* @group dummy-included
*/
class DummyIncludedTest extends \PHPUnit\Framework\TestCase
{
public function testDummyAssert()
{
$this->assertTrue(true);
}
}
9 changes: 9 additions & 0 deletions tests/Queue/ReadFromInputAndPushIntoTheQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public function shouldPushIntoTheQueueTheXMLFile()
'ProcessorCounterTest.php',
];

$dummyDir = __DIR__ . '/Fixture/tests/';
$dummyFiles = [
'DummyIncludedTest.php',
];

$assertion = new TestsQueue();

foreach ($files as $file) {
Expand All @@ -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')
Expand Down

0 comments on commit fa196c5

Please sign in to comment.