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

Fix processing events when input writes during data event #15

Merged
merged 1 commit into from
Jun 13, 2016
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: 1 addition & 0 deletions src/ControlCodeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public function handleData($data)
$this->buffer = substr($this->buffer, $c0);

$this->emit('data', array($data));
continue;
}

// C0 is now at start of buffer
Expand Down
35 changes: 35 additions & 0 deletions tests/ControlCodeParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,41 @@ public function testEmitsDataAndC0()
$this->input->emit('data', array("hello world\n"));
}

public function testEmitsDataInTwoChunksWithC0InBetween()
{
// first data event is everything before control code
$first = $this->expectCallableOnceWith('hello');
$this->parser->once('data', $first);

// second data event will fire with remaining buffer
$second = $this->expectCallableOnceWith('world');
$parser = $this->parser;
$this->parser->once('data', function () use ($parser, $second) {
$parser->once('data', $second);
});

$this->input->emit('data', array("hello\nworld"));
}

public function testEmitsDataInTwoChunkWithC0InBetweenWhileAddingDuringDataEvent()
{
// first data event is everything before control code
$first = $this->expectCallableOnceWith('hello');
$this->parser->once('data', $first);

// second data event will fire with remaining buffer
// we write to buffer while processing the first event and expect the full buffer
$second = $this->expectCallableOnceWith('worldagain');
$input = $this->input;
$parser = $this->parser;
$this->parser->once('data', function () use ($input, $parser, $second) {
$parser->once('data', $second);
$input->emit('data', array('again'));
});

$this->input->emit('data', array("hello\nworld"));
}

public function testEmitsC0AndData()
{
$this->parser->on('data', $this->expectCallableOnceWith("hello world"));
Expand Down