|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace DirectoryTree\OpenSearchAdapter\Testing\Fakes; |
| 4 | + |
| 5 | +use DirectoryTree\OpenSearchAdapter\Documents\Document; |
| 6 | +use DirectoryTree\OpenSearchAdapter\Documents\DocumentManagerInterface; |
| 7 | +use DirectoryTree\OpenSearchAdapter\Documents\DocumentRouting; |
| 8 | +use DirectoryTree\OpenSearchAdapter\Search\SearchRequest; |
| 9 | +use DirectoryTree\OpenSearchAdapter\Search\SearchResponse; |
| 10 | +use PHPUnit\Framework\Assert as PHPUnit; |
| 11 | + |
| 12 | +/** |
| 13 | + * Fakes OpenSearch document operations for tests. |
| 14 | + */ |
| 15 | +class FakeDocumentManager implements DocumentManagerInterface |
| 16 | +{ |
| 17 | + /** |
| 18 | + * The indexed document operations. |
| 19 | + * |
| 20 | + * @var array<int, array{index: string, documents: array<int, Document>, refresh: bool, routing: DocumentRouting|null}> |
| 21 | + */ |
| 22 | + protected array $indexed = []; |
| 23 | + |
| 24 | + /** |
| 25 | + * The deleted document operations. |
| 26 | + * |
| 27 | + * @var array<int, array{index: string, ids: array<int, string>, refresh: bool, routing: DocumentRouting|null}> |
| 28 | + */ |
| 29 | + protected array $deleted = []; |
| 30 | + |
| 31 | + /** |
| 32 | + * The delete by query operations. |
| 33 | + * |
| 34 | + * @var array<int, array{index: string, query: array<string, mixed>, refresh: bool}> |
| 35 | + */ |
| 36 | + protected array $deletedByQuery = []; |
| 37 | + |
| 38 | + /** |
| 39 | + * The search operations. |
| 40 | + * |
| 41 | + * @var array<int, array{index: string, request: SearchRequest}> |
| 42 | + */ |
| 43 | + protected array $searched = []; |
| 44 | + |
| 45 | + /** |
| 46 | + * The response that should be returned for searches. |
| 47 | + */ |
| 48 | + protected SearchResponse $response; |
| 49 | + |
| 50 | + /** |
| 51 | + * Create a new fake document manager instance. |
| 52 | + */ |
| 53 | + public function __construct() |
| 54 | + { |
| 55 | + $this->response = new SearchResponse([ |
| 56 | + 'hits' => [ |
| 57 | + 'hits' => [], |
| 58 | + ], |
| 59 | + ]); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Set the search response returned by the fake. |
| 64 | + */ |
| 65 | + public function respondWith(SearchResponse $response): static |
| 66 | + { |
| 67 | + $this->response = $response; |
| 68 | + |
| 69 | + return $this; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Index the given documents into OpenSearch. |
| 74 | + * |
| 75 | + * @param array<int, Document> $documents |
| 76 | + */ |
| 77 | + public function index(string $index, array $documents, bool $refresh = false, ?DocumentRouting $routing = null): static |
| 78 | + { |
| 79 | + $this->indexed[] = compact('index', 'documents', 'refresh', 'routing'); |
| 80 | + |
| 81 | + return $this; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Delete the given documents from OpenSearch. |
| 86 | + * |
| 87 | + * @param array<int, string> $ids |
| 88 | + */ |
| 89 | + public function delete(string $index, array $ids, bool $refresh = false, ?DocumentRouting $routing = null): static |
| 90 | + { |
| 91 | + $this->deleted[] = compact('index', 'ids', 'refresh', 'routing'); |
| 92 | + |
| 93 | + return $this; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Delete documents that match the given OpenSearch query. |
| 98 | + * |
| 99 | + * @param array<string, mixed> $query |
| 100 | + */ |
| 101 | + public function deleteByQuery(string $index, array $query, bool $refresh = false): static |
| 102 | + { |
| 103 | + $this->deletedByQuery[] = compact('index', 'query', 'refresh'); |
| 104 | + |
| 105 | + return $this; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Search an index using the given search request. |
| 110 | + */ |
| 111 | + public function search(string $index, SearchRequest $request): SearchResponse |
| 112 | + { |
| 113 | + $this->searched[] = compact('index', 'request'); |
| 114 | + |
| 115 | + return $this->response; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Assert that the given documents were indexed. |
| 120 | + * |
| 121 | + * @param array<int, Document> $documents |
| 122 | + */ |
| 123 | + public function assertIndexed(string $index, array $documents, bool $refresh = false, ?DocumentRouting $routing = null): static |
| 124 | + { |
| 125 | + PHPUnit::assertContainsEquals(compact('index', 'documents', 'refresh', 'routing'), $this->indexed); |
| 126 | + |
| 127 | + return $this; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Assert that the given document IDs were deleted. |
| 132 | + * |
| 133 | + * @param array<int, string> $ids |
| 134 | + */ |
| 135 | + public function assertDeleted(string $index, array $ids, bool $refresh = false, ?DocumentRouting $routing = null): static |
| 136 | + { |
| 137 | + PHPUnit::assertContainsEquals(compact('index', 'ids', 'refresh', 'routing'), $this->deleted); |
| 138 | + |
| 139 | + return $this; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Assert that the given query was deleted. |
| 144 | + * |
| 145 | + * @param array<string, mixed> $query |
| 146 | + */ |
| 147 | + public function assertDeletedByQuery(string $index, array $query, bool $refresh = false): static |
| 148 | + { |
| 149 | + PHPUnit::assertContainsEquals(compact('index', 'query', 'refresh'), $this->deletedByQuery); |
| 150 | + |
| 151 | + return $this; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Assert that the given index was searched. |
| 156 | + */ |
| 157 | + public function assertSearched(string $index, SearchRequest $request): static |
| 158 | + { |
| 159 | + PHPUnit::assertContainsEquals(compact('index', 'request'), $this->searched); |
| 160 | + |
| 161 | + return $this; |
| 162 | + } |
| 163 | +} |
0 commit comments