KeysAndPermissionsTest.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. declare(strict_types=1);
  3. namespace Tests\Endpoints;
  4. use MeiliSearch\Client;
  5. use MeiliSearch\Exceptions\ApiException;
  6. use Tests\TestCase;
  7. final class KeysAndPermissionsTest extends TestCase
  8. {
  9. public function testGetKeys(): void
  10. {
  11. $response = $this->client->getKeys();
  12. $this->assertArrayHasKey('private', $response);
  13. $this->assertArrayHasKey('public', $response);
  14. $this->assertIsString($response['private']);
  15. $this->assertNotNull($response['private']);
  16. $this->assertIsString($response['public']);
  17. $this->assertNotNull($response['public']);
  18. }
  19. public function testSearchingIfPublicKeyProvided(): void
  20. {
  21. $this->client->createIndex('index');
  22. $newClient = new Client(self::HOST, $this->getKeys()['public']);
  23. $response = $newClient->index('index')->search('test');
  24. $this->assertArrayHasKey('hits', $response->toArray());
  25. }
  26. public function testGetSettingsIfPrivateKeyProvided(): void
  27. {
  28. $this->client->createIndex('index');
  29. $newClient = new Client(self::HOST, $this->getKeys()['private']);
  30. $response = $newClient->index('index')->getSettings();
  31. $this->assertEquals(['*'], $response['searchableAttributes']);
  32. }
  33. public function testExceptionIfNoMasterKeyProvided(): void
  34. {
  35. $newClient = new Client(self::HOST);
  36. $this->expectException(ApiException::class);
  37. $newClient->index('index')->search('test');
  38. }
  39. public function testExceptionIfBadKeyProvidedToGetSettings(): void
  40. {
  41. $this->client->createIndex('index');
  42. $response = $this->client->index('index')->getSettings();
  43. $this->assertEquals(['*'], $response['searchableAttributes']);
  44. $newClient = new Client(self::HOST, 'bad-key');
  45. $this->expectException(ApiException::class);
  46. $newClient->index('index')->getSettings();
  47. }
  48. public function testExceptionIfBadKeyProvidedToGetKeys(): void
  49. {
  50. $this->expectException(ApiException::class);
  51. $client = new Client(self::HOST, 'bad-key');
  52. $client->getKeys();
  53. }
  54. private function getKeys(): array
  55. {
  56. return $this->client->getKeys();
  57. }
  58. }