Skip to content
This repository was archived by the owner on Oct 30, 2024. It is now read-only.

Commit 4053af8

Browse files
author
jgil
committed
Fixing Issue 4. Return exception if failing autorizacion
1 parent 5121c0f commit 4053af8

3 files changed

Lines changed: 33 additions & 9 deletions

File tree

src/Connection.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@ private function getStream($address, $timeout = null)
186186
}
187187
$errno = null;
188188
$errstr = null;
189-
//$fp = stream_socket_client($address, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT);
190189

191190
$fp = $this->streamWrapper->getStreamSocketClient($address, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT);
192191

@@ -221,6 +220,17 @@ public function connect($timeout = null)
221220
$this->streamSocket = $this->getStream($this->options->getAddress(), $timeout);
222221
$msg = 'CONNECT '.$this->options;
223222
$this->send($msg);
223+
224+
$response = $this->receive();
225+
226+
$this->ping();
227+
$response = $this->receive();
228+
229+
if ($response !== "PONG") {
230+
if (strpos($response, '-ERR')!== false) {
231+
throw new \Exception("Failing connection: $response");
232+
}
233+
}
224234
}
225235

226236
/**

tests/Unit/.ConnectionTest.php.swp

20 KB
Binary file not shown.

tests/Unit/ConnectionTest.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
use Nats;
55
use Nats\ConnectionOptions;
66
use Nats\StreamWrapper;
7-
use org\bovigo\vfs\vfsStream;
8-
use org\bovigo\vfs\vfsStreamFile;
97
use Prophecy\Argument;
108

119
/**
@@ -43,20 +41,35 @@ class ConnectionTest extends \PHPUnit_Framework_TestCase
4341
public function setUp()
4442
{
4543
$options = new ConnectionOptions();
44+
$streamWrapper = $this->getMockStreamSocketClient();
4645

46+
$this->c = new Nats\Connection($options);
47+
$this->c->setStreamWrapper($streamWrapper->reveal());
48+
$this->c->connect();
49+
}
50+
51+
/**
52+
* Function for building Socket Mock.
53+
*
54+
* @return StreamWrapper
55+
*/
56+
private function getMockStreamSocketClient() {
4757
$streamWrapper = $this->prophesize("Nats\StreamWrapper");
58+
$streamWrapper->setStreamTimeout(Argument::any(), Argument::any())->willReturn(true);
4859
$streamWrapper->getStreamSocketClient(Argument::any(), Argument::any(), Argument::any(), Argument::any(), Argument::any())->will(function ($args) {
49-
return fopen("/tmp/".uniqid(), 'w');
60+
$fileName = "/tmp/".uniqid();
61+
$f = fopen($fileName, 'w');
62+
fwrite($f, "INFO: \n");
63+
fwrite($f, "-ERR \n");
5064

51-
});
65+
return $f;
5266

53-
$streamWrapper->setStreamTimeout(Argument::any(), Argument::any())->willReturn(true);
67+
});
5468

55-
$this->c = new Nats\Connection($options);
56-
$this->c->setStreamWrapper($streamWrapper->reveal());
57-
$this->c->connect();
69+
return $streamWrapper;
5870
}
5971

72+
6073
/**
6174
* Test Connection.
6275
*
@@ -73,6 +86,7 @@ public function testConnection()
7386
$this->assertFalse($this->c->isConnected());
7487
}
7588

89+
7690
/**
7791
* Test Ping command.
7892
*

0 commit comments

Comments
 (0)