From 3a3a6e2e251f44b8541ee0df8d3cbf9e7de65642 Mon Sep 17 00:00:00 2001 From: Beau Date: Thu, 12 Dec 2024 16:26:56 +0100 Subject: [PATCH] fix: issue when using Future class (#15) --- composer.json | 6 ++-- src/Cbor/Types/Future.php | 2 +- tests/core/cbor/FutureTest.php | 22 ++++++++++++ tests/protocols/http/QueryTest.php | 1 + tests/v1/assets/setup.surql | 3 ++ tests/v2/assets/setup.surql | 3 ++ tests/v2/query/FutureTest.php | 56 ++++++++++++++++++++++++++++++ 7 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 tests/core/cbor/FutureTest.php create mode 100644 tests/v2/query/FutureTest.php diff --git a/composer.json b/composer.json index 26ebf5c..0d5f439 100644 --- a/composer.json +++ b/composer.json @@ -15,9 +15,9 @@ "phpstan": "vendor/bin/phpstan -c phpstan.neon", "test-coverage-v1": "./vendor/bin/phpunit -c tests/phpunit_config_v1.xml", "test-coverage-v2": "./vendor/bin/phpunit -c tests/phpunit_config_v2.xml", - "run-surreal-1.4.2": "docker run --rm --pull always -p 8000:8000 surrealdb/surrealdb:v1.4.2 start --auth --user root --pass root --allow-all --log trace ", - "run-surreal-1.5.4": "docker run --rm --pull always -p 8000:8000 surrealdb/surrealdb:v1.5.4 start --auth --user root --pass root --allow-all --log trace ", - "run-surreal-2.0.4": "docker run --rm --pull always -p 8000:8000 surrealdb/surrealdb:v2.0.4 start --username root --pass root --allow-all --log trace" + "run-surreal-1.4.2": "docker run --rm --pull always -p 8000:8000 surrealdb/surrealdb:v1.4.2 start --auth --user root --pass root --allow-all --strict --log trace ", + "run-surreal-1.5.4": "docker run --rm --pull always -p 8000:8000 surrealdb/surrealdb:v1.5.4 start --auth --user root --pass root --allow-all --strict --log trace ", + "run-surreal-2.0.4": "docker run --rm --pull always -p 8000:8000 surrealdb/surrealdb:v2.0.4 start --username root --pass root --allow-all --strict --log trace" }, "authors": [ { diff --git a/src/Cbor/Types/Future.php b/src/Cbor/Types/Future.php index e25c994..b7fb2e1 100644 --- a/src/Cbor/Types/Future.php +++ b/src/Cbor/Types/Future.php @@ -25,6 +25,6 @@ public function __toString(): string public function toString(): string { - return " { $this->inner }"; + return " $this->inner"; } } \ No newline at end of file diff --git a/tests/core/cbor/FutureTest.php b/tests/core/cbor/FutureTest.php new file mode 100644 index 0000000..d2dda4f --- /dev/null +++ b/tests/core/cbor/FutureTest.php @@ -0,0 +1,22 @@ +assertEquals(" { time::now() }", $future->__toString()); + $this->assertEquals(" { time::now() }", $future->toString()); + } + + public function testJsonSerialize() + { + $future = new Future("{ time::now() }"); + $this->assertEquals(" { time::now() }", $future->jsonSerialize()); + } +} \ No newline at end of file diff --git a/tests/protocols/http/QueryTest.php b/tests/protocols/http/QueryTest.php index d42cfd0..e588273 100644 --- a/tests/protocols/http/QueryTest.php +++ b/tests/protocols/http/QueryTest.php @@ -5,6 +5,7 @@ use Beau\CborPHP\exceptions\CborException; use Exception; use PHPUnit\Framework\TestCase; +use Surreal\Cbor\Types\Future; use Surreal\Cbor\Types\None; use Surreal\Cbor\Types\Record\RecordId; use Surreal\Cbor\Types\Table; diff --git a/tests/v1/assets/setup.surql b/tests/v1/assets/setup.surql index ac84f21..107ec81 100644 --- a/tests/v1/assets/setup.surql +++ b/tests/v1/assets/setup.surql @@ -22,6 +22,9 @@ DEFINE TABLE likes DEFINE TABLE test PERMISSIONS FULL; +DEFINE TABLE future_test + PERMISSIONS FULL; + DEFINE TABLE user PERMISSIONS FOR select WHERE id = $auth.id; diff --git a/tests/v2/assets/setup.surql b/tests/v2/assets/setup.surql index 74a3a9f..37bd337 100644 --- a/tests/v2/assets/setup.surql +++ b/tests/v2/assets/setup.surql @@ -28,6 +28,9 @@ DEFINE TABLE insert_relation DEFINE TABLE upsert PERMISSIONS FULL; +DEFINE TABLE future_test + PERMISSIONS FULL; + DEFINE TABLE user PERMISSIONS FOR select WHERE id = $auth.id; diff --git a/tests/v2/query/FutureTest.php b/tests/v2/query/FutureTest.php new file mode 100644 index 0000000..8d50b54 --- /dev/null +++ b/tests/v2/query/FutureTest.php @@ -0,0 +1,56 @@ +connect("http://localhost:8000", [ + "namespace" => "test", + "database" => "test" + ]); + + $token = $db->signin([ + "user" => "root", + "pass" => "root" + ]); + + $this->assertIsString($token, "Token is not a string"); + + return $db; + } + + public function testFutureQuery(): void + { + $db = $this->getDb(); + + $future = new Future("{ duration::years(time::now() - birthday) >= 18 }"); + $db->let("canDrive", $future); + + $response = $db->queryRaw(' + CREATE future_test + SET + birthday = time::now(), + can_drive = $canDrive + '); + + $this->assertIsArray($response); + + [$data] = $response; + + $this->assertArrayHasKey("result", $data); + $this->assertArrayHasKey("time", $data); + $this->assertArrayHasKey("status", $data); + + $this->assertEquals("OK", $data["status"]); + $this->assertFalse($data["result"][0]["can_drive"]); + + $db->close(); + } +} \ No newline at end of file