|
| 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