Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/Protocol/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Clue\React\Ami\Protocol;

use Clue\Hexdump\Hexdump;
class Parser
{
const EOM = "\r\n\r\n";
Expand Down
27 changes: 26 additions & 1 deletion src/Protocol/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,33 @@ public function __construct(array $fields)
$this->fields = $fields;
}

/**
* Get the resulting output of a "command" Action.
*
* This value is only available if this is actually a response to a "command"
* action, otherwise it defaults to null.
*
* ```php$sender->command('help')->then(function (Response $response) {
* echo $response->getCommandOutput();
* });
* ```
*
* @return ?string
*/
public function getCommandOutput()
{
return $this->getFieldValue(self::FIELD_COMMAND_OUTPUT);
// legacy Asterisk uses custom format for command output
$output = $this->getFieldValue(self::FIELD_COMMAND_OUTPUT);
if ($output !== null) {
return $output;
}

// Asterisk 14+ uses multiple "Output" fields: https://github.com/asterisk/asterisk/commit/2f418c052ec
$output = $this->getFieldValues('Output');
if (!$output) {
return null;
}

return implode("\n", $output);
}
}
20 changes: 18 additions & 2 deletions tests/Protocol/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,23 @@ public function testParseResponseMultipleValues()
$this->assertEquals(array('one', 'two'), $first->getFieldValues('Message'));
}

public function testParsingCommandResponse()
public function testParsingAsterisk14CommandResponse()
{
$parser = new Parser();
$this->assertEquals(array(), $parser->push("Asterisk Call Manager/1.3\r\n"));

$ret = $parser->push("Response: Follows\r\nOutput: Testing: yes\r\nOutput: Another Line\r\n\r\n");
$this->assertCount(1, $ret);

$first = reset($ret);
/* @var $first \Clue\React\Ami\Protocol\Response */

$this->assertInstanceOf('Clue\React\Ami\Protocol\Response', $first);
$this->assertEquals('Follows', $first->getFieldValue('Response'));
$this->assertEquals("Testing: yes\nAnother Line", $first->getCommandOutput());
}

public function testParsingLegacyCommandResponse()
{
$parser = new Parser();
$this->assertEquals(array(), $parser->push("Asterisk Call Manager/1.3\r\n"));
Expand All @@ -84,7 +100,7 @@ public function testParsingCommandResponse()
$this->assertEquals("Testing: yes\nAnother Line\n--END COMMAND--", $first->getCommandOutput());
}

public function testParsingCommandResponseEmpty()
public function testParsingLegacyCommandResponseEmpty()
{
$parser = new Parser();
$this->assertEquals(array(), $parser->push("Asterisk Call Manager/1.3\r\n"));
Expand Down
41 changes: 41 additions & 0 deletions tests/Protocol/ResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Clue\Tests\React\Ami\Protocol;

use Clue\Tests\React\Ami\TestCase;
use Clue\React\Ami\Protocol\Response;

class ResponseTest extends TestCase
{
public function testGetCommandOutputReturnsOutputFieldsConcatenated()
{
$response = new Response(array(
'Output' => array(
'First',
'Second',
'Third'
)
));

$this->assertEquals("First\nSecond\nThird", $response->getCommandOutput());
}

public function testGetCommandOutputReturnsLegacyOutputFieldsWhenPresent()
{
$response = new Response(array(
Response::FIELD_COMMAND_OUTPUT => 'legacy',
'Output' => 'ignored'
));

$this->assertEquals("legacy", $response->getCommandOutput());
}

public function testGetCommandOutputReturnsNullForEmptyResponse()
{
$response = new Response(array(
'Foo' => 'bar'
));

$this->assertNull($response->getCommandOutput());
}
}