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

Commit 0ec2087

Browse files
committed
Merge branch 'develop' into phpspec
2 parents a9f8b20 + c751563 commit 0ec2087

6 files changed

Lines changed: 88 additions & 194 deletions

File tree

.travis.yml

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
1+
sudo: required
2+
services:
3+
- docker
14
language: php
25
php:
3-
# - 5.4
46
- 5.5
57
- 5.6
68
- 7
7-
# - nightly
89
cache:
910
directories:
1011
- vendor
1112
- $HOME/.composer/cache
13+
before_install:
14+
- docker run -d -p 4222:4222 --name nats nats
1215
before_script:
13-
# - sudo apt-get install python-software-properties # 12.04
14-
# - sudo add-apt-repository -y ppa:duh/golang
15-
# - sudo apt-get update
16-
# - sudo apt-get install golang
17-
# - export GOROOT=`go env GOROOT` && export GOPATH=$HOME/go
18-
# - go get -u github.com/nats-io/gnatsd
19-
# - gnats="$GOPATH/bin/gnatsd"
20-
# - $gnats &
2116
- make deps
2217
script:
23-
# - make cs
2418
- mkdir -p build/logs
2519
- make test
2620
after_script:

src/Nats/Connection.php

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,6 @@ public function getSubscriptions()
114114
*/
115115
private $streamSocket;
116116

117-
/**
118-
* Stream wrapper for testing purposes.
119-
*
120-
* @var mixed StreamWrapper.
121-
*/
122-
private $streamWrapper;
123-
124117
/**
125118
* @var Generator
126119
*/
@@ -137,7 +130,6 @@ public function __construct(ConnectionOptions $options = null)
137130
$this->pubs = 0;
138131
$this->subscriptions = [];
139132
$this->options = $options;
140-
$this->streamWrapper = new StreamWrapper();
141133
$randomFactory = new Factory();
142134
$this->randomGenerator = $randomFactory->getLowStrengthGenerator();
143135

@@ -146,18 +138,6 @@ public function __construct(ConnectionOptions $options = null)
146138
}
147139
}
148140

149-
/**
150-
* Setter for $streamWrapper. For testing purposes.
151-
*
152-
* @param StreamWrapper $streamWrapper StreamWrapper for testing purposes.
153-
*
154-
* @return void
155-
*/
156-
public function setStreamWrapper(StreamWrapper $streamWrapper)
157-
{
158-
$this->streamWrapper = $streamWrapper;
159-
}
160-
161141
/**
162142
* Sends data thought the stream.
163143
*
@@ -220,13 +200,16 @@ private function getStream($address, $timeout = null)
220200
$errno = null;
221201
$errstr = null;
222202

223-
$fp = $this->streamWrapper->getStreamSocketClient($address, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT);
203+
$fp = stream_socket_client($address, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT);
204+
$timeout = number_format($timeout, 3);
205+
$seconds = floor($timeout);
206+
$microseconds = ($timeout - $seconds) * 1000;
207+
stream_set_timeout($fp, $seconds, $microseconds);
224208

225209
if (!$fp) {
226210
throw new \Exception($errstr, $errno);
227211
}
228212

229-
//stream_set_blocking($fp, 0);
230213
return $fp;
231214
}
232215

@@ -252,17 +235,19 @@ public function connect($timeout = null)
252235
{
253236
$this->timeout = $timeout;
254237
$this->streamSocket = $this->getStream($this->options->getAddress(), $timeout);
238+
255239
$msg = 'CONNECT '.$this->options;
256240
$this->send($msg);
257-
258-
$response = $this->receive();
241+
$connect_response = $this->receive();
242+
if (strpos($connect_response, '-ERR')!== false) {
243+
throw new \Exception("Failing connection: $connect_response");
244+
}
259245

260246
$this->ping();
261-
$response = $this->receive();
262-
263-
if ($response !== "PONG") {
264-
if (strpos($response, '-ERR')!== false) {
265-
throw new \Exception("Failing connection: $response");
247+
$ping_response = $this->receive();
248+
if ($ping_response !== "PONG") {
249+
if (strpos($ping_response, '-ERR')!== false) {
250+
throw new \Exception("Failing on first ping: $ping_response");
266251
}
267252
}
268253
}

src/Nats/StreamWrapper.php

Lines changed: 0 additions & 44 deletions
This file was deleted.

test/ConnectionOptionsTest.php

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class ConnectionOptionsTest extends \PHPUnit_Framework_TestCase
1010
{
1111
/**
12-
* Tests Connection Options getters and setters. Only necessary for code coverage.
12+
* Tests Connection Options getters and setters.
1313
*
1414
* @return void
1515
*/
@@ -37,4 +37,56 @@ public function testSettersAndGetters()
3737
$this->assertTrue($options->isPedantic());
3838
$this->assertTrue($options->isReconnect());
3939
}
40+
41+
/**
42+
* Tests Connection Options getters and setters without setting user and password.
43+
*
44+
* @return void
45+
*/
46+
public function testSettersAndGettersWithoutCredentials()
47+
{
48+
$options = new ConnectionOptions();
49+
$options
50+
->setHost('host')
51+
->setPort(4222)
52+
->setLang('lang')
53+
->setVersion('version')
54+
->setVerbose(true)
55+
->setPedantic(true)
56+
->setReconnect(true);
57+
58+
$this->assertEquals('host', $options->getHost());
59+
$this->assertEquals(4222, $options->getPort());
60+
$this->assertNull($options->getUser());
61+
$this->assertNull($options->getPass());
62+
$this->assertEquals('lang', $options->getLang());
63+
$this->assertEquals('version', $options->getVersion());
64+
$this->assertTrue($options->isVerbose());
65+
$this->assertTrue($options->isPedantic());
66+
$this->assertTrue($options->isReconnect());
67+
}
68+
69+
/**
70+
* Test string representation of ConnectionOptions.
71+
*
72+
* @return void
73+
*/
74+
public function testStringRepresentation()
75+
{
76+
$options = new ConnectionOptions();
77+
$this->assertEquals("{\"lang\":\"php\",\"version\":\"0.0.5\",\"verbose\":false,\"pedantic\":false}", $options->__toString());
78+
}
79+
80+
/**
81+
* Test string representation of ConnectionOptions with credentials.
82+
*
83+
* @return void
84+
*/
85+
public function testStringRepresentationWithCredentials()
86+
{
87+
$options = new ConnectionOptions();
88+
$options->setUser("username");
89+
$options->setPass("password");
90+
$this->assertEquals("{\"lang\":\"php\",\"version\":\"0.0.5\",\"verbose\":false,\"pedantic\":false,\"user\":\"username\",\"pass\":\"password\"}", $options->__toString());
91+
}
4092
}

test/ConnectionTest.php

Lines changed: 0 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
use Nats;
55
use Nats\ConnectionOptions;
6-
use Nats\StreamWrapper;
76
use Prophecy\Argument;
87

98
/**
@@ -18,20 +17,6 @@ class ConnectionTest extends \PHPUnit_Framework_TestCase
1817
*/
1918
private $c;
2019

21-
/**
22-
* Process.
23-
*
24-
* @var resource A separated process.
25-
*/
26-
private static $process;
27-
28-
/**
29-
* Gnatsd switch.
30-
*
31-
* @var bool Am I using a real or a fake server?
32-
*/
33-
private static $isGnatsd = false;
34-
3520

3621
/**
3722
* SetUp test suite.
@@ -41,35 +26,10 @@ class ConnectionTest extends \PHPUnit_Framework_TestCase
4126
public function setUp()
4227
{
4328
$options = new ConnectionOptions();
44-
$streamWrapper = $this->getMockStreamSocketClient();
45-
4629
$this->c = new Nats\Connection($options);
47-
$this->c->setStreamWrapper($streamWrapper->reveal());
4830
$this->c->connect();
4931
}
5032

51-
/**
52-
* Function for building Socket Mock.
53-
*
54-
* @return StreamWrapper
55-
*/
56-
private function getMockStreamSocketClient()
57-
{
58-
$streamWrapper = $this->prophesize("Nats\StreamWrapper");
59-
$streamWrapper->setStreamTimeout(Argument::any(), Argument::any())->willReturn(true);
60-
$streamWrapper->getStreamSocketClient(Argument::any(), Argument::any(), Argument::any(), Argument::any(), Argument::any())->will(function ($args) {
61-
$fileName = "/tmp/".uniqid();
62-
$f = fopen($fileName, 'w');
63-
fwrite($f, "INFO: \n");
64-
fwrite($f, "-ERR \n");
65-
66-
return $f;
67-
68-
});
69-
70-
return $streamWrapper;
71-
}
72-
7333

7434
/**
7535
* Test Connection.
@@ -130,60 +90,6 @@ public function testReconnect()
13090
$this->c->close();
13191
}
13292

133-
/**
134-
* Test Subscription command.
135-
*
136-
* @return void
137-
*/
138-
public function testSubscription()
139-
{
140-
$callback = function ($message) {
141-
$this->assertNotNull($message);
142-
$this->assertEquals($message, 'bar');
143-
};
144-
145-
$this->c->subscribe('foo', $callback);
146-
$this->assertGreaterThan(0, $this->c->subscriptionsCount());
147-
$subscriptions = $this->c->getSubscriptions();
148-
$this->assertInternalType('array', $subscriptions);
149-
150-
$this->c->publish('foo', 'bar');
151-
$this->assertEquals(1, $this->c->pubsCount());
152-
/*
153-
$process = new BackgroundProcess('/usr/bin/php ./test/Util/ClientServerStub.php ');
154-
$process->run();
155-
*/
156-
// time_nanosleep(1, 0);
157-
$this->c->wait(1);
158-
}
159-
160-
/**
161-
* Test Queue Subscription command.
162-
*
163-
* @return void
164-
*/
165-
public function testQueueSubscription()
166-
{
167-
$callback = function ($message) {
168-
$this->assertNotNull($message);
169-
$this->assertEquals($message, 'bar');
170-
};
171-
172-
$this->c->queueSubscribe('foo', 'bar', $callback);
173-
$this->assertGreaterThan(0, $this->c->subscriptionsCount());
174-
$subscriptions = $this->c->getSubscriptions();
175-
$this->assertInternalType('array', $subscriptions);
176-
177-
$this->c->publish('foo', 'bar');
178-
$this->assertEquals(1, $this->c->pubsCount());
179-
/*
180-
$process = new BackgroundProcess('/usr/bin/php ./test/Util/ClientServerStub.php ');
181-
$process->run();
182-
*/
183-
// time_nanosleep(1, 0);
184-
$this->c->wait(1);
185-
}
186-
18793
/**
18894
* Test Request command.
18995
*
@@ -226,15 +132,4 @@ function ($res) {
226132

227133
$this->assertTrue(true);
228134
}
229-
230-
/**
231-
* Test setStreamTimeout command.
232-
*
233-
* @return void
234-
*/
235-
public function testSetStreamTimeout()
236-
{
237-
$this->assertTrue($this->c->setStreamTimeout(2));
238-
$this->assertFalse($this->c->setStreamTimeout("hello"));
239-
}
240135
}

0 commit comments

Comments
 (0)