Skip to content

Commit ea50577

Browse files
committed
Add tests for Protocol
1 parent 7e44ea9 commit ea50577

File tree

4 files changed

+134
-1
lines changed

4 files changed

+134
-1
lines changed

src/DNode/Protocol.php

Lines changed: 2 additions & 1 deletion
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
{
@@ -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;

tests/DNode/FunctionalTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
class FunctionalTest extends \PHPUnit_Framework_TestCase
66
{
77
/**
8+
* @covers DNode\DNode::__construct
89
* @covers DNode\DNode::connect
910
* @covers DNode\DNode::listen
1011
* @test

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+
}

tests/DNode/ServerStub.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
namespace DNode;
3+
use Evenement\EventEmitter;
4+
use React\Socket\ServerInterface;
5+
6+
class ServerStub extends EventEmitter implements ServerInterface
7+
{
8+
public function listen($port, $host = '127.0.0.1')
9+
{
10+
}
11+
12+
public function getPort()
13+
{
14+
}
15+
16+
public function shutdown()
17+
{
18+
}
19+
}

0 commit comments

Comments
 (0)