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

Commit e6a7bc3

Browse files
author
jgil
committed
Refactor unit testing with wrapper for sockect functions
1 parent a0f4de3 commit e6a7bc3

5 files changed

Lines changed: 82 additions & 43 deletions

File tree

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

24 KB
Binary file not shown.

src/Connection.php

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,20 +98,32 @@ 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
*
104-
* @param ConnectionOptions $options Connection options object.
111+
* @param ConnectionOptions $options Connection options object.
112+
* @param StreamWrapper $streamWrapper Wrapper for stream functions. Testing purposes.
105113
*/
106-
public function __construct(ConnectionOptions $options = null)
114+
public function __construct(ConnectionOptions $options = null, StreamWrapper $streamWrapper = null)
107115
{
108116
$this->pings = 0;
109117
$this->pubs = 0;
110118
$this->subscriptions = [];
111119
$this->options = $options;
120+
$this->streamWrapper = $streamWrapper;
112121
if (is_null($options)) {
113122
$this->options = new ConnectionOptions();
114123
}
124+
if (is_null($streamWrapper)) {
125+
$this->streamWrapper = new StreamWrapper();
126+
}
115127
}
116128

117129
/**
@@ -163,10 +175,16 @@ private function getStream($address, $timeout = null)
163175
if (is_null($timeout)) {
164176
$timeout = intval(ini_get('default_socket_timeout'));
165177
}
166-
$fp = stream_socket_client($address, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT);
178+
$errno = null;
179+
$errstr = null;
180+
//$fp = stream_socket_client($address, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT);
181+
182+
$fp = $this->streamWrapper->getStreamSocketClient($address, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT);
183+
167184
if (!$fp) {
168185
throw new \Exception($errstr, $errno);
169186
}
187+
170188
//stream_set_blocking($fp, 0);
171189
return $fp;
172190
}
@@ -364,10 +382,12 @@ public function wait($quantity = 0)
364382
public function setStreamTimeout($seconds)
365383
{
366384
if ($this->isConnected()) {
367-
try {
368-
return stream_set_timeout($this->streamSocket, $seconds);
369-
} catch (\Exception $e) {
370-
return false;
385+
if (is_int($seconds)) {
386+
try {
387+
return $this->streamWrapper->setStreamTimeout($this->streamSocket, $seconds);
388+
} catch (\Exception $e) {
389+
return false;
390+
}
371391
}
372392
}
373393

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 & 35 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,19 @@ 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-
}
73-
$this->c = new Nats\Connection($options);
46+
47+
$streamWrapper = $this->prophesize(StreamWrapper::class);
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+
55+
$this->c = new Nats\Connection($options, $streamWrapper->reveal());
7456
$this->c->connect();
7557
}
7658

77-
7859
/**
7960
* Test Connection.
8061
*
@@ -122,7 +103,6 @@ public function testPublish()
122103

123104
/**
124105
* Test Reconnect command.
125-
*
126106
* @return void
127107
*/
128108
public function testReconnect()
@@ -153,9 +133,10 @@ public function testSubscription()
153133

154134
$this->c->publish('foo', 'bar');
155135
$this->assertEquals(1, $this->c->pubsCount());
156-
136+
/*
157137
$process = new BackgroundProcess('/usr/bin/php ./tests/Util/ClientServerStub.php ');
158138
$process->run();
139+
*/
159140
// time_nanosleep(1, 0);
160141
$this->c->wait(1);
161142
}

0 commit comments

Comments
 (0)