ProcessExecutorTest.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Test\Util;
  12. use Composer\Util\ProcessExecutor;
  13. use Composer\TestCase;
  14. use Composer\IO\BufferIO;
  15. use Symfony\Component\Console\Output\StreamOutput;
  16. class ProcessExecutorTest extends TestCase
  17. {
  18. public function testExecuteCapturesOutput()
  19. {
  20. $process = new ProcessExecutor;
  21. $process->execute('echo foo', $output);
  22. $this->assertEquals("foo".PHP_EOL, $output);
  23. }
  24. public function testExecuteOutputsIfNotCaptured()
  25. {
  26. $process = new ProcessExecutor;
  27. ob_start();
  28. $process->execute('echo foo');
  29. $output = ob_get_clean();
  30. $this->assertEquals("foo".PHP_EOL, $output);
  31. }
  32. public function testUseIOIsNotNullAndIfNotCaptured()
  33. {
  34. $io = $this->getMock('Composer\IO\IOInterface');
  35. $io->expects($this->once())
  36. ->method('write')
  37. ->with($this->equalTo('foo'.PHP_EOL), false);
  38. $process = new ProcessExecutor($io);
  39. $process->execute('echo foo');
  40. }
  41. public function testExecuteCapturesStderr()
  42. {
  43. $process = new ProcessExecutor;
  44. $process->execute('cat foo', $output);
  45. $this->assertNotNull($process->getErrorOutput());
  46. }
  47. public function testTimeout()
  48. {
  49. ProcessExecutor::setTimeout(1);
  50. $process = new ProcessExecutor;
  51. $this->assertEquals(1, $process->getTimeout());
  52. ProcessExecutor::setTimeout(60);
  53. }
  54. public function testHidePasswords()
  55. {
  56. $process = new ProcessExecutor($buffer = new BufferIO('', StreamOutput::VERBOSITY_DEBUG));
  57. $process->execute('echo https://foo:bar@example.org/ && echo http://foo@example.org && echo http://abcdef1234567890234578:x-oauth-token@github.com/', $output);
  58. $this->assertEquals('Executing command (CWD): echo https://foo:***@example.org/ && echo http://foo@example.org && echo http://***:***@github.com/', trim($buffer->getOutput()));
  59. }
  60. public function testDoesntHidePorts()
  61. {
  62. $process = new ProcessExecutor($buffer = new BufferIO('', StreamOutput::VERBOSITY_DEBUG));
  63. $process->execute('echo https://localhost:1234/', $output);
  64. $this->assertEquals('Executing command (CWD): echo https://localhost:1234/', trim($buffer->getOutput()));
  65. }
  66. public function testSplitLines()
  67. {
  68. $process = new ProcessExecutor;
  69. $this->assertEquals(array(), $process->splitLines(''));
  70. $this->assertEquals(array(), $process->splitLines(null));
  71. $this->assertEquals(array('foo'), $process->splitLines('foo'));
  72. $this->assertEquals(array('foo', 'bar'), $process->splitLines("foo\nbar"));
  73. $this->assertEquals(array('foo', 'bar'), $process->splitLines("foo\r\nbar"));
  74. $this->assertEquals(array('foo', 'bar'), $process->splitLines("foo\r\nbar\n"));
  75. }
  76. }