Skip to content

Commit c2e8921

Browse files
committed
Unit tests for collector
1 parent bddf584 commit c2e8921

2 files changed

Lines changed: 61 additions & 3 deletions

File tree

src/Collector.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
use Clue\React\Ami\Client;
66
use Clue\React\Ami\Protocol\Response;
7-
use Clue\React\Ami\Protocol\Action;
8-
use UnexpectedValueException;
97
use Clue\React\Ami\Protocol\Event;
108
use Clue\React\Ami\Protocol\Collection;
119
use React\Promise\Deferred;
@@ -36,7 +34,7 @@ public function agents()
3634

3735
private function collectEvents($command, $expectedEndEvent)
3836
{
39-
$req = new Action($command);
37+
$req = $this->client->createAction($command);
4038
$ret = $this->client->request($req);
4139
$id = $req->getActionId();
4240

tests/CollectorTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
use Clue\React\Ami\Collector;
4+
use Clue\React\Ami\Protocol\Collection;
5+
use Clue\React\Ami\Protocol\Response;
6+
use Clue\React\Ami\Client;
7+
use Clue\React\Ami\Protocol\Event;
8+
use Clue\React\Ami\Protocol\Action;
9+
10+
class CollectorTest extends TestCase
11+
{
12+
public function testCollectingSIPEvents()
13+
{
14+
$client = $this->createClientMock();
15+
16+
// expect a single outgoing action request (and mock its ID)
17+
$client->expects($this->once())
18+
->method('createAction')
19+
->with($this->equalTo('SIPPeers'), $this->equalTo(array()))
20+
->will($this->returnValue(new Action('SIPPeers', array('ActionID' => '123'))));
21+
22+
$collector = new Collector($client);
23+
24+
$promise = $collector->sipPeers();
25+
26+
// save resolved result for comparisions
27+
$resolved = null;
28+
$promise->then(function($result) use (&$resolved) {
29+
$resolved = $result;
30+
});
31+
32+
// should not start out resolved
33+
$this->assertNull($resolved);
34+
35+
$response = new Response(array('Response' => 'Success', 'ActionID' => '123'));
36+
37+
$client->handleMessage($response);
38+
$client->handleMessage(new Event('PeerEntry', array('ActionID' => '123')));
39+
$client->handleMessage(new Event('PeerEntry', array('ActionID' => '123')));
40+
41+
$this->assertNull($resolved);
42+
43+
$client->handleMessage(new Event('PeerlistComplete', array('EventList' => 'complete', 'ListItems' => '2', 'ActionID' => '123')));
44+
45+
$this->assertNotNull($resolved);
46+
47+
$promise->then(
48+
$this->expectCallableOnce()
49+
);
50+
}
51+
52+
private function createClientMock()
53+
{
54+
$stream = $this->getMockBuilder('React\Stream\Stream')->disableOriginalConstructor()->getMock();
55+
56+
$client = $this->getMockBuilder('Clue\React\Ami\Client')->setMethods(array('createAction'))->setConstructorArgs(array($stream))->getMock();
57+
58+
return $client;
59+
}
60+
}

0 commit comments

Comments
 (0)