|
2 | 2 |
|
3 | 3 | namespace DirectoryTree\OpenSearchAdapter\Tests\Unit\Search; |
4 | 4 |
|
| 5 | +use DirectoryTree\OpenSearchAdapter\Documents\Document; |
5 | 6 | use DirectoryTree\OpenSearchAdapter\Search\Aggregation; |
6 | 7 | use DirectoryTree\OpenSearchAdapter\Search\Hit; |
7 | 8 | use DirectoryTree\OpenSearchAdapter\Search\SearchResponse; |
|
190 | 191 | ], |
191 | 192 | ], $searchResponse->raw()); |
192 | 193 | }); |
| 194 | + |
| 195 | +test('fake response can be created without hits', function () { |
| 196 | + $searchResponse = SearchResponse::fake(); |
| 197 | + |
| 198 | + expect($searchResponse->hits())->toBe([]) |
| 199 | + ->and($searchResponse->total())->toBe(0); |
| 200 | +}); |
| 201 | + |
| 202 | +test('fake response can be created with documents', function () { |
| 203 | + $searchResponse = SearchResponse::fake([ |
| 204 | + new Document('1', ['title' => 'First']), |
| 205 | + new Document('2', ['title' => 'Second']), |
| 206 | + ], 'posts'); |
| 207 | + |
| 208 | + expect($searchResponse->total())->toBe(2) |
| 209 | + ->and($searchResponse->hits()[0]->index())->toBe('posts') |
| 210 | + ->and($searchResponse->hits()[0]->id())->toBe('1') |
| 211 | + ->and($searchResponse->hits()[0]->source())->toBe(['title' => 'First']); |
| 212 | +}); |
| 213 | + |
| 214 | +test('fake response can be created with source arrays', function () { |
| 215 | + $searchResponse = SearchResponse::fake([ |
| 216 | + ['title' => 'First'], |
| 217 | + ]); |
| 218 | + |
| 219 | + expect($searchResponse->hits()[0]->id())->toBe('1') |
| 220 | + ->and($searchResponse->hits()[0]->source())->toBe(['title' => 'First']); |
| 221 | +}); |
| 222 | + |
| 223 | +test('fake response can be created with raw hits', function () { |
| 224 | + $searchResponse = SearchResponse::fake([ |
| 225 | + [ |
| 226 | + '_index' => 'articles', |
| 227 | + '_id' => 'post-1', |
| 228 | + '_score' => 4.2, |
| 229 | + '_source' => [ |
| 230 | + 'title' => 'First', |
| 231 | + ], |
| 232 | + ], |
| 233 | + ]); |
| 234 | + |
| 235 | + expect($searchResponse->hits()[0]->index())->toBe('articles') |
| 236 | + ->and($searchResponse->hits()[0]->id())->toBe('post-1') |
| 237 | + ->and($searchResponse->hits()[0]->score())->toBe(4.2); |
| 238 | +}); |
0 commit comments