Skip to content

Commit 3fa31f2

Browse files
committed
Merge pull request #3 from clue/tests
Add Tests
2 parents c49163a + 36cd87c commit 3fa31f2

File tree

4 files changed

+134
-1
lines changed

4 files changed

+134
-1
lines changed

.travis.yml

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

Datagram/Socket.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ public function close()
9292
$this->emit('close', array($this));
9393
$this->pause();
9494

95+
fclose($this->socket);
9596
$this->socket = false;
9697
$this->buffer->close();
97-
fclose($this->socket);
9898

9999
$this->removeAllListeners();
100100
}

phpunit.xml.dist

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

tests/FactoryTest.php

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
3+
use Datagram\Socket;
4+
5+
use React\Promise\When;
6+
7+
use React\Promise\PromiseInterface;
8+
9+
require __DIR__.'/../vendor/autoload.php';
10+
11+
class FactoryTest extends PHPUnit_Framework_TestCase
12+
{
13+
private $factory;
14+
15+
public function setUp()
16+
{
17+
$this->loop = React\EventLoop\Factory::create();
18+
$this->factory = new Datagram\Factory($this->loop, $this->createResolverMock());
19+
}
20+
21+
public function testCreateClient()
22+
{
23+
$promise = $this->factory->createClient('127.0.0.1', 12345);
24+
25+
$capturedClient = $this->getValueFromResolvedPromise($promise);
26+
$this->assertInstanceOf('Datagram\Socket', $capturedClient);
27+
28+
$capturedClient->close();
29+
}
30+
31+
public function testCreateClientLocalhost()
32+
{
33+
$promise = $this->factory->createClient('localhost', 12345);
34+
35+
$capturedClient = $this->getValueFromResolvedPromise($promise);
36+
$this->assertInstanceOf('Datagram\Socket', $capturedClient);
37+
38+
$capturedClient->close();
39+
}
40+
41+
public function testCreateClientIpv6()
42+
{
43+
$promise = $this->factory->createClient('::1', 12345);
44+
45+
$capturedClient = $this->getValueFromResolvedPromise($promise);
46+
$this->assertInstanceOf('Datagram\Socket', $capturedClient);
47+
48+
$capturedClient->close();
49+
}
50+
51+
public function testCreateServer()
52+
{
53+
$promise = $this->factory->createServer(12345, '127.0.0.1');
54+
55+
$capturedServer = $this->getValueFromResolvedPromise($promise);
56+
$this->assertInstanceOf('Datagram\Socket', $capturedServer);
57+
58+
$capturedServer->close();
59+
}
60+
61+
protected function getValueFromResolvedPromise($promise)
62+
{
63+
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
64+
65+
$loop = $this->loop;
66+
$capturedValue = null;
67+
$promise->then(function ($value) use (&$capturedValue, $loop) {
68+
$capturedValue = $value;
69+
$loop->stop();
70+
}, $this->expectCallableNever());
71+
72+
// future-turn resolutions are not enforced, so the value MAY be known here already
73+
if ($capturedValue === null) {
74+
$loop->run();
75+
}
76+
77+
return $capturedValue;
78+
}
79+
80+
protected function expectCallableOnce()
81+
{
82+
$mock = $this->createCallableMock();
83+
$mock
84+
->expects($this->once())
85+
->method('__invoke');
86+
87+
return $mock;
88+
}
89+
90+
protected function expectCallableNever()
91+
{
92+
$mock = $this->createCallableMock();
93+
$mock
94+
->expects($this->never())
95+
->method('__invoke');
96+
97+
return $mock;
98+
}
99+
100+
protected function createCallableMock()
101+
{
102+
return $this->getMock('React\Tests\Socket\Stub\CallableStub');
103+
}
104+
105+
private function createResolverMock()
106+
{
107+
return $this->getMockBuilder('React\Dns\Resolver\Resolver')
108+
->disableOriginalConstructor()
109+
->getMock();
110+
}
111+
}

0 commit comments

Comments
 (0)