PhpProcessTest.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Process\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Process\Exception\LogicException;
  13. use Symfony\Component\Process\PhpExecutableFinder;
  14. use Symfony\Component\Process\PhpProcess;
  15. class PhpProcessTest extends TestCase
  16. {
  17. public function testNonBlockingWorks()
  18. {
  19. $expected = 'hello world!';
  20. $process = new PhpProcess(<<<PHP
  21. <?php echo '$expected';
  22. PHP
  23. );
  24. $process->start();
  25. $process->wait();
  26. $this->assertEquals($expected, $process->getOutput());
  27. }
  28. public function testCommandLine()
  29. {
  30. $process = new PhpProcess(<<<'PHP'
  31. <?php echo phpversion().PHP_SAPI;
  32. PHP
  33. );
  34. $commandLine = $process->getCommandLine();
  35. $process->start();
  36. $this->assertStringContainsString($commandLine, $process->getCommandLine(), '::getCommandLine() returns the command line of PHP after start');
  37. $process->wait();
  38. $this->assertStringContainsString($commandLine, $process->getCommandLine(), '::getCommandLine() returns the command line of PHP after wait');
  39. $this->assertSame(PHP_VERSION.\PHP_SAPI, $process->getOutput());
  40. }
  41. public function testPassingPhpExplicitly()
  42. {
  43. $finder = new PhpExecutableFinder();
  44. $php = array_merge([$finder->find(false)], $finder->findArguments());
  45. $expected = 'hello world!';
  46. $script = <<<PHP
  47. <?php echo '$expected';
  48. PHP;
  49. $process = new PhpProcess($script, null, null, 60, $php);
  50. $process->run();
  51. $this->assertEquals($expected, $process->getOutput());
  52. }
  53. public function testProcessCannotBeCreatedUsingFromShellCommandLine()
  54. {
  55. static::expectException(LogicException::class);
  56. static::expectExceptionMessage('The "Symfony\Component\Process\PhpProcess::fromShellCommandline()" method cannot be called when using "Symfony\Component\Process\PhpProcess".');
  57. PhpProcess::fromShellCommandline(<<<PHP
  58. <?php echo 'Hello World!';
  59. PHP
  60. );
  61. }
  62. }