Skip to content

Commit 3551a54

Browse files
committed
TASK: Simplify circularHierarchy check and improve errormessage and test
1 parent 3395126 commit 3551a54

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

Neos.Media/Classes/Domain/Model/AssetCollection.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,13 @@ public function getParent(): ?self
198198
private function validateCircularHierarchy(): void
199199
{
200200
$parent = $this->parent;
201-
$parents = [$parent];
202201
while ($parent !== null) {
203202
$parent = $parent->getParent();
204-
if (in_array($parent, $parents, true)) {
205-
throw new \InvalidArgumentException('Circular reference detected', 1680328041);
203+
if ($parent === $this->parent) {
204+
throw new \InvalidArgumentException(sprintf(
205+
'Circular reference detected, parent AssetCollection "%s" appeared twice in the hierarchy',
206+
$parent->getTitle()
207+
), 1680328041);
206208
}
207209
}
208210
}

Neos.Media/Tests/Functional/Domain/Model/AssetCollectionTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,22 @@ public function parentChildrenRelation(): void
7373
}
7474

7575
/**
76+
* Verifies the following hierarchie throws an error:
77+
* first -> second -> third -> first
78+
*
7679
* @test
7780
*/
7881
public function circularParentChildrenRelationThrowsErrorWhenSettingParent(): void
7982
{
80-
$child = new AssetCollection('child');
81-
$childOfChild = new AssetCollection('childOfChild');
82-
$parent = new AssetCollection('parent');
83+
$firstCollection = new AssetCollection('first');
84+
$secondCollection = new AssetCollection('second');
85+
$thirdCollection = new AssetCollection('third');
8386

84-
$child->setParent($parent);
85-
$childOfChild->setParent($child);
87+
$secondCollection->setParent($firstCollection);
88+
$thirdCollection->setParent($secondCollection);
8689

8790
$this->expectException(\InvalidArgumentException::class);
88-
$parent->setParent($childOfChild);
91+
$firstCollection->setParent($thirdCollection);
8992
}
9093

9194
/**

0 commit comments

Comments
 (0)