Skip to content

Commit a30734a

Browse files
committed
Better exception messages for Tree issues.
1 parent e44466f commit a30734a

File tree

2 files changed

+85
-5
lines changed

2 files changed

+85
-5
lines changed

src/View/Helper/TreeHelper.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
use Cake\Core\Configure;
1414
use Cake\View\Helper;
15-
use Exception;
15+
use RuntimeException;
1616

1717
/**
1818
* Helper to generate tree representations of MPTT or recursively nested data.
@@ -230,7 +230,7 @@ protected function _generate($data, array $config, $parent = null) {
230230
$lastChild = true;
231231
}
232232
} else {
233-
throw new Exception('Invalid Tree Structure');
233+
throw new RuntimeException('Invalid Tree Structure: ' . print_r($result, true));
234234
}
235235

236236
$activePathElement = null;
@@ -309,7 +309,6 @@ protected function _generate($data, array $config, $parent = null) {
309309
if ($numberOfDirectChildren) {
310310
$config['depth'] = $depth + 1;
311311
$children = $result['children'];
312-
//unset($result['children']);
313312

314313
$return .= $this->_suffix();
315314
$return .= $this->_generate($children, $config, $result);
@@ -534,15 +533,15 @@ protected function _attributes($rType, array $elementData = [], $clear = true, b
534533
* @throws \Exception
535534
* @return void
536535
*/
537-
protected function _markUnrelatedAsHidden(&$tree, array $path, $level = 0) {
536+
protected function _markUnrelatedAsHidden(&$tree, array $path, int $level = 0) {
538537
extract($this->_config);
539538
$siblingIsActive = false;
540539
foreach ($tree as $key => &$subTree) {
541540
if (is_object($subTree) && method_exists($subTree, 'toArray')) {
542541
$subTree = $subTree->toArray();
543542
}
544543
if (!isset($subTree['children'])) {
545-
throw new Exception('Only works with threaded (nested children) results');
544+
throw new RuntimeException('Only works with threaded (nested children) results: ' . print_r($subTree, true));
546545
}
547546

548547
if (!empty($path[$level]) && $subTree['id'] == $path[$level]) {

tests/TestCase/View/Helper/TreeHelperTest.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Cake\ORM\Entity;
77
use Cake\Utility\Hash;
88
use Cake\View\View;
9+
use RuntimeException;
910
use Shim\TestSuite\TestCase;
1011
use Tools\View\Helper\TreeHelper;
1112

@@ -664,6 +665,86 @@ public function testGenerateWithEntityUsage() {
664665
$this->assertTextEquals($expected, $output);
665666
}
666667

668+
/**
669+
* @return void
670+
*/
671+
public function testGenerateFromArrayWithChildren(): void {
672+
$treeData = [
673+
[
674+
'name' => 'Foo',
675+
'children' => [
676+
[
677+
'name' => 'Bar',
678+
'children' => [
679+
],
680+
],
681+
[
682+
'name' => 'Baz',
683+
'children' => [
684+
[
685+
'name' => 'Baz Child',
686+
'children' => [],
687+
],
688+
],
689+
],
690+
],
691+
],
692+
];
693+
694+
$output = $this->Tree->generate($treeData);
695+
$expected = <<<TEXT
696+
697+
<ul>
698+
<li>Foo
699+
<ul>
700+
<li>Bar</li>
701+
<li>Baz
702+
<ul>
703+
<li>Baz Child</li>
704+
</ul>
705+
</li>
706+
</ul>
707+
</li>
708+
</ul>
709+
710+
TEXT;
711+
$output = str_replace(["\t", "\r", "\n"], '', $output);
712+
$expected = str_replace(["\t", "\r", "\n"], '', $expected);
713+
$this->assertTextEquals($expected, $output);
714+
}
715+
716+
/**
717+
* @return void
718+
*/
719+
public function testGenerateFromArrayWithChildrenInvalid(): void {
720+
$treeData = [
721+
[
722+
'name' => 'Foo',
723+
'children' => [
724+
[
725+
'name' => 'Bar',
726+
'children' => [
727+
],
728+
],
729+
[
730+
'name' => 'Baz',
731+
'children' => [
732+
[
733+
'name' => 'Baz Child',
734+
//'children' => [] // Missing element
735+
],
736+
],
737+
],
738+
],
739+
],
740+
];
741+
742+
$this->expectException(RuntimeException::class);
743+
$this->expectExceptionMessage('Invalid Tree Structure: Array');
744+
745+
$this->Tree->generate($treeData);
746+
}
747+
667748
/**
668749
* @param array $data
669750
* @return string|null

0 commit comments

Comments
 (0)