getClassTree(5); $classes = $service->getAllClasses($tree); $this->assertSame(5, $this->getNestingLevel($classes)); } public function testGetAllClassesLimitedNesting(): void { $service = new ResourceService( [ 'class-nesting-level' => 2 ] ); $tree = $this->getClassTree(5); $classes = $service->getAllClasses($tree); $this->assertSame(2, $this->getNestingLevel($classes)); } private function getNestingLevel(array $array, int $level = 0): int { if (!empty($array['children'][0])) { return $this->getNestingLevel($array['children'][0], $level + 1); } return $level; } /** * @param int $level * * @return core_kernel_classes_Class */ private function getClassTree(int $level): core_kernel_classes_Class { $subClasses = []; ++$level; // +1 root level for ($i = 0; $i < $level; ++$i) { $class = $this->createMock(core_kernel_classes_Class::class); $class->method('getUri')->willReturn(sprintf('http://class_%s', $i)); $class->method('getLabel')->willReturn(sprintf('class_%s', $i)); $subClasses[] = $class; } for ($i = 0; $i < $level - 1; ++$i) { $subClasses[$i]->method('getSubClasses')->willReturnCallback( static function (bool $recursive) use ($subClasses, $i) { return $recursive ? array_slice($subClasses, $i + 1) : [$subClasses[$i + 1]]; } ); } $subClasses[$i]->method('getSubClasses')->willReturn([]); return $subClasses[0]; } }