From 5ba53f7e440d7493318794075b84d0ecffec48df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Wed, 18 Mar 2015 23:11:00 +0100 Subject: [PATCH] Emit "error" event for unexpected response messages --- src/Client.php | 5 +++-- src/Protocol/UnexpectedMessageException.php | 21 +++++++++++++++++++ tests/ClientTest.php | 11 ++++++++++ .../UnexpectedMessageExceptionTest.php | 16 ++++++++++++++ 4 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 src/Protocol/UnexpectedMessageException.php create mode 100644 tests/Protocol/UnexpectedMessageExceptionTest.php 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()); + } +}