{"record":{"id":"12769a8766bac1ee","repo":"sebastianbergmann/phpunit","slug":"the-current-item-is-not-a-testsuite-instance-and-t","errorCode":null,"errorMessage":"The current item is not a TestSuite instance and therefore does not have any children.","messagePattern":"The current item is not a TestSuite instance and therefore does not have any children\\.","errorType":"exception","errorClass":"NoChildTestSuiteException","httpStatus":null,"severity":"error","filePath":"src/Framework/TestSuiteIterator.php","lineNumber":75,"sourceCode":"    public function current(): Test\n    {\n        assert(isset($this->tests[$this->position]));\n\n        return $this->tests[$this->position];\n    }\n\n    public function next(): void\n    {\n        $this->position++;\n    }\n\n    /**\n     * @throws NoChildTestSuiteException\n     */\n    public function getChildren(): self\n    {\n        if (!$this->hasChildren()) {\n            throw new NoChildTestSuiteException(\n                'The current item is not a TestSuite instance and therefore does not have any children.',\n            );\n        }\n\n        $current = $this->current();\n\n        assert($current instanceof TestSuite);\n\n        return new self($current);\n    }\n\n    public function hasChildren(): bool\n    {\n        return $this->valid() && $this->current() instanceof TestSuite;\n    }\n}\n","sourceCodeStart":57,"sourceCodeEnd":92,"githubUrl":"https://github.com/sebastianbergmann/phpunit/blob/f123cdb2a2d49f15025794166cfed8bda8627dd2/src/Framework/TestSuiteIterator.php#L57-L92","documentation":"Thrown by TestSuiteIterator::getChildren() when the current element is a leaf Test (a TestCase) rather than a composite TestSuite. getChildren() must return an iterator over a nested suite, so calling it on a non-container element raises NoChildTestSuiteException after hasChildren() returns false.","triggerScenarios":"Calling $iterator->getChildren() while positioned on a TestCase; custom reporters/filters that walk the iterator tree and unconditionally descend instead of checking for children first.","commonSituations":"Custom test listeners or collectors traversing suites with a recursive algorithm that assumes every node is composite; iterating a flat suite whose direct children are all TestCases.","solutions":["Guard the descent: call hasChildren() (or check $current instanceof TestSuite) before getChildren()","Handle the leaf case explicitly in your traversal (process the test, do not recurse)","Use the iterator's standard recursive traversal instead of a hand-written one"],"exampleFix":"// before\n$children = $iterator->getChildren(); // throws on a TestCase node\n\n// after\nif ($iterator->hasChildren()) {\n    $children = $iterator->getChildren();\n} else {\n    $test = $iterator->current(); // leaf TestCase, process it\n}","handlingStrategy":"type-guard","validationCode":"if ($iterator->hasChildren()) {\n    $childIterator = $iterator->getChildren();\n}","typeGuard":"/** @param TestSuiteIterator<TestCase> $iterator */\nfunction currentIsSuite(TestSuiteIterator $iterator): bool\n{\n    return $iterator->current() instanceof \\PHPUnit\\Framework\\TestSuite;\n}","tryCatchPattern":null,"preventionTips":["Always branch on hasChildren() before getChildren() when walking the iterator tree","Handle TestCase leaf nodes explicitly in custom reporters/filters"],"tags":["phpunit","iterator","composite-pattern","test-suite"],"backgroundTag":"iterator-no-children","analyzedSha":"f123cdb2a2d49f15025794166cfed8bda8627dd2","analyzedAt":"2026-08-23T01:20:58.058Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}