Skip to content

Commit 702b29f

Browse files
committed
Fixes basis-company/nats.php/#30
1 parent e71e0d6 commit 702b29f

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

src/Client.php

+20
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class Client
2020

2121
private string $name = '';
2222

23+
/** @var array<Closure|Queue> */
2324
private array $handlers = [];
2425
private array $subscriptions = [];
2526

@@ -240,4 +241,23 @@ public function skipInvalidMessages(bool $skipInvalidMessages): self
240241
$this->skipInvalidMessages = $skipInvalidMessages;
241242
return $this;
242243
}
244+
245+
public function unsubscribeAll(): self
246+
{
247+
foreach ($this->subscriptions as $index => $subscription) {
248+
unset($this->subscriptions[$index]);
249+
$this->connection->sendMessage(new Unsubscribe(['sid' => $subscription['sid']]));
250+
unset($this->handlers[$subscription['sid']]);
251+
}
252+
253+
return $this;
254+
}
255+
256+
public function disconnect(): self
257+
{
258+
$this->unsubscribeAll();
259+
$this->connection->close();
260+
261+
return $this;
262+
}
243263
}

src/Connection.php

+8
Original file line numberDiff line numberDiff line change
@@ -290,4 +290,12 @@ private function processException(Throwable $e)
290290
]));
291291
}
292292
}
293+
294+
public function close(): void
295+
{
296+
if ($this->socket) {
297+
fclose($this->socket);
298+
$this->socket = null;
299+
}
300+
}
293301
}

tests/Functional/ClientTest.php

+17
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,21 @@ public function testInvalidTlsKey()
112112
]);
113113
$client->ping();
114114
}
115+
116+
public function testCloseClosesSocket(): void
117+
{
118+
$client = $this->createClient([]);
119+
self::assertTrue($client->ping());
120+
121+
$connection = $client->connection;
122+
123+
// Call the close method
124+
$connection->close();
125+
126+
$property = new ReflectionProperty(Connection::class, 'socket');
127+
$property->setAccessible(true);
128+
129+
// Assert that the socket is closed and set to null
130+
self::assertNull($property->getValue($connection));
131+
}
115132
}

tests/Functional/SubjectTest.php

+42
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Tests\Functional;
66

77
use Basis\Nats\Client;
8+
use Basis\Nats\Connection;
89
use Basis\Nats\Message\Payload;
910
use ReflectionProperty;
1011
use Tests\FunctionalTestCase;
@@ -226,4 +227,45 @@ public function greet(Payload $payload): string
226227
{
227228
return 'Hello, ' . $payload->body;
228229
}
230+
231+
public function testUnsubscribeAll(): void
232+
{
233+
$property = new ReflectionProperty(Client::class, 'handlers');
234+
$property->setAccessible(true);
235+
236+
$client = $this->createClient();
237+
238+
$subjects = ['hello.request1', 'hello.request2'];
239+
foreach ($subjects as $subject) {
240+
$client->subscribe($subject, $this->greet(...));
241+
}
242+
self::assertCount(2, $property->getValue($client));
243+
244+
$client->unsubscribeAll();
245+
self::assertCount(0, $property->getValue($client));
246+
}
247+
248+
public function testDisconnect(): void
249+
{
250+
$property = new ReflectionProperty(Client::class, 'handlers');
251+
$property->setAccessible(true);
252+
253+
$client = $this->createClient();
254+
$connection = $client->connection;
255+
256+
$subjects = ['hello.request1', 'hello.request2'];
257+
foreach ($subjects as $subject) {
258+
$client->subscribe($subject, $this->greet(...));
259+
}
260+
self::assertCount(2, $property->getValue($client));
261+
262+
$client->disconnect();
263+
self::assertCount(0, $property->getValue($client));
264+
265+
$property = new ReflectionProperty(Connection::class, 'socket');
266+
$property->setAccessible(true);
267+
268+
// Assert that the socket is closed and set to null
269+
self::assertNull($property->getValue($connection));
270+
}
229271
}

0 commit comments

Comments
 (0)