AbstractBinaryTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. namespace Alchemy\Tests\BinaryDriver;
  3. use Alchemy\BinaryDriver\AbstractBinary;
  4. use Alchemy\BinaryDriver\BinaryDriverTestCase;
  5. use Alchemy\BinaryDriver\Configuration;
  6. use Alchemy\BinaryDriver\Exception\ExecutableNotFoundException;
  7. use Alchemy\BinaryDriver\Listeners\ListenerInterface;
  8. use Symfony\Component\Process\ExecutableFinder;
  9. class AbstractBinaryTest extends BinaryDriverTestCase
  10. {
  11. protected function getPhpBinary()
  12. {
  13. $finder = new ExecutableFinder();
  14. $php = $finder->find('php');
  15. if (null === $php) {
  16. $this->markTestSkipped('Unable to find a php binary');
  17. }
  18. return $php;
  19. }
  20. public function testSimpleLoadWithBinaryPath()
  21. {
  22. $php = $this->getPhpBinary();
  23. $imp = Implementation::load($php);
  24. $this->assertInstanceOf('Alchemy\Tests\BinaryDriver\Implementation', $imp);
  25. $this->assertEquals($php, $imp->getProcessBuilderFactory()->getBinary());
  26. }
  27. public function testMultipleLoadWithBinaryPath()
  28. {
  29. $php = $this->getPhpBinary();
  30. $imp = Implementation::load(array('/zz/path/to/unexisting/command', $php));
  31. $this->assertInstanceOf('Alchemy\Tests\BinaryDriver\Implementation', $imp);
  32. $this->assertEquals($php, $imp->getProcessBuilderFactory()->getBinary());
  33. }
  34. public function testSimpleLoadWithBinaryName()
  35. {
  36. $php = $this->getPhpBinary();
  37. $imp = Implementation::load('php');
  38. $this->assertInstanceOf('Alchemy\Tests\BinaryDriver\Implementation', $imp);
  39. $this->assertEquals($php, $imp->getProcessBuilderFactory()->getBinary());
  40. }
  41. public function testMultipleLoadWithBinaryName()
  42. {
  43. $php = $this->getPhpBinary();
  44. $imp = Implementation::load(array('bachibouzouk', 'php'));
  45. $this->assertInstanceOf('Alchemy\Tests\BinaryDriver\Implementation', $imp);
  46. $this->assertEquals($php, $imp->getProcessBuilderFactory()->getBinary());
  47. }
  48. public function testLoadWithMultiplePathExpectingAFailure()
  49. {
  50. $this->setExpectedException(ExecutableNotFoundException::class);
  51. Implementation::load(array('bachibouzouk', 'moribon'));
  52. }
  53. public function testLoadWithUniquePathExpectingAFailure()
  54. {
  55. $this->setExpectedException(ExecutableNotFoundException::class);
  56. Implementation::load('bachibouzouk');
  57. }
  58. public function testLoadWithCustomLogger()
  59. {
  60. $logger = $this->getMock('Psr\Log\LoggerInterface');
  61. $imp = Implementation::load('php', $logger);
  62. $this->assertEquals($logger, $imp->getProcessRunner()->getLogger());
  63. }
  64. public function testLoadWithCustomConfigurationAsArray()
  65. {
  66. $conf = array('timeout' => 200);
  67. $imp = Implementation::load('php', null, $conf);
  68. $this->assertEquals($conf, $imp->getConfiguration()->all());
  69. }
  70. public function testLoadWithCustomConfigurationAsObject()
  71. {
  72. $conf = $this->getMock('Alchemy\BinaryDriver\ConfigurationInterface');
  73. $imp = Implementation::load('php', null, $conf);
  74. $this->assertEquals($conf, $imp->getConfiguration());
  75. }
  76. public function testProcessBuilderFactoryGetterAndSetters()
  77. {
  78. $imp = Implementation::load('php');
  79. $factory = $this->getMock('Alchemy\BinaryDriver\ProcessBuilderFactoryInterface');
  80. $imp->setProcessBuilderFactory($factory);
  81. $this->assertEquals($factory, $imp->getProcessBuilderFactory());
  82. }
  83. public function testConfigurationGetterAndSetters()
  84. {
  85. $imp = Implementation::load('php');
  86. $conf = $this->getMock('Alchemy\BinaryDriver\ConfigurationInterface');
  87. $imp->setConfiguration($conf);
  88. $this->assertEquals($conf, $imp->getConfiguration());
  89. }
  90. public function testTimeoutIsSetOnConstruction()
  91. {
  92. $imp = Implementation::load('php', null, array('timeout' => 42));
  93. $this->assertEquals(42, $imp->getProcessBuilderFactory()->getTimeout());
  94. }
  95. public function testTimeoutIsSetOnConfigurationSetting()
  96. {
  97. $imp = Implementation::load('php', null);
  98. $imp->setConfiguration(new Configuration(array('timeout' => 42)));
  99. $this->assertEquals(42, $imp->getProcessBuilderFactory()->getTimeout());
  100. }
  101. public function testTimeoutIsSetOnProcessBuilderSetting()
  102. {
  103. $imp = Implementation::load('php', null, array('timeout' => 42));
  104. $factory = $this->getMock('Alchemy\BinaryDriver\ProcessBuilderFactoryInterface');
  105. $factory->expects($this->once())
  106. ->method('setTimeout')
  107. ->with(42);
  108. $imp->setProcessBuilderFactory($factory);
  109. }
  110. public function testListenRegistersAListener()
  111. {
  112. $imp = Implementation::load('php');
  113. $listeners = $this->getMockBuilder('Alchemy\BinaryDriver\Listeners\Listeners')
  114. ->disableOriginalConstructor()
  115. ->getMock();
  116. $listener = $this->getMock('Alchemy\BinaryDriver\Listeners\ListenerInterface');
  117. $listeners->expects($this->once())
  118. ->method('register')
  119. ->with($this->equalTo($listener), $this->equalTo($imp));
  120. $reflexion = new \ReflectionClass('Alchemy\BinaryDriver\AbstractBinary');
  121. $prop = $reflexion->getProperty('listenersManager');
  122. $prop->setAccessible(true);
  123. $prop->setValue($imp, $listeners);
  124. $imp->listen($listener);
  125. }
  126. /**
  127. * @dataProvider provideCommandParameters
  128. */
  129. public function testCommandRunsAProcess($parameters, $bypassErrors, $expectedParameters, $output)
  130. {
  131. $imp = Implementation::load('php');
  132. $factory = $this->getMock('Alchemy\BinaryDriver\ProcessBuilderFactoryInterface');
  133. $processRunner = $this->getMock('Alchemy\BinaryDriver\ProcessRunnerInterface');
  134. $process = $this->getMockBuilder('Symfony\Component\Process\Process')
  135. ->disableOriginalConstructor()
  136. ->getMock();
  137. $processRunner->expects($this->once())
  138. ->method('run')
  139. ->with($this->equalTo($process), $this->isInstanceOf('SplObjectStorage'), $this->equalTo($bypassErrors))
  140. ->will($this->returnValue($output));
  141. $factory->expects($this->once())
  142. ->method('create')
  143. ->with($expectedParameters)
  144. ->will($this->returnValue($process));
  145. $imp->setProcessBuilderFactory($factory);
  146. $imp->setProcessRunner($processRunner);
  147. $this->assertEquals($output, $imp->command($parameters, $bypassErrors));
  148. }
  149. /**
  150. * @dataProvider provideCommandWithListenersParameters
  151. */
  152. public function testCommandWithTemporaryListeners($parameters, $bypassErrors, $expectedParameters, $output, $count, $listeners)
  153. {
  154. $imp = Implementation::load('php');
  155. $factory = $this->getMock('Alchemy\BinaryDriver\ProcessBuilderFactoryInterface');
  156. $processRunner = $this->getMock('Alchemy\BinaryDriver\ProcessRunnerInterface');
  157. $process = $this->getMockBuilder('Symfony\Component\Process\Process')
  158. ->disableOriginalConstructor()
  159. ->getMock();
  160. $firstStorage = $secondStorage = null;
  161. $processRunner->expects($this->exactly(2))
  162. ->method('run')
  163. ->with($this->equalTo($process), $this->isInstanceOf('SplObjectStorage'), $this->equalTo($bypassErrors))
  164. ->will($this->returnCallback(function ($process, $storage, $errors) use ($output, &$firstStorage, &$secondStorage) {
  165. if (null === $firstStorage) {
  166. $firstStorage = $storage;
  167. } else {
  168. $secondStorage = $storage;
  169. }
  170. return $output;
  171. }));
  172. $factory->expects($this->exactly(2))
  173. ->method('create')
  174. ->with($expectedParameters)
  175. ->will($this->returnValue($process));
  176. $imp->setProcessBuilderFactory($factory);
  177. $imp->setProcessRunner($processRunner);
  178. $this->assertEquals($output, $imp->command($parameters, $bypassErrors, $listeners));
  179. $this->assertCount($count, $firstStorage);
  180. $this->assertEquals($output, $imp->command($parameters, $bypassErrors));
  181. $this->assertCount(0, $secondStorage);
  182. }
  183. public function provideCommandWithListenersParameters()
  184. {
  185. return array(
  186. array('-a', false, array('-a'), 'loubda', 2, array($this->getMockListener(), $this->getMockListener())),
  187. array('-a', false, array('-a'), 'loubda', 1, array($this->getMockListener())),
  188. array('-a', false, array('-a'), 'loubda', 1, $this->getMockListener()),
  189. array('-a', false, array('-a'), 'loubda', 0, array()),
  190. );
  191. }
  192. public function provideCommandParameters()
  193. {
  194. return array(
  195. array('-a', false, array('-a'), 'loubda'),
  196. array('-a', true, array('-a'), 'loubda'),
  197. array('-a -b', false, array('-a -b'), 'loubda'),
  198. array(array('-a'), false, array('-a'), 'loubda'),
  199. array(array('-a'), true, array('-a'), 'loubda'),
  200. array(array('-a', '-b'), false, array('-a', '-b'), 'loubda'),
  201. );
  202. }
  203. public function testUnlistenUnregistersAListener()
  204. {
  205. $imp = Implementation::load('php');
  206. $listeners = $this->getMockBuilder('Alchemy\BinaryDriver\Listeners\Listeners')
  207. ->disableOriginalConstructor()
  208. ->getMock();
  209. $listener = $this->getMock('Alchemy\BinaryDriver\Listeners\ListenerInterface');
  210. $listeners->expects($this->once())
  211. ->method('unregister')
  212. ->with($this->equalTo($listener), $this->equalTo($imp));
  213. $reflexion = new \ReflectionClass('Alchemy\BinaryDriver\AbstractBinary');
  214. $prop = $reflexion->getProperty('listenersManager');
  215. $prop->setAccessible(true);
  216. $prop->setValue($imp, $listeners);
  217. $imp->unlisten($listener);
  218. }
  219. /**
  220. * @return \PHPUnit_Framework_MockObject_MockObject
  221. */
  222. private function getMockListener()
  223. {
  224. $listener = $this->getMock(ListenerInterface::class);
  225. $listener->expects($this->any())
  226. ->method('forwardedEvents')
  227. ->willReturn(array());
  228. return $listener;
  229. }
  230. }
  231. class Implementation extends AbstractBinary
  232. {
  233. public function getName()
  234. {
  235. return 'Implementation';
  236. }
  237. }