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