forked from clue/reactphp-ami
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientTest.php
More file actions
62 lines (46 loc) · 1.91 KB
/
ClientTest.php
File metadata and controls
62 lines (46 loc) · 1.91 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
<?php
namespace Clue\Tests\React\Ami;
use Clue\React\Ami\Client;
use Clue\React\Ami\Protocol\Parser;
use Clue\React\Ami\Protocol\Response;
class ClientTest extends TestCase
{
public function testClosingStreamClosesClient()
{
$stream = $this->createStreamMock();
$client = new Client($stream);
$client->on('close', $this->expectCallableOnce());
$stream->emit('close');
}
public function testParserExceptionForwardsErrorAndClosesClient()
{
$stream = $this->createStreamMock();
$stream->expects($this->once())->method('close');
$parser = new Parser();
$client = new Client($stream, $parser);
$client->on('error', $this->expectCallableOnce());
$stream->emit('data', array("invalid chunk\r\n\r\ninvalid chunk\r\n\r\n"));
}
public function testUnexpectedResponseEmitsErrorAndClosesClient()
{
$client = new Client($this->createStreamMock());
$client->on('error', $this->expectCallableOnce());
$client->on('close', $this->expectCallableOnce());
$client->handleMessage(new Response(array('ActionID' => 1)));
}
public function testCtorThrowsForInvalidParser()
{
$this->setExpectedException('InvalidArgumentException', 'Argument #2 ($parser) expected null|Clue\React\Ami\Protocol\Parser');
new Client($this->createStreamMock(), 'parser');
}
private function createStreamMock()
{
if (method_exists('PHPUnit\Framework\MockObject\MockBuilder', 'onlyMethods')) {
// PHPUnit 9+
return $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->onlyMethods(array('write', 'close'))->getMock();
} else {
// legacy PHPUnit 4 - PHPUnit 8
return $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->setMethods(array('write', 'close'))->getMock();
}
}
}