DocumentsTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. declare(strict_types=1);
  3. namespace Tests\Endpoints;
  4. use MeiliSearch\Exceptions\ApiException;
  5. use MeiliSearch\Exceptions\InvalidArgumentException;
  6. use Tests\TestCase;
  7. final class DocumentsTest extends TestCase
  8. {
  9. public function testAddDocuments(): void
  10. {
  11. $index = $this->client->createIndex('documents');
  12. $promise = $index->addDocuments(self::DOCUMENTS);
  13. $this->assertIsValidPromise($promise);
  14. $index->waitForPendingUpdate($promise['updateId']);
  15. $response = $index->getDocuments();
  16. $this->assertCount(\count(self::DOCUMENTS), $response);
  17. }
  18. public function testGetSingleDocumentWithIntegerDocumentId(): void
  19. {
  20. $index = $this->client->createIndex('documents');
  21. $response = $index->addDocuments(self::DOCUMENTS);
  22. $index->waitForPendingUpdate($response['updateId']);
  23. $doc = $this->findDocumentWithId(self::DOCUMENTS, 4);
  24. $response = $index->getDocument($doc['id']);
  25. $this->assertIsArray($response);
  26. $this->assertSame($doc['id'], $response['id']);
  27. $this->assertSame($doc['title'], $response['title']);
  28. }
  29. public function testGetSingleDocumentWithStringDocumentId(): void
  30. {
  31. $stringDocumentId = 'myUniqueId';
  32. $index = $this->client->createIndex('documents');
  33. $addDocumentResponse = $index->addDocuments([['id' => $stringDocumentId]]);
  34. $index->waitForPendingUpdate($addDocumentResponse['updateId']);
  35. $response = $index->getDocument($stringDocumentId);
  36. $this->assertIsArray($response);
  37. $this->assertSame($stringDocumentId, $response['id']);
  38. }
  39. public function testReplaceDocuments(): void
  40. {
  41. $index = $this->client->createIndex('documents');
  42. $response = $index->addDocuments(self::DOCUMENTS);
  43. $index->waitForPendingUpdate($response['updateId']);
  44. $replacement = [
  45. 'id' => 2,
  46. 'title' => 'The Red And The Black',
  47. ];
  48. $response = $index->addDocuments([$replacement]);
  49. $this->assertIsValidPromise($response);
  50. $index->waitForPendingUpdate($response['updateId']);
  51. $response = $index->getDocument($replacement['id']);
  52. $this->assertSame($replacement['id'], $response['id']);
  53. $this->assertSame($replacement['title'], $response['title']);
  54. $this->assertFalse(array_search('comment', $response));
  55. $response = $index->getDocuments();
  56. $this->assertCount(\count(self::DOCUMENTS), $response);
  57. }
  58. public function testUpdateDocuments(): void
  59. {
  60. $index = $this->client->createIndex('documents');
  61. $promise = $index->addDocuments(self::DOCUMENTS);
  62. $index->waitForPendingUpdate($promise['updateId']);
  63. $replacement = [
  64. 'id' => 456,
  65. 'title' => 'The Little Prince',
  66. ];
  67. $promise = $index->updateDocuments([$replacement]);
  68. $this->assertIsValidPromise($promise);
  69. $index->waitForPendingUpdate($promise['updateId']);
  70. $response = $index->getDocument($replacement['id']);
  71. $this->assertSame($replacement['id'], $response['id']);
  72. $this->assertSame($replacement['title'], $response['title']);
  73. $this->assertArrayHasKey('comment', $response);
  74. $response = $index->getDocuments();
  75. $this->assertCount(\count(self::DOCUMENTS), $response);
  76. }
  77. public function testAddWithUpdateDocuments(): void
  78. {
  79. $index = $this->client->createIndex('documents');
  80. $response = $index->addDocuments(self::DOCUMENTS);
  81. $index->waitForPendingUpdate($response['updateId']);
  82. $document = [
  83. 'id' => 9,
  84. 'title' => '1984',
  85. ];
  86. $promise = $index->updateDocuments([$document]);
  87. $this->assertIsValidPromise($promise);
  88. $index->waitForPendingUpdate($promise['updateId']);
  89. $response = $index->getDocument($document['id']);
  90. $this->assertSame($document['id'], $response['id']);
  91. $this->assertSame($document['title'], $response['title']);
  92. $this->assertFalse(array_search('comment', $response));
  93. $response = $index->getDocuments();
  94. $this->assertCount(\count(self::DOCUMENTS) + 1, $response);
  95. }
  96. public function testDeleteNonExistingDocument(): void
  97. {
  98. $index = $this->client->createIndex('documents');
  99. $response = $index->addDocuments(self::DOCUMENTS);
  100. $index->waitForPendingUpdate($response['updateId']);
  101. $documentId = 9;
  102. $promise = $index->deleteDocument($documentId);
  103. $this->assertIsValidPromise($promise);
  104. $index->waitForPendingUpdate($promise['updateId']);
  105. $response = $index->getDocuments();
  106. $this->assertCount(\count(self::DOCUMENTS), $response);
  107. $this->assertNull($this->findDocumentWithId($response, $documentId));
  108. }
  109. public function testDeleteSingleExistingDocumentWithDocumentIdAsInteger(): void
  110. {
  111. $index = $this->client->createIndex('documents');
  112. $response = $index->addDocuments(self::DOCUMENTS);
  113. $index->waitForPendingUpdate($response['updateId']);
  114. $documentId = 123;
  115. $promise = $index->deleteDocument($documentId);
  116. $this->assertIsValidPromise($promise);
  117. $index->waitForPendingUpdate($promise['updateId']);
  118. $response = $index->getDocuments();
  119. $this->assertCount(\count(self::DOCUMENTS) - 1, $response);
  120. $this->assertNull($this->findDocumentWithId($response, $documentId));
  121. }
  122. public function testDeleteSingleExistingDocumentWithDocumentIdAsString(): void
  123. {
  124. $stringDocumentId = 'myUniqueId';
  125. $index = $this->client->createIndex('documents');
  126. $addDocumentResponse = $index->addDocuments([['id' => $stringDocumentId]]);
  127. $index->waitForPendingUpdate($addDocumentResponse['updateId']);
  128. $promise = $index->deleteDocument($stringDocumentId);
  129. $index->waitForPendingUpdate($promise['updateId']);
  130. $response = $index->getDocuments();
  131. $this->assertEmpty($response);
  132. }
  133. public function testDeleteMultipleDocumentsWithDocumentIdAsInteger(): void
  134. {
  135. $index = $this->client->createIndex('documents');
  136. $response = $index->addDocuments(self::DOCUMENTS);
  137. $index->waitForPendingUpdate($response['updateId']);
  138. $documentIds = [1, 2];
  139. $promise = $index->deleteDocuments($documentIds);
  140. $this->assertIsValidPromise($promise);
  141. $index->waitForPendingUpdate($promise['updateId']);
  142. $response = $index->getDocuments();
  143. $this->assertCount(\count(self::DOCUMENTS) - 2, $response);
  144. $this->assertNull($this->findDocumentWithId($response, $documentIds[0]));
  145. $this->assertNull($this->findDocumentWithId($response, $documentIds[1]));
  146. }
  147. public function testDeleteMultipleDocumentsWithDocumentIdAsString(): void
  148. {
  149. $documents = [
  150. ['id' => 'myUniqueId1'],
  151. ['id' => 'myUniqueId2'],
  152. ['id' => 'myUniqueId3'],
  153. ];
  154. $index = $this->client->createIndex('documents');
  155. $addDocumentResponse = $index->addDocuments($documents);
  156. $index->waitForPendingUpdate($addDocumentResponse['updateId']);
  157. $promise = $index->deleteDocuments(['myUniqueId1', 'myUniqueId3']);
  158. $index->waitForPendingUpdate($promise['updateId']);
  159. $response = $index->getDocuments();
  160. $this->assertCount(1, $response);
  161. $this->assertSame([['id' => 'myUniqueId2']], $response);
  162. }
  163. public function testDeleteAllDocuments(): void
  164. {
  165. $index = $this->client->createIndex('documents');
  166. $response = $index->addDocuments(self::DOCUMENTS);
  167. $index->waitForPendingUpdate($response['updateId']);
  168. $promise = $index->deleteAllDocuments();
  169. $this->assertIsValidPromise($promise);
  170. $index->waitForPendingUpdate($promise['updateId']);
  171. $response = $index->getDocuments();
  172. $this->assertCount(0, $response);
  173. }
  174. public function testExceptionIfNoDocumentIdWhenGetting(): void
  175. {
  176. $index = $this->client->createIndex('new-index');
  177. $this->expectException(ApiException::class);
  178. $index->getDocument(1);
  179. }
  180. public function testAddDocumentWithPrimaryKey(): void
  181. {
  182. $documents = [
  183. [
  184. 'id' => 1,
  185. 'unique' => 1,
  186. 'title' => 'Le Rouge et le Noir',
  187. ],
  188. ];
  189. $index = $this->client->createIndex('an-index');
  190. $response = $index->addDocuments($documents, 'unique');
  191. $this->assertArrayHasKey('updateId', $response);
  192. $index->waitForPendingUpdate($response['updateId']);
  193. $this->assertSame('unique', $index->fetchPrimaryKey());
  194. $this->assertCount(1, $index->getDocuments());
  195. }
  196. public function testUpdateDocumentWithPrimaryKey(): void
  197. {
  198. $documents = [
  199. [
  200. 'id' => 1,
  201. 'unique' => 1,
  202. 'title' => 'Le Rouge et le Noir',
  203. ],
  204. ];
  205. $index = $this->client->createIndex('index');
  206. $promise = $index->updateDocuments($documents, 'unique');
  207. $this->assertIsValidPromise($promise);
  208. $index->waitForPendingUpdate($promise['updateId']);
  209. $this->assertSame('unique', $index->fetchPrimaryKey());
  210. $this->assertCount(1, $index->getDocuments());
  211. }
  212. /**
  213. * @dataProvider invalidDocumentIds
  214. */
  215. public function testFetchingDocumentWithInvalidId($documentId): void
  216. {
  217. $index = $this->client->createIndex('an-index');
  218. $this->expectException(InvalidArgumentException::class);
  219. $index->getDocument($documentId);
  220. }
  221. /**
  222. * @dataProvider invalidDocumentIds
  223. */
  224. public function testDeletingDocumentWithInvalidId($documentId): void
  225. {
  226. $index = $this->client->createIndex('an-index');
  227. $this->expectException(InvalidArgumentException::class);
  228. $index->deleteDocument($documentId);
  229. }
  230. public function invalidDocumentIds(): array
  231. {
  232. return [
  233. 'documentId as null' => [null],
  234. 'documentId as bool' => [true],
  235. 'documentId as empty string' => [''],
  236. 'documentId as float' => [2.1],
  237. 'documentId as array' => [[]],
  238. 'documentId as object' => [new \stdClass()],
  239. 'documentId as resource' => [tmpfile()],
  240. ];
  241. }
  242. private function findDocumentWithId($documents, $documentId)
  243. {
  244. foreach ($documents as $document) {
  245. if ($document['id'] == $documentId) {
  246. return $document;
  247. }
  248. }
  249. }
  250. }