From 8c45d4cb9299a93548fe6191f4651c7945b5a09f Mon Sep 17 00:00:00 2001 From: Guillaume Loulier Date: Mon, 1 Sep 2025 09:12:01 +0200 Subject: [PATCH] ref(store): SurrealDB --- src/store/src/Bridge/SurrealDb/Store.php | 31 ++++++++++++------- .../tests/Bridge/SurrealDb/StoreTest.php | 14 +++++++++ 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/store/src/Bridge/SurrealDb/Store.php b/src/store/src/Bridge/SurrealDb/Store.php index 1e6201428..b60c1e933 100644 --- a/src/store/src/Bridge/SurrealDb/Store.php +++ b/src/store/src/Bridge/SurrealDb/Store.php @@ -75,6 +75,8 @@ public function query(Vector $vector, array $options = []): array public function drop(): void { + $this->authenticate(); + $this->request('DELETE', \sprintf('key/%s', $this->table), []); } @@ -87,9 +89,13 @@ private function request(string $method, string $endpoint, array|string $payload { $url = \sprintf('%s/%s', $this->endpointUrl, $endpoint); - $finalPayload = [ - 'json' => $payload, - ]; + $finalPayload = []; + + if (\is_array($payload) && [] !== $payload) { + $finalPayload = [ + 'body' => $payload, + ]; + } if (\is_string($payload)) { $finalPayload = [ @@ -97,15 +103,18 @@ private function request(string $method, string $endpoint, array|string $payload ]; } - $response = $this->httpClient->request($method, $url, array_merge($finalPayload, [ - 'headers' => [ - 'Accept' => 'application/json', - 'Content-Type' => 'application/json', - 'Surreal-NS' => $this->namespace, - 'Surreal-DB' => $this->database, - 'Authorization' => \sprintf('Bearer %s', $this->authenticationToken), + $response = $this->httpClient->request($method, $url, [ + ...$finalPayload, + ...[ + 'headers' => [ + 'Accept' => 'application/json', + 'Content-Type' => 'application/json', + 'Surreal-NS' => $this->namespace, + 'Surreal-DB' => $this->database, + 'Authorization' => \sprintf('Bearer %s', $this->authenticationToken), + ], ], - ])); + ]); return $response->toArray(); } diff --git a/src/store/tests/Bridge/SurrealDb/StoreTest.php b/src/store/tests/Bridge/SurrealDb/StoreTest.php index 3f767b114..d4b11b398 100644 --- a/src/store/tests/Bridge/SurrealDb/StoreTest.php +++ b/src/store/tests/Bridge/SurrealDb/StoreTest.php @@ -97,6 +97,13 @@ public function testStoreCanSetupOnValidAuthenticationAndIndexResponse() public function testStoreCannotDropOnInvalidResponse() { $httpClient = new MockHttpClient([ + new JsonMockResponse([ + 'code' => 200, + 'details' => 'Authentication succeeded.', + 'token' => 'bar', + ], [ + 'http_code' => 200, + ]), new JsonMockResponse([], [ 'http_code' => 400, ]), @@ -129,6 +136,13 @@ public function testStoreCanDrop() ], [ 'http_code' => 200, ]), + new JsonMockResponse([ + 'code' => 200, + 'details' => 'Authentication succeeded.', + 'token' => 'bar', + ], [ + 'http_code' => 200, + ]), new JsonMockResponse([], [ 'http_code' => 200, ]),