Skip to content

Commit 92d7f4b

Browse files
author
Anders Björkland
committed
Added save-method to RelationFactory. Closes #2832
1 parent a7cac53 commit 92d7f4b

File tree

2 files changed

+139
-1
lines changed

2 files changed

+139
-1
lines changed

src/Factory/RelationFactory.php

+24-1
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,24 @@
77
use Bolt\Entity\Content;
88
use Bolt\Entity\Relation;
99
use Bolt\Repository\RelationRepository;
10+
use Doctrine\ORM\EntityManager;
11+
use Doctrine\ORM\EntityManagerInterface;
1012
use Tightenco\Collect\Support\Collection;
1113

1214
class RelationFactory
1315
{
16+
/** @var EntityManagerInterface */
17+
private $em;
18+
1419
/** @var RelationRepository */
1520
private $repository;
1621

1722
/** @var Collection */
1823
private $relations;
1924

20-
public function __construct(RelationRepository $repository)
25+
public function __construct(RelationRepository $repository, EntityManagerInterface $em)
2126
{
27+
$this->em = $em;
2228
$this->repository = $repository;
2329
$this->relations = collect([]);
2430
}
@@ -51,4 +57,21 @@ private function getFromMemory(Content $from, Content $to): ?Relation
5157
|| ($relation->getToContent() === $to && $relation->getToContent() === $from);
5258
})->last(null, null);
5359
}
60+
61+
/**
62+
* @param Relation|Relation[] $relation
63+
*/
64+
public function save($relation): void
65+
{
66+
if ($relation instanceof Relation) {
67+
$this->em->persist($relation);
68+
69+
} elseif (is_iterable($relation)) {
70+
foreach ($relation as $r) {
71+
$this->em->persist($r);
72+
}
73+
}
74+
75+
$this->em->flush();
76+
}
5477
}
+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bolt\Tests\Factory;
4+
5+
6+
use Bolt\Entity\Content;
7+
use Bolt\Entity\Relation;
8+
use Bolt\Factory\ContentFactory;
9+
use Bolt\Factory\RelationFactory;
10+
use Bolt\Tests\DbAwareTestCase;
11+
use Doctrine\Common\Collections\Collection;
12+
13+
final class RelationFactoryTest extends DbAwareTestCase
14+
{
15+
16+
public function testSavePersistsTheRelation(): array
17+
{
18+
/** @var RelationFactory $relationFactory */
19+
$relationFactory = new RelationFactory($this->getEm()->getRepository(Relation::class), $this->getEm());
20+
21+
/**
22+
* @var Content $page
23+
*/
24+
$page = $this->getEm()->getRepository(Content::class)->findOneBy(['contentType' => 'pages']);
25+
$nonRelatedEntry = $this->getNonRelatedEntryForPage($page);
26+
27+
28+
29+
/**
30+
* @var Relation $newRelation
31+
*/
32+
$newRelation = $relationFactory->create($nonRelatedEntry, $page);
33+
34+
$relationFactory->save($newRelation);
35+
36+
$this->assertNotNull($newRelation->getId(), 'If id is null, the relation has not been persisted.');
37+
38+
return [
39+
"page" => $page,
40+
"entry" => $nonRelatedEntry,
41+
"relation" => $newRelation
42+
];
43+
}
44+
45+
46+
47+
/**
48+
* @depends testSavePersistsTheRelation
49+
*/
50+
public function testPersistedRelationCascadesToContent(array $entities): void
51+
{
52+
/** @var Content $page */
53+
$page = $entities["page"];
54+
55+
/** @var Content $entry */
56+
$entry = $entities["entry"];
57+
58+
/** @var Relation $persistedRelation */
59+
$persistedRelation = $entities["relation"];
60+
61+
/** @var array $entryRelationIds */
62+
$entryRelationIds = $this->getContentRelatedIds($entry->getRelationsFromThisContent());
63+
64+
/** @var array $pageRelationIds */
65+
$pageRelationIds = $this->getContentRelatedIds($page->getRelationsToThisContent());
66+
67+
$this->assertTrue(in_array($persistedRelation->getId(), $pageRelationIds), "It seems like relation has not persisted for contentType pages");
68+
$this->assertTrue(in_array($persistedRelation->getId(), $entryRelationIds), "It seems like relation has not persisted for contentType entries");
69+
70+
}
71+
72+
/**
73+
* Returns a Content entity with contentType 'Entry' that does not have a
74+
* relation with the argument.
75+
*
76+
* @return Content $nonRelatedEntry
77+
*/
78+
private function getNonRelatedEntryForPage(Content $page): ?Content
79+
{
80+
$relations = $page->getRelationsToThisContent();
81+
$relatedIds = [];
82+
foreach ($relations as $relative) {
83+
$relatedIds[] = $relative->getId();
84+
}
85+
86+
$entries = $this->getEm()->getRepository(Content::class)->findBy(['contentType' => 'entries']);
87+
$entryIds = array_map(function ($entry) {
88+
return $entry->getId();
89+
}, $entries);
90+
$nonRelatedEntries = array_filter($entryIds, fn ($id) => !in_array($id, $relatedIds));
91+
92+
reset($nonRelatedEntries);
93+
94+
$randomNonRelatedEntryIndex = random_int(0, count($nonRelatedEntries) - 1);
95+
$randomNonRelatedEntryId = $nonRelatedEntries[$randomNonRelatedEntryIndex];
96+
97+
/**
98+
* @var Content $nonRelatedEntry
99+
*/
100+
$nonRelatedEntry = $this->getEm()->getRepository(Content::class)->findOneBy(['id' => $randomNonRelatedEntryId]);
101+
102+
return $nonRelatedEntry;
103+
}
104+
105+
private function getContentRelatedIds(Collection $contentRelations): array
106+
{
107+
$relatedIds = [];
108+
foreach ($contentRelations as $relation) {
109+
$relatedIds[] = $relation->getId();
110+
}
111+
112+
return $relatedIds;
113+
}
114+
115+
}

0 commit comments

Comments
 (0)