use Symfony\Component\Process\Exception\LogicException;
use Symfony\Component\Process\Exception\ProcessTimedOutException;
use Symfony\Component\Process\Exception\RuntimeException;
+use Symfony\Component\Process\InputStream;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\Pipes\PipesInterface;
use Symfony\Component\Process\Process;
/**
* @dataProvider provideInvalidInputValues
* @expectedException \Symfony\Component\Process\Exception\InvalidArgumentException
- * @expectedExceptionMessage Symfony\Component\Process\Process::setInput only accepts strings or stream resources.
+ * @expectedExceptionMessage Symfony\Component\Process\Process::setInput only accepts strings, Traversable objects or stream resources.
*/
public function testInvalidInput($value)
{
);
}
- /**
- * @dataProvider provideLegacyInputValues
- * @group legacy
- */
- public function testLegacyValidInput($expected, $value)
- {
- $process = $this->getProcess(self::$phpBin.' -v');
- $process->setInput($value);
- $this->assertSame($expected, $process->getInput());
- }
-
- public function provideLegacyInputValues()
- {
- return array(
- array('stringifiable', new Stringifiable()),
- );
- }
-
public function chainedCommandsOutputProvider()
{
if ('\\' === DIRECTORY_SEPARATOR) {
$this->assertTrue($called, 'The callback should be executed with the output');
}
+ public function testCallbackIsExecutedForOutputWheneverOutputIsDisabled()
+ {
+ $p = $this->getProcess(sprintf('%s -r %s', self::$phpBin, escapeshellarg('echo \'foo\';')));
+ $p->disableOutput();
+
+ $called = false;
+ $p->run(function ($type, $buffer) use (&$called) {
+ $called = $buffer === 'foo';
+ });
+
+ $this->assertTrue($called, 'The callback should be executed with the output');
+ }
+
public function testGetErrorOutput()
{
$p = $this->getProcess(sprintf('%s -r %s', self::$phpBin, escapeshellarg('$n = 0; while ($n < 3) { file_put_contents(\'php://stderr\', \'ERROR\'); $n++; }')));
$this->assertGreaterThan(0, $process->getExitCode());
}
+ /**
+ * @group tty
+ */
public function testTTYCommand()
{
if ('\\' === DIRECTORY_SEPARATOR) {
$this->assertSame(Process::STATUS_TERMINATED, $process->getStatus());
}
+ /**
+ * @group tty
+ */
public function testTTYCommandExitCode()
{
if ('\\' === DIRECTORY_SEPARATOR) {
throw $e;
}
+ /**
+ * @expectedException \Symfony\Component\Process\Exception\ProcessTimedOutException
+ * @expectedExceptionMessage exceeded the timeout of 0.1 seconds.
+ */
+ public function testIterateOverProcessWithTimeout()
+ {
+ $process = $this->getProcess(self::$phpBin.' -r "sleep(30);"');
+ $process->setTimeout(0.1);
+ $start = microtime(true);
+ try {
+ $process->start();
+ foreach ($process as $buffer);
+ $this->fail('A RuntimeException should have been raised');
+ } catch (RuntimeException $e) {
+ }
+
+ $this->assertLessThan(15, microtime(true) - $start);
+
+ throw $e;
+ }
+
public function testCheckTimeoutOnNonStartedProcess()
{
$process = $this->getProcess('echo foo');
$this->assertSame($process, $process->setIdleTimeout(null));
}
- /**
- * @dataProvider provideStartMethods
- */
- public function testStartWithACallbackAndDisabledOutput($startMethod, $exception, $exceptionMessage)
- {
- $p = $this->getProcess('foo');
- $p->disableOutput();
-
- if (method_exists($this, 'expectException')) {
- $this->expectException($exception);
- $this->expectExceptionMessage($exceptionMessage);
- } else {
- $this->setExpectedException($exception, $exceptionMessage);
- }
-
- if ('mustRun' === $startMethod) {
- $this->skipIfNotEnhancedSigchild();
- }
- $p->{$startMethod}(function () {});
- }
-
- public function provideStartMethods()
- {
- return array(
- array('start', 'Symfony\Component\Process\Exception\LogicException', 'Output has been disabled, enable it to allow the use of a callback.'),
- array('run', 'Symfony\Component\Process\Exception\LogicException', 'Output has been disabled, enable it to allow the use of a callback.'),
- array('mustRun', 'Symfony\Component\Process\Exception\LogicException', 'Output has been disabled, enable it to allow the use of a callback.'),
- );
- }
-
/**
* @dataProvider provideOutputFetchingMethods
* @expectedException \Symfony\Component\Process\Exception\LogicException
);
}
+ public function testIteratorInput()
+ {
+ $input = function () {
+ yield 'ping';
+ yield 'pong';
+ };
+
+ $process = $this->getProcess(self::$phpBin.' -r '.escapeshellarg('stream_copy_to_stream(STDIN, STDOUT);'), null, null, $input());
+ $process->run();
+ $this->assertSame('pingpong', $process->getOutput());
+ }
+
+ public function testSimpleInputStream()
+ {
+ $input = new InputStream();
+
+ $process = $this->getProcess(self::$phpBin.' -r '.escapeshellarg('echo \'ping\'; stream_copy_to_stream(STDIN, STDOUT);'));
+ $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->getProcess(self::$phpBin.' -r '.escapeshellarg('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->getProcess(self::$phpBin.' -r '.escapeshellarg('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->getProcess(self::$phpBin.' -r '.escapeshellarg('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->getProcess(self::$phpBin.' -r '.escapeshellarg('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->getProcess(self::$phpBin.' -r '.escapeshellarg('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 = new Process(self::$phpBin.' -r '.escapeshellarg('fwrite(STDERR, 123); fwrite(STDOUT, 456);'));
+ $p2 = $this->getProcess(sprintf('%s -r %s', self::$phpBin, escapeshellarg('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');
+ $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'));
+ }
+
+ public function testInheritEnvEnabled()
+ {
+ $process = $this->getProcess(self::$phpBin.' -r '.escapeshellarg('echo serialize($_SERVER);'), null, array('BAR' => 'BAZ'));
+
+ putenv('FOO=BAR');
+
+ $this->assertSame($process, $process->inheritEnvironmentVariables(1));
+ $this->assertTrue($process->areEnvironmentVariablesInherited());
+
+ $process->run();
+
+ $expected = array('BAR' => 'BAZ', 'FOO' => 'BAR');
+ $env = array_intersect_key(unserialize($process->getOutput()), $expected);
+
+ $this->assertEquals($expected, $env);
+ }
+
+ public function testInheritEnvDisabled()
+ {
+ $process = $this->getProcess(self::$phpBin.' -r '.escapeshellarg('echo serialize($_SERVER);'), null, array('BAR' => 'BAZ'));
+
+ putenv('FOO=BAR');
+
+ $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);
+ }
+
/**
* @param string $commandline
* @param null|string $cwd
}
}
-class Stringifiable
-{
- public function __toString()
- {
- return 'stringifiable';
- }
-}
-
class NonStringifiable
{
}