|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace SpameriTests\Elastic\Factory\EntityFactory; |
| 4 | + |
| 5 | +require_once __DIR__ . '/../../../../bootstrap.php'; |
| 6 | + |
| 7 | +/** |
| 8 | + * A value collection is written as a flat list of scalars and has to come back |
| 9 | + * as the same list of value objects. |
| 10 | + * |
| 11 | + * PrepareEntityArray has always known how: it walks a ValueCollectionInterface |
| 12 | + * and writes value() for each member. EntityFactory did not, so the property |
| 13 | + * fell through to the generic "instantiate from nested properties" tail, where |
| 14 | + * the collection's own properties are looked for under `genres.*` — nothing is |
| 15 | + * stored there, because the document holds `genres: ["Action"]` — and every |
| 16 | + * read produced an empty collection. |
| 17 | + * |
| 18 | + * Nothing reported an error. The data was in Elasticsearch, indexed and |
| 19 | + * searchable, and simply never reached anything that read an entity. |
| 20 | + * |
| 21 | + * @testCase |
| 22 | + */ |
| 23 | +class ValueCollectionTest extends \SpameriTests\Elastic\AbstractTestCase |
| 24 | +{ |
| 25 | + |
| 26 | + public function testValueCollectionSurvivesTheRoundTrip(): void |
| 27 | + { |
| 28 | + /** @var \Spameri\Elastic\EntityManager $entityManager */ |
| 29 | + $entityManager = $this->container->getByType(\Spameri\Elastic\EntityManager::class); |
| 30 | + /** @var \Spameri\Elastic\Factory\EntityFactory $entityFactory */ |
| 31 | + $entityFactory = $this->container->getByType(\Spameri\Elastic\Factory\EntityFactory::class); |
| 32 | + /** @var \Spameri\Elastic\Model\Insert\PrepareEntityArray $prepareEntityArray */ |
| 33 | + $prepareEntityArray = $this->container->getByType(\Spameri\Elastic\Model\Insert\PrepareEntityArray::class); |
| 34 | + |
| 35 | + $entity = new \SpameriTests\Elastic\Data\Entity\EntityWithValueCollection( |
| 36 | + new \Spameri\Elastic\Entity\Property\ElasticId('vc-1'), |
| 37 | + new \SpameriTests\Elastic\Data\Entity\Video\Details\GenreCollection( |
| 38 | + new \SpameriTests\Elastic\Data\Entity\Video\Details\Genre('Action'), |
| 39 | + new \SpameriTests\Elastic\Data\Entity\Video\Details\Genre('Science Fiction'), |
| 40 | + ), |
| 41 | + ); |
| 42 | + |
| 43 | + $source = $prepareEntityArray->prepare($entity); |
| 44 | + |
| 45 | + // What the write side puts in the document: a flat list of scalars. |
| 46 | + \Tester\Assert::same(['Action', 'Science Fiction'], $source['genres']); |
| 47 | + |
| 48 | + $hit = new \Spameri\ElasticQuery\Response\Result\Hit( |
| 49 | + source: $source, |
| 50 | + position: 0, index: '', type: '', id: 'vc-1', score: 0.0, version: 0, |
| 51 | + ); |
| 52 | + |
| 53 | + /** @var \SpameriTests\Elastic\Data\Entity\EntityWithValueCollection $hydrated */ |
| 54 | + $hydrated = $entityFactory->create( |
| 55 | + $hit, |
| 56 | + \SpameriTests\Elastic\Data\Entity\EntityWithValueCollection::class, |
| 57 | + $entityManager, |
| 58 | + ); |
| 59 | + |
| 60 | + $genres = []; |
| 61 | + foreach ($hydrated->genres as $genre) { |
| 62 | + $genres[] = $genre->value(); |
| 63 | + } |
| 64 | + |
| 65 | + \Tester\Assert::same(['Action', 'Science Fiction'], $genres); |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + public function testAnEmptyValueCollectionStaysEmpty(): void |
| 70 | + { |
| 71 | + /** @var \Spameri\Elastic\EntityManager $entityManager */ |
| 72 | + $entityManager = $this->container->getByType(\Spameri\Elastic\EntityManager::class); |
| 73 | + /** @var \Spameri\Elastic\Factory\EntityFactory $entityFactory */ |
| 74 | + $entityFactory = $this->container->getByType(\Spameri\Elastic\Factory\EntityFactory::class); |
| 75 | + |
| 76 | + $hit = new \Spameri\ElasticQuery\Response\Result\Hit( |
| 77 | + source: ['genres' => []], |
| 78 | + position: 0, index: '', type: '', id: 'vc-2', score: 0.0, version: 0, |
| 79 | + ); |
| 80 | + |
| 81 | + /** @var \SpameriTests\Elastic\Data\Entity\EntityWithValueCollection $hydrated */ |
| 82 | + $hydrated = $entityFactory->create( |
| 83 | + $hit, |
| 84 | + \SpameriTests\Elastic\Data\Entity\EntityWithValueCollection::class, |
| 85 | + $entityManager, |
| 86 | + ); |
| 87 | + |
| 88 | + \Tester\Assert::same(0, \iterator_count($hydrated->genres->getIterator())); |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + public function testAnAbsentValueCollectionIsNotAnError(): void |
| 93 | + { |
| 94 | + /** @var \Spameri\Elastic\EntityManager $entityManager */ |
| 95 | + $entityManager = $this->container->getByType(\Spameri\Elastic\EntityManager::class); |
| 96 | + /** @var \Spameri\Elastic\Factory\EntityFactory $entityFactory */ |
| 97 | + $entityFactory = $this->container->getByType(\Spameri\Elastic\Factory\EntityFactory::class); |
| 98 | + |
| 99 | + // A document written before the field existed. It has to read as empty |
| 100 | + // rather than throw, or one old document takes down a whole index. |
| 101 | + $hit = new \Spameri\ElasticQuery\Response\Result\Hit( |
| 102 | + source: [], |
| 103 | + position: 0, index: '', type: '', id: 'vc-3', score: 0.0, version: 0, |
| 104 | + ); |
| 105 | + |
| 106 | + /** @var \SpameriTests\Elastic\Data\Entity\EntityWithValueCollection $hydrated */ |
| 107 | + $hydrated = $entityFactory->create( |
| 108 | + $hit, |
| 109 | + \SpameriTests\Elastic\Data\Entity\EntityWithValueCollection::class, |
| 110 | + $entityManager, |
| 111 | + ); |
| 112 | + |
| 113 | + \Tester\Assert::same(0, \iterator_count($hydrated->genres->getIterator())); |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | + public function testNullMembersAreNotTurnedIntoValues(): void |
| 118 | + { |
| 119 | + /** @var \Spameri\Elastic\EntityManager $entityManager */ |
| 120 | + $entityManager = $this->container->getByType(\Spameri\Elastic\EntityManager::class); |
| 121 | + /** @var \Spameri\Elastic\Factory\EntityFactory $entityFactory */ |
| 122 | + $entityFactory = $this->container->getByType(\Spameri\Elastic\Factory\EntityFactory::class); |
| 123 | + |
| 124 | + $hit = new \Spameri\ElasticQuery\Response\Result\Hit( |
| 125 | + source: ['genres' => ['Action', NULL, '', 'Drama']], |
| 126 | + position: 0, index: '', type: '', id: 'vc-4', score: 0.0, version: 0, |
| 127 | + ); |
| 128 | + |
| 129 | + /** @var \SpameriTests\Elastic\Data\Entity\EntityWithValueCollection $hydrated */ |
| 130 | + $hydrated = $entityFactory->create( |
| 131 | + $hit, |
| 132 | + \SpameriTests\Elastic\Data\Entity\EntityWithValueCollection::class, |
| 133 | + $entityManager, |
| 134 | + ); |
| 135 | + |
| 136 | + $genres = []; |
| 137 | + foreach ($hydrated->genres as $genre) { |
| 138 | + $genres[] = $genre->value(); |
| 139 | + } |
| 140 | + |
| 141 | + \Tester\Assert::same(['Action', 'Drama'], $genres); |
| 142 | + } |
| 143 | + |
| 144 | +} |
| 145 | + |
| 146 | +(new ValueCollectionTest())->run(); |
0 commit comments