function it_listens_to_events(StreamInterface $inputStream, StreamInterface $outputStream) { $handler = new Handler(); $header = ''; $inputStream->eof()->willReturn(false); $inputStream->read(1)->will(function ($args) use(&$header) { if (empty($header)) { $header = str_split("ver:3.0 server:supervisor serial:21 pool:listener poolserial:10 eventname:PROCESS_COMMUNICATION_STDOUT len:86\n"); return "\n"; } return array_shift($header); }); $inputStream->read(86)->will(function ($args) use(&$header) { $header = str_split("ver:3.0 server:supervisor serial:21 pool:listener poolserial:10 eventname:PROCESS_COMMUNICATION_STDOUT len:86\n"); return "processname:foo groupname:bar pid:123\nThis is the data that was sent between the tags"; }); $outputStream->write("READY\n")->shouldBeCalledTimes(4); $outputStream->write("RESULT 2\nOK")->shouldBeCalled(); $outputStream->write("RESULT 4\nFAIL")->shouldBeCalled(); $this->listen($handler); }
/** * Copy the contents of a stream into another stream until the given number * of bytes have been read. * * @param StreamInterface $source Stream to read from * @param StreamInterface $dest Stream to write to * @param int $maxLen Maximum number of bytes to read. Pass -1 * to read the entire stream. */ public static function copyToStream(StreamInterface $source, StreamInterface $dest, $maxLen = -1) { if ($maxLen === -1) { while (!$source->eof()) { if (!$dest->write($source->read(1048576))) { break; } } return; } $bytes = 0; while (!$source->eof()) { $buf = $source->read($maxLen - $bytes); if (!($len = strlen($buf))) { break; } $bytes += $len; $dest->write($buf); if ($bytes == $maxLen) { break; } } }
private function write($text) { $this->output->write(date('c') . ': ' . $text . PHP_EOL); }
/** * {@inheritdoc} */ protected function write($value) { return $this->outputStream->write($value); }
/** * {@inheritdoc} */ protected function doWrite($string) { return $this->stream->write($string); }