Skip to content

Commit 75b5060

Browse files
committed
Support Asterisk 14+ command output format as well as legacy format
1 parent 39b25a9 commit 75b5060

4 files changed

Lines changed: 85 additions & 4 deletions

File tree

src/Protocol/Parser.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Clue\React\Ami\Protocol;
44

5-
use Clue\Hexdump\Hexdump;
65
class Parser
76
{
87
const EOM = "\r\n\r\n";

src/Protocol/Response.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,33 @@ public function __construct(array $fields)
1212
$this->fields = $fields;
1313
}
1414

15+
/**
16+
* Get the resulting output of a "command" Action.
17+
*
18+
* This value is only available if this is actually a response to a "command"
19+
* action, otherwise it defaults to null.
20+
*
21+
* ```php$sender->command('help')->then(function (Response $response) {
22+
* echo $response->getCommandOutput();
23+
* });
24+
* ```
25+
*
26+
* @return ?string
27+
*/
1528
public function getCommandOutput()
1629
{
17-
return $this->getFieldValue(self::FIELD_COMMAND_OUTPUT);
30+
// legacy Asterisk uses custom format for command output
31+
$output = $this->getFieldValue(self::FIELD_COMMAND_OUTPUT);
32+
if ($output !== null) {
33+
return $output;
34+
}
35+
36+
// Asterisk 14+ uses multiple "Output" fields: https://github.com/asterisk/asterisk/commit/2f418c052ec
37+
$output = $this->getFieldValues('Output');
38+
if (!$output) {
39+
return null;
40+
}
41+
42+
return implode("\n", $output);
1843
}
1944
}

tests/Protocol/ParserTest.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,23 @@ public function testParseResponseMultipleValues()
6868
$this->assertEquals(array('one', 'two'), $first->getFieldValues('Message'));
6969
}
7070

71-
public function testParsingCommandResponse()
71+
public function testParsingAsterisk14CommandResponse()
72+
{
73+
$parser = new Parser();
74+
$this->assertEquals(array(), $parser->push("Asterisk Call Manager/1.3\r\n"));
75+
76+
$ret = $parser->push("Response: Follows\r\nOutput: Testing: yes\r\nOutput: Another Line\r\n\r\n");
77+
$this->assertCount(1, $ret);
78+
79+
$first = reset($ret);
80+
/* @var $first \Clue\React\Ami\Protocol\Response */
81+
82+
$this->assertInstanceOf('Clue\React\Ami\Protocol\Response', $first);
83+
$this->assertEquals('Follows', $first->getFieldValue('Response'));
84+
$this->assertEquals("Testing: yes\nAnother Line", $first->getCommandOutput());
85+
}
86+
87+
public function testParsingLegacyCommandResponse()
7288
{
7389
$parser = new Parser();
7490
$this->assertEquals(array(), $parser->push("Asterisk Call Manager/1.3\r\n"));
@@ -84,7 +100,7 @@ public function testParsingCommandResponse()
84100
$this->assertEquals("Testing: yes\nAnother Line\n--END COMMAND--", $first->getCommandOutput());
85101
}
86102

87-
public function testParsingCommandResponseEmpty()
103+
public function testParsingLegacyCommandResponseEmpty()
88104
{
89105
$parser = new Parser();
90106
$this->assertEquals(array(), $parser->push("Asterisk Call Manager/1.3\r\n"));

tests/Protocol/ResponseTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Clue\Tests\React\Ami\Protocol;
4+
5+
use Clue\Tests\React\Ami\TestCase;
6+
use Clue\React\Ami\Protocol\Response;
7+
8+
class ResponseTest extends TestCase
9+
{
10+
public function testGetCommandOutputReturnsOutputFieldsConcatenated()
11+
{
12+
$response = new Response(array(
13+
'Output' => array(
14+
'First',
15+
'Second',
16+
'Third'
17+
)
18+
));
19+
20+
$this->assertEquals("First\nSecond\nThird", $response->getCommandOutput());
21+
}
22+
23+
public function testGetCommandOutputReturnsLegacyOutputFieldsWhenPresent()
24+
{
25+
$response = new Response(array(
26+
Response::FIELD_COMMAND_OUTPUT => 'legacy',
27+
'Output' => 'ignored'
28+
));
29+
30+
$this->assertEquals("legacy", $response->getCommandOutput());
31+
}
32+
33+
public function testGetCommandOutputReturnsNullForEmptyResponse()
34+
{
35+
$response = new Response(array(
36+
'Foo' => 'bar'
37+
));
38+
39+
$this->assertNull($response->getCommandOutput());
40+
}
41+
}

0 commit comments

Comments
 (0)