1<?php
2/*
3 * This file is part of PHPUnit.
4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11/**
12 * We have a TestSuite object A.
13 * In TestSuite object A we have Tests tagged with @group.
14 * We want a TestSuite object B that contains TestSuite objects C, D, ...
15 * for the Tests tagged with @group C, @group D, ...
16 * Running the Tests from TestSuite object B results in Tests tagged with both
17 *
18 * @group C and @group D in TestSuite object A to be run twice .
19 *
20 * <code>
21 * $suite = new PHPUnit_Extensions_GroupTestSuite($A, array('C', 'D'));
22 * </code>
23 */
24class PHPUnit_Extensions_GroupTestSuite extends PHPUnit_Framework_TestSuite
25{
26    public function __construct(PHPUnit_Framework_TestSuite $suite, array $groups)
27    {
28        $groupSuites = [];
29        $name        = $suite->getName();
30
31        foreach ($groups as $group) {
32            $groupSuites[$group] = new PHPUnit_Framework_TestSuite($name . ' - ' . $group);
33            $this->addTest($groupSuites[$group]);
34        }
35
36        $tests = new RecursiveIteratorIterator(
37            new PHPUnit_Util_TestSuiteIterator($suite),
38            RecursiveIteratorIterator::LEAVES_ONLY
39        );
40
41        foreach ($tests as $test) {
42            if ($test instanceof PHPUnit_Framework_TestCase) {
43                $testGroups = PHPUnit_Util_Test::getGroups(
44                    get_class($test),
45                    $test->getName(false)
46                );
47
48                foreach ($groups as $group) {
49                    foreach ($testGroups as $testGroup) {
50                        if ($group == $testGroup) {
51                            $groupSuites[$group]->addTest($test);
52                        }
53                    }
54                }
55            }
56        }
57    }
58}
59