Skip to content

Commit 4d25842

Browse files
committed
Merge pull request bergie#18 from igorw/test-suite
Test suite
2 parents a32dfe1 + ca9944d commit 4d25842

16 files changed

+371
-4
lines changed

.travis.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
language: php
2+
3+
php:
4+
- 5.3
5+
- 5.4
6+
7+
before_script:
8+
- composer install --dev
9+
10+
script: phpunit --coverage-text

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ This project implements the [DNode](http://substack.net/posts/85e1bd/DNode-Async
55

66
You can read more about DNode and PHP in the [introductory blog post](http://bergie.iki.fi/blog/dnode-make_php_and_node-js_talk_to_each_other/).
77

8+
[![Build Status](https://secure.travis-ci.org/bergie/dnode-php.png?branch=master)](http://travis-ci.org/bergie/dnode-php)
9+
810
## Installing
911

1012
dnode-php can be installed using the [Composer](http://packagist.org/) tool. You can either add `dnode/dnode` to your package dependencies, or if you want to install dnode-php as standalone, go to the main directory of this package and run:

phpunit.xml.dist

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit backupGlobals="false"
4+
colors="true"
5+
bootstrap="tests/bootstrap.php"
6+
>
7+
<testsuites>
8+
<testsuite name="DNode-PHP Test Suite">
9+
<directory>./tests/DNode/</directory>
10+
</testsuite>
11+
</testsuites>
12+
13+
<filter>
14+
<whitelist>
15+
<directory>./src/</directory>
16+
</whitelist>
17+
</filter>
18+
</phpunit>

src/DNode/DNode.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ public function listen()
6464
$that->handleConnection($conn, $params);
6565
});
6666
$server->listen($params['port'], $params['host']);
67+
68+
return $server;
6769
}
6870

6971
public function handleConnection(ConnectionInterface $conn, $params)

src/DNode/Protocol.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22
namespace DNode;
3+
use React\Socket\ServerInterface;
34

45
class Protocol
56
{
@@ -14,7 +15,7 @@ public function __construct($wrapper)
1415
public function create()
1516
{
1617
// FIXME: Random ID generation, should be unique
17-
$id = time();
18+
$id = microtime();
1819
$this->sessions[$id] = new Session($id, $this->wrapper);
1920
return $this->sessions[$id];
2021
}
@@ -70,7 +71,7 @@ public function parseArgs($args) {
7071
continue;
7172
}
7273

73-
throw new \Exception("Not sure what to do about " . gettype($arg) . " arguments");
74+
throw new \InvalidArgumentException("Not sure what to do about " . gettype($arg) . " arguments");
7475
}
7576

7677
return $params;

src/DNode/RemoteProxy.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class RemoteProxy
55
{
66
private $methods = array();
77

8-
public function getMethods()
8+
public function getMethods()
99
{
1010
return $this->methods;
1111
}
@@ -18,7 +18,7 @@ public function setMethod($method, $closure)
1818
public function __call($method, $args)
1919
{
2020
if (!isset($this->methods[$method])) {
21-
throw new \Exception("Method {$method} not available");
21+
throw new \BadMethodCallException("Method {$method} not available");
2222
}
2323

2424
call_user_func_array($this->methods[$method], $args);

tests/DNode/CallableStub.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
namespace DNode;
3+
4+
class CallableStub
5+
{
6+
public function __invoke()
7+
{
8+
}
9+
}

tests/DNode/Dog.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
namespace DNode;
3+
4+
class Dog
5+
{
6+
public function bark()
7+
{
8+
}
9+
10+
public function meow()
11+
{
12+
}
13+
}

tests/DNode/FunctionalTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
namespace DNode;
3+
use React\EventLoop\StreamSelectLoop;
4+
5+
class FunctionalTest extends \PHPUnit_Framework_TestCase
6+
{
7+
/**
8+
* @covers DNode\DNode::__construct
9+
* @covers DNode\DNode::connect
10+
* @covers DNode\DNode::listen
11+
* @test
12+
*/
13+
public function transformerShouldRespondCorrectly()
14+
{
15+
$captured = null;
16+
17+
$loop = new StreamSelectLoop();
18+
19+
$server = new DNode($loop, new Transformer());
20+
$socket = $server->listen(5004);
21+
22+
$client = new DNode($loop);
23+
$client->connect(5004, function ($remote, $conn) use (&$captured, $socket) {
24+
$remote->transform('fou', function ($transformed) use ($conn, &$captured, $socket) {
25+
$captured = $transformed;
26+
$conn->end();
27+
$socket->shutdown();
28+
});
29+
});
30+
31+
$loop->run();
32+
33+
$this->assertSame('FOO', $captured);
34+
}
35+
}

tests/DNode/ProtocolTest.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
namespace DNode;
3+
4+
class ProtocolTest extends TestCase
5+
{
6+
public function setUp()
7+
{
8+
$this->protocol = new Protocol(new Dog());
9+
}
10+
11+
/**
12+
* @test
13+
* @covers DNode\Protocol::__construct
14+
* @covers DNode\Protocol::create
15+
*/
16+
public function createShouldReturnSession()
17+
{
18+
$session = $this->protocol->create();
19+
$this->assertInstanceOf('DNode\Session', $session);
20+
}
21+
22+
/**
23+
* @test
24+
* @covers DNode\Protocol::destroy
25+
*/
26+
public function destroyShouldUnsetSession()
27+
{
28+
$session = $this->protocol->create();
29+
$this->protocol->destroy($session->id);
30+
}
31+
32+
/**
33+
* @test
34+
* @covers DNode\Protocol::end
35+
*/
36+
public function endShouldCallEndOnAllSessions()
37+
{
38+
$sessions = array(
39+
$this->protocol->create(),
40+
$this->protocol->create(),
41+
);
42+
43+
foreach ($sessions as $session) {
44+
$session->on('end', $this->expectCallableOnce());
45+
}
46+
47+
$this->protocol->end();
48+
}
49+
50+
/**
51+
* @test
52+
* @covers DNode\Protocol::parseArgs
53+
* @dataProvider provideParseArgs
54+
*/
55+
public function parseArgsShouldParseArgsCorrectly($expected, $args)
56+
{
57+
$this->assertSame($expected, $this->protocol->parseArgs($args));
58+
}
59+
60+
public function provideParseArgs()
61+
{
62+
$closure = function () {};
63+
$server = new ServerStub();
64+
65+
$obj = new \stdClass();
66+
$obj->foo = 'bar';
67+
$obj->baz = 'qux';
68+
69+
return array(
70+
'string number becomes port' => array(
71+
array('port' => '8080'),
72+
array('8080'),
73+
),
74+
'leading / becomes path' => array(
75+
array('path' => '/foo'),
76+
array('/foo'),
77+
),
78+
'string becomes host' => array(
79+
array('host' => 'foo'),
80+
array('foo'),
81+
),
82+
'integer becomes port' => array(
83+
array('port' => 8080),
84+
array(8080),
85+
),
86+
'Closure becomes block' => array(
87+
array('block' => $closure),
88+
array($closure),
89+
),
90+
'ServerInterface becomes server' => array(
91+
array('server' => $server),
92+
array($server),
93+
),
94+
'random object becomes key => val' => array(
95+
array('foo' => 'bar', 'baz' => 'qux'),
96+
array($obj),
97+
),
98+
);
99+
}
100+
101+
/**
102+
* @test
103+
* @covers DNode\Protocol::parseArgs
104+
* @expectedException InvalidArgumentException
105+
* @expectedExceptionMessage Not sure what to do about array arguments
106+
*/
107+
public function parseArgsShouldRejectInvalidArgs()
108+
{
109+
$args = array(array('wat'));
110+
$this->protocol->parseArgs($args);
111+
}
112+
}

0 commit comments

Comments
 (0)