Skip to content

Commit 85e3f1b

Browse files
committed
Add test suite
1 parent a66d5a7 commit 85e3f1b

File tree

5 files changed

+126
-0
lines changed

5 files changed

+126
-0
lines changed

.travis.yml

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

phpunit.xml.dist

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

src/Client.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use React\EventLoop\LoopInterface;
66
use Clue\React\Multicast\Factory as MulticastFactory;
77
use React\Promise\Deferred;
8+
use RuntimeException;
89

910
class Client
1011
{

tests/ClientTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
use Clue\React\Ssdp\Client;
4+
use React\EventLoop\Factory;
5+
6+
class ClientTest extends TestCase
7+
{
8+
public function testCtor()
9+
{
10+
$loop = $this->getMock('React\EventLoop\LoopInterface');
11+
new Client($loop);
12+
}
13+
14+
public function testSearchCancel()
15+
{
16+
$loop = $this->getMock('React\EventLoop\LoopInterface');
17+
$multicast = $this->getMockBuilder('Clue\React\Multicast\Factory')->disableOriginalConstructor()->getMock();
18+
$client = new Client($loop, $multicast);
19+
20+
$socket = $this->getMock('React\Datagram\SocketInterface');
21+
$socket->expects($this->once())->method('send');
22+
23+
$timer = $this->getMock('React\EventLoop\Timer\TimerInterface');
24+
$loop->expects($this->once())->method('addTimer')->will($this->returnValue($timer));
25+
26+
$multicast->expects($this->once())->method('createSender')->will($this->returnValue($socket));
27+
28+
$promise = $client->search();
29+
30+
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
31+
32+
if (!($promise instanceof React\Promise\CancellablePromiseInterface)) {
33+
$this->markTestSkipped();
34+
}
35+
36+
$socket->expects($this->once())->method('close');
37+
$timer->expects($this->once())->method('cancel');
38+
39+
$promise->cancel();
40+
41+
$promise->then(null, $this->expectCallableOnce());
42+
}
43+
44+
public function testSearchTimeout()
45+
{
46+
$loop = Factory::create();
47+
$client = new Client($loop);
48+
49+
$promise = $client->search('ssdp:all', 0.01);
50+
51+
$loop->run();
52+
53+
$promise->then($this->expectCallableOnce(), $this->expectCallableNever(), $this->expectCallableNever());
54+
}
55+
}

tests/bootstrap.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
require __DIR__ . '/../vendor/autoload.php';
4+
5+
class TestCase extends PHPUnit_Framework_TestCase
6+
{
7+
protected function expectCallableOnce()
8+
{
9+
$mock = $this->createCallableMock();
10+
11+
$mock
12+
->expects($this->once())
13+
->method('__invoke');
14+
15+
return $mock;
16+
}
17+
18+
protected function expectCallableNever()
19+
{
20+
$mock = $this->createCallableMock();
21+
$mock
22+
->expects($this->never())
23+
->method('__invoke');
24+
25+
return $mock;
26+
}
27+
28+
/**
29+
* @link https://github.com/reactphp/react/blob/master/tests/React/Tests/Socket/TestCase.php (taken from reactphp/react)
30+
*/
31+
protected function createCallableMock()
32+
{
33+
return $this->getMock('CallableStub');
34+
}
35+
}
36+
37+
class CallableStub
38+
{
39+
public function __invoke()
40+
{
41+
}
42+
}

0 commit comments

Comments
 (0)