ProcessTest.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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\Tests\Component\Process;
  11. use Symfony\Component\Process\Process;
  12. /**
  13. * @author Robert Schönthal <seroscho@googlemail.com>
  14. */
  15. class ProcessTest extends \PHPUnit_Framework_TestCase
  16. {
  17. /**
  18. * tests getter/setter
  19. *
  20. * @dataProvider methodProvider
  21. */
  22. public function testDefaultGetterSetter($fn)
  23. {
  24. $p = new Process('php');
  25. $setter = 'set'.$fn;
  26. $getter = 'get'.$fn;
  27. $this->assertNull($p->$setter(array('foo')));
  28. $this->assertSame(array('foo'), $p->$getter(array('foo')));
  29. }
  30. /**
  31. * tests results from sub processes
  32. *
  33. * @dataProvider responsesCodeProvider
  34. */
  35. public function testProcessResponses($expected, $getter, $code)
  36. {
  37. $p = new Process(sprintf('php -r %s', escapeshellarg($code)));
  38. $p->run();
  39. $this->assertSame($expected, $p->$getter());
  40. }
  41. /**
  42. * tests results from sub processes
  43. *
  44. * @dataProvider pipesCodeProvider
  45. */
  46. public function testProcessPipes($expected, $code)
  47. {
  48. if (strpos(PHP_OS, "WIN") === 0 && version_compare(phpversion(), "5.3.9", "<")) {
  49. $this->markTestSkipped('Test hangs on Windows & PHP due to https://bugs.php.net/bug.php?id=60120 fixed in http://svn.php.net/viewvc?view=revision&revision=318366');
  50. }
  51. $p = new Process(sprintf('php -r %s', escapeshellarg($code)));
  52. $p->setStdin($expected);
  53. $p->run();
  54. $this->assertSame($expected, $p->getOutput());
  55. $this->assertSame($expected, $p->getErrorOutput());
  56. $this->assertSame(0, $p->getExitCode());
  57. }
  58. public function responsesCodeProvider()
  59. {
  60. return array(
  61. //expected output / getter / code to execute
  62. //array(1,'getExitCode','exit(1);'),
  63. //array(true,'isSuccessful','exit();'),
  64. array('output', 'getOutput', 'echo \'output\';'),
  65. );
  66. }
  67. public function pipesCodeProvider()
  68. {
  69. $variations = array(
  70. 'fwrite(STDOUT, $in = file_get_contents(\'php://stdin\')); fwrite(STDERR, $in);',
  71. 'include \'' . __DIR__ . '/ProcessTestHelper.php\';',
  72. );
  73. $baseData = str_repeat('*', 1024);
  74. $codes = array();
  75. foreach (array(1, 16, 64, 1024, 4096) as $size)
  76. {
  77. $data = str_repeat($baseData, $size) . '!';
  78. foreach ($variations as $code) {
  79. $codes[] = array($data, $code);
  80. }
  81. }
  82. return $codes;
  83. }
  84. /**
  85. * provides default method names for simple getter/setter
  86. */
  87. public function methodProvider()
  88. {
  89. $defaults = array(
  90. array('CommandLine'),
  91. array('Timeout'),
  92. array('WorkingDirectory'),
  93. array('Env'),
  94. array('Stdin'),
  95. array('Options')
  96. );
  97. return $defaults;
  98. }
  99. }