+ public function testIteratorInput()
+ {
+ $input = function () {
+ yield 'ping';
+ yield 'pong';
+ };
+
+ $process = $this->getProcessForCode('stream_copy_to_stream(STDIN, STDOUT);', null, null, $input());
+ $process->run();
+ $this->assertSame('pingpong', $process->getOutput());
+ }
+
+ public function testSimpleInputStream()
+ {
+ $input = new InputStream();
+
+ $process = $this->getProcessForCode('echo \'ping\'; echo fread(STDIN, 4); echo fread(STDIN, 4);');
+ $process->setInput($input);
+
+ $process->start(function ($type, $data) use ($input) {
+ if ('ping' === $data) {
+ $input->write('pang');
+ } elseif (!$input->isClosed()) {
+ $input->write('pong');
+ $input->close();
+ }
+ });
+
+ $process->wait();
+ $this->assertSame('pingpangpong', $process->getOutput());
+ }
+
+ public function testInputStreamWithCallable()
+ {
+ $i = 0;
+ $stream = fopen('php://memory', 'w+');
+ $stream = function () use ($stream, &$i) {
+ if ($i < 3) {
+ rewind($stream);
+ fwrite($stream, ++$i);
+ rewind($stream);
+
+ return $stream;
+ }
+ };
+
+ $input = new InputStream();
+ $input->onEmpty($stream);
+ $input->write($stream());
+
+ $process = $this->getProcessForCode('echo fread(STDIN, 3);');
+ $process->setInput($input);
+ $process->start(function ($type, $data) use ($input) {
+ $input->close();
+ });
+
+ $process->wait();
+ $this->assertSame('123', $process->getOutput());
+ }
+
+ public function testInputStreamWithGenerator()
+ {
+ $input = new InputStream();
+ $input->onEmpty(function ($input) {
+ yield 'pong';
+ $input->close();
+ });
+
+ $process = $this->getProcessForCode('stream_copy_to_stream(STDIN, STDOUT);');
+ $process->setInput($input);
+ $process->start();
+ $input->write('ping');
+ $process->wait();
+ $this->assertSame('pingpong', $process->getOutput());
+ }
+
+ public function testInputStreamOnEmpty()
+ {
+ $i = 0;
+ $input = new InputStream();
+ $input->onEmpty(function () use (&$i) { ++$i; });
+
+ $process = $this->getProcessForCode('echo 123; echo fread(STDIN, 1); echo 456;');
+ $process->setInput($input);
+ $process->start(function ($type, $data) use ($input) {
+ if ('123' === $data) {
+ $input->close();
+ }
+ });
+ $process->wait();
+
+ $this->assertSame(0, $i, 'InputStream->onEmpty callback should be called only when the input *becomes* empty');
+ $this->assertSame('123456', $process->getOutput());
+ }
+
+ public function testIteratorOutput()
+ {
+ $input = new InputStream();
+
+ $process = $this->getProcessForCode('fwrite(STDOUT, 123); fwrite(STDERR, 234); flush(); usleep(10000); fwrite(STDOUT, fread(STDIN, 3)); fwrite(STDERR, 456);');
+ $process->setInput($input);
+ $process->start();
+ $output = array();
+
+ foreach ($process as $type => $data) {
+ $output[] = array($type, $data);
+ break;
+ }
+ $expectedOutput = array(
+ array($process::OUT, '123'),
+ );
+ $this->assertSame($expectedOutput, $output);
+
+ $input->write(345);
+
+ foreach ($process as $type => $data) {
+ $output[] = array($type, $data);
+ }
+
+ $this->assertSame('', $process->getOutput());
+ $this->assertFalse($process->isRunning());
+
+ $expectedOutput = array(
+ array($process::OUT, '123'),
+ array($process::ERR, '234'),
+ array($process::OUT, '345'),
+ array($process::ERR, '456'),
+ );
+ $this->assertSame($expectedOutput, $output);
+ }
+
+ public function testNonBlockingNorClearingIteratorOutput()
+ {
+ $input = new InputStream();
+
+ $process = $this->getProcessForCode('fwrite(STDOUT, fread(STDIN, 3));');
+ $process->setInput($input);
+ $process->start();
+ $output = array();
+
+ foreach ($process->getIterator($process::ITER_NON_BLOCKING | $process::ITER_KEEP_OUTPUT) as $type => $data) {
+ $output[] = array($type, $data);
+ break;
+ }
+ $expectedOutput = array(
+ array($process::OUT, ''),
+ );
+ $this->assertSame($expectedOutput, $output);
+
+ $input->write(123);
+
+ foreach ($process->getIterator($process::ITER_NON_BLOCKING | $process::ITER_KEEP_OUTPUT) as $type => $data) {
+ if ('' !== $data) {
+ $output[] = array($type, $data);
+ }
+ }
+
+ $this->assertSame('123', $process->getOutput());
+ $this->assertFalse($process->isRunning());
+
+ $expectedOutput = array(
+ array($process::OUT, ''),
+ array($process::OUT, '123'),
+ );
+ $this->assertSame($expectedOutput, $output);
+ }
+
+ public function testChainedProcesses()
+ {
+ $p1 = $this->getProcessForCode('fwrite(STDERR, 123); fwrite(STDOUT, 456);');
+ $p2 = $this->getProcessForCode('stream_copy_to_stream(STDIN, STDOUT);');
+ $p2->setInput($p1);
+
+ $p1->start();
+ $p2->run();
+
+ $this->assertSame('123', $p1->getErrorOutput());
+ $this->assertSame('', $p1->getOutput());
+ $this->assertSame('', $p2->getErrorOutput());
+ $this->assertSame('456', $p2->getOutput());
+ }
+
+ public function testSetBadEnv()
+ {
+ $process = $this->getProcess('echo hello');
+ $process->setEnv(array('bad%%' => '123'));
+ $process->inheritEnvironmentVariables(true);
+
+ $process->run();
+
+ $this->assertSame('hello'.PHP_EOL, $process->getOutput());
+ $this->assertSame('', $process->getErrorOutput());
+ }
+
+ public function testEnvBackupDoesNotDeleteExistingVars()
+ {
+ putenv('existing_var=foo');
+ $_ENV['existing_var'] = 'foo';
+ $process = $this->getProcess('php -r "echo getenv(\'new_test_var\');"');
+ $process->setEnv(array('existing_var' => 'bar', 'new_test_var' => 'foo'));
+ $process->inheritEnvironmentVariables();
+
+ $process->run();
+
+ $this->assertSame('foo', $process->getOutput());
+ $this->assertSame('foo', getenv('existing_var'));
+ $this->assertFalse(getenv('new_test_var'));
+
+ putenv('existing_var');
+ unset($_ENV['existing_var']);
+ }
+
+ public function testEnvIsInherited()
+ {
+ $process = $this->getProcessForCode('echo serialize($_SERVER);', null, array('BAR' => 'BAZ', 'EMPTY' => ''));
+
+ putenv('FOO=BAR');
+ $_ENV['FOO'] = 'BAR';
+
+ $process->run();
+
+ $expected = array('BAR' => 'BAZ', 'EMPTY' => '', 'FOO' => 'BAR');
+ $env = array_intersect_key(unserialize($process->getOutput()), $expected);
+
+ $this->assertEquals($expected, $env);
+
+ putenv('FOO');
+ unset($_ENV['FOO']);
+ }
+
+ /**
+ * @group legacy
+ */
+ public function testInheritEnvDisabled()
+ {
+ $process = $this->getProcessForCode('echo serialize($_SERVER);', null, array('BAR' => 'BAZ'));
+
+ putenv('FOO=BAR');
+ $_ENV['FOO'] = 'BAR';
+
+ $this->assertSame($process, $process->inheritEnvironmentVariables(false));
+ $this->assertFalse($process->areEnvironmentVariablesInherited());
+
+ $process->run();
+
+ $expected = array('BAR' => 'BAZ', 'FOO' => 'BAR');
+ $env = array_intersect_key(unserialize($process->getOutput()), $expected);
+ unset($expected['FOO']);
+
+ $this->assertSame($expected, $env);
+
+ putenv('FOO');
+ unset($_ENV['FOO']);
+ }
+
+ public function testGetCommandLine()
+ {
+ $p = new Process(array('/usr/bin/php'));
+
+ $expected = '\\' === \DIRECTORY_SEPARATOR ? '"/usr/bin/php"' : "'/usr/bin/php'";
+ $this->assertSame($expected, $p->getCommandLine());
+ }
+
+ /**
+ * @dataProvider provideEscapeArgument
+ */
+ public function testEscapeArgument($arg)
+ {
+ $p = new Process(array(self::$phpBin, '-r', 'echo $argv[1];', $arg));
+ $p->run();
+
+ $this->assertSame((string) $arg, $p->getOutput());
+ }
+
+ /**
+ * @dataProvider provideEscapeArgument
+ * @group legacy
+ */
+ public function testEscapeArgumentWhenInheritEnvDisabled($arg)
+ {
+ $p = new Process(array(self::$phpBin, '-r', 'echo $argv[1];', $arg), null, array('BAR' => 'BAZ'));
+ $p->inheritEnvironmentVariables(false);
+ $p->run();
+
+ $this->assertSame((string) $arg, $p->getOutput());
+ }
+
+ public function testRawCommandLine()
+ {
+ $p = new Process(sprintf('"%s" -r %s "a" "" "b"', self::$phpBin, escapeshellarg('print_r($argv);')));
+ $p->run();
+
+ $expected = <<<EOTXT
+Array
+(
+ [0] => -
+ [1] => a
+ [2] =>
+ [3] => b
+)
+
+EOTXT;
+ $this->assertSame($expected, str_replace('Standard input code', '-', $p->getOutput()));
+ }
+
+ public function provideEscapeArgument()
+ {
+ yield array('a"b%c%');
+ yield array('a"b^c^');
+ yield array("a\nb'c");
+ yield array('a^b c!');
+ yield array("a!b\tc");
+ yield array('a\\\\"\\"');
+ yield array('éÉèÈàÀöä');
+ yield array(null);
+ yield array(1);
+ yield array(1.1);
+ }
+
+ public function testEnvArgument()
+ {
+ $env = array('FOO' => 'Foo', 'BAR' => 'Bar');
+ $cmd = '\\' === \DIRECTORY_SEPARATOR ? 'echo !FOO! !BAR! !BAZ!' : 'echo $FOO $BAR $BAZ';
+ $p = new Process($cmd, null, $env);
+ $p->run(null, array('BAR' => 'baR', 'BAZ' => 'baZ'));
+
+ $this->assertSame('Foo baR baZ', rtrim($p->getOutput()));
+ $this->assertSame($env, $p->getEnv());
+ }
+