AbstractProcessTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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. use Symfony\Component\Process\Exception\RuntimeException;
  13. /**
  14. * @author Robert Schönthal <seroscho@googlemail.com>
  15. */
  16. abstract class AbstractProcessTest extends \PHPUnit_Framework_TestCase
  17. {
  18. /**
  19. * @expectedException \Symfony\Component\Process\Exception\InvalidArgumentException
  20. */
  21. public function testNegativeTimeoutFromConstructor()
  22. {
  23. $this->getProcess('', null, null, null, -1);
  24. }
  25. /**
  26. * @expectedException \Symfony\Component\Process\Exception\InvalidArgumentException
  27. */
  28. public function testNegativeTimeoutFromSetter()
  29. {
  30. $p = $this->getProcess('');
  31. $p->setTimeout(-1);
  32. }
  33. public function testNullTimeout()
  34. {
  35. $p = $this->getProcess('');
  36. $p->setTimeout(10);
  37. $p->setTimeout(null);
  38. $this->assertNull($p->getTimeout());
  39. }
  40. public function testStopWithTimeoutIsActuallyWorking()
  41. {
  42. $this->verifyPosixIsEnabled();
  43. // exec is mandatory here since we send a signal to the process
  44. // see https://github.com/symfony/symfony/issues/5030 about prepending
  45. // command with exec
  46. $p = $this->getProcess('exec php '.__DIR__.'/NonStopableProcess.php 3');
  47. $p->start();
  48. usleep(100000);
  49. $start = microtime(true);
  50. $p->stop(1.1, SIGKILL);
  51. while ($p->isRunning()) {
  52. usleep(1000);
  53. }
  54. $duration = microtime(true) - $start;
  55. $this->assertLessThan(1.8, $duration);
  56. }
  57. public function testCallbacksAreExecutedWithStart()
  58. {
  59. $data = '';
  60. $process = $this->getProcess('echo "foo";sleep 1;echo "foo"');
  61. $process->start(function ($type, $buffer) use (&$data) {
  62. $data .= $buffer;
  63. });
  64. $start = microtime(true);
  65. while ($process->isRunning()) {
  66. usleep(10000);
  67. }
  68. $this->assertEquals("foo\nfoo\n", $data);
  69. }
  70. /**
  71. * tests results from sub processes
  72. *
  73. * @dataProvider responsesCodeProvider
  74. */
  75. public function testProcessResponses($expected, $getter, $code)
  76. {
  77. $p = $this->getProcess(sprintf('php -r %s', escapeshellarg($code)));
  78. $p->run();
  79. $this->assertSame($expected, $p->$getter());
  80. }
  81. /**
  82. * tests results from sub processes
  83. *
  84. * @dataProvider pipesCodeProvider
  85. */
  86. public function testProcessPipes($code, $size)
  87. {
  88. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  89. $this->markTestSkipped('Test hangs on Windows & PHP due to https://bugs.php.net/bug.php?id=60120 and https://bugs.php.net/bug.php?id=51800');
  90. }
  91. $expected = str_repeat(str_repeat('*', 1024), $size) . '!';
  92. $expectedLength = (1024 * $size) + 1;
  93. $p = $this->getProcess(sprintf('php -r %s', escapeshellarg($code)));
  94. $p->setStdin($expected);
  95. $p->run();
  96. $this->assertEquals($expectedLength, strlen($p->getOutput()));
  97. $this->assertEquals($expectedLength, strlen($p->getErrorOutput()));
  98. }
  99. public function chainedCommandsOutputProvider()
  100. {
  101. return array(
  102. array("1\n1\n", ';', '1'),
  103. array("2\n2\n", '&&', '2'),
  104. );
  105. }
  106. /**
  107. *
  108. * @dataProvider chainedCommandsOutputProvider
  109. */
  110. public function testChainedCommandsOutput($expected, $operator, $input)
  111. {
  112. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  113. $this->markTestSkipped('Does it work on windows ?');
  114. }
  115. $process = $this->getProcess(sprintf('echo %s %s echo %s', $input, $operator, $input));
  116. $process->run();
  117. $this->assertEquals($expected, $process->getOutput());
  118. }
  119. public function testCallbackIsExecutedForOutput()
  120. {
  121. $p = $this->getProcess(sprintf('php -r %s', escapeshellarg('echo \'foo\';')));
  122. $called = false;
  123. $p->run(function ($type, $buffer) use (&$called) {
  124. $called = $buffer === 'foo';
  125. });
  126. $this->assertTrue($called, 'The callback should be executed with the output');
  127. }
  128. public function testGetErrorOutput()
  129. {
  130. $p = new Process(sprintf('php -r %s', escapeshellarg('$n = 0; while ($n < 3) { file_put_contents(\'php://stderr\', \'ERROR\'); $n++; }')));
  131. $p->run();
  132. $this->assertEquals(3, preg_match_all('/ERROR/', $p->getErrorOutput(), $matches));
  133. }
  134. public function testGetIncrementalErrorOutput()
  135. {
  136. $p = new Process(sprintf('php -r %s', escapeshellarg('$n = 0; while ($n < 3) { usleep(50000); file_put_contents(\'php://stderr\', \'ERROR\'); $n++; }')));
  137. $p->start();
  138. while ($p->isRunning()) {
  139. $this->assertLessThanOrEqual(1, preg_match_all('/ERROR/', $p->getIncrementalErrorOutput(), $matches));
  140. usleep(20000);
  141. }
  142. }
  143. public function testGetOutput()
  144. {
  145. $p = new Process(sprintf('php -r %s', escapeshellarg('$n=0;while ($n<3) {echo \' foo \';$n++;}')));
  146. $p->run();
  147. $this->assertEquals(3, preg_match_all('/foo/', $p->getOutput(), $matches));
  148. }
  149. public function testGetIncrementalOutput()
  150. {
  151. $p = new Process(sprintf('php -r %s', escapeshellarg('$n=0;while ($n<3) { echo \' foo \'; usleep(50000); $n++; }')));
  152. $p->start();
  153. while ($p->isRunning()) {
  154. $this->assertLessThanOrEqual(1, preg_match_all('/foo/', $p->getIncrementalOutput(), $matches));
  155. usleep(20000);
  156. }
  157. }
  158. public function testExitCodeCommandFailed()
  159. {
  160. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  161. $this->markTestSkipped('Windows does not support POSIX exit code');
  162. }
  163. // such command run in bash return an exitcode 127
  164. $process = $this->getProcess('nonexistingcommandIhopeneversomeonewouldnameacommandlikethis');
  165. $process->run();
  166. $this->assertGreaterThan(0, $process->getExitCode());
  167. }
  168. public function testTTYCommand()
  169. {
  170. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  171. $this->markTestSkipped('Windows does have /dev/tty support');
  172. }
  173. $process = $this->getProcess('echo "foo" >> /dev/null');
  174. $process->setTTY(true);
  175. $process->run();
  176. $this->assertSame(Process::STATUS_TERMINATED, $process->getStatus());
  177. }
  178. public function testExitCodeText()
  179. {
  180. $process = $this->getProcess('');
  181. $r = new \ReflectionObject($process);
  182. $p = $r->getProperty('exitcode');
  183. $p->setAccessible(true);
  184. $p->setValue($process, 2);
  185. $this->assertEquals('Misuse of shell builtins', $process->getExitCodeText());
  186. }
  187. public function testStartIsNonBlocking()
  188. {
  189. $process = $this->getProcess('php -r "sleep(4);"');
  190. $start = microtime(true);
  191. $process->start();
  192. $end = microtime(true);
  193. $this->assertLessThan(1 , $end-$start);
  194. }
  195. public function testUpdateStatus()
  196. {
  197. $process = $this->getProcess('php -h');
  198. $process->run();
  199. $this->assertTrue(strlen($process->getOutput()) > 0);
  200. }
  201. public function testGetExitCodeIsNullOnStart()
  202. {
  203. $process = $this->getProcess('php -r "usleep(200000);"');
  204. $this->assertNull($process->getExitCode());
  205. $process->start();
  206. $this->assertNull($process->getExitCode());
  207. $process->wait();
  208. $this->assertEquals(0, $process->getExitCode());
  209. }
  210. public function testGetExitCodeIsNullOnWhenStartingAgain()
  211. {
  212. $process = $this->getProcess('php -r "usleep(200000);"');
  213. $process->run();
  214. $this->assertEquals(0, $process->getExitCode());
  215. $process->start();
  216. $this->assertNull($process->getExitCode());
  217. $process->wait();
  218. $this->assertEquals(0, $process->getExitCode());
  219. }
  220. public function testGetExitCode()
  221. {
  222. $process = $this->getProcess('php -m');
  223. $process->run();
  224. $this->assertEquals(0, $process->getExitCode());
  225. }
  226. public function testStatus()
  227. {
  228. $process = $this->getProcess('php -r "usleep(500000);"');
  229. $this->assertFalse($process->isRunning());
  230. $this->assertFalse($process->isStarted());
  231. $this->assertFalse($process->isTerminated());
  232. $this->assertSame(Process::STATUS_READY, $process->getStatus());
  233. $process->start();
  234. $this->assertTrue($process->isRunning());
  235. $this->assertTrue($process->isStarted());
  236. $this->assertFalse($process->isTerminated());
  237. $this->assertSame(Process::STATUS_STARTED, $process->getStatus());
  238. $process->wait();
  239. $this->assertFalse($process->isRunning());
  240. $this->assertTrue($process->isStarted());
  241. $this->assertTrue($process->isTerminated());
  242. $this->assertSame(Process::STATUS_TERMINATED, $process->getStatus());
  243. }
  244. public function testStop()
  245. {
  246. $process = $this->getProcess('php -r "sleep(4);"');
  247. $process->start();
  248. $this->assertTrue($process->isRunning());
  249. $process->stop();
  250. $this->assertFalse($process->isRunning());
  251. }
  252. public function testIsSuccessful()
  253. {
  254. $process = $this->getProcess('php -m');
  255. $process->run();
  256. $this->assertTrue($process->isSuccessful());
  257. }
  258. public function testIsSuccessfulOnlyAfterTerminated()
  259. {
  260. $process = $this->getProcess('sleep 1');
  261. $process->start();
  262. while ($process->isRunning()) {
  263. $this->assertFalse($process->isSuccessful());
  264. usleep(300000);
  265. }
  266. $this->assertTrue($process->isSuccessful());
  267. }
  268. public function testIsNotSuccessful()
  269. {
  270. $process = $this->getProcess('php -r "sleep(4);"');
  271. $process->start();
  272. $this->assertTrue($process->isRunning());
  273. $process->stop();
  274. $this->assertFalse($process->isSuccessful());
  275. }
  276. public function testProcessIsNotSignaled()
  277. {
  278. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  279. $this->markTestSkipped('Windows does not support POSIX signals');
  280. }
  281. $process = $this->getProcess('php -m');
  282. $process->run();
  283. $this->assertFalse($process->hasBeenSignaled());
  284. }
  285. public function testProcessWithoutTermSignalIsNotSignaled()
  286. {
  287. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  288. $this->markTestSkipped('Windows does not support POSIX signals');
  289. }
  290. $process = $this->getProcess('php -m');
  291. $process->run();
  292. $this->assertFalse($process->hasBeenSignaled());
  293. }
  294. public function testProcessWithoutTermSignal()
  295. {
  296. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  297. $this->markTestSkipped('Windows does not support POSIX signals');
  298. }
  299. $process = $this->getProcess('php -m');
  300. $process->run();
  301. $this->assertEquals(0, $process->getTermSignal());
  302. }
  303. public function testProcessIsSignaledIfStopped()
  304. {
  305. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  306. $this->markTestSkipped('Windows does not support POSIX signals');
  307. }
  308. $process = $this->getProcess('php -r "sleep(4);"');
  309. $process->start();
  310. $process->stop();
  311. $this->assertTrue($process->hasBeenSignaled());
  312. }
  313. public function testProcessWithTermSignal()
  314. {
  315. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  316. $this->markTestSkipped('Windows does not support POSIX signals');
  317. }
  318. // SIGTERM is only defined if pcntl extension is present
  319. $termSignal = defined('SIGTERM') ? SIGTERM : 15;
  320. $process = $this->getProcess('php -r "sleep(4);"');
  321. $process->start();
  322. $process->stop();
  323. $this->assertEquals($termSignal, $process->getTermSignal());
  324. }
  325. public function testProcessThrowsExceptionWhenExternallySignaled()
  326. {
  327. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  328. $this->markTestSkipped('Windows does not support POSIX signals');
  329. }
  330. if (!function_exists('posix_kill')) {
  331. $this->markTestSkipped('posix_kill is required for this test');
  332. }
  333. $termSignal = defined('SIGKILL') ? SIGKILL : 9;
  334. $process = $this->getProcess('exec php -r "while (true) {}"');
  335. $process->start();
  336. posix_kill($process->getPid(), $termSignal);
  337. $this->setExpectedException('Symfony\Component\Process\Exception\RuntimeException', 'The process has been signaled with signal "9".');
  338. $process->wait();
  339. }
  340. public function testRestart()
  341. {
  342. $process1 = $this->getProcess('php -r "echo getmypid();"');
  343. $process1->run();
  344. $process2 = $process1->restart();
  345. usleep(300000); // wait for output
  346. // Ensure that both processed finished and the output is numeric
  347. $this->assertFalse($process1->isRunning());
  348. $this->assertFalse($process2->isRunning());
  349. $this->assertTrue(is_numeric($process1->getOutput()));
  350. $this->assertTrue(is_numeric($process2->getOutput()));
  351. // Ensure that restart returned a new process by check that the output is different
  352. $this->assertNotEquals($process1->getOutput(), $process2->getOutput());
  353. }
  354. public function testPhpDeadlock()
  355. {
  356. $this->markTestSkipped('Can course php to hang');
  357. // Sleep doesn't work as it will allow the process to handle signals and close
  358. // file handles from the other end.
  359. $process = $this->getProcess('php -r "while (true) {}"');
  360. $process->start();
  361. // PHP will deadlock when it tries to cleanup $process
  362. }
  363. public function testRunProcessWithTimeout()
  364. {
  365. $timeout = 0.5;
  366. $process = $this->getProcess('sleep 3');
  367. $process->setTimeout($timeout);
  368. $start = microtime(true);
  369. try {
  370. $process->run();
  371. $this->fail('A RuntimeException should have been raised');
  372. } catch (RuntimeException $e) {
  373. }
  374. $duration = microtime(true) - $start;
  375. $this->assertLessThan($timeout + Process::TIMEOUT_PRECISION, $duration);
  376. }
  377. public function testCheckTimeoutOnStartedProcess()
  378. {
  379. $timeout = 0.5;
  380. $precision = 100000;
  381. $process = $this->getProcess('sleep 3');
  382. $process->setTimeout($timeout);
  383. $start = microtime(true);
  384. $process->start();
  385. try {
  386. while ($process->isRunning()) {
  387. $process->checkTimeout();
  388. usleep($precision);
  389. }
  390. $this->fail('A RuntimeException should have been raised');
  391. } catch (RuntimeException $e) {
  392. }
  393. $duration = microtime(true) - $start;
  394. $this->assertLessThan($timeout + $precision, $duration);
  395. $this->assertFalse($process->isSuccessful());
  396. }
  397. public function testGetPid()
  398. {
  399. $process = $this->getProcess('php -r "sleep(1);"');
  400. $process->start();
  401. $this->assertGreaterThan(0, $process->getPid());
  402. $process->stop();
  403. }
  404. public function testGetPidIsNullBeforeStart()
  405. {
  406. $process = $this->getProcess('php -r "sleep(1);"');
  407. $this->assertNull($process->getPid());
  408. }
  409. public function testGetPidIsNullAfterRun()
  410. {
  411. $process = $this->getProcess('php -m');
  412. $process->run();
  413. $this->assertNull($process->getPid());
  414. }
  415. public function testSignal()
  416. {
  417. $this->verifyPosixIsEnabled();
  418. $process = $this->getProcess('exec php -f ' . __DIR__ . '/SignalListener.php');
  419. $process->start();
  420. usleep(500000);
  421. $process->signal(SIGUSR1);
  422. while ($process->isRunning() && false === strpos($process->getoutput(), 'Caught SIGUSR1')) {
  423. usleep(10000);
  424. }
  425. $this->assertEquals('Caught SIGUSR1', $process->getOutput());
  426. }
  427. public function testExitCodeIsAvailableAfterSignal()
  428. {
  429. $this->verifyPosixIsEnabled();
  430. $process = $this->getProcess('sleep 4');
  431. $process->start();
  432. $process->signal(SIGKILL);
  433. while ($process->isRunning()) {
  434. usleep(10000);
  435. }
  436. $this->assertFalse($process->isRunning());
  437. $this->assertTrue($process->hasBeenSignaled());
  438. $this->assertFalse($process->isSuccessful());
  439. $this->assertEquals(137, $process->getExitCode());
  440. }
  441. /**
  442. * @expectedException Symfony\Component\Process\Exception\LogicException
  443. */
  444. public function testSignalProcessNotRunning()
  445. {
  446. $this->verifyPosixIsEnabled();
  447. $process = $this->getProcess('php -m');
  448. $process->signal(SIGHUP);
  449. }
  450. private function verifyPosixIsEnabled()
  451. {
  452. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  453. $this->markTestSkipped('POSIX signals do not work on windows');
  454. }
  455. if (!defined('SIGUSR1')) {
  456. $this->markTestSkipped('The pcntl extension is not enabled');
  457. }
  458. }
  459. /**
  460. * @expectedException Symfony\Component\Process\Exception\RuntimeException
  461. */
  462. public function testSignalWithWrongIntSignal()
  463. {
  464. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  465. $this->markTestSkipped('POSIX signals do not work on windows');
  466. }
  467. $process = $this->getProcess('php -r "sleep(3);"');
  468. $process->start();
  469. $process->signal(-4);
  470. }
  471. /**
  472. * @expectedException Symfony\Component\Process\Exception\RuntimeException
  473. */
  474. public function testSignalWithWrongNonIntSignal()
  475. {
  476. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  477. $this->markTestSkipped('POSIX signals do not work on windows');
  478. }
  479. $process = $this->getProcess('php -r "sleep(3);"');
  480. $process->start();
  481. $process->signal('Céphalopodes');
  482. }
  483. public function responsesCodeProvider()
  484. {
  485. return array(
  486. //expected output / getter / code to execute
  487. //array(1,'getExitCode','exit(1);'),
  488. //array(true,'isSuccessful','exit();'),
  489. array('output', 'getOutput', 'echo \'output\';'),
  490. );
  491. }
  492. public function pipesCodeProvider()
  493. {
  494. $variations = array(
  495. 'fwrite(STDOUT, $in = file_get_contents(\'php://stdin\')); fwrite(STDERR, $in);',
  496. 'include \''.__DIR__.'/ProcessTestHelper.php\';',
  497. );
  498. $codes = array();
  499. foreach (array(1, 16, 64, 1024, 4096) as $size) {
  500. foreach ($variations as $code) {
  501. $codes[] = array($code, $size);
  502. }
  503. }
  504. return $codes;
  505. }
  506. /**
  507. * provides default method names for simple getter/setter
  508. */
  509. public function methodProvider()
  510. {
  511. $defaults = array(
  512. array('CommandLine'),
  513. array('Timeout'),
  514. array('WorkingDirectory'),
  515. array('Env'),
  516. array('Stdin'),
  517. array('Options')
  518. );
  519. return $defaults;
  520. }
  521. /**
  522. * @param string $commandline
  523. * @param null $cwd
  524. * @param array $env
  525. * @param null $stdin
  526. * @param integer $timeout
  527. * @param array $options
  528. *
  529. * @return Process
  530. */
  531. abstract protected function getProcess($commandline, $cwd = null, array $env = null, $stdin = null, $timeout = 60, array $options = array());
  532. }