Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit "error" event for unexpected response messages #21

Merged
merged 1 commit into from
Mar 31, 2015
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
5 changes: 3 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use UnexpectedValueException;
use Clue\React\Ami\Protocol\Message;
use Clue\React\Ami\Protocol\ErrorException;
use Clue\React\Ami\Protocol\UnexpectedMessageException;

class Client extends EventEmitter
{
Expand Down Expand Up @@ -66,6 +67,7 @@ public function request(Action $message)
return $deferred->promise();
}

/** @internal */
public function handleMessage(Message $message)
{
if ($message instanceof Event) {
Expand All @@ -74,8 +76,7 @@ public function handleMessage(Message $message)
}
$id = $message->getActionId();
if (!isset($this->pending[$id])) {
var_dump('unexpected', $message);
// unexpected message
$this->emit('error', array(new UnexpectedMessageException($message), $this));
return;
}

Expand Down
21 changes: 21 additions & 0 deletions src/Protocol/UnexpectedMessageException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Clue\React\Ami\Protocol;

use UnexpectedValueException;

class UnexpectedMessageException extends UnexpectedValueException
{
private $response;

public function __construct(Response $response)
{
parent::__construct('Unexpected message with action ID "' . $response->getActionId() . '" received');
$this->response = $response;
}

public function getResponse()
{
return $this->response;
}
}
11 changes: 11 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Clue\React\Ami\Protocol\Parser;
use Clue\React\Ami\Client;
use React\EventLoop\Factory;
use Clue\React\Ami\Protocol\Response;

class ClientTest extends TestCase
{
Expand Down Expand Up @@ -32,6 +33,16 @@ public function testParserExceptionForwardsErrorAndClosesClient()
$stream->emit('data', array("invalid chunk\r\n\r\ninvalid chunk\r\n\r\n"));
}

public function testUnexpectedResponseEmitsErrorAndClosesClient()
{
$client = new Client($this->createStreamMock());

$client->on('error', $this->expectCallableOnce());
$client->on('close', $this->expectCallableOnce());

$client->handleMessage(new Response(array('ActionID' => 1)));
}

private function createStreamMock()
{
return new Stream(fopen('php://memory', 'r+'), $this->getMock('React\EventLoop\LoopInterface'));
Expand Down
16 changes: 16 additions & 0 deletions tests/Protocol/UnexpectedMessageExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

use Clue\React\Ami\Protocol\Response;
use Clue\React\Ami\Protocol\UnexpectedMessageException;

class UnexpectedMessageExceptionTest extends TestCase
{
public function testGetResponse()
{
$response = new Response(array('ActionID' => 1));

$exception = new UnexpectedMessageException($response);

$this->assertSame($response, $exception->getResponse());
}
}