Skip to content

Commit f2bdad6

Browse files
committed
Merge branch 'release/0.7.2'
2 parents ac053f9 + a8857b9 commit f2bdad6

18 files changed

+443
-399
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
.contrib
2+
bin
23
vendor
34
cover
45
*.phar
5-
composer.lock
6+
/composer.phar
7+
/composer.lock
68
build
79
.idea

.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:

Makefile

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
1+
lint:
2+
find src -name *.php -exec php -l {} \;
3+
find test -name *.php -exec php -l {} \;
4+
find spec -name *.php -exec php -l {} \;
5+
find examples -name *.php -exec php -l {} \;
6+
17
cs: lint
2-
./vendor/bin/phpcbf --standard=PSR2 src tests examples
3-
./vendor/bin/phpcs --standard=PSR2 --warning-severity=0 src tests examples
4-
./vendor/bin/phpcs --standard=Squiz --sniffs=Squiz.Commenting.FunctionComment,Squiz.Commenting.FunctionCommentThrowTag,Squiz.Commenting.ClassComment,Squiz.Commenting.VariableComment src tests examples
8+
./bin/phpcbf --standard=PSR2 src tests examples
9+
./bin/phpcs --standard=PSR2 --warning-severity=0 src tests examples
10+
./bin/phpcs --standard=Squiz --sniffs=Squiz.Commenting.FunctionComment,Squiz.Commenting.FunctionCommentThrowTag,Squiz.Commenting.ClassComment,Squiz.Commenting.VariableComment src tests examples
511

612
test:
7-
./vendor/bin/phpunit
8-
9-
cover:
10-
./vendor/bin/phpunit --coverage-html ./cover
13+
./vendor/bin/phpunit test
1114

12-
lint:
13-
find src -name *.php -exec php -l {} \;
14-
find tests -name *.php -exec php -l {} \;
15-
find examples -name *.php -exec php -l {} \;
15+
bdd:
16+
./vendor/bin/phpspec run --format=pretty
1617

18+
cover:
19+
./vendor/bin/phpunit --coverage-html ./cover test
1720
deps:
1821
wget -q https://getcomposer.org/composer.phar -O ./composer.phar
1922
chmod +x composer.phar
2023
php composer.phar install
2124

2225
dist-clean:
26+
rm -rf bin
2327
rm -rf vendor
2428
rm -f composer.phar
2529
rm -f composer.lock

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ Developer's Information
9696
Tests are in the `tests` folder.
9797
To run them, you need `PHPUnit` and execute `make test`.
9898

99+
We also have a BDD test suite under the `spec` folder.
100+
To run the suite, you need `PHPSpec` and execute `make bdd`.
99101

100102
### Code Quality
101103

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.7.1
1+
0.7.2

composer.json

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"require-dev": {
1111
"phpunit/phpunit": "4.7.*",
1212
"satooshi/php-coveralls": "dev-master",
13-
"squizlabs/php_codesniffer": "~2.0"
13+
"squizlabs/php_codesniffer": "~2.0",
14+
"phpspec/phpspec": "~2.0"
1415
},
1516
"authors": [
1617
{
@@ -38,13 +39,9 @@
3839
}
3940
],
4041
"autoload": {
41-
"psr-4": {
42-
"Nats\\": "src"
43-
}
44-
},
45-
"autoload-dev": {
46-
"psr-4": {
47-
"Tests\\Nats\\": "tests/Unit"
42+
"psr-0": {
43+
"Nats": "src",
44+
"Nats\\Test": "src"
4845
}
4946
}
5047
}

spec/Nats/ConnectionOptionsSpec.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace spec\Nats;
4+
5+
use PhpSpec\ObjectBehavior;
6+
use Prophecy\Argument;
7+
8+
class ConnectionOptionsSpec extends ObjectBehavior
9+
{
10+
function it_is_initializable()
11+
{
12+
$this->shouldHaveType('Nats\ConnectionOptions');
13+
}
14+
15+
function it_has_default_host_value_as_localhost() {
16+
$this->getHost()->shouldEqual("localhost");
17+
}
18+
19+
function it_has_default_port_value_as_4222() {
20+
$this->getPort()->shouldBe(4222);
21+
}
22+
23+
function it_has_default_user_value_as_null() {
24+
$this->getUser()->shouldBe(null);
25+
}
26+
27+
function it_has_default_pass_value_as_null() {
28+
$this->getPass()->shouldBe(null);
29+
}
30+
31+
function it_has_default_lang_value_as_php() {
32+
$this->getLang()->shouldEqual("php");
33+
}
34+
35+
function it_has_default_version_value_as_null() {
36+
$this->getVersion()->shouldEqual("0.0.5");
37+
}
38+
39+
function it_has_default_verbose_value_as_null() {
40+
$this->isVerbose()->shouldBe(false);
41+
}
42+
43+
function it_has_default_pedantic_value_as_null() {
44+
$this->isPedantic()->shouldBe(false);
45+
}
46+
47+
function it_has_default_reconnect_value_as_null() {
48+
$this->isReconnect()->shouldBe(true);
49+
}
50+
51+
function it_returns_a_valid_default_address() {
52+
$this->getAddress()->shouldEqual("tcp://localhost:4222");
53+
}
54+
}

spec/Nats/ConnectionSpec.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
namespace spec\Nats;
3+
4+
use PhpSpec\ObjectBehavior;
5+
use Prophecy\Argument;
6+
7+
class ConnectionSpec extends ObjectBehavior
8+
{
9+
function it_is_initializable()
10+
{
11+
$this->shouldHaveType('Nats\Connection');
12+
}
13+
14+
function it_has_ping_count_to_zero()
15+
{
16+
$this->pingsCount()->shouldBe(0);
17+
}
18+
19+
function it_has_pubs_count_to_zero()
20+
{
21+
$this->pubsCount()->shouldBe(0);
22+
}
23+
24+
function it_has_reconnects_count_to_zero()
25+
{
26+
$this->reconnectsCount()->shouldBe(0);
27+
}
28+
29+
function it_has_subscriptions_count_to_zero()
30+
{
31+
$this->subscriptionsCount()->shouldBe(0);
32+
}
33+
34+
function it_subscriptions_array_is_empty()
35+
{
36+
$this->getSubscriptions()->shouldHaveCount(0);
37+
}
38+
39+
function it_is_disconnected()
40+
{
41+
$this->isConnected()->shouldBe(false);
42+
}
43+
44+
function it_can_connect_and_disconnect_with_default_options()
45+
{
46+
$this->connect();
47+
$this->shouldHaveType('Nats\Connection');
48+
$this->isConnected()->shouldBe(true);
49+
$this->close();
50+
$this->isConnected()->shouldBe(false);
51+
}
52+
53+
function it_increases_reconnects_count_on_each_reconnection()
54+
{
55+
$this->connect();
56+
$this->reconnect();
57+
$this->reconnectsCount()->shouldBe(1);
58+
$this->isConnected()->shouldBe(true);
59+
$this->close();
60+
}
61+
62+
function it_a_ping_is_sent_after_a_successful_connection()
63+
{
64+
$this->connect();
65+
$this->pingsCount()->shouldBe(1);
66+
$this->close();
67+
}
68+
69+
function it_increases_pubs_after_publishing_a_message()
70+
{
71+
$this->connect();
72+
$this->publish("foo");
73+
$this->pubsCount()->shouldBe(1);
74+
$this->close();
75+
}
76+
77+
function it_increases_subscriptions_after_subscribing_to_a_topic()
78+
{
79+
$this->connect();
80+
$callback = function($payload) {};
81+
$sid = $this->subscribe("foo", $callback);
82+
$this->subscriptionsCount()->shouldBe(1);
83+
$this->unsubscribe($sid);
84+
$this->close();
85+
}
86+
87+
function it_decreases_subscriptions_after_unsubscribing_to_a_topic()
88+
{
89+
$this->connect();
90+
$callback = function($payload) {};
91+
$sid = $this->subscribe("foo", $callback);
92+
$this->unsubscribe($sid);
93+
$this->subscriptionsCount()->shouldBe(0);
94+
$this->close();
95+
}
96+
97+
98+
}

src/Connection.php renamed to src/Nats/Connection.php

Lines changed: 17 additions & 32 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
*
@@ -187,7 +167,7 @@ private function receive($len = null)
187167
$receivedBytes = 0;
188168
while ($receivedBytes < $len) {
189169
$bytesLeft = $len - $receivedBytes;
190-
if ( $bytesLeft < 1500 ) {
170+
if ( $bytesLeft < $this->chunkSize ) {
191171
$chunkSize = $bytesLeft;
192172
}
193173

@@ -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

@@ -250,19 +233,22 @@ public function isConnected()
250233
*/
251234
public function connect($timeout = null)
252235
{
236+
253237
$this->timeout = $timeout;
254238
$this->streamSocket = $this->getStream($this->options->getAddress(), $timeout);
239+
255240
$msg = 'CONNECT '.$this->options;
256241
$this->send($msg);
257-
258-
$response = $this->receive();
242+
$connect_response = $this->receive();
243+
if (strpos($connect_response, '-ERR')!== false) {
244+
throw new \Exception("Failing connection: $connect_response");
245+
}
259246

260247
$this->ping();
261-
$response = $this->receive();
262-
263-
if ($response !== "PONG") {
264-
if (strpos($response, '-ERR')!== false) {
265-
throw new \Exception("Failing connection: $response");
248+
$ping_response = $this->receive();
249+
if ($ping_response !== "PONG") {
250+
if (strpos($ping_response, '-ERR')!== false) {
251+
throw new \Exception("Failing on first ping: $ping_response");
266252
}
267253
}
268254
}
@@ -381,7 +367,7 @@ private function handlePING()
381367
/**
382368
* Handles MSG command.
383369
*
384-
* @param string $line Message command from NATS.
370+
* @param string $line Message command from Nats.
385371
*
386372
* @return void
387373
* @throws Exception
@@ -471,7 +457,6 @@ public function setStreamTimeout($seconds)
471457
}
472458
}
473459
}
474-
475460
return false;
476461
}
477462

File renamed without changes.

0 commit comments

Comments
 (0)