bug #10757 [2.3][Process] Setting STDIN while running should not be possible (romainneutron)

This PR was merged into the 2.3 branch.

Discussion
----------

[2.3][Process] Setting STDIN while running should not be possible

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT

Commits
-------

3e3517a [Process] Setting STDIN while running should not be possible
This commit is contained in:
Fabien Potencier 2014-04-22 09:18:13 +02:00
commit 882e9bac1e
2 changed files with 21 additions and 0 deletions

View File

@ -876,9 +876,15 @@ class Process
* @param string|null $stdin The new contents
*
* @return self The current Process instance
*
* @throws LogicException In case the process is running
*/
public function setStdin($stdin)
{
if ($this->isRunning()) {
throw new LogicException('STDIN can not be set while the process is running.');
}
$this->stdin = $stdin;
return $this;

View File

@ -11,6 +11,7 @@
namespace Symfony\Component\Process\Tests;
use Symfony\Component\Process\Exception\LogicException;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\RuntimeException;
use Symfony\Component\Process\ProcessPipes;
@ -156,6 +157,20 @@ abstract class AbstractProcessTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($expectedLength, strlen($p->getErrorOutput()));
}
public function testSetStdinWhileRunningThrowsAnException()
{
$process = $this->getProcess('php -r "usleep(500000);"');
$process->start();
try {
$process->setStdin('foobar');
$process->stop();
$this->fail('A LogicException should have been raised.');
} catch (LogicException $e) {
$this->assertEquals('STDIN can not be set while the process is running.', $e->getMessage());
}
$process->stop();
}
public function chainedCommandsOutputProvider()
{
if (defined('PHP_WINDOWS_VERSION_BUILD')) {