|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Liquetsoft\Fias\Elastic\Tests\ElasticSearchRepository; |
| 6 | + |
| 7 | +use Elasticsearch\Client; |
| 8 | +use Liquetsoft\Fias\Elastic\ClientProvider\ClientProvider; |
| 9 | +use Liquetsoft\Fias\Elastic\ElasticSearchRepository\BaseElasticSearchRepository; |
| 10 | +use Liquetsoft\Fias\Elastic\Exception\ElasticSearchRepositoryException; |
| 11 | +use Liquetsoft\Fias\Elastic\QueryBuilder\QueryBuilder; |
| 12 | +use Liquetsoft\Fias\Elastic\Tests\BaseCase; |
| 13 | +use stdClass; |
| 14 | +use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
| 15 | +use Throwable; |
| 16 | + |
| 17 | +/** |
| 18 | + * Тест для репозитория elasticsearch. |
| 19 | + * |
| 20 | + * @internal |
| 21 | + */ |
| 22 | +class BaseElasticSearchRepositoryTest extends BaseCase |
| 23 | +{ |
| 24 | + /** |
| 25 | + * Проверяет, что репозтиторий правильно вернет один объект по условию. |
| 26 | + * |
| 27 | + * @throws Throwable |
| 28 | + */ |
| 29 | + public function testOne(): void |
| 30 | + { |
| 31 | + $class = 'test'; |
| 32 | + $queryData = [ |
| 33 | + 'test' => 'value', |
| 34 | + ]; |
| 35 | + $hits = [ |
| 36 | + 'hits' => [ |
| 37 | + 'hits' => [ |
| 38 | + [ |
| 39 | + '_source' => ['test' => 'value'], |
| 40 | + ], |
| 41 | + ], |
| 42 | + ], |
| 43 | + ]; |
| 44 | + |
| 45 | + $query = $this->getMockBuilder(QueryBuilder::class)->getMock(); |
| 46 | + $query->method('getQuery')->willReturn($queryData); |
| 47 | + |
| 48 | + $client = $this->getMockBuilder(Client::class)->disableOriginalConstructor()->getMock(); |
| 49 | + $client->expects($this->once()) |
| 50 | + ->method('search') |
| 51 | + ->with( |
| 52 | + $this->identicalTo($queryData) |
| 53 | + ) |
| 54 | + ->willReturn($hits) |
| 55 | + ; |
| 56 | + |
| 57 | + $clientProvider = $this->getMockBuilder(ClientProvider::class)->getMock(); |
| 58 | + $clientProvider->method('provide')->willReturn($client); |
| 59 | + |
| 60 | + $object = new stdClass(); |
| 61 | + |
| 62 | + $denormalizer = $this->getMockBuilder(DenormalizerInterface::class)->getMock(); |
| 63 | + $denormalizer->expects($this->once()) |
| 64 | + ->method('denormalize') |
| 65 | + ->with( |
| 66 | + $this->identicalTo($hits['hits']['hits'][0]['_source']), |
| 67 | + $this->identicalTo($class) |
| 68 | + ) |
| 69 | + ->willReturn($object) |
| 70 | + ; |
| 71 | + |
| 72 | + $repo = new BaseElasticSearchRepository($clientProvider, $denormalizer); |
| 73 | + $testObject = $repo->one($query, $class); |
| 74 | + |
| 75 | + $this->assertSame($object, $testObject); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Проверяет, что репозтиторий правильно вернет список объектов по условию. |
| 80 | + * |
| 81 | + * @throws Throwable |
| 82 | + */ |
| 83 | + public function testAll(): void |
| 84 | + { |
| 85 | + $class = 'test'; |
| 86 | + $queryData = [ |
| 87 | + 'test' => 'value', |
| 88 | + ]; |
| 89 | + $hits = [ |
| 90 | + 'hits' => [ |
| 91 | + 'hits' => [ |
| 92 | + [ |
| 93 | + '_source' => ['test' => 'value'], |
| 94 | + ], |
| 95 | + [ |
| 96 | + '_source' => ['test1' => 'value 1'], |
| 97 | + ], |
| 98 | + ], |
| 99 | + ], |
| 100 | + ]; |
| 101 | + |
| 102 | + $query = $this->getMockBuilder(QueryBuilder::class)->getMock(); |
| 103 | + $query->method('getQuery')->willReturn($queryData); |
| 104 | + |
| 105 | + $client = $this->getMockBuilder(Client::class)->disableOriginalConstructor()->getMock(); |
| 106 | + $client->expects($this->once()) |
| 107 | + ->method('search') |
| 108 | + ->with( |
| 109 | + $this->identicalTo($queryData) |
| 110 | + ) |
| 111 | + ->willReturn($hits) |
| 112 | + ; |
| 113 | + |
| 114 | + $clientProvider = $this->getMockBuilder(ClientProvider::class)->getMock(); |
| 115 | + $clientProvider->method('provide')->willReturn($client); |
| 116 | + |
| 117 | + $object = new stdClass(); |
| 118 | + $object1 = new stdClass(); |
| 119 | + |
| 120 | + $denormalizer = $this->getMockBuilder(DenormalizerInterface::class)->getMock(); |
| 121 | + $denormalizer->method('denormalize') |
| 122 | + ->willReturnMap( |
| 123 | + [ |
| 124 | + [$hits['hits']['hits'][0]['_source'], $class, null, [], $object], |
| 125 | + [$hits['hits']['hits'][1]['_source'], $class, null, [], $object1], |
| 126 | + ] |
| 127 | + ) |
| 128 | + ; |
| 129 | + |
| 130 | + $repo = new BaseElasticSearchRepository($clientProvider, $denormalizer); |
| 131 | + $testObjects = $repo->all($query, $class); |
| 132 | + |
| 133 | + $this->assertSame([$object, $object1], $testObjects); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Проверяет, что репозтиторий выбросит исключение при неполном ответе от elasticsearch. |
| 138 | + * |
| 139 | + * @throws Throwable |
| 140 | + */ |
| 141 | + public function testEmptySourceException(): void |
| 142 | + { |
| 143 | + $class = 'test'; |
| 144 | + $queryData = [ |
| 145 | + 'test' => 'value', |
| 146 | + ]; |
| 147 | + $hits = [ |
| 148 | + 'hits' => [ |
| 149 | + 'hits' => [ |
| 150 | + [], |
| 151 | + ], |
| 152 | + ], |
| 153 | + ]; |
| 154 | + |
| 155 | + $query = $this->getMockBuilder(QueryBuilder::class)->getMock(); |
| 156 | + $query->method('getQuery')->willReturn($queryData); |
| 157 | + |
| 158 | + $client = $this->getMockBuilder(Client::class)->disableOriginalConstructor()->getMock(); |
| 159 | + $client->method('search')->willReturn($hits); |
| 160 | + |
| 161 | + $clientProvider = $this->getMockBuilder(ClientProvider::class)->getMock(); |
| 162 | + $clientProvider->method('provide')->willReturn($client); |
| 163 | + |
| 164 | + $denormalizer = $this->getMockBuilder(DenormalizerInterface::class)->getMock(); |
| 165 | + |
| 166 | + $repo = new BaseElasticSearchRepository($clientProvider, $denormalizer); |
| 167 | + |
| 168 | + $this->expectException(ElasticSearchRepositoryException::class); |
| 169 | + $repo->one($query, $class); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Проверяет, что репозтиторий выбросит исключение при неправильном ответе от denprmalizer. |
| 174 | + * |
| 175 | + * @throws Throwable |
| 176 | + */ |
| 177 | + public function testBrokenDenormalizeException(): void |
| 178 | + { |
| 179 | + $class = 'test'; |
| 180 | + $queryData = [ |
| 181 | + 'test' => 'value', |
| 182 | + ]; |
| 183 | + $hits = [ |
| 184 | + 'hits' => [ |
| 185 | + 'hits' => [ |
| 186 | + [ |
| 187 | + '_source' => ['test' => 'value'], |
| 188 | + ], |
| 189 | + ], |
| 190 | + ], |
| 191 | + ]; |
| 192 | + |
| 193 | + $query = $this->getMockBuilder(QueryBuilder::class)->getMock(); |
| 194 | + $query->method('getQuery')->willReturn($queryData); |
| 195 | + |
| 196 | + $client = $this->getMockBuilder(Client::class)->disableOriginalConstructor()->getMock(); |
| 197 | + $client->method('search')->willReturn($hits); |
| 198 | + |
| 199 | + $clientProvider = $this->getMockBuilder(ClientProvider::class)->getMock(); |
| 200 | + $clientProvider->method('provide')->willReturn($client); |
| 201 | + |
| 202 | + $denormalizer = $this->getMockBuilder(DenormalizerInterface::class)->getMock(); |
| 203 | + $denormalizer->method('denormalize')->willReturn('test'); |
| 204 | + |
| 205 | + $repo = new BaseElasticSearchRepository($clientProvider, $denormalizer); |
| 206 | + |
| 207 | + $this->expectException(ElasticSearchRepositoryException::class); |
| 208 | + $repo->one($query, $class); |
| 209 | + } |
| 210 | +} |
0 commit comments