/** * @depends test_isSystemCallInterupt * @covers ::transferStreams * @covers ::_transferStdIn * @covers ::_transferStdOut * @covers ::_transferStdErr * @covers ::getStatus */ public function test_transferStreams() { $Input = tempnam(sys_get_temp_dir(), 'input'); $Pipes = array(Command::STDIN => fopen($Input, 'w'), Command::STDOUT => fopen('data:text/plain,test output', 'r'), Command::STDERR => fopen('data:text/plain,test error', 'r')); $Property = new ReflectionProperty($this->Command, '_Pipes'); $Property->setAccessible(true); $Property->setValue($this->Command, $Pipes); $this->Input->setMediator($this->Mediator); $this->Output->setMediator($this->Mediator); $this->Command->transferStreams($this->Input, $this->Output, 10); $this->assertEquals('test output', $this->Output->stdOut->getContents(), 'ShellCommand::_transferStreams() Failed to transfer STDOUT'); $this->assertEquals('test error', $this->Output->stdErr->getContents(), 'ShellCommand::_transferStreams() Failed to transfer STDERR'); $this->assertEquals($this->Input->stdIn->getContents(), file_get_contents($Input), 'ShellCommand::_transferStreams() Failed to trasfer STDIN'); foreach ($Pipes as $fp) { fclose($fp); } sleep(1); @unlink($Input); }
/** * Sends a JSON command to PhantomJS * * @codeCoverageIngore * * @param array $Command * Command to send * @param integer $Timeout * Timeout for reads / writes to process. * @return string Output of command. <code>FALSE</code> on error. */ private function _sendJSON(array $Command, $Timeout = 10) { // `evaluate` command $Command = json_encode($Command); // Add end of transmition marker $Command .= "\r\n"; // Run Processes $Input = new CommandInput(new StringStream($Command)); $Output = new CommandOutput(new StringStream($stdOut, IFile::WRITE | IFile::TRUNCATE), new StringStream($stdErr, IFile::WRITE | IFile::TRUNCATE)); // Transfer output try { $this->_Process->transferStreams($Input, $Output, $Timeout); // @codeCoverageIgnoreStart } catch (CommandException $e) { // Restart PhantomJS just to be sure $this->phantomRestart(); return false; } // @codeCoverageIgnoreEnd unset($Input, $Output); // Normalize output $stdOut = substr($stdOut, strpos($stdOut, '{'), strrpos($stdOut, '}') - strlen($stdOut) + 1); // Done return $stdOut; }