From 3eb60bb35c87cb1395a06ff88b395aabc2152a15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Tue, 14 Jun 2016 01:23:32 +0200 Subject: [PATCH] Stop emitting events when closing stream during event handler --- src/ControlCodeParser.php | 1 + tests/ControlCodeParserTest.php | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/src/ControlCodeParser.php b/src/ControlCodeParser.php index eb1a592..a1bb90f 100644 --- a/src/ControlCodeParser.php +++ b/src/ControlCodeParser.php @@ -84,6 +84,7 @@ public function close() } $this->closed = true; + $this->buffer = ''; $this->input->close(); diff --git a/tests/ControlCodeParserTest.php b/tests/ControlCodeParserTest.php index 7b2cda3..ec2b8d7 100644 --- a/tests/ControlCodeParserTest.php +++ b/tests/ControlCodeParserTest.php @@ -122,6 +122,18 @@ public function testEmitsDataInTwoChunkWithC0InBetweenWhileAddingDuringDataEvent $this->input->emit('data', array("hello\nworld")); } + public function testEmitsDataOnlyFirstChunkOfMultipleWhenClosingDuringFirstDataEvent() + { + // first data event is everything before control code + $first = $this->expectCallableOnceWith('hello'); + $this->parser->on('data', $first); + + // close the input stream on the first data event => no more data events + $this->parser->once('data', array($this->input, 'close')); + + $this->input->emit('data', array("hello\nworld")); + } + public function testEmitsC0AndData() { $this->parser->on('data', $this->expectCallableOnceWith("hello world"));