Skip to content

Commit 3a827d8

Browse files
author
jgil
committed
Trying to test with a process in background, but i couldn't
1 parent 377cdad commit 3a827d8

File tree

5 files changed

+203
-40
lines changed

5 files changed

+203
-40
lines changed

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"homepage": "http://repejota.com"
1111
}],
1212
"require": {
13+
"cocur/background-process": "dev-master"
1314
},
1415
"require-dev": {
1516
"phpunit/phpunit": "4.7.*",

phpunit.xml.dist

+2
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@
1616
</testsuites>
1717
<logging>
1818
<log type="coverage-clover" target="build/logs/clover.xml"/>
19+
<log type="coverage-html" target="./build/coverage" lowUpperBound="35"
20+
highLowerBound="70"/>
1921
</logging>
2022
</phpunit>

tests/Unit/ClientServerStub.php

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace Nats\tests\Unit;
4+
5+
require 'vendor/autoload.php';
6+
use Cocur\BackgroundProcess\BackgroundProcess;
7+
8+
9+
class ClientServerStub
10+
{
11+
protected $client;
12+
13+
protected $sock;
14+
15+
protected $addr;
16+
17+
protected $port;
18+
19+
public function __construct()
20+
{
21+
$this->sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
22+
socket_connect($this->sock, 'localhost', 55555);
23+
}
24+
25+
public function write()
26+
{
27+
socket_write($this->sock, "PUBLISH");
28+
29+
}
30+
31+
public function close()
32+
{
33+
socket_close($this->sock);
34+
}
35+
36+
public function read()
37+
{
38+
// Read the input from the client &#8211; 1024 bytes
39+
$input = socket_read($client, 1024);
40+
41+
return $input;
42+
}
43+
44+
public function getAddr()
45+
{
46+
return $this->addr;
47+
}
48+
49+
public function getPort()
50+
{
51+
return $this->port;
52+
}
53+
54+
public function getSock()
55+
{
56+
return $this->sock;
57+
}
58+
}
59+
60+
$client = new ClientServerStub();
61+
$client->write();
62+
63+
$client->close();

tests/Unit/ConnectionTest.php

+51-40
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,73 @@
11
<?php
2+
23
/**
3-
* TestConnection Class
4+
* TestConnection Class.
45
*
56
* PHP version 5
67
*
78
* @category Class
8-
* @package Nats\Tests\Unit
9+
*
910
* @author Raül Përez <[email protected]>
1011
* @license http://opensource.org/licenses/MIT The MIT License (MIT)
12+
*
1113
* @link https://github.com/repejota/phpnats
1214
*/
13-
namespace Nats\Tests\Unit;
15+
16+
namespace Nats\tests\Unit;
1417

1518
use Nats;
19+
use Cocur\BackgroundProcess\BackgroundProcess;
20+
21+
1622

1723
/**
18-
* Class TestConnection
24+
* Class TestConnection.
1925
*
2026
* @category Class
21-
* @package Nats\Tests\Unit
27+
*
2228
* @author Raül Përez <[email protected]>
2329
* @license http://opensource.org/licenses/MIT The MIT License (MIT)
30+
*
2431
* @link https://github.com/repejota/phpnats
2532
*/
26-
class TestConnection extends \PHPUnit_Framework_TestCase
33+
class ConnectionTest extends \PHPUnit_Framework_TestCase
2734
{
2835
private $_c;
2936

30-
/**
31-
* Setup tests
32-
*
33-
* @return null
34-
*/
37+
private static $_process;
38+
39+
private static $_port;
40+
41+
public static function setUpBeforeClass()
42+
{
43+
44+
self::$_process = new BackgroundProcess('/usr/bin/php tests/Unit/ListeningServerStub.php &');
45+
self::$_process->run();
46+
47+
}
48+
49+
public static function tearDownAfterClass()
50+
{
51+
self::$_process->stop();
52+
//socket_close(self::$_server);
53+
}
54+
3555
public function setUp()
3656
{
37-
$this->_c = new Nats\Connection();
57+
$this->_c = new Nats\Connection('localhost', 55555);
3858
$this->_c->connect();
3959
}
4060

4161
/**
42-
* Test Dummy
43-
*
44-
* @return null
62+
* Test Dummy.
4563
*/
4664
public function testDummy()
4765
{
4866
$this->assertTrue(true);
4967
}
5068

5169
/**
52-
* Test Connection
53-
*
54-
* @return null
70+
* Test Connection.
5571
*/
5672
public function testConnection()
5773
{
@@ -65,66 +81,61 @@ public function testConnection()
6581
}
6682

6783
/**
68-
* Test Ping command
69-
*
70-
* @return null
84+
* Test Ping command.
7185
*/
7286
public function testPing()
7387
{
7488
$this->_c->ping();
7589
$count = $this->_c->pingsCount();
76-
$this->assertInternalType("int", $count);
90+
$this->assertInternalType('int', $count);
7791
$this->assertGreaterThan(0, $count);
7892
$this->_c->close();
7993
}
8094

8195
/**
82-
* Test Publish command
83-
*
84-
* @return null
96+
* Test Publish command.
8597
*/
8698
public function testPublish()
8799
{
88-
$this->_c->publish("foo", "bar");
100+
$this->_c->publish('foo', 'bar');
89101
$count = $this->_c->pubsCount();
90-
$this->assertInternalType("int", $count);
102+
$this->assertInternalType('int', $count);
91103
$this->assertGreaterThan(0, $count);
92104
$this->_c->close();
93105
}
94106

95107
/**
96-
* Test Server reconnection
97-
*
98-
* @return null
108+
* Test Reconnect command.
99109
*/
100110
public function testReconnect()
101111
{
102112
$this->_c->reconnect();
103113
$count = $this->_c->reconnectsCount();
104-
$this->assertInternalType("int", $count);
114+
$this->assertInternalType('int', $count);
105115
$this->assertGreaterThan(0, $count);
106116
$this->_c->close();
107117
}
108118

109119
/**
110-
* Test Server subscription
111-
*
112-
* @return null
120+
* Test Subscription command.
113121
*/
114122
public function testSubscription()
115123
{
116124
$callback = function ($message) {
117125
$this->assertNotNull($message);
118-
$this->assertEquals($message, "bar");
126+
$this->assertEquals($message, 'bar');
119127
};
120-
$this->_c->subscribe("foo", $callback);
128+
$this->_c->subscribe('foo', $callback);
121129
$this->assertGreaterThan(0, $this->_c->subscriptionsCount());
122-
123130
$subscriptions = $this->_c->getSubscriptions();
124-
$this->assertInternalType("array", $subscriptions);
131+
$this->assertInternalType('array', $subscriptions);
125132

126-
$this->_c->publish("foo", "bar");
127-
$this->_c->wait(1);
133+
$this->_c->publish('foo', 'bar');
134+
$this->assertEquals(1, $this->_c->pubsCount());
135+
// $this->_c->wait(1);
128136
}
129137

138+
public function testWait()
139+
{
140+
}
130141
}

tests/Unit/ListeningServerStub.php

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace Nats\tests\Unit;
4+
5+
require 'vendor/autoload.php';
6+
7+
8+
class ListeningServerStub
9+
{
10+
protected $client;
11+
12+
protected $sock;
13+
14+
protected $addr;
15+
16+
protected $port;
17+
18+
public function __construct()
19+
{
20+
try {
21+
if (($this->sock = socket_create_listen(55555)) === false) {
22+
echo socket_strerror(socket_last_error());
23+
} else {
24+
echo "Socket created\n";
25+
}
26+
27+
socket_getsockname($this->sock, $this->addr, $this->port);
28+
} catch (\Exception $e) {
29+
echo $e->getMessage();
30+
}
31+
}
32+
33+
public function listen()
34+
{
35+
36+
// Bind the socket to an address/port
37+
38+
// Start listening for connections
39+
socket_listen($this->sock);
40+
41+
// Accept incoming requests and handle them as child processes.
42+
$this->client = socket_accept($this->sock);
43+
}
44+
45+
public function close()
46+
{
47+
socket_close($this->sock);
48+
}
49+
50+
public function read()
51+
{
52+
// Read the input from the client &#8211; 1024 bytes
53+
$input = socket_read($client, 1024);
54+
55+
return $input;
56+
}
57+
58+
public function getAddr()
59+
{
60+
return $this->addr;
61+
}
62+
63+
public function getPort()
64+
{
65+
return $this->port;
66+
}
67+
68+
public function getSock()
69+
{
70+
return $this->sock;
71+
}
72+
}
73+
74+
$server = new ListeningServerStub();
75+
76+
while (true) {
77+
$clientSocket = socket_accept($server->getSock());
78+
if (false == $clientSocket) {
79+
continue;
80+
}
81+
var_dump($clientSocket);
82+
}
83+
84+
$server->close();
85+
// echo "Start listening...";
86+
// $server->listen();

0 commit comments

Comments
 (0)