diff --git a/src/Client.php b/src/Client.php index 29ccfe7..7e1f381 100644 --- a/src/Client.php +++ b/src/Client.php @@ -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 { @@ -66,6 +67,7 @@ public function request(Action $message) return $deferred->promise(); } + /** @internal */ public function handleMessage(Message $message) { if ($message instanceof Event) { @@ -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; } diff --git a/src/Protocol/UnexpectedMessageException.php b/src/Protocol/UnexpectedMessageException.php new file mode 100644 index 0000000..125ff74 --- /dev/null +++ b/src/Protocol/UnexpectedMessageException.php @@ -0,0 +1,21 @@ +getActionId() . '" received'); + $this->response = $response; + } + + public function getResponse() + { + return $this->response; + } +} diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 2c523a2..c858c2e 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -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 { @@ -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')); diff --git a/tests/Protocol/UnexpectedMessageExceptionTest.php b/tests/Protocol/UnexpectedMessageExceptionTest.php new file mode 100644 index 0000000..30a1a62 --- /dev/null +++ b/tests/Protocol/UnexpectedMessageExceptionTest.php @@ -0,0 +1,16 @@ + 1)); + + $exception = new UnexpectedMessageException($response); + + $this->assertSame($response, $exception->getResponse()); + } +}