forked from clue/reactphp-mdns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMulticastExecutorTest.php
More file actions
79 lines (57 loc) · 3.43 KB
/
MulticastExecutorTest.php
File metadata and controls
79 lines (57 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
namespace Clue\Tests\React\Mdns;
use Clue\React\Mdns\MulticastExecutor;
use Clue\React\Mdns\Factory;
use React\Dns\Query\Query;
class MulticastExecutorTest extends TestCase
{
public function testQueryWillReturnPromise()
{
$nameserver = Factory::DNS;
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$parser = $this->getMockBuilder('React\Dns\Protocol\Parser')->getMock();
$dumper = $this->getMockBuilder('React\Dns\Protocol\BinaryDumper')->getMock();
$sockets = $this->getMockBuilder('Clue\React\Multicast\Factory')->disableOriginalConstructor()->getMock();
$executor = new MulticastExecutor($loop, $parser, $dumper, 5, $sockets);
$socket = $this->getMockBuilder('React\Datagram\SocketInterface')->getMock();
$dumper->expects($this->once())->method('toBinary')->will($this->returnValue('message'));
$sockets->expects($this->once())->method('createSender')->will($this->returnValue($socket));
$socket->expects($this->once())->method('send')->with($this->equalTo('message'), $this->equalTo($nameserver));
$query = new Query('name', 'type', 'class', time());
$ret = $executor->query($nameserver, $query);
$this->assertInstanceOf('React\Promise\PromiseInterface', $ret);
}
public function testConstructWithoutLoopAssignsLoopAutomatically()
{
$executor = new MulticastExecutor();
$ref = new \ReflectionProperty($executor, 'loop');
$ref->setAccessible(true);
$loop = $ref->getValue($executor);
$this->assertInstanceOf('React\EventLoop\LoopInterface', $loop);
}
public function testCancellingPromiseWillCloseSocketAndReject()
{
$nameserver = Factory::DNS;
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$parser = $this->getMockBuilder('React\Dns\Protocol\Parser')->getMock();
$dumper = $this->getMockBuilder('React\Dns\Protocol\BinaryDumper')->getMock();
$sockets = $this->getMockBuilder('Clue\React\Multicast\Factory')->disableOriginalConstructor()->getMock();
$executor = new MulticastExecutor($loop, $parser, $dumper, 5, $sockets);
$socket = $this->getMockBuilder('React\Datagram\SocketInterface')->getMock();
$socket->expects($this->once())->method('close');
$socket->expects($this->once())->method('send')->with($this->equalTo('message'), $this->equalTo($nameserver));
$sockets->expects($this->once())->method('createSender')->will($this->returnValue($socket));
// prefer newer EventLoop 1.0/0.5+ TimerInterface or fall back to legacy namespace
$timer = $this->getMockBuilder(
interface_exists('React\EventLoop\TimerInterface') ? 'React\EventLoop\TimerInterface' : 'React\EventLoop\Timer\TimerInterface'
)->getMock();
$loop->expects($this->once())->method('addTimer')->willReturn($timer);
$loop->expects($this->once())->method('cancelTimer')->with($timer);
$dumper->expects($this->once())->method('toBinary')->will($this->returnValue('message'));
$query = new Query('name', 'type', 'class', time());
$ret = $executor->query($nameserver, $query);
$this->assertInstanceOf('React\Promise\CancellablePromiseInterface', $ret);
$ret->cancel();
$ret->then($this->expectCallableNever(), $this->expectCallableOnceWith($this->isInstanceOf('RuntimeException')));
}
}