|
| 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