ProcessRunnerTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /*
  3. * This file is part of Alchemy\BinaryDriver.
  4. *
  5. * (c) Alchemy <info@alchemy.fr>
  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 Alchemy\Tests\BinaryDriver;
  11. use Alchemy\BinaryDriver\ProcessRunner;
  12. use Alchemy\BinaryDriver\BinaryDriverTestCase;
  13. use Alchemy\BinaryDriver\Exception\ExecutionFailureException;
  14. use Alchemy\BinaryDriver\Listeners\ListenerInterface;
  15. use Evenement\EventEmitter;
  16. use Symfony\Component\Process\Exception\RuntimeException as ProcessRuntimeException;
  17. class ProcessRunnerTest extends BinaryDriverTestCase
  18. {
  19. public function getProcessRunner($logger)
  20. {
  21. return new ProcessRunner($logger, 'test-runner');
  22. }
  23. public function testRunSuccessFullProcess()
  24. {
  25. $logger = $this->createLoggerMock();
  26. $runner = $this->getProcessRunner($logger);
  27. $process = $this->createProcessMock(1, true, '--helloworld--', "Kikoo Romain", null, true);
  28. $logger
  29. ->expects($this->never())
  30. ->method('error');
  31. $logger
  32. ->expects($this->exactly(2))
  33. ->method('info');
  34. $this->assertEquals('Kikoo Romain', $runner->run($process, new \SplObjectStorage(), false));
  35. }
  36. public function testRunSuccessFullProcessBypassingErrors()
  37. {
  38. $logger = $this->createLoggerMock();
  39. $runner = $this->getProcessRunner($logger);
  40. $process = $this->createProcessMock(1, true, '--helloworld--', "Kikoo Romain", null, true);
  41. $logger
  42. ->expects($this->never())
  43. ->method('error');
  44. $logger
  45. ->expects($this->exactly(2))
  46. ->method('info');
  47. $this->assertEquals('Kikoo Romain', $runner->run($process, new \SplObjectStorage(), true));
  48. }
  49. public function testRunFailingProcess()
  50. {
  51. $logger = $this->createLoggerMock();
  52. $runner = $this->getProcessRunner($logger);
  53. $process = $this->createProcessMock(1, false, '--helloworld--', null, null, true);
  54. $logger
  55. ->expects($this->once())
  56. ->method('error');
  57. $logger
  58. ->expects($this->once())
  59. ->method('info');
  60. try {
  61. $runner->run($process, new \SplObjectStorage(), false);
  62. $this->fail('An exception should have been raised');
  63. } catch (ExecutionFailureException $e) {
  64. }
  65. }
  66. public function testRunFailingProcessWithException()
  67. {
  68. $logger = $this->createLoggerMock();
  69. $runner = $this->getProcessRunner($logger);
  70. $exception = new ProcessRuntimeException('Process Failed');
  71. $process = $this->getMockBuilder('Symfony\Component\Process\Process')
  72. ->disableOriginalConstructor()
  73. ->getMock();
  74. $process->expects($this->once())
  75. ->method('run')
  76. ->will($this->throwException($exception));
  77. $logger
  78. ->expects($this->once())
  79. ->method('error');
  80. $logger
  81. ->expects($this->once())
  82. ->method('info');
  83. try {
  84. $runner->run($process, new \SplObjectStorage(), false);
  85. $this->fail('An exception should have been raised');
  86. } catch (ExecutionFailureException $e) {
  87. $this->assertEquals($exception, $e->getPrevious());
  88. }
  89. }
  90. public function testRunfailingProcessBypassingErrors()
  91. {
  92. $logger = $this->createLoggerMock();
  93. $runner = $this->getProcessRunner($logger);
  94. $process = $this->createProcessMock(1, false, '--helloworld--', 'Hello output', null, true);
  95. $logger
  96. ->expects($this->once())
  97. ->method('error');
  98. $logger
  99. ->expects($this->once())
  100. ->method('info');
  101. $this->assertNull($runner->run($process, new \SplObjectStorage(), true));
  102. }
  103. public function testRunFailingProcessWithExceptionBypassingErrors()
  104. {
  105. $logger = $this->createLoggerMock();
  106. $runner = $this->getProcessRunner($logger);
  107. $exception = new ProcessRuntimeException('Process Failed');
  108. $process = $this->getMockBuilder('Symfony\Component\Process\Process')
  109. ->disableOriginalConstructor()
  110. ->getMock();
  111. $process->expects($this->once())
  112. ->method('run')
  113. ->will($this->throwException($exception));
  114. $logger
  115. ->expects($this->once())
  116. ->method('error');
  117. $logger
  118. ->expects($this->once())
  119. ->method('info');
  120. $this->assertNull($runner->run($process, new \SplObjectStorage(), true));
  121. }
  122. public function testRunSuccessFullProcessWithHandlers()
  123. {
  124. $logger = $this->createLoggerMock();
  125. $runner = $this->getProcessRunner($logger);
  126. $capturedCallback = null;
  127. $process = $this->createProcessMock(1, true, '--helloworld--', "Kikoo Romain", null, true);
  128. $process->expects($this->once())
  129. ->method('run')
  130. ->with($this->isInstanceOf('Closure'))
  131. ->will($this->returnCallback(function ($callback) use (&$capturedCallback) {
  132. $capturedCallback = $callback;
  133. }));
  134. $logger
  135. ->expects($this->never())
  136. ->method('error');
  137. $logger
  138. ->expects($this->exactly(2))
  139. ->method('info');
  140. $listener = new TestListener();
  141. $storage = new \SplObjectStorage();
  142. $storage->attach($listener);
  143. $capturedType = $capturedData = null;
  144. $listener->on('received', function ($type, $data) use (&$capturedType, &$capturedData) {
  145. $capturedData = $data;
  146. $capturedType = $type;
  147. });
  148. $this->assertEquals('Kikoo Romain', $runner->run($process, $storage, false));
  149. $type = 'err';
  150. $data = 'data';
  151. $capturedCallback($type, $data);
  152. $this->assertEquals($data, $capturedData);
  153. $this->assertEquals($type, $capturedType);
  154. }
  155. }
  156. class TestListener extends EventEmitter implements ListenerInterface
  157. {
  158. public function handle($type, $data)
  159. {
  160. return $this->emit('received', array($type, $data));
  161. }
  162. public function forwardedEvents()
  163. {
  164. return array();
  165. }
  166. }