Skip to content

Commit 5b50f26

Browse files
committed
TASK: Only use parent property for manipulating and traversing asset collection hierarchy
1 parent 65cccfe commit 5b50f26

File tree

3 files changed

+8
-136
lines changed

3 files changed

+8
-136
lines changed

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

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,6 @@ class AssetCollection
5252
*/
5353
protected $parent;
5454

55-
/**
56-
* @var Collection<AssetCollection>
57-
* @ORM\OneToMany(mappedBy="parent", orphanRemoval=true, cascade={"persist"})
58-
* @ORM\OrderBy({"title"="ASC"})
59-
* @Flow\Lazy
60-
*/
61-
protected $children;
62-
6355
/**
6456
* @param string $title
6557
*/
@@ -221,41 +213,4 @@ public function setParent(?self $parent): void
221213
$this->parent = $parent;
222214
$this->validateCircularHierarchy();
223215
}
224-
225-
public function addChild(self $child): void
226-
{
227-
if (!$this->children->contains($child)) {
228-
$this->children->add($child);
229-
$child->setParent($this);
230-
}
231-
}
232-
233-
public function removeChild(self $child): void
234-
{
235-
if ($this->children->contains($child)) {
236-
$this->children->removeElement($child);
237-
}
238-
}
239-
240-
/**
241-
* @return Collection<self>
242-
*/
243-
public function getChildren(): Collection
244-
{
245-
return $this->children ?? new ArrayCollection();
246-
}
247-
248-
/**
249-
* @param Collection<self> $children
250-
*/
251-
public function setChildren(Collection $children): void
252-
{
253-
foreach ($this->children as $child) {
254-
$child->setParent(null);
255-
}
256-
foreach ($children as $child) {
257-
$child->setParent($this);
258-
}
259-
$this->validateCircularHierarchy();
260-
}
261216
}

Neos.Media/Classes/Domain/Repository/AssetCollectionRepository.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313

1414
use Neos\Flow\Annotations as Flow;
1515
use Neos\Flow\Persistence\QueryInterface;
16+
use Neos\Flow\Persistence\QueryResultInterface;
1617
use Neos\Flow\Persistence\Repository;
1718
use Neos\Media\Domain\Model\AssetCollection;
1819

1920
/**
2021
* A repository for AssetCollections
2122
*
2223
* @method AssetCollection findOneByTitle(string $title)
24+
* @method QueryResultInterface<AssetCollection> findByParent(AssetCollection $parent)
2325
*
2426
* @Flow\Scope("singleton")
2527
*/

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

Lines changed: 6 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,11 @@ public function parentChildrenRelation(): void
5151
$parent = new AssetCollection('parent');
5252

5353
$child1->setParent($parent);
54-
$parent->addChild($child2);
54+
$child2->setParent($parent);
5555

5656
$this->assetCollectionRepository->add($parent);
5757
$this->assetCollectionRepository->add($child1);
58+
$this->assetCollectionRepository->add($child2);
5859

5960
$this->persistenceManager->persistAll();
6061
$this->persistenceManager->clearState();
@@ -65,7 +66,7 @@ public function parentChildrenRelation(): void
6566
self::assertEquals('parent', $persistedParent->getTitle());
6667
self::assertNull($persistedParent->getParent());
6768

68-
$children = $persistedParent->getChildren();
69+
$children = $this->assetCollectionRepository->findByParent($parent);
6970
self::assertEquals(2, $children->count());
7071
self::assertEquals('child1', $children->offsetGet(0)->getTitle());
7172
self::assertEquals('child2', $children->offsetGet(1)->getTitle());
@@ -87,69 +88,6 @@ public function circularParentChildrenRelationThrowsErrorWhenSettingParent(): vo
8788
$parent->setParent($childOfChild);
8889
}
8990

90-
/**
91-
* @test
92-
*/
93-
public function circularParentChildrenRelationThrowsErrorWhenAddingChild(): void
94-
{
95-
$child = new AssetCollection('child');
96-
$childOfChild = new AssetCollection('childOfChild');
97-
$parent = new AssetCollection('parent');
98-
99-
$parent->addChild($child);
100-
$child->addChild($childOfChild);
101-
102-
$this->expectException(\InvalidArgumentException::class);
103-
$childOfChild->addChild($parent);
104-
}
105-
106-
/**
107-
* @test
108-
*/
109-
public function circularParentChildrenRelationThrowsErrorWhenSettingChildren(): void
110-
{
111-
$child = new AssetCollection('child');
112-
$childOfChild = new AssetCollection('childOfChild');
113-
$parent = new AssetCollection('parent');
114-
115-
$parent->setChildren(new ArrayCollection([$child]));
116-
$child->setChildren(new ArrayCollection([$childOfChild]));
117-
118-
$this->expectException(\InvalidArgumentException::class);
119-
$childOfChild->setChildren(new ArrayCollection([$parent]));
120-
}
121-
122-
/**
123-
* @test
124-
*/
125-
public function removingTheChildDeletesIt(): void
126-
{
127-
$child = new AssetCollection('child');
128-
$parent = new AssetCollection('parent');
129-
130-
$child->setParent($parent);
131-
132-
$this->assetCollectionRepository->add($parent);
133-
$this->assetCollectionRepository->add($child);
134-
135-
$this->persistenceManager->persistAll();
136-
$this->persistenceManager->clearState();
137-
138-
$persistedChild = $this->assetCollectionRepository->findOneByTitle('child');
139-
$persistedParent = $this->assetCollectionRepository->findOneByTitle('parent');
140-
141-
$persistedParent->removeChild($persistedChild);
142-
143-
$this->persistenceManager->persistAll();
144-
$this->persistenceManager->clearState();
145-
146-
$persistedChild = $this->assetCollectionRepository->findOneByTitle('child');
147-
$persistedParent = $this->assetCollectionRepository->findOneByTitle('parent');
148-
149-
self::assertNull($persistedChild);
150-
self::assertEquals(0, $persistedParent->getChildren()->count());
151-
}
152-
15391
/**
15492
* @test
15593
*/
@@ -176,8 +114,10 @@ public function unsettingTheParentRemovesChildFromParent(): void
176114
$persistedChild = $this->assetCollectionRepository->findOneByTitle('child');
177115
$persistedParent = $this->assetCollectionRepository->findOneByTitle('parent');
178116

117+
$children = $this->assetCollectionRepository->findByParent($persistedParent);
118+
179119
self::assertNull($persistedChild->getParent());
180-
self::assertEquals(0, $persistedParent->getChildren()->count());
120+
self::assertEquals(0, count($children));
181121
}
182122

183123
/**
@@ -208,29 +148,4 @@ public function deletingTheParentDeletesTheChild(): void
208148
self::assertNull($persistedChild);
209149
self::assertNull($persistedParent);
210150
}
211-
212-
/**
213-
* @test
214-
*/
215-
public function settingChildrenSetsTheirParent(): void
216-
{
217-
$child1 = new AssetCollection('child1');
218-
$child2 = new AssetCollection('child2');
219-
$parent = new AssetCollection('parent');
220-
221-
$parent->setChildren(new ArrayCollection([$child1, $child2]));
222-
223-
$this->assetCollectionRepository->add($parent);
224-
$this->assetCollectionRepository->add($child1);
225-
$this->assetCollectionRepository->add($child2);
226-
227-
$this->persistenceManager->persistAll();
228-
$this->persistenceManager->clearState();
229-
230-
$persistedChild1 = $this->assetCollectionRepository->findOneByTitle('child1');
231-
$persistedChild2 = $this->assetCollectionRepository->findOneByTitle('child2');
232-
233-
self::assertEquals('parent', $persistedChild1->getParent()->getTitle());
234-
self::assertEquals('parent', $persistedChild2->getParent()->getTitle());
235-
}
236151
}

0 commit comments

Comments
 (0)