IndexTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. declare(strict_types=1);
  3. namespace Tests\Endpoints;
  4. use MeiliSearch\Endpoints\Indexes;
  5. use MeiliSearch\Exceptions\ApiException;
  6. use MeiliSearch\Exceptions\TimeOutException;
  7. use Tests\TestCase;
  8. final class IndexTest extends TestCase
  9. {
  10. private $index;
  11. protected function setUp(): void
  12. {
  13. parent::setUp();
  14. $this->index = $this->client->createIndex('index');
  15. }
  16. public function testGetPrimaryKey(): void
  17. {
  18. $indexB = $this->client->createIndex(
  19. 'indexB',
  20. ['primaryKey' => 'objectId']
  21. );
  22. $this->assertNull($this->index->getPrimaryKey());
  23. $this->assertSame('objectId', $indexB->getPrimaryKey());
  24. }
  25. public function testGetUid(): void
  26. {
  27. $indexB = $this->client->createIndex(
  28. 'indexB',
  29. ['primaryKey' => 'objectId']
  30. );
  31. $this->assertSame('index', $this->index->getUid());
  32. $this->assertSame('indexB', $indexB->getUid());
  33. }
  34. public function testfetchRawInfo(): void
  35. {
  36. $index = $this->client->createIndex(
  37. 'indexB',
  38. ['primaryKey' => 'objectId']
  39. );
  40. $response = $index->fetchRawInfo();
  41. $this->assertArrayHasKey('primaryKey', $response);
  42. $this->assertArrayHasKey('uid', $response);
  43. $this->assertArrayHasKey('createdAt', $response);
  44. $this->assertArrayHasKey('updatedAt', $response);
  45. $this->assertSame($response['primaryKey'], 'objectId');
  46. $this->assertSame($response['uid'], 'indexB');
  47. }
  48. public function testPrimaryKeyUpdate(): void
  49. {
  50. $primaryKey = 'id';
  51. $index = $this->index->update(['primaryKey' => $primaryKey]);
  52. $this->assertInstanceOf(Indexes::class, $index);
  53. $this->assertSame($index->getPrimaryKey(), $primaryKey);
  54. $this->assertSame($index->getUid(), 'index');
  55. $this->assertSame($this->index->getPrimaryKey(), $primaryKey);
  56. $this->assertSame($this->index->getUid(), 'index');
  57. }
  58. public function testExceptionIsThrownWhenOverwritingPrimaryKey(): void
  59. {
  60. $index = $this->client->createIndex(
  61. 'indexB',
  62. ['primaryKey' => 'objectId']
  63. );
  64. $this->expectException(ApiException::class);
  65. $index->update(['primaryKey' => 'objectID']);
  66. }
  67. public function testIndexStats(): void
  68. {
  69. $stats = $this->index->stats();
  70. $this->assertArrayHasKey('numberOfDocuments', $stats);
  71. $this->assertEquals(0, $stats['numberOfDocuments']);
  72. $this->assertArrayHasKey('isIndexing', $stats);
  73. $this->assertArrayHasKey('fieldsDistribution', $stats);
  74. }
  75. public function testFetchInfo(): void
  76. {
  77. $uid = 'indexA';
  78. $this->client->createIndex(
  79. $uid,
  80. ['primaryKey' => 'objectID']
  81. );
  82. $index = $this->client->index($uid);
  83. $this->assertInstanceOf(Indexes::class, $index);
  84. $this->assertNull($index->getPrimaryKey());
  85. $index = $index->fetchInfo();
  86. $this->assertInstanceOf(Indexes::class, $index);
  87. $this->assertSame('objectID', $index->getPrimaryKey());
  88. }
  89. public function testGetAndFetchPrimaryKey(): void
  90. {
  91. $uid = 'indexA';
  92. $this->client->createIndex(
  93. $uid,
  94. ['primaryKey' => 'objectID']
  95. );
  96. $index = $this->client->index($uid);
  97. $this->assertNull($index->getPrimaryKey());
  98. $this->assertSame('objectID', $index->fetchPrimaryKey());
  99. $this->assertSame('objectID', $index->getPrimaryKey());
  100. }
  101. public function testWaitForPendingUpdateDefault(): void
  102. {
  103. $promise = $this->index->addDocuments([['id' => 1, 'title' => 'Pride and Prejudice']]);
  104. $response = $this->index->waitForPendingUpdate($promise['updateId']);
  105. $this->assertIsArray($response);
  106. $this->assertSame($response['status'], 'processed');
  107. $this->assertSame($response['updateId'], $promise['updateId']);
  108. $this->assertArrayHasKey('type', $response);
  109. $this->assertIsArray($response['type']);
  110. $this->assertArrayHasKey('duration', $response);
  111. $this->assertArrayHasKey('enqueuedAt', $response);
  112. $this->assertArrayHasKey('processedAt', $response);
  113. }
  114. public function testWaitForPendingUpdateWithTimeoutAndInterval(): void
  115. {
  116. $promise = $this->index->addDocuments([['id' => 1, 'title' => 'Pride and Prejudice']]);
  117. $response = $this->index->waitForPendingUpdate($promise['updateId'], 100, 20);
  118. $this->assertIsArray($response);
  119. $this->assertSame($response['status'], 'processed');
  120. $this->assertSame($response['updateId'], $promise['updateId']);
  121. $this->assertArrayHasKey('type', $response);
  122. $this->assertIsArray($response['type']);
  123. $this->assertArrayHasKey('duration', $response);
  124. $this->assertArrayHasKey('enqueuedAt', $response);
  125. $this->assertArrayHasKey('processedAt', $response);
  126. }
  127. public function testWaitForPendingUpdateWithTimeout(): void
  128. {
  129. $promise = $this->index->addDocuments([['id' => 1, 'title' => 'Pride and Prejudice']]);
  130. $response = $this->index->waitForPendingUpdate($promise['updateId'], 100);
  131. $this->assertIsArray($response);
  132. $this->assertSame($response['status'], 'processed');
  133. $this->assertSame($response['updateId'], $promise['updateId']);
  134. $this->assertArrayHasKey('type', $response);
  135. $this->assertIsArray($response['type']);
  136. $this->assertArrayHasKey('duration', $response);
  137. $this->assertArrayHasKey('enqueuedAt', $response);
  138. $this->assertArrayHasKey('processedAt', $response);
  139. }
  140. public function testExceptionWhenPendingUpdateTimeOut(): void
  141. {
  142. $this->expectException(TimeOutException::class);
  143. $res = $this->index->addDocuments([['id' => 1, 'title' => 'Pride and Prejudice']]);
  144. $this->index->waitForPendingUpdate($res['updateId'], 0, 20);
  145. }
  146. public function testDeleteIndexes(): void
  147. {
  148. $this->index = $this->client->createIndex('indexA');
  149. $indexB = $this->client->createIndex('indexB');
  150. $res = $this->index->delete();
  151. $this->assertEmpty($res);
  152. $res = $indexB->delete();
  153. $this->assertEmpty($res);
  154. }
  155. public function testExceptionIsThrownIfNoIndexWhenShowing(): void
  156. {
  157. $this->index->delete();
  158. $this->expectException(ApiException::class);
  159. $this->index->fetchInfo();
  160. }
  161. public function testExceptionIsThrownIfNoIndexWhenUpdating(): void
  162. {
  163. $this->index->delete();
  164. $this->expectException(ApiException::class);
  165. $this->index->update(['primaryKey' => 'objectID']);
  166. }
  167. public function testExceptionIsThrownIfNoIndexWhenDeleting(): void
  168. {
  169. $this->index->delete();
  170. $this->expectException(ApiException::class);
  171. $this->index->delete();
  172. }
  173. }