SettingsTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. declare(strict_types=1);
  3. namespace Tests\Settings;
  4. use Tests\TestCase;
  5. final class SettingsTest extends TestCase
  6. {
  7. const DEFAULT_RANKING_RULES = [
  8. 'typo',
  9. 'words',
  10. 'proximity',
  11. 'attribute',
  12. 'wordsPosition',
  13. 'exactness',
  14. ];
  15. const DEFAULT_SEARCHABLE_ATTRIBUTES = ['*'];
  16. const DEFAULT_DISPLAYED_ATTRIBUTES = ['*'];
  17. public function testGetDefaultSettings(): void
  18. {
  19. $primaryKey = 'ObjectID';
  20. $settingA = $this->client
  21. ->createIndex('indexA')
  22. ->getSettings();
  23. $settingB = $this->client
  24. ->createIndex(
  25. 'indexB',
  26. ['primaryKey' => $primaryKey]
  27. )->getSettings();
  28. $this->assertEquals(self::DEFAULT_RANKING_RULES, $settingA['rankingRules']);
  29. $this->assertNull($settingA['distinctAttribute']);
  30. $this->assertIsArray($settingA['searchableAttributes']);
  31. $this->assertEquals(self::DEFAULT_SEARCHABLE_ATTRIBUTES, $settingA['searchableAttributes']);
  32. $this->assertIsArray($settingA['displayedAttributes']);
  33. $this->assertEquals(self::DEFAULT_DISPLAYED_ATTRIBUTES, $settingA['displayedAttributes']);
  34. $this->assertIsArray($settingA['stopWords']);
  35. $this->assertEmpty($settingA['stopWords']);
  36. $this->assertIsArray($settingA['synonyms']);
  37. $this->assertEmpty($settingA['synonyms']);
  38. $this->assertEquals(self::DEFAULT_RANKING_RULES, $settingB['rankingRules']);
  39. $this->assertNull($settingB['distinctAttribute']);
  40. $this->assertEquals(self::DEFAULT_SEARCHABLE_ATTRIBUTES, $settingB['searchableAttributes']);
  41. $this->assertEquals(self::DEFAULT_DISPLAYED_ATTRIBUTES, $settingB['displayedAttributes']);
  42. $this->assertIsArray($settingB['stopWords']);
  43. $this->assertEmpty($settingB['stopWords']);
  44. $this->assertIsArray($settingB['synonyms']);
  45. $this->assertEmpty($settingB['synonyms']);
  46. }
  47. public function testUpdateSettings(): void
  48. {
  49. $index = $this->client->createIndex('index');
  50. $promise = $index->updateSettings([
  51. 'distinctAttribute' => 'title',
  52. 'rankingRules' => ['asc(title)', 'typo'],
  53. 'stopWords' => ['the'],
  54. ]);
  55. $this->assertIsValidPromise($promise);
  56. $index->waitForPendingUpdate($promise['updateId']);
  57. $settings = $index->getSettings();
  58. $this->assertEquals(['asc(title)', 'typo'], $settings['rankingRules']);
  59. $this->assertEquals('title', $settings['distinctAttribute']);
  60. $this->assertIsArray($settings['searchableAttributes']);
  61. $this->assertEquals(self::DEFAULT_SEARCHABLE_ATTRIBUTES, $settings['searchableAttributes']);
  62. $this->assertIsArray($settings['displayedAttributes']);
  63. $this->assertEquals(self::DEFAULT_DISPLAYED_ATTRIBUTES, $settings['displayedAttributes']);
  64. $this->assertEquals(['the'], $settings['stopWords']);
  65. $this->assertIsArray($settings['synonyms']);
  66. $this->assertEmpty($settings['synonyms']);
  67. }
  68. public function testUpdateSettingsWithoutOverwritingThem(): void
  69. {
  70. $index = $this->client->createIndex('index');
  71. $promise = $index->updateSettings([
  72. 'distinctAttribute' => 'title',
  73. 'rankingRules' => ['asc(title)', 'typo'],
  74. 'stopWords' => ['the'],
  75. ]);
  76. $this->assertIsValidPromise($promise);
  77. $index->waitForPendingUpdate($promise['updateId']);
  78. $promise = $index->updateSettings([
  79. 'searchableAttributes' => ['title'],
  80. ]);
  81. $this->assertIsValidPromise($promise);
  82. $index->waitForPendingUpdate($promise['updateId']);
  83. $settings = $index->getSettings();
  84. $this->assertEquals(['asc(title)', 'typo'], $settings['rankingRules']);
  85. $this->assertEquals('title', $settings['distinctAttribute']);
  86. $this->assertEquals(['title'], $settings['searchableAttributes']);
  87. $this->assertIsArray($settings['displayedAttributes']);
  88. $this->assertEquals(self::DEFAULT_SEARCHABLE_ATTRIBUTES, $settings['displayedAttributes']);
  89. $this->assertEquals(['the'], $settings['stopWords']);
  90. $this->assertIsArray($settings['synonyms']);
  91. $this->assertEmpty($settings['synonyms']);
  92. }
  93. public function testResetSettings(): void
  94. {
  95. $index = $this->client->createIndex('index');
  96. $promise = $index->updateSettings([
  97. 'distinctAttribute' => 'title',
  98. 'rankingRules' => ['asc(title)', 'typo'],
  99. 'stopWords' => ['the'],
  100. ]);
  101. $this->assertIsValidPromise($promise);
  102. $index->waitForPendingUpdate($promise['updateId']);
  103. $promise = $index->resetSettings();
  104. $this->assertIsValidPromise($promise);
  105. $index->waitForPendingUpdate($promise['updateId']);
  106. $settings = $index->getSettings();
  107. $this->assertEquals(self::DEFAULT_RANKING_RULES, $settings['rankingRules']);
  108. $this->assertNull($settings['distinctAttribute']);
  109. $this->assertIsArray($settings['searchableAttributes']);
  110. $this->assertEquals(self::DEFAULT_SEARCHABLE_ATTRIBUTES, $settings['searchableAttributes']);
  111. $this->assertIsArray($settings['displayedAttributes']);
  112. $this->assertEquals(self::DEFAULT_SEARCHABLE_ATTRIBUTES, $settings['displayedAttributes']);
  113. $this->assertIsArray($settings['stopWords']);
  114. $this->assertEmpty($settings['stopWords']);
  115. $this->assertIsArray($settings['synonyms']);
  116. $this->assertEmpty($settings['synonyms']);
  117. }
  118. }