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

Commit 6cf2682

Browse files
committed
Merge branch 'release/0.5.0'
2 parents b7fa9b9 + a6a8608 commit 6cf2682

7 files changed

Lines changed: 90 additions & 203 deletions

File tree

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.4.0
1+
0.5.0

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
"require-dev": {
1010
"phpunit/phpunit": "4.7.*",
1111
"satooshi/php-coveralls": "dev-master",
12-
"cocur/background-process": "dev-master",
1312
"squizlabs/php_codesniffer": "~2.0"
1413
},
1514
"authors": [

src/Connection.php

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ public function getSubscriptions()
9898
*/
9999
private $streamSocket;
100100

101+
/**
102+
* Stream wrapper for testing purposes.
103+
*
104+
* @var mixed StreamWrapper.
105+
*/
106+
private $streamWrapper;
107+
101108
/**
102109
* Constructor.
103110
*
@@ -109,11 +116,25 @@ public function __construct(ConnectionOptions $options = null)
109116
$this->pubs = 0;
110117
$this->subscriptions = [];
111118
$this->options = $options;
119+
$this->streamWrapper = new StreamWrapper();
120+
112121
if (is_null($options)) {
113122
$this->options = new ConnectionOptions();
114123
}
115124
}
116125

126+
/**
127+
* Setter for $streamWrapper. For testing purposes.
128+
*
129+
* @param StreamWrapper $streamWrapper StreamWrapper for testing purposes.
130+
*
131+
* @return void
132+
*/
133+
public function setStreamWrapper(StreamWrapper $streamWrapper)
134+
{
135+
$this->streamWrapper = $streamWrapper;
136+
}
137+
117138
/**
118139
* Sends data thought the stream.
119140
*
@@ -163,10 +184,16 @@ private function getStream($address, $timeout = null)
163184
if (is_null($timeout)) {
164185
$timeout = intval(ini_get('default_socket_timeout'));
165186
}
166-
$fp = stream_socket_client($address, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT);
187+
$errno = null;
188+
$errstr = null;
189+
//$fp = stream_socket_client($address, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT);
190+
191+
$fp = $this->streamWrapper->getStreamSocketClient($address, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT);
192+
167193
if (!$fp) {
168194
throw new \Exception($errstr, $errno);
169195
}
196+
170197
//stream_set_blocking($fp, 0);
171198
return $fp;
172199
}
@@ -364,10 +391,12 @@ public function wait($quantity = 0)
364391
public function setStreamTimeout($seconds)
365392
{
366393
if ($this->isConnected()) {
367-
try {
368-
return stream_set_timeout($this->streamSocket, $seconds);
369-
} catch (\Exception $e) {
370-
return false;
394+
if (is_int($seconds)) {
395+
try {
396+
return $this->streamWrapper->setStreamTimeout($this->streamSocket, $seconds);
397+
} catch (\Exception $e) {
398+
return false;
399+
}
371400
}
372401
}
373402

src/StreamWrapper.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Nats;
4+
5+
/**
6+
* StreamWrapper class.
7+
*/
8+
class StreamWrapper
9+
{
10+
/**
11+
* Wrapper for stream_socket_client
12+
*
13+
* @param string $address Address to connect the socket.
14+
* @param integer $errno Number of error.
15+
* @param string $errstr Description of error.
16+
* @param integer $timeout Timeout.
17+
* @param integer $typeStream Type of stream.
18+
*
19+
* @return stream
20+
*/
21+
public function getStreamSocketClient($address, &$errno, &$errstr, $timeout, $typeStream)
22+
{
23+
return stream_socket_client($address, $errno, $errstr, $timeout, $typeStream);
24+
}
25+
26+
/**
27+
* Wrapper for stream_set_timeout
28+
*
29+
* @param mixed $stream Stream.
30+
* @param integer $seconds Seconds for timeout.
31+
*
32+
* @return boolean
33+
*
34+
*/
35+
public function setStreamTimeout($stream, $seconds)
36+
{
37+
return stream_set_timeout($stream, $seconds);
38+
}
39+
}

tests/Unit/ConnectionTest.php

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44
use Nats;
55
use Nats\ConnectionOptions;
6-
use Cocur\BackgroundProcess\BackgroundProcess;
6+
use Nats\StreamWrapper;
7+
use org\bovigo\vfs\vfsStream;
8+
use org\bovigo\vfs\vfsStreamFile;
9+
use Prophecy\Argument;
710

811
/**
912
* Class ConnectionTest.
@@ -31,32 +34,6 @@ class ConnectionTest extends \PHPUnit_Framework_TestCase
3134
*/
3235
private static $isGnatsd = false;
3336

34-
/**
35-
* Before Class code setup.
36-
*
37-
* @return void
38-
*/
39-
public static function setUpBeforeClass()
40-
{
41-
if (($socket = @fsockopen("localhost", 4222, $err))!==false) {
42-
self::$isGnatsd = true;
43-
} else {
44-
self::$process = new BackgroundProcess('/usr/bin/php ./tests/Util/ListeningServerStub.php ');
45-
self::$process->run();
46-
}
47-
}
48-
49-
/**
50-
* After Class code setup.
51-
*
52-
* @return void
53-
*/
54-
public static function tearDownAfterClass()
55-
{
56-
if (!self::$isGnatsd) {
57-
self::$process->stop();
58-
}
59-
}
6037

6138
/**
6239
* SetUp test suite.
@@ -66,15 +43,20 @@ public static function tearDownAfterClass()
6643
public function setUp()
6744
{
6845
$options = new ConnectionOptions();
69-
if (!self::$isGnatsd) {
70-
time_nanosleep(1, 700000000);
71-
$options->setPort(4222);
72-
}
46+
47+
$streamWrapper = $this->prophesize("Nats\StreamWrapper");
48+
$streamWrapper->getStreamSocketClient(Argument::any(), Argument::any(), Argument::any(), Argument::any(), Argument::any())->will(function ($args) {
49+
return fopen("/tmp/".uniqid(), 'w');
50+
51+
});
52+
53+
$streamWrapper->setStreamTimeout(Argument::any(), Argument::any())->willReturn(true);
54+
7355
$this->c = new Nats\Connection($options);
56+
$this->c->setStreamWrapper($streamWrapper->reveal());
7457
$this->c->connect();
7558
}
7659

77-
7860
/**
7961
* Test Connection.
8062
*
@@ -122,7 +104,6 @@ public function testPublish()
122104

123105
/**
124106
* Test Reconnect command.
125-
*
126107
* @return void
127108
*/
128109
public function testReconnect()
@@ -153,9 +134,10 @@ public function testSubscription()
153134

154135
$this->c->publish('foo', 'bar');
155136
$this->assertEquals(1, $this->c->pubsCount());
156-
137+
/*
157138
$process = new BackgroundProcess('/usr/bin/php ./tests/Util/ClientServerStub.php ');
158139
$process->run();
140+
*/
159141
// time_nanosleep(1, 0);
160142
$this->c->wait(1);
161143
}

tests/Util/ClientServerStub.php

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

tests/Util/ListeningServerStub.php

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

0 commit comments

Comments
 (0)