|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace SpameriTests\Elastic\EntityManager; |
| 4 | + |
| 5 | +require_once __DIR__ . '/../../../bootstrap.php'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Single-entity references of a whole page load in one query instead of one find() per hit. |
| 9 | + * |
| 10 | + * @testCase |
| 11 | + */ |
| 12 | +class PreloadReferencesTest extends \SpameriTests\Elastic\AbstractTestCase |
| 13 | +{ |
| 14 | + |
| 15 | + private \Spameri\Elastic\EntityManager $entityManager; |
| 16 | + |
| 17 | + |
| 18 | + protected function setUp(): void |
| 19 | + { |
| 20 | + parent::setUp(); |
| 21 | + |
| 22 | + $this->deleteIndices(); |
| 23 | + |
| 24 | + /** @var \Spameri\Elastic\Model\Indices\Create $create */ |
| 25 | + $create = $this->container->getByType(\Spameri\Elastic\Model\Indices\Create::class); |
| 26 | + $create->execute(\SpameriTests\Elastic\Config::INDEX_TITLE, []); |
| 27 | + $create->execute(\SpameriTests\Elastic\Config::INDEX_IMAGE, []); |
| 28 | + |
| 29 | + $this->entityManager = $this->container->getByType(\Spameri\Elastic\EntityManager::class); |
| 30 | + } |
| 31 | + |
| 32 | + |
| 33 | + public function testPageOfReferencesLoadsInOneQuery(): void |
| 34 | + { |
| 35 | + $imageIds = []; |
| 36 | + |
| 37 | + for ($i = 0; $i < 5; $i++) { |
| 38 | + $image = new \SpameriTests\Elastic\Data\Entity\Image( |
| 39 | + new \Spameri\Elastic\Entity\Property\EmptyElasticId(), |
| 40 | + null, |
| 41 | + ); |
| 42 | + $this->entityManager->persist($image); |
| 43 | + |
| 44 | + $title = new \SpameriTests\Elastic\Data\Entity\Title( |
| 45 | + new \Spameri\Elastic\Entity\Property\EmptyElasticId(), |
| 46 | + $image, |
| 47 | + ); |
| 48 | + $this->entityManager->persist($title); |
| 49 | + |
| 50 | + $imageIds[$title->id()->value()] = $image->id()->value(); |
| 51 | + } |
| 52 | + |
| 53 | + $this->refreshIndices(); |
| 54 | + $this->entityManager->clear(); |
| 55 | + |
| 56 | + $searchesBefore = $this->imageSearchCount(); |
| 57 | + $titles = $this->entityManager->findAll(\SpameriTests\Elastic\Data\Entity\Title::class); |
| 58 | + $searches = $this->imageSearchCount() - $searchesBefore; |
| 59 | + |
| 60 | + \Tester\Assert::same(5, $titles->count()); |
| 61 | + \Tester\Assert::same(1, $searches); |
| 62 | + |
| 63 | + /** @var \SpameriTests\Elastic\Data\Entity\Title $title */ |
| 64 | + foreach ($titles as $title) { |
| 65 | + \Tester\Assert::type(\SpameriTests\Elastic\Data\Entity\Image::class, $title->backdrop); |
| 66 | + \Tester\Assert::same($imageIds[$title->id()->value()], $title->backdrop->id()->value()); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + public function testSharedReferenceResolvesToOneInstance(): void |
| 72 | + { |
| 73 | + $image = new \SpameriTests\Elastic\Data\Entity\Image( |
| 74 | + new \Spameri\Elastic\Entity\Property\EmptyElasticId(), |
| 75 | + null, |
| 76 | + ); |
| 77 | + $this->entityManager->persist($image); |
| 78 | + |
| 79 | + for ($i = 0; $i < 3; $i++) { |
| 80 | + $this->entityManager->persist( |
| 81 | + new \SpameriTests\Elastic\Data\Entity\Title( |
| 82 | + new \Spameri\Elastic\Entity\Property\EmptyElasticId(), |
| 83 | + $image, |
| 84 | + ), |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + $this->refreshIndices(); |
| 89 | + $this->entityManager->clear(); |
| 90 | + |
| 91 | + $titles = \array_values(\iterator_to_array($this->entityManager->findAll(\SpameriTests\Elastic\Data\Entity\Title::class))); |
| 92 | + |
| 93 | + \Tester\Assert::count(3, $titles); |
| 94 | + \Tester\Assert::same($titles[0]->backdrop, $titles[1]->backdrop); |
| 95 | + \Tester\Assert::same($titles[0]->backdrop, $titles[2]->backdrop); |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | + public function testReferenceCycleHydratesBothSides(): void |
| 100 | + { |
| 101 | + $title = new \SpameriTests\Elastic\Data\Entity\Title( |
| 102 | + new \Spameri\Elastic\Entity\Property\EmptyElasticId(), |
| 103 | + null, |
| 104 | + ); |
| 105 | + $image = new \SpameriTests\Elastic\Data\Entity\Image( |
| 106 | + new \Spameri\Elastic\Entity\Property\EmptyElasticId(), |
| 107 | + $title, |
| 108 | + ); |
| 109 | + $this->entityManager->persist($image); |
| 110 | + $title->backdrop = $image; |
| 111 | + $this->entityManager->persist($title); |
| 112 | + |
| 113 | + $this->refreshIndices(); |
| 114 | + $this->entityManager->clear(); |
| 115 | + |
| 116 | + $titles = $this->entityManager->findAll(\SpameriTests\Elastic\Data\Entity\Title::class); |
| 117 | + |
| 118 | + \Tester\Assert::same(1, $titles->count()); |
| 119 | + /** @var \SpameriTests\Elastic\Data\Entity\Title $loadedTitle */ |
| 120 | + $loadedTitle = $titles->first(); |
| 121 | + \Tester\Assert::type(\SpameriTests\Elastic\Data\Entity\Image::class, $loadedTitle->backdrop); |
| 122 | + \Tester\Assert::same($image->id()->value(), $loadedTitle->backdrop->id()->value()); |
| 123 | + \Tester\Assert::same($loadedTitle, $loadedTitle->backdrop->title); |
| 124 | + } |
| 125 | + |
| 126 | + |
| 127 | + public function testAlreadyLoadedReferenceIsNotQueriedAgain(): void |
| 128 | + { |
| 129 | + $image = new \SpameriTests\Elastic\Data\Entity\Image( |
| 130 | + new \Spameri\Elastic\Entity\Property\EmptyElasticId(), |
| 131 | + null, |
| 132 | + ); |
| 133 | + $this->entityManager->persist($image); |
| 134 | + $this->entityManager->persist( |
| 135 | + new \SpameriTests\Elastic\Data\Entity\Title( |
| 136 | + new \Spameri\Elastic\Entity\Property\EmptyElasticId(), |
| 137 | + $image, |
| 138 | + ), |
| 139 | + ); |
| 140 | + |
| 141 | + $this->refreshIndices(); |
| 142 | + $this->entityManager->clear(); |
| 143 | + |
| 144 | + $loadedImage = $this->entityManager->find($image->id()->value(), \SpameriTests\Elastic\Data\Entity\Image::class); |
| 145 | + |
| 146 | + $searchesBefore = $this->imageSearchCount(); |
| 147 | + $titles = $this->entityManager->findAll(\SpameriTests\Elastic\Data\Entity\Title::class); |
| 148 | + |
| 149 | + \Tester\Assert::same(0, $this->imageSearchCount() - $searchesBefore); |
| 150 | + \Tester\Assert::same($loadedImage, $titles->first()->backdrop); |
| 151 | + } |
| 152 | + |
| 153 | + |
| 154 | + public function testMissingReferenceStillDropsTheReferencingHit(): void |
| 155 | + { |
| 156 | + $image = new \SpameriTests\Elastic\Data\Entity\Image( |
| 157 | + new \Spameri\Elastic\Entity\Property\EmptyElasticId(), |
| 158 | + null, |
| 159 | + ); |
| 160 | + $this->entityManager->persist($image); |
| 161 | + $this->entityManager->persist( |
| 162 | + new \SpameriTests\Elastic\Data\Entity\Title( |
| 163 | + new \Spameri\Elastic\Entity\Property\EmptyElasticId(), |
| 164 | + $image, |
| 165 | + ), |
| 166 | + ); |
| 167 | + $this->entityManager->persist( |
| 168 | + new \SpameriTests\Elastic\Data\Entity\Title( |
| 169 | + new \Spameri\Elastic\Entity\Property\EmptyElasticId(), |
| 170 | + null, |
| 171 | + ), |
| 172 | + ); |
| 173 | + |
| 174 | + $this->refreshIndices(); |
| 175 | + $this->entityManager->clear(); |
| 176 | + |
| 177 | + /** @var \Spameri\Elastic\ClientProvider $clientProvider */ |
| 178 | + $clientProvider = $this->container->getByType(\Spameri\Elastic\ClientProvider::class); |
| 179 | + $clientProvider->client()->delete([ |
| 180 | + 'index' => \SpameriTests\Elastic\Config::INDEX_IMAGE, |
| 181 | + 'id' => $image->id()->value(), |
| 182 | + 'refresh' => true, |
| 183 | + ]); |
| 184 | + |
| 185 | + $titles = $this->entityManager->findAll(\SpameriTests\Elastic\Data\Entity\Title::class); |
| 186 | + |
| 187 | + \Tester\Assert::same(1, $titles->count()); |
| 188 | + \Tester\Assert::null($titles->first()->backdrop); |
| 189 | + } |
| 190 | + |
| 191 | + |
| 192 | + protected function tearDown(): void |
| 193 | + { |
| 194 | + $this->deleteIndices(); |
| 195 | + } |
| 196 | + |
| 197 | + |
| 198 | + private function deleteIndices(): void |
| 199 | + { |
| 200 | + /** @var \Spameri\Elastic\ClientProvider $clientProvider */ |
| 201 | + $clientProvider = $this->container->getByType(\Spameri\Elastic\ClientProvider::class); |
| 202 | + |
| 203 | + foreach ([\SpameriTests\Elastic\Config::INDEX_TITLE, \SpameriTests\Elastic\Config::INDEX_IMAGE] as $index) { |
| 204 | + try { |
| 205 | + $clientProvider->client()->indices()->delete(['index' => $index]); |
| 206 | + } catch (\Throwable) { |
| 207 | + // The index does not exist yet. |
| 208 | + } |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + |
| 213 | + private function refreshIndices(): void |
| 214 | + { |
| 215 | + /** @var \Spameri\Elastic\ClientProvider $clientProvider */ |
| 216 | + $clientProvider = $this->container->getByType(\Spameri\Elastic\ClientProvider::class); |
| 217 | + $clientProvider->client()->indices()->refresh([ |
| 218 | + 'index' => \SpameriTests\Elastic\Config::INDEX_TITLE . ',' . \SpameriTests\Elastic\Config::INDEX_IMAGE, |
| 219 | + ]); |
| 220 | + } |
| 221 | + |
| 222 | + |
| 223 | + private function imageSearchCount(): int |
| 224 | + { |
| 225 | + /** @var \Spameri\Elastic\ClientProvider $clientProvider */ |
| 226 | + $clientProvider = $this->container->getByType(\Spameri\Elastic\ClientProvider::class); |
| 227 | + $stats = $clientProvider->client()->indices()->stats([ |
| 228 | + 'index' => \SpameriTests\Elastic\Config::INDEX_IMAGE, |
| 229 | + 'metric' => 'search', |
| 230 | + ])->asArray(); |
| 231 | + |
| 232 | + return (int) $stats['_all']['primaries']['search']['query_total']; |
| 233 | + } |
| 234 | + |
| 235 | +} |
| 236 | + |
| 237 | +(new PreloadReferencesTest())->run(); |
0 commit comments