Skip to content

Commit ebe35f8

Browse files
committed
Merge pull request #18 from clue/functional
Add functional test
2 parents f8cebb5 + f19dd58 commit ebe35f8

3 files changed

Lines changed: 125 additions & 1 deletion

File tree

.travis.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ php:
33
- 5.3
44
- 5.6
55
- hhvm
6-
before_script:
6+
env:
7+
- LOGIN='username:password@localhost'
8+
install:
9+
- bash -c '[ "$(id -un)" == "root" ] && apt-get update && apt-get install -y sudo || true'
10+
- sudo apt-get install -y asterisk
11+
- sudo cp tests/username.conf /etc/asterisk/manager.d/username.conf
12+
- sudo /etc/init.d/asterisk restart
713
- composer install --prefer-source --no-interaction
814
script:
915
- phpunit --coverage-text

tests/FunctionalTest.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
use Clue\React\Ami\Factory;
4+
use React\Promise\PromiseInterface;
5+
use Clue\React\Ami\Client;
6+
use Clue\React\Ami\Api;
7+
use Clue\React\Ami\Protocol\Response;
8+
9+
class FunctionalTest extends TestCase
10+
{
11+
private static $address = false;
12+
private static $loop;
13+
14+
public static function setUpBeforeClass()
15+
{
16+
self::$address = getenv('LOGIN');
17+
self::$loop = React\EventLoop\Factory::create();
18+
}
19+
20+
public function setUp()
21+
{
22+
if (self::$address === false) {
23+
$this->markTestSkipped('No ENV named LOGIN found. Please use "export LOGIN=\'user:pass@host\'.');
24+
}
25+
}
26+
27+
public function testConnection()
28+
{
29+
$factory = new Factory(self::$loop);
30+
31+
$client = $this->waitFor($factory->createClient(self::$address));
32+
/* @var $client Client */
33+
34+
$this->assertFalse($client->isBusy());
35+
36+
return $client;
37+
}
38+
39+
/**
40+
* @depends testConnection
41+
* @param Client $client
42+
*/
43+
public function testPing(Client $client)
44+
{
45+
$api = new Api($client);
46+
47+
$pong = $this->waitFor($api->ping());
48+
/* @var $pong Response */
49+
}
50+
51+
/**
52+
* @depends testConnection
53+
* @param Client $client
54+
* @expectedException Exception
55+
*/
56+
public function testInvalidCommandGetsRejected(Client $client)
57+
{
58+
$this->waitFor($client->request($client->createAction('Invalid')));
59+
}
60+
61+
/**
62+
* @depends testConnection
63+
* @param Client $client
64+
*/
65+
public function testApiLogoffDisconnects(Client $client)
66+
{
67+
$api = new Api($client);
68+
69+
$ret = $this->waitFor($api->logoff());
70+
/* @var $ret Response */
71+
72+
$this->assertFalse($client->isBusy());
73+
74+
//$client->on('close', $this->expectCallableOnce());
75+
76+
self::$loop->run();
77+
78+
return $client;
79+
}
80+
81+
/**
82+
* @depends testApiLogoffDisconnects
83+
* @param Client $client
84+
* @expectedException Exception
85+
*/
86+
public function testSendRejectedAfterClose(Client $client)
87+
{
88+
$this->waitFor($client->request($client->createAction('Ping')));
89+
}
90+
91+
private function waitFor(PromiseInterface $promise)
92+
{
93+
$resolved = null;
94+
$exception = null;
95+
96+
$promise->then(function ($c) use (&$resolved) {
97+
$resolved = $c;
98+
}, function($error) use (&$exception) {
99+
$exception = $error;
100+
});
101+
102+
while ($resolved === null && $exception === null) {
103+
self::$loop->tick();
104+
}
105+
106+
if ($exception !== null) {
107+
throw $exception;
108+
}
109+
110+
return $resolved;
111+
}
112+
}

tests/username.conf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
; demo config for functional test
2+
; should be placed in: /etc/asterisk/manager.d/username.conf
3+
[username]
4+
secret = password
5+
read = system,call,log,verbose,command,agent,user,originate
6+
write = system,call,log,verbose,command,agent,user,originate

0 commit comments

Comments
 (0)