123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- <?php
- namespace Alchemy\Tests\BinaryDriver;
- use Alchemy\BinaryDriver\AbstractBinary;
- use Alchemy\BinaryDriver\BinaryDriverTestCase;
- use Alchemy\BinaryDriver\Configuration;
- use Alchemy\BinaryDriver\Exception\ExecutableNotFoundException;
- use Alchemy\BinaryDriver\Listeners\ListenerInterface;
- use Symfony\Component\Process\ExecutableFinder;
- class AbstractBinaryTest extends BinaryDriverTestCase
- {
- protected function getPhpBinary()
- {
- $finder = new ExecutableFinder();
- $php = $finder->find('php');
- if (null === $php) {
- $this->markTestSkipped('Unable to find a php binary');
- }
- return $php;
- }
- public function testSimpleLoadWithBinaryPath()
- {
- $php = $this->getPhpBinary();
- $imp = Implementation::load($php);
- $this->assertInstanceOf('Alchemy\Tests\BinaryDriver\Implementation', $imp);
- $this->assertEquals($php, $imp->getProcessBuilderFactory()->getBinary());
- }
- public function testMultipleLoadWithBinaryPath()
- {
- $php = $this->getPhpBinary();
- $imp = Implementation::load(array('/zz/path/to/unexisting/command', $php));
- $this->assertInstanceOf('Alchemy\Tests\BinaryDriver\Implementation', $imp);
- $this->assertEquals($php, $imp->getProcessBuilderFactory()->getBinary());
- }
- public function testSimpleLoadWithBinaryName()
- {
- $php = $this->getPhpBinary();
- $imp = Implementation::load('php');
- $this->assertInstanceOf('Alchemy\Tests\BinaryDriver\Implementation', $imp);
- $this->assertEquals($php, $imp->getProcessBuilderFactory()->getBinary());
- }
- public function testMultipleLoadWithBinaryName()
- {
- $php = $this->getPhpBinary();
- $imp = Implementation::load(array('bachibouzouk', 'php'));
- $this->assertInstanceOf('Alchemy\Tests\BinaryDriver\Implementation', $imp);
- $this->assertEquals($php, $imp->getProcessBuilderFactory()->getBinary());
- }
- public function testLoadWithMultiplePathExpectingAFailure()
- {
- $this->setExpectedException(ExecutableNotFoundException::class);
- Implementation::load(array('bachibouzouk', 'moribon'));
- }
- public function testLoadWithUniquePathExpectingAFailure()
- {
- $this->setExpectedException(ExecutableNotFoundException::class);
- Implementation::load('bachibouzouk');
- }
- public function testLoadWithCustomLogger()
- {
- $logger = $this->getMock('Psr\Log\LoggerInterface');
- $imp = Implementation::load('php', $logger);
- $this->assertEquals($logger, $imp->getProcessRunner()->getLogger());
- }
- public function testLoadWithCustomConfigurationAsArray()
- {
- $conf = array('timeout' => 200);
- $imp = Implementation::load('php', null, $conf);
- $this->assertEquals($conf, $imp->getConfiguration()->all());
- }
- public function testLoadWithCustomConfigurationAsObject()
- {
- $conf = $this->getMock('Alchemy\BinaryDriver\ConfigurationInterface');
- $imp = Implementation::load('php', null, $conf);
- $this->assertEquals($conf, $imp->getConfiguration());
- }
- public function testProcessBuilderFactoryGetterAndSetters()
- {
- $imp = Implementation::load('php');
- $factory = $this->getMock('Alchemy\BinaryDriver\ProcessBuilderFactoryInterface');
- $imp->setProcessBuilderFactory($factory);
- $this->assertEquals($factory, $imp->getProcessBuilderFactory());
- }
- public function testConfigurationGetterAndSetters()
- {
- $imp = Implementation::load('php');
- $conf = $this->getMock('Alchemy\BinaryDriver\ConfigurationInterface');
- $imp->setConfiguration($conf);
- $this->assertEquals($conf, $imp->getConfiguration());
- }
- public function testTimeoutIsSetOnConstruction()
- {
- $imp = Implementation::load('php', null, array('timeout' => 42));
- $this->assertEquals(42, $imp->getProcessBuilderFactory()->getTimeout());
- }
- public function testTimeoutIsSetOnConfigurationSetting()
- {
- $imp = Implementation::load('php', null);
- $imp->setConfiguration(new Configuration(array('timeout' => 42)));
- $this->assertEquals(42, $imp->getProcessBuilderFactory()->getTimeout());
- }
- public function testTimeoutIsSetOnProcessBuilderSetting()
- {
- $imp = Implementation::load('php', null, array('timeout' => 42));
- $factory = $this->getMock('Alchemy\BinaryDriver\ProcessBuilderFactoryInterface');
- $factory->expects($this->once())
- ->method('setTimeout')
- ->with(42);
- $imp->setProcessBuilderFactory($factory);
- }
- public function testListenRegistersAListener()
- {
- $imp = Implementation::load('php');
- $listeners = $this->getMockBuilder('Alchemy\BinaryDriver\Listeners\Listeners')
- ->disableOriginalConstructor()
- ->getMock();
- $listener = $this->getMock('Alchemy\BinaryDriver\Listeners\ListenerInterface');
- $listeners->expects($this->once())
- ->method('register')
- ->with($this->equalTo($listener), $this->equalTo($imp));
- $reflexion = new \ReflectionClass('Alchemy\BinaryDriver\AbstractBinary');
- $prop = $reflexion->getProperty('listenersManager');
- $prop->setAccessible(true);
- $prop->setValue($imp, $listeners);
- $imp->listen($listener);
- }
- /**
- * @dataProvider provideCommandParameters
- */
- public function testCommandRunsAProcess($parameters, $bypassErrors, $expectedParameters, $output)
- {
- $imp = Implementation::load('php');
- $factory = $this->getMock('Alchemy\BinaryDriver\ProcessBuilderFactoryInterface');
- $processRunner = $this->getMock('Alchemy\BinaryDriver\ProcessRunnerInterface');
- $process = $this->getMockBuilder('Symfony\Component\Process\Process')
- ->disableOriginalConstructor()
- ->getMock();
- $processRunner->expects($this->once())
- ->method('run')
- ->with($this->equalTo($process), $this->isInstanceOf('SplObjectStorage'), $this->equalTo($bypassErrors))
- ->will($this->returnValue($output));
- $factory->expects($this->once())
- ->method('create')
- ->with($expectedParameters)
- ->will($this->returnValue($process));
- $imp->setProcessBuilderFactory($factory);
- $imp->setProcessRunner($processRunner);
- $this->assertEquals($output, $imp->command($parameters, $bypassErrors));
- }
- /**
- * @dataProvider provideCommandWithListenersParameters
- */
- public function testCommandWithTemporaryListeners($parameters, $bypassErrors, $expectedParameters, $output, $count, $listeners)
- {
- $imp = Implementation::load('php');
- $factory = $this->getMock('Alchemy\BinaryDriver\ProcessBuilderFactoryInterface');
- $processRunner = $this->getMock('Alchemy\BinaryDriver\ProcessRunnerInterface');
- $process = $this->getMockBuilder('Symfony\Component\Process\Process')
- ->disableOriginalConstructor()
- ->getMock();
- $firstStorage = $secondStorage = null;
- $processRunner->expects($this->exactly(2))
- ->method('run')
- ->with($this->equalTo($process), $this->isInstanceOf('SplObjectStorage'), $this->equalTo($bypassErrors))
- ->will($this->returnCallback(function ($process, $storage, $errors) use ($output, &$firstStorage, &$secondStorage) {
- if (null === $firstStorage) {
- $firstStorage = $storage;
- } else {
- $secondStorage = $storage;
- }
- return $output;
- }));
- $factory->expects($this->exactly(2))
- ->method('create')
- ->with($expectedParameters)
- ->will($this->returnValue($process));
- $imp->setProcessBuilderFactory($factory);
- $imp->setProcessRunner($processRunner);
- $this->assertEquals($output, $imp->command($parameters, $bypassErrors, $listeners));
- $this->assertCount($count, $firstStorage);
- $this->assertEquals($output, $imp->command($parameters, $bypassErrors));
- $this->assertCount(0, $secondStorage);
- }
- public function provideCommandWithListenersParameters()
- {
- return array(
- array('-a', false, array('-a'), 'loubda', 2, array($this->getMockListener(), $this->getMockListener())),
- array('-a', false, array('-a'), 'loubda', 1, array($this->getMockListener())),
- array('-a', false, array('-a'), 'loubda', 1, $this->getMockListener()),
- array('-a', false, array('-a'), 'loubda', 0, array()),
- );
- }
- public function provideCommandParameters()
- {
- return array(
- array('-a', false, array('-a'), 'loubda'),
- array('-a', true, array('-a'), 'loubda'),
- array('-a -b', false, array('-a -b'), 'loubda'),
- array(array('-a'), false, array('-a'), 'loubda'),
- array(array('-a'), true, array('-a'), 'loubda'),
- array(array('-a', '-b'), false, array('-a', '-b'), 'loubda'),
- );
- }
- public function testUnlistenUnregistersAListener()
- {
- $imp = Implementation::load('php');
- $listeners = $this->getMockBuilder('Alchemy\BinaryDriver\Listeners\Listeners')
- ->disableOriginalConstructor()
- ->getMock();
- $listener = $this->getMock('Alchemy\BinaryDriver\Listeners\ListenerInterface');
- $listeners->expects($this->once())
- ->method('unregister')
- ->with($this->equalTo($listener), $this->equalTo($imp));
- $reflexion = new \ReflectionClass('Alchemy\BinaryDriver\AbstractBinary');
- $prop = $reflexion->getProperty('listenersManager');
- $prop->setAccessible(true);
- $prop->setValue($imp, $listeners);
- $imp->unlisten($listener);
- }
- /**
- * @return \PHPUnit_Framework_MockObject_MockObject
- */
- private function getMockListener()
- {
- $listener = $this->getMock(ListenerInterface::class);
- $listener->expects($this->any())
- ->method('forwardedEvents')
- ->willReturn(array());
- return $listener;
- }
- }
- class Implementation extends AbstractBinary
- {
- public function getName()
- {
- return 'Implementation';
- }
- }
|