From c9ffd0b4339f3002aa94623f9fa8e302f605a663 Mon Sep 17 00:00:00 2001 From: dmitry krokhin Date: Tue, 26 Mar 2024 17:54:51 +0300 Subject: [PATCH] bucket null values getter --- src/KeyValue/Bucket.php | 14 +++++++++++--- tests/Functional/KeyValue/BucketTest.php | 10 ++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/KeyValue/Bucket.php b/src/KeyValue/Bucket.php index 1caf414..006473e 100644 --- a/src/KeyValue/Bucket.php +++ b/src/KeyValue/Bucket.php @@ -6,8 +6,9 @@ use Basis\Nats\Client; use Basis\Nats\Consumer\Configuration as ConsumerConfiguration; -use Basis\Nats\Stream\Stream; use Basis\Nats\Message\Payload; +use Basis\Nats\Stream\Stream; +use Exception; class Bucket { @@ -24,8 +25,15 @@ public function __construct( public function get(string $key) { - $entry = $this->getEntry($key); - return $entry ? $entry->value : null; + try { + $entry = $this->getEntry($key); + return $entry ? $entry->value : null; + } catch (Exception $e) { + if ($e->getMessage() == 'no message found') { + return null; + } + throw $e; + } } public function getConfiguration(): Configuration diff --git a/tests/Functional/KeyValue/BucketTest.php b/tests/Functional/KeyValue/BucketTest.php index 7306f44..707887e 100644 --- a/tests/Functional/KeyValue/BucketTest.php +++ b/tests/Functional/KeyValue/BucketTest.php @@ -9,6 +9,16 @@ class BucketTest extends FunctionalTestCase { + public function testNullValues() + { + $bucket = $this->createClient() + ->getApi() + ->getBucket('test_bucket'); + + $this->assertSame(0, $bucket->getStatus()->values); + $this->assertNull($bucket->get('username')); + } + public function testBasics() { $bucket = $this->createClient()