I wrote a test and found that BufferTest::testWritingToClosedStream() have the same logic I wrote.
I decide to not duplicate it. But it is required to make some changes in the code.
When $sent = fwrite($this->stream, $this->data); executes we need to check errors.
Better solution is to check errno. set_error_handler always handle error if one occurred.
if ($this->lastError['number'] > 0) {
In this case, that part of code is not needed anymore:
if (0 === $sent && feof($this->stream)) { $this->emit('error', array(new \RuntimeException('Tried to write to closed stream.'), $this)); return; }
...because in test testWritingToClosedStream() this type of error is catched by set_error_handler().
Error message contains fwrite(): send of 3 bytes failed with errno=32 Broken pipe, errno=8.
To be sure I replaced part of code I mentioned above by checking $sent === false
if ($sent === false) { $this->emit('error', array(new \RuntimeException('Send failed'), $this)); return; }
In conclusion error checking looks like that:
if ($this->lastError['number'] > 0) { $this->emit('error', array( new \ErrorException( $this->lastError['message'], 0, $this->lastError['number'], $this->lastError['file'], $this->lastError['line'] ), $this )); return; } if ($sent === false) { $this->emit('error', array(new \RuntimeException('Send failed'), $this)); return; }
and testWritingToClosedStream asserion looks like that:
$this->assertSame('fwrite(): send of 3 bytes failed with errno=32 Broken pipe', $error->getMessage());
All 66 tests have executed successfully.