edf7b9514d4ee0781e9ff647f7ff4a6214253099
[yaffs-website] / TestSuiteIterator.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  * Iterator for test suites.
13  *
14  * @since Class available since Release 3.1.0
15  */
16 class PHPUnit_Util_TestSuiteIterator implements RecursiveIterator
17 {
18     /**
19      * @var int
20      */
21     protected $position;
22
23     /**
24      * @var PHPUnit_Framework_Test[]
25      */
26     protected $tests;
27
28     /**
29      * @param PHPUnit_Framework_TestSuite $testSuite
30      */
31     public function __construct(PHPUnit_Framework_TestSuite $testSuite)
32     {
33         $this->tests = $testSuite->tests();
34     }
35
36     /**
37      * Rewinds the Iterator to the first element.
38      */
39     public function rewind()
40     {
41         $this->position = 0;
42     }
43
44     /**
45      * Checks if there is a current element after calls to rewind() or next().
46      *
47      * @return bool
48      */
49     public function valid()
50     {
51         return $this->position < count($this->tests);
52     }
53
54     /**
55      * Returns the key of the current element.
56      *
57      * @return int
58      */
59     public function key()
60     {
61         return $this->position;
62     }
63
64     /**
65      * Returns the current element.
66      *
67      * @return PHPUnit_Framework_Test
68      */
69     public function current()
70     {
71         return $this->valid() ? $this->tests[$this->position] : null;
72     }
73
74     /**
75      * Moves forward to next element.
76      */
77     public function next()
78     {
79         $this->position++;
80     }
81
82     /**
83      * Returns the sub iterator for the current element.
84      *
85      * @return PHPUnit_Util_TestSuiteIterator
86      */
87     public function getChildren()
88     {
89         return new self(
90             $this->tests[$this->position]
91         );
92     }
93
94     /**
95      * Checks whether the current element has children.
96      *
97      * @return bool
98      */
99     public function hasChildren()
100     {
101         return $this->tests[$this->position] instanceof PHPUnit_Framework_TestSuite;
102     }
103 }