Version 1
[yaffs-website] / vendor / phpunit / phpunit / src / Extensions / GroupTestSuite.php
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  *
24  * @since Class available since Release 3.3.0
25  */
26 class PHPUnit_Extensions_GroupTestSuite extends PHPUnit_Framework_TestSuite
27 {
28     public function __construct(PHPUnit_Framework_TestSuite $suite, array $groups)
29     {
30         $groupSuites = array();
31         $name        = $suite->getName();
32
33         foreach ($groups as $group) {
34             $groupSuites[$group] = new PHPUnit_Framework_TestSuite($name . ' - ' . $group);
35             $this->addTest($groupSuites[$group]);
36         }
37
38         $tests = new RecursiveIteratorIterator(
39             new PHPUnit_Util_TestSuiteIterator($suite),
40             RecursiveIteratorIterator::LEAVES_ONLY
41         );
42
43         foreach ($tests as $test) {
44             if ($test instanceof PHPUnit_Framework_TestCase) {
45                 $testGroups = PHPUnit_Util_Test::getGroups(
46                     get_class($test),
47                     $test->getName(false)
48                 );
49
50                 foreach ($groups as $group) {
51                     foreach ($testGroups as $testGroup) {
52                         if ($group == $testGroup) {
53                             $groupSuites[$group]->addTest($test);
54                         }
55                     }
56                 }
57             }
58         }
59     }
60 }