ProcessTest.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. $p = new Process(sprintf('php -r %s', escapeshellarg($code)));
  49. $p->setStdin($expected);
  50. $p->run();
  51. $this->assertSame($expected, $p->getOutput());
  52. $this->assertSame($expected, $p->getErrorOutput());
  53. $this->assertSame(0, $p->getExitCode());
  54. }
  55. public function responsesCodeProvider()
  56. {
  57. return array(
  58. //expected output / getter / code to execute
  59. //array(1,'getExitCode','exit(1);'),
  60. //array(true,'isSuccessful','exit();'),
  61. array('output', 'getOutput', 'echo \'output\';'),
  62. );
  63. }
  64. public function pipesCodeProvider()
  65. {
  66. $variations = array(
  67. 'fwrite(STDOUT, $in = file_get_contents(\'php://stdin\')); fwrite(STDERR, $in);',
  68. 'include \'' . __DIR__ . '/ProcessTestHelper.php\';',
  69. );
  70. $baseData = str_repeat('*', 1024);
  71. $codes = array();
  72. foreach (array(1, 16, 64, 1024, 4096) as $size)
  73. {
  74. $data = str_repeat($baseData, $size) . '!';
  75. foreach ($variations as $code) {
  76. $codes[] = array($data, $code);
  77. }
  78. }
  79. return $codes;
  80. }
  81. /**
  82. * provides default method names for simple getter/setter
  83. */
  84. public function methodProvider()
  85. {
  86. $defaults = array(
  87. array('CommandLine'),
  88. array('Timeout'),
  89. array('WorkingDirectory'),
  90. array('Env'),
  91. array('Stdin'),
  92. array('Options')
  93. );
  94. return $defaults;
  95. }
  96. }