Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added save-method to RelationFactory. Closes #2832 #2833

Merged
merged 4 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/Factory/RelationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,23 @@
use Bolt\Entity\Content;
use Bolt\Entity\Relation;
use Bolt\Repository\RelationRepository;
use Doctrine\ORM\EntityManagerInterface;
use Tightenco\Collect\Support\Collection;

class RelationFactory
{
/** @var EntityManagerInterface */
private $em;

/** @var RelationRepository */
private $repository;

/** @var Collection */
private $relations;

public function __construct(RelationRepository $repository)
public function __construct(RelationRepository $repository, EntityManagerInterface $em)
{
$this->em = $em;
$this->repository = $repository;
$this->relations = collect([]);
}
Expand Down Expand Up @@ -51,4 +56,20 @@ private function getFromMemory(Content $from, Content $to): ?Relation
|| ($relation->getToContent() === $to && $relation->getToContent() === $from);
})->last(null, null);
}

/**
* @param Relation|Relation[] $relation
*/
public function save($relation): void
{
if ($relation instanceof Relation) {
$this->em->persist($relation);
} elseif (is_iterable($relation)) {
foreach ($relation as $r) {
$this->em->persist($r);
}
}

$this->em->flush();
}
}
151 changes: 151 additions & 0 deletions tests/php/Factory/RelationFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php

declare(strict_types=1);

namespace Bolt\Tests\Factory;

use Bolt\Entity\Content;
use Bolt\Entity\Relation;
use Bolt\Factory\RelationFactory;
use Bolt\Repository\ContentRepository;
use Bolt\Repository\RelationRepository;
use Bolt\Tests\DbAwareTestCase;
use Doctrine\Common\Collections\Collection;

final class RelationFactoryTest extends DbAwareTestCase
{
public function testSavePersistsTheRelation(): array
{
/** @var RelationRepository */
$relationRepository = $this->getEm()->getRepository(Relation::class);

/** @var RelationFactory $relationFactory */
$relationFactory = new RelationFactory($relationRepository, $this->getEm());

/** @var Content $page */
$page = $this->getEm()->getRepository(Content::class)->findOneBy(['contentType' => 'pages']);

/** @var Content|null $nonRelatedEntry */
$nonRelatedEntry = $this->getNonRelatedEntryForPage($page);

/** @var Relation $newRelation */
$newRelation = $relationFactory->create($nonRelatedEntry, $page);

$relationFactory->save($newRelation);

$this->assertNotNull($newRelation->getId(), 'If id is null, the relation has not been persisted.');

return [
'page' => $page,
'entry' => $nonRelatedEntry,
'relation' => $newRelation,
];
}

/**
* @depends testSavePersistsTheRelation
*/
public function testPersistedRelationCascadesToContent(array $entities): void
{
/** @var Content $page */
$page = $entities['page'];

/** @var Content $entry */
$entry = $entities['entry'];

/** @var Relation $persistedRelation */
$persistedRelation = $entities['relation'];

/** @var array $entryRelationIds */
$entryRelationIds = $this->getContentRelatedIds($entry->getRelationsFromThisContent());

/** @var array $pageRelationIds */
$pageRelationIds = $this->getContentRelatedIds($page->getRelationsToThisContent());

$this->assertTrue(in_array($persistedRelation->getId(), $pageRelationIds, true), 'It seems like relation has not persisted for contentType pages');
$this->assertTrue(in_array($persistedRelation->getId(), $entryRelationIds, true), 'It seems like relation has not persisted for contentType entries');
}

public function testSaveMultipleRelations(): void
{
/** @var RelationRepository */
$relationRepository = $this->getEm()->getRepository(Relation::class);

/** @var RelationFactory $relationFactory */
$relationFactory = new RelationFactory($relationRepository, $this->getEm());

/** @var ContentRepository $contentRepository */
$contentRepository = $this->getEm()->getRepository(Content::class);

/** @var Content $page */
$page = $this->getEm()->getRepository(Content::class)->findOneBy(['contentType' => 'pages']);

/** @var array $nonRelatedEntryIds */
$nonRelatedEntryIds = $this->getNonRelatedEntryIds($page);

$entries = [];
$relations = [];

$limit = count($nonRelatedEntryIds) > 5 ? 5 : count($nonRelatedEntryIds);
foreach ($nonRelatedEntryIds as $id) {
/** @var Content|null $entry */
$entry = $contentRepository->findOneById($id);
$entries[] = $entry;
$relations[] = $relationFactory->create($entry, $page);

if (count($relations) >= $limit) {
break;
}
}

$relationFactory->save($relations);
$relationIds = array_map(function ($relation) {
return $relation->getId();
}, $relations);

$this->assertFalse(in_array(null, $relationIds, true), 'Some of the relations has not been persisted.');
}

/**
* Returns a Content entity with contentType 'Entry' that does not have a
* relation with the argument.
*/
private function getNonRelatedEntryForPage(Content $page): ?Content
{
$nonRelatedEntries = $this->getNonRelatedEntryIds($page);

$randomNonRelatedEntryIndex = random_int(0, count($nonRelatedEntries) - 1);
$randomNonRelatedEntryId = $nonRelatedEntries[$randomNonRelatedEntryIndex];

return $this->getEm()->getRepository(Content::class)->findOneBy(['id' => $randomNonRelatedEntryId]);
}

private function getNonRelatedEntryIds(Content $page): array
{
$relations = $page->getRelationsToThisContent();
$relatedIds = [];
foreach ($relations as $relative) {
$relatedIds[] = $relative->getId();
}

$entries = $this->getEm()->getRepository(Content::class)->findBy(['contentType' => 'entries']);
$entryIds = array_map(function ($entry) {
return $entry->getId();
}, $entries);
$nonRelatedEntries = array_filter($entryIds, function ($id) use ($relatedIds) {
return ! in_array($id, $relatedIds, true);
});

return array_values($nonRelatedEntries);
}

private function getContentRelatedIds(Collection $contentRelations): array
{
$relatedIds = [];
foreach ($contentRelations as $relation) {
$relatedIds[] = $relation->getId();
}

return $relatedIds;
}
}