SimpleProcessTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 Symfony\Component\Process\Process;
  12. class SimpleProcessTest extends AbstractProcessTest
  13. {
  14. private $enabledSigchild = false;
  15. protected function setUp()
  16. {
  17. ob_start();
  18. phpinfo(INFO_GENERAL);
  19. $this->enabledSigchild = false !== strpos(ob_get_clean(), '--enable-sigchild');
  20. }
  21. public function testGetExitCode()
  22. {
  23. $this->skipIfPHPSigchild(); // This test use exitcode that is not available in this case
  24. parent::testGetExitCode();
  25. }
  26. public function testExitCodeCommandFailed()
  27. {
  28. $this->skipIfPHPSigchild(); // This test use exitcode that is not available in this case
  29. parent::testExitCodeCommandFailed();
  30. }
  31. public function testProcessIsSignaledIfStopped()
  32. {
  33. $this->expectExceptionIfPHPSigchild('Symfony\Component\Process\Exception\RuntimeException', 'This PHP has been compiled with --enable-sigchild. Term signal can not be retrieved');
  34. parent::testProcessIsSignaledIfStopped();
  35. }
  36. public function testProcessWithTermSignal()
  37. {
  38. $this->expectExceptionIfPHPSigchild('Symfony\Component\Process\Exception\RuntimeException', 'This PHP has been compiled with --enable-sigchild. Term signal can not be retrieved');
  39. parent::testProcessWithTermSignal();
  40. }
  41. public function testProcessIsNotSignaled()
  42. {
  43. $this->expectExceptionIfPHPSigchild('Symfony\Component\Process\Exception\RuntimeException', 'This PHP has been compiled with --enable-sigchild. Term signal can not be retrieved');
  44. parent::testProcessIsNotSignaled();
  45. }
  46. public function testProcessWithoutTermSignal()
  47. {
  48. $this->expectExceptionIfPHPSigchild('Symfony\Component\Process\Exception\RuntimeException', 'This PHP has been compiled with --enable-sigchild. Term signal can not be retrieved');
  49. parent::testProcessWithoutTermSignal();
  50. }
  51. public function testExitCodeText()
  52. {
  53. $this->skipIfPHPSigchild(); // This test use exitcode that is not available in this case
  54. parent::testExitCodeText();
  55. }
  56. public function testIsSuccessful()
  57. {
  58. $this->skipIfPHPSigchild(); // This test use PID that is not available in this case
  59. parent::testIsSuccessful();
  60. }
  61. public function testIsNotSuccessful()
  62. {
  63. $this->skipIfPHPSigchild(); // This test use PID that is not available in this case
  64. parent::testIsNotSuccessful();
  65. }
  66. public function testGetPid()
  67. {
  68. $this->skipIfPHPSigchild(); // This test use PID that is not available in this case
  69. parent::testGetPid();
  70. }
  71. public function testGetPidIsNullBeforeStart()
  72. {
  73. $this->skipIfPHPSigchild(); // This test use PID that is not available in this case
  74. parent::testGetPidIsNullBeforeStart();
  75. }
  76. public function testGetPidIsNullAfterRun()
  77. {
  78. $this->skipIfPHPSigchild(); // This test use PID that is not available in this case
  79. parent::testGetPidIsNullAfterRun();
  80. }
  81. public function testSignal()
  82. {
  83. $this->expectExceptionIfPHPSigchild('Symfony\Component\Process\Exception\RuntimeException', 'This PHP has been compiled with --enable-sigchild. The process can not be signaled.');
  84. parent::testSignal();
  85. }
  86. public function testProcessWithoutTermSignalIsNotSignaled()
  87. {
  88. $this->expectExceptionIfPHPSigchild('Symfony\Component\Process\Exception\RuntimeException', 'This PHP has been compiled with --enable-sigchild. Term signal can not be retrieved');
  89. parent::testProcessWithoutTermSignalIsNotSignaled();
  90. }
  91. public function testProcessThrowsExceptionWhenExternallySignaled()
  92. {
  93. $this->skipIfPHPSigchild(); // This test use PID that is not available in this case
  94. parent::testProcessThrowsExceptionWhenExternallySignaled();
  95. }
  96. public function testExitCodeIsAvailableAfterSignal()
  97. {
  98. $this->expectExceptionIfPHPSigchild('Symfony\Component\Process\Exception\RuntimeException', 'This PHP has been compiled with --enable-sigchild. The process can not be signaled.');
  99. parent::testExitCodeIsAvailableAfterSignal();
  100. }
  101. public function testSignalProcessNotRunning()
  102. {
  103. $this->setExpectedException('Symfony\Component\Process\Exception\LogicException', 'Can not send signal on a non running process.');
  104. parent::testSignalProcessNotRunning();
  105. }
  106. public function testSignalWithWrongIntSignal()
  107. {
  108. if ($this->enabledSigchild) {
  109. $this->expectExceptionIfPHPSigchild('Symfony\Component\Process\Exception\RuntimeException', 'This PHP has been compiled with --enable-sigchild. The process can not be signaled.');
  110. } else {
  111. $this->setExpectedException('Symfony\Component\Process\Exception\RuntimeException', 'Error while sending signal `-4`.');
  112. }
  113. parent::testSignalWithWrongIntSignal();
  114. }
  115. public function testSignalWithWrongNonIntSignal()
  116. {
  117. if ($this->enabledSigchild) {
  118. $this->expectExceptionIfPHPSigchild('Symfony\Component\Process\Exception\RuntimeException', 'This PHP has been compiled with --enable-sigchild. The process can not be signaled.');
  119. } else {
  120. $this->setExpectedException('Symfony\Component\Process\Exception\RuntimeException', 'Error while sending signal `Céphalopodes`.');
  121. }
  122. parent::testSignalWithWrongNonIntSignal();
  123. }
  124. public function testStopTerminatesProcessCleanly()
  125. {
  126. try {
  127. $process = $this->getProcess(self::$phpBin.' -r "echo \'foo\'; sleep(1); echo \'bar\';"');
  128. $process->run(function () use ($process) {
  129. $process->stop();
  130. });
  131. } catch (\RuntimeException $e) {
  132. $this->fail('A call to stop() is not expected to cause wait() to throw a RuntimeException');
  133. }
  134. }
  135. public function testKillSignalTerminatesProcessCleanly()
  136. {
  137. $this->expectExceptionIfPHPSigchild('Symfony\Component\Process\Exception\RuntimeException', 'This PHP has been compiled with --enable-sigchild. The process can not be signaled.');
  138. try {
  139. $process = $this->getProcess(self::$phpBin.' -r "echo \'foo\'; sleep(1); echo \'bar\';"');
  140. $process->run(function () use ($process) {
  141. if ($process->isRunning()) {
  142. $process->signal(defined('SIGKILL') ? SIGKILL : 9);
  143. }
  144. });
  145. } catch (\RuntimeException $e) {
  146. $this->fail('A call to signal() is not expected to cause wait() to throw a RuntimeException');
  147. }
  148. }
  149. public function testTermSignalTerminatesProcessCleanly()
  150. {
  151. $this->expectExceptionIfPHPSigchild('Symfony\Component\Process\Exception\RuntimeException', 'This PHP has been compiled with --enable-sigchild. The process can not be signaled.');
  152. try {
  153. $process = $this->getProcess(self::$phpBin.' -r "echo \'foo\'; sleep(1); echo \'bar\';"');
  154. $process->run(function () use ($process) {
  155. if ($process->isRunning()) {
  156. $process->signal(defined('SIGTERM') ? SIGTERM : 15);
  157. }
  158. });
  159. } catch (\RuntimeException $e) {
  160. $this->fail('A call to signal() is not expected to cause wait() to throw a RuntimeException');
  161. }
  162. }
  163. public function testStopWithTimeoutIsActuallyWorking()
  164. {
  165. $this->skipIfPHPSigchild();
  166. parent::testStopWithTimeoutIsActuallyWorking();
  167. }
  168. /**
  169. * {@inheritdoc}
  170. */
  171. protected function getProcess($commandline, $cwd = null, array $env = null, $input = null, $timeout = 60, array $options = array())
  172. {
  173. return new Process($commandline, $cwd, $env, $input, $timeout, $options);
  174. }
  175. private function skipIfPHPSigchild()
  176. {
  177. if ($this->enabledSigchild) {
  178. $this->markTestSkipped('Your PHP has been compiled with --enable-sigchild, this test can not be executed');
  179. }
  180. }
  181. private function expectExceptionIfPHPSigchild($classname, $message)
  182. {
  183. if ($this->enabledSigchild) {
  184. $this->setExpectedException($classname, $message);
  185. }
  186. }
  187. }