From 56205601cecd563cc06a055f02ed3be1692eb79a Mon Sep 17 00:00:00 2001 From: Artem Lopata Date: Fri, 16 Aug 2019 16:43:02 +0200 Subject: [PATCH 1/2] Moving towards php 7.1 and new PHPUnit --- phpunit.xml.dist | 3 +- src/Alchemy/BinaryDriver/AbstractBinary.php | 10 +- .../BinaryDriver/BinaryDriverTestCase.php | 31 ++-- src/Alchemy/BinaryDriver/BinaryInterface.php | 4 +- .../BinaryDriver/Listeners/DebugListener.php | 2 +- .../BinaryDriver/Listeners/Listeners.php | 5 +- .../BinaryDriver/ProcessBuilderFactory.php | 94 +--------- .../ProcessBuilderFactoryAwareInterface.php | 2 + .../ProcessBuilderFactoryInterface.php | 6 +- src/Alchemy/BinaryDriver/ProcessRunner.php | 7 +- .../Tests/BinaryDriver/AbstractBinaryTest.php | 172 +++++++++--------- .../AbstractProcessBuilderFactoryTest.php | 29 ++- .../Tests/BinaryDriver/ConfigurationTest.php | 22 +-- .../Listeners/DebugListenerTest.php | 6 +- .../BinaryDriver/Listeners/ListenersTest.php | 8 +- .../NONLTSProcessBuilderFactoryTest.php | 15 -- .../ProcessBuilderFactoryTest.php | 13 ++ .../Tests/BinaryDriver/ProcessRunnerTest.php | 35 ++-- 18 files changed, 195 insertions(+), 269 deletions(-) delete mode 100644 tests/Alchemy/Tests/BinaryDriver/NONLTSProcessBuilderFactoryTest.php create mode 100644 tests/Alchemy/Tests/BinaryDriver/ProcessBuilderFactoryTest.php diff --git a/phpunit.xml.dist b/phpunit.xml.dist index de5d458..436c1ac 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -21,8 +21,7 @@ - vendor - tests + src diff --git a/src/Alchemy/BinaryDriver/AbstractBinary.php b/src/Alchemy/BinaryDriver/AbstractBinary.php index b5372ca..4963826 100644 --- a/src/Alchemy/BinaryDriver/AbstractBinary.php +++ b/src/Alchemy/BinaryDriver/AbstractBinary.php @@ -61,7 +61,7 @@ public function listen(ListenerInterface $listener) : BinaryInterface */ public function unlisten(ListenerInterface $listener) : BinaryInterface { - $this->listenersManager->unregister($listener, $this); + $this->listenersManager->unregister($listener); return $this; } @@ -175,7 +175,7 @@ public static function load($binaries, ? LoggerInterface $logger = null, $config * * @return string */ - abstract public function getName(); + abstract public function getName() : string; /** * Executes a process, logs events @@ -188,11 +188,11 @@ abstract public function getName(); * * @throws ExecutionFailureException in case of process failure. */ - protected function run(Process $process, $bypassErrors = false, $listeners = null) + protected function run(Process $process, $bypassErrors = false, $listeners = null) : string { if (null !== $listeners) { if (!is_array($listeners)) { - $listeners = array($listeners); + $listeners = [$listeners]; } $listenersManager = clone $this->listenersManager; @@ -207,7 +207,7 @@ protected function run(Process $process, $bypassErrors = false, $listeners = nul return $this->processRunner->run($process, $listenersManager->storage, $bypassErrors); } - private function applyProcessConfiguration() + private function applyProcessConfiguration() : self { if ($this->configuration->has('timeout')) { $this->factory->setTimeout($this->configuration->get('timeout')); diff --git a/src/Alchemy/BinaryDriver/BinaryDriverTestCase.php b/src/Alchemy/BinaryDriver/BinaryDriverTestCase.php index 9b9b3d8..3722f68 100644 --- a/src/Alchemy/BinaryDriver/BinaryDriverTestCase.php +++ b/src/Alchemy/BinaryDriver/BinaryDriverTestCase.php @@ -3,6 +3,7 @@ namespace Alchemy\BinaryDriver; +use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; use Symfony\Component\Process\Process; use PHPUnit\Framework\TestCase; @@ -13,11 +14,11 @@ class BinaryDriverTestCase extends TestCase { /** - * @return ProcessBuilderFactoryInterface + * @return ProcessBuilderFactoryInterface|MockObject */ - public function createProcessBuilderFactoryMock() + public function createProcessBuilderFactoryMock() : ProcessBuilderFactoryInterface { - return $this->getMockBuilder(\Alchemy\BinaryDriver\ProcessBuilderFactoryInterface::class)->getMock(); + return $this->createMock(ProcessBuilderFactoryInterface::class); } /** @@ -28,13 +29,11 @@ public function createProcessBuilderFactoryMock() * @param string $error The process error output * @param bool $enableCallback * - * @return Process + * @return Process|MockObject */ - public function createProcessMock(int $runs = 1, bool $success = true, string $commandLine = null, string $output = null, string $error = null, bool $enableCallback = false) + public function createProcessMock(int $runs = 1, bool $success = true, ?string $commandLine = null, ?string $output = null, string $error = null, bool $enableCallback = false) : Process { - $process = $this->getMockBuilder(\Symfony\Component\Process\Process::class) - ->disableOriginalConstructor() - ->getMock(); + $process = $this->createMock(Process::class); $builder = $process->expects($this->exactly($runs)) ->method('run'); @@ -45,7 +44,7 @@ public function createProcessMock(int $runs = 1, bool $success = true, string $c $process->expects($this->any()) ->method('isSuccessful') - ->will($this->returnValue($success)); + ->willReturn($success); foreach ([ 'getOutput' => $output, @@ -55,25 +54,25 @@ public function createProcessMock(int $runs = 1, bool $success = true, string $c $process ->expects($this->any()) ->method($command) - ->will($this->returnValue($value)); + ->willReturn($value); } return $process; } /** - * @return LoggerInterface + * @return LoggerInterface|MockObject */ - public function createLoggerMock() + public function createLoggerMock() : LoggerInterface { - return $this->getMockBuilder(\Psr\Log\LoggerInterface::class)->getMock(); + return $this->createMock(LoggerInterface::class); } /** - * @return ConfigurationInterface + * @return ConfigurationInterface|MockObject */ - public function createConfigurationMock() + public function createConfigurationMock() : ConfigurationInterface { - return $this->getMockBuilder(\Alchemy\BinaryDriver\ConfigurationInterface::class)->getMock(); + return $this->createMock(ConfigurationInterface::class); } } diff --git a/src/Alchemy/BinaryDriver/BinaryInterface.php b/src/Alchemy/BinaryDriver/BinaryInterface.php index e320a68..42fa9c4 100644 --- a/src/Alchemy/BinaryDriver/BinaryInterface.php +++ b/src/Alchemy/BinaryDriver/BinaryInterface.php @@ -44,7 +44,7 @@ public function unlisten(ListenerInterface $listener) : self; * Calling this method on a `ls` driver with the command `-a` would run `ls -a`. * * @param array|string $command A command or an array of command - * @param bool $bypassErrors If set to true, an erronous process will not throw an exception + * @param bool $bypassErrors If set to true, an erronous process will not throw an exception * @param ListenerInterface|array $listeners A listener or an array of listeners to register for this unique run * * @return string The command output @@ -57,7 +57,7 @@ public function command($command, bool $bypassErrors = false, $listeners = null) * Loads a binary * * @param string|array $binaries A binary name or an array of binary names - * @param null|LoggerInterface $logger A Logger + * @param null|LoggerInterface $logger A Logger * @param array|ConfigurationInterface $configuration The configuration * * @throws ExecutableNotFoundException In case none of the binaries were found diff --git a/src/Alchemy/BinaryDriver/Listeners/DebugListener.php b/src/Alchemy/BinaryDriver/Listeners/DebugListener.php index 4690cf0..a80f651 100644 --- a/src/Alchemy/BinaryDriver/Listeners/DebugListener.php +++ b/src/Alchemy/BinaryDriver/Listeners/DebugListener.php @@ -50,7 +50,7 @@ public function forwardedEvents() : array return array_unique([$this->eventErr, $this->eventOut]); } - private function emitLines(string $event, string $prefix, string $lines) + private function emitLines(string $event, string $prefix, string $lines) : void { foreach (explode("\n", $lines) as $line) { $this->emit($event, [$prefix . $line]); diff --git a/src/Alchemy/BinaryDriver/Listeners/Listeners.php b/src/Alchemy/BinaryDriver/Listeners/Listeners.php index 1b949cf..6990131 100644 --- a/src/Alchemy/BinaryDriver/Listeners/Listeners.php +++ b/src/Alchemy/BinaryDriver/Listeners/Listeners.php @@ -3,6 +3,7 @@ namespace Alchemy\BinaryDriver\Listeners; +use Alchemy\BinaryDriver\Exception\InvalidArgumentException; use SplObjectStorage; use Evenement\EventEmitter; @@ -29,7 +30,7 @@ public function __clone() * @param ListenerInterface $listener * @param null|EventEmitter $target * - * @return ListenersInterface + * @return ListenerInterface */ public function register(ListenerInterface $listener, EventEmitter $target = null) : self { @@ -49,7 +50,7 @@ public function register(ListenerInterface $listener, EventEmitter $target = nul * * @param ListenerInterface $listener * - * @return ListenersInterface + * @return ListenerInterface * * @throws InvalidArgumentException In case the listener is not registered */ diff --git a/src/Alchemy/BinaryDriver/ProcessBuilderFactory.php b/src/Alchemy/BinaryDriver/ProcessBuilderFactory.php index dd7a57f..2dd09ca 100644 --- a/src/Alchemy/BinaryDriver/ProcessBuilderFactory.php +++ b/src/Alchemy/BinaryDriver/ProcessBuilderFactory.php @@ -27,29 +27,10 @@ class ProcessBuilderFactory implements ProcessBuilderFactoryInterface /** * The timeout for the generated processes * - * @var integer|float + * @var null|float */ private $timeout; - /** - * An internal ProcessBuilder. - * - * Note that this one is used only if Symfony ProcessBuilder has method - * setPrefix (2.3) - * - * @var Process - */ - private $builder; - - /** - * Tells whether Symfony LTS ProcessBuilder should be emulated or not. - * - * This symfony version provided a brand new ::setPrefix method. - * - * @var bool - */ - public static $emulateSfLTS; - /** * Constructor * @@ -59,38 +40,9 @@ class ProcessBuilderFactory implements ProcessBuilderFactoryInterface */ public function __construct(string $binary) { - $this->detectEmulation(); - - if (!self::$emulateSfLTS) { - $this->builder = new Process(); - } - $this->useBinary($binary); } - /** - * Covenient method for unit testing - * - * @return type - */ - public function getBuilder() - { - return $this->builder; - } - - /** - * Covenient method for unit testing - * - * @param ProcessBuilder $builder - * @return ProcessBuilderFactory - */ - public function setBuilder(ProcessBuilder $builder) - { - $this->builder = $builder; - - return $this; - } - /** * @inheritDoc */ @@ -110,31 +62,23 @@ public function useBinary(string $binary) : ProcessBuilderFactoryInterface $this->binary = $binary; - if (!static::$emulateSfLTS) { - $this->builder->setPrefix($binary); - } - return $this; } /** * @inheritDoc */ - public function setTimeout($timeout) + public function setTimeout(float $timeout = null) : ProcessBuilderFactoryInterface { $this->timeout = $timeout; - if (!static::$emulateSfLTS) { - $this->builder->setTimeout($this->timeout); - } - return $this; } /** * @inheritDoc */ - public function getTimeout() + public function getTimeout() : ?float { return $this->timeout; } @@ -152,35 +96,11 @@ public function create($arguments = []) : Process $arguments = (array)$arguments; } - if (static::$emulateSfLTS) { - array_unshift($arguments, $this->binary); - if (method_exists('Symfony\Component\Process\ProcessUtils', 'escapeArgument')) { - $script = implode(' ', array_map(array('Symfony\Component\Process\ProcessUtils', 'escapeArgument'), $arguments)); - } else { - $script = $arguments; - } - - $env = array_replace($_ENV, $_SERVER); - $env = array_filter($env, function ($value) { - return !is_array($value); - }); - - return new Process($script, null, $env, null, $this->timeout); - } else { - return $this->builder - ->setArguments($arguments) - ->getProcess(); - } - } + array_unshift($arguments, $this->binary); - private function detectEmulation() - { - if (null !== static::$emulateSfLTS) { - return $this; - } + $process = new Process($arguments, null, null, null, $this->timeout); + $process->inheritEnvironmentVariables(); - static::$emulateSfLTS = !method_exists('Symfony\Component\Process\Process', 'setPrefix'); - - return $this; + return $process; } } diff --git a/src/Alchemy/BinaryDriver/ProcessBuilderFactoryAwareInterface.php b/src/Alchemy/BinaryDriver/ProcessBuilderFactoryAwareInterface.php index ae48739..2b0c162 100644 --- a/src/Alchemy/BinaryDriver/ProcessBuilderFactoryAwareInterface.php +++ b/src/Alchemy/BinaryDriver/ProcessBuilderFactoryAwareInterface.php @@ -25,6 +25,8 @@ public function getProcessBuilderFactory() : ProcessBuilderFactoryInterface; * Set a process builder factory * * @param ProcessBuilderFactoryInterface $factory + * + * @return ProcessBuilderFactoryAwareInterface */ public function setProcessBuilderFactory(ProcessBuilderFactoryInterface $factory) : ProcessBuilderFactoryAwareInterface; } diff --git a/src/Alchemy/BinaryDriver/ProcessBuilderFactoryInterface.php b/src/Alchemy/BinaryDriver/ProcessBuilderFactoryInterface.php index fcb9d5e..c80ebd4 100644 --- a/src/Alchemy/BinaryDriver/ProcessBuilderFactoryInterface.php +++ b/src/Alchemy/BinaryDriver/ProcessBuilderFactoryInterface.php @@ -55,12 +55,12 @@ public function useBinary(string $binary) : ProcessBuilderFactoryInterface; * * @throws InvalidArgumentException In case the timeout is not valid */ - public function setTimeout($timeout); + public function setTimeout(float $timeout = null) : ProcessBuilderFactoryInterface; /** * Returns the current timeout applied to the created processes. * - * @return integer|float + * @return null|float */ - public function getTimeout(); + public function getTimeout() : ?float; } diff --git a/src/Alchemy/BinaryDriver/ProcessRunner.php b/src/Alchemy/BinaryDriver/ProcessRunner.php index fc6676b..37ac5a4 100644 --- a/src/Alchemy/BinaryDriver/ProcessRunner.php +++ b/src/Alchemy/BinaryDriver/ProcessRunner.php @@ -85,7 +85,7 @@ public function run(Process $process, SplObjectStorage $listeners, bool $bypassE } } - private function buildCallback(SplObjectStorage $listeners) + private function buildCallback(SplObjectStorage $listeners) : callable { return function ($type, $data) use ($listeners) { foreach ($listeners as $listener) { @@ -95,9 +95,12 @@ private function buildCallback(SplObjectStorage $listeners) } /** - * @throws ExecutionFailureException + * @param string|null $command + * @param \Throwable|null $e * * @return void + * + * @throws ExecutionFailureException */ private function doExecutionFailure(? string $command, \Throwable $e = null) : void { diff --git a/tests/Alchemy/Tests/BinaryDriver/AbstractBinaryTest.php b/tests/Alchemy/Tests/BinaryDriver/AbstractBinaryTest.php index df80c43..890e7ce 100644 --- a/tests/Alchemy/Tests/BinaryDriver/AbstractBinaryTest.php +++ b/tests/Alchemy/Tests/BinaryDriver/AbstractBinaryTest.php @@ -5,13 +5,13 @@ use Alchemy\BinaryDriver\AbstractBinary; use Alchemy\BinaryDriver\BinaryDriverTestCase; use Alchemy\BinaryDriver\Configuration; -use Alchemy\BinaryDriver\Exception\ExecutableNotFoundException; use Alchemy\BinaryDriver\Listeners\ListenerInterface; +use PHPUnit\Framework\MockObject\MockObject; use Symfony\Component\Process\ExecutableFinder; class AbstractBinaryTest extends BinaryDriverTestCase { - protected function getPhpBinary() + protected function getPhpBinary() : string { $finder = new ExecutableFinder(); $php = $finder->find('php'); @@ -23,71 +23,69 @@ protected function getPhpBinary() return $php; } - public function testSimpleLoadWithBinaryPath() + public function testSimpleLoadWithBinaryPath() : void { $php = $this->getPhpBinary(); $imp = Implementation::load($php); - $this->assertInstanceOf('Alchemy\Tests\BinaryDriver\Implementation', $imp); + $this->assertInstanceOf(\Alchemy\Tests\BinaryDriver\Implementation::class, $imp); $this->assertEquals($php, $imp->getProcessBuilderFactory()->getBinary()); } - public function testMultipleLoadWithBinaryPath() + public function testMultipleLoadWithBinaryPath() : void { $php = $this->getPhpBinary(); - $imp = Implementation::load(array('/zz/path/to/unexisting/command', $php)); - $this->assertInstanceOf('Alchemy\Tests\BinaryDriver\Implementation', $imp); + $imp = Implementation::load(['/zz/path/to/unexisting/command', $php]); + $this->assertInstanceOf(\Alchemy\Tests\BinaryDriver\Implementation::class, $imp); $this->assertEquals($php, $imp->getProcessBuilderFactory()->getBinary()); } - public function testSimpleLoadWithBinaryName() + public function testSimpleLoadWithBinaryName() : void { $php = $this->getPhpBinary(); $imp = Implementation::load('php'); - $this->assertInstanceOf('Alchemy\Tests\BinaryDriver\Implementation', $imp); + $this->assertInstanceOf(\Alchemy\Tests\BinaryDriver\Implementation::class, $imp); $this->assertEquals($php, $imp->getProcessBuilderFactory()->getBinary()); } - public function testMultipleLoadWithBinaryName() + public function testMultipleLoadWithBinaryName() : void { $php = $this->getPhpBinary(); - $imp = Implementation::load(array('bachibouzouk', 'php')); - $this->assertInstanceOf('Alchemy\Tests\BinaryDriver\Implementation', $imp); + $imp = Implementation::load(['bachibouzouk', 'php']); + $this->assertInstanceOf(\Alchemy\Tests\BinaryDriver\Implementation::class, $imp); $this->assertEquals($php, $imp->getProcessBuilderFactory()->getBinary()); } /** - * @expectedException \Alchemy\BinaryDriver\Exception\ExecutableNotFoundException - * * @return void */ public function testLoadWithMultiplePathExpectingAFailure() : void { + $this->expectException(\Alchemy\BinaryDriver\Exception\ExecutableNotFoundException::class); Implementation::load(['bachibouzouk', 'moribon']); } /** - * @expectedException \Alchemy\BinaryDriver\Exception\ExecutableNotFoundException - * * @return void */ public function testLoadWithUniquePathExpectingAFailure() : void { + $this->expectException(\Alchemy\BinaryDriver\Exception\ExecutableNotFoundException::class); Implementation::load('bachibouzouk'); } - public function testLoadWithCustomLogger() + public function testLoadWithCustomLogger() : void { - $logger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)->getMock(); + $logger = $this->createMock(\Psr\Log\LoggerInterface::class); $imp = Implementation::load('php', $logger); $this->assertEquals($logger, $imp->getProcessRunner()->getLogger()); } - public function testLoadWithCustomConfigurationAsArray() + public function testLoadWithCustomConfigurationAsArray() : void { $conf = ['timeout' => 200]; $imp = Implementation::load('php', null, $conf); @@ -95,50 +93,50 @@ public function testLoadWithCustomConfigurationAsArray() $this->assertEquals($conf, $imp->getConfiguration()->all()); } - public function testLoadWithCustomConfigurationAsObject() + public function testLoadWithCustomConfigurationAsObject() : void { - $conf = $this->getMockBuilder(\Alchemy\BinaryDriver\ConfigurationInterface::class)->getMock(); + $conf = $this->createMock(\Alchemy\BinaryDriver\ConfigurationInterface::class); $imp = Implementation::load('php', null, $conf); $this->assertEquals($conf, $imp->getConfiguration()); } - public function testProcessBuilderFactoryGetterAndSetters() + public function testProcessBuilderFactoryGetterAndSetters() : void { $imp = Implementation::load('php'); - $factory = $this->getMockBuilder(\Alchemy\BinaryDriver\ProcessBuilderFactoryInterface::class)->getMock(); + $factory = $this->createMock(\Alchemy\BinaryDriver\ProcessBuilderFactoryInterface::class); $imp->setProcessBuilderFactory($factory); $this->assertEquals($factory, $imp->getProcessBuilderFactory()); } - public function testConfigurationGetterAndSetters() + public function testConfigurationGetterAndSetters() : void { $imp = Implementation::load('php'); - $conf = $this->getMockBuilder(\Alchemy\BinaryDriver\ConfigurationInterface::class)->getMock(); + $conf = $this->createMock(\Alchemy\BinaryDriver\ConfigurationInterface::class); $imp->setConfiguration($conf); $this->assertEquals($conf, $imp->getConfiguration()); } - public function testTimeoutIsSetOnConstruction() + public function testTimeoutIsSetOnConstruction() : void { $imp = Implementation::load('php', null, ['timeout' => 42]); $this->assertEquals(42, $imp->getProcessBuilderFactory()->getTimeout()); } - public function testTimeoutIsSetOnConfigurationSetting() + public function testTimeoutIsSetOnConfigurationSetting() : void { $imp = Implementation::load('php', null); $imp->setConfiguration(new Configuration(['timeout' => 42])); $this->assertEquals(42, $imp->getProcessBuilderFactory()->getTimeout()); } - public function testTimeoutIsSetOnProcessBuilderSetting() + public function testTimeoutIsSetOnProcessBuilderSetting() : void { $imp = Implementation::load('php', null, ['timeout' => 42]); - $factory = $this->getMockBuilder(\Alchemy\BinaryDriver\ProcessBuilderFactoryInterface::class)->getMock(); + $factory = $this->createMock(\Alchemy\BinaryDriver\ProcessBuilderFactoryInterface::class); $factory->expects($this->once()) ->method('setTimeout') ->with(42); @@ -146,21 +144,19 @@ public function testTimeoutIsSetOnProcessBuilderSetting() $imp->setProcessBuilderFactory($factory); } - public function testListenRegistersAListener() + public function testListenRegistersAListener() : void { $imp = Implementation::load('php'); - $listeners = $this->getMockBuilder(\Alchemy\BinaryDriver\Listeners\Listeners::class) - ->disableOriginalConstructor() - ->getMock(); + $listeners = $this->createMock(\Alchemy\BinaryDriver\Listeners\Listeners::class); - $listener = $this->getMockBuilder('Alchemy\BinaryDriver\Listeners\ListenerInterface')->getMock(); + $listener = $this->createMock(ListenerInterface::class); $listeners->expects($this->once()) ->method('register') ->with($this->equalTo($listener), $this->equalTo($imp)); - $reflexion = new \ReflectionClass('Alchemy\BinaryDriver\AbstractBinary'); + $reflexion = new \ReflectionClass(AbstractBinary::class); $prop = $reflexion->getProperty('listenersManager'); $prop->setAccessible(true); $prop->setValue($imp, $listeners); @@ -169,27 +165,30 @@ public function testListenRegistersAListener() } /** + * @param array|string $parameters + * @param bool $bypassErrors + * @param array $expectedParameters + * @param string $output + * * @dataProvider provideCommandParameters */ - public function testCommandRunsAProcess($parameters, $bypassErrors, $expectedParameters, $output) + public function testCommandRunsAProcess($parameters, bool $bypassErrors, array $expectedParameters, string $output) : void { $imp = Implementation::load('php'); - $factory = $this->getMockBuilder(\Alchemy\BinaryDriver\ProcessBuilderFactoryInterface::class)->getMock(); - $processRunner = $this->getMockBuilder(\Alchemy\BinaryDriver\ProcessRunnerInterface::class)->getMock(); + $factory = $this->createMock(\Alchemy\BinaryDriver\ProcessBuilderFactoryInterface::class); + $processRunner = $this->createMock(\Alchemy\BinaryDriver\ProcessRunnerInterface::class); - $process = $this->getMockBuilder(\Symfony\Component\Process\Process::class) - ->disableOriginalConstructor() - ->getMock(); + $process = $this->createMock(\Symfony\Component\Process\Process::class); $processRunner->expects($this->once()) ->method('run') ->with($this->equalTo($process), $this->isInstanceOf(\SplObjectStorage::class), $this->equalTo($bypassErrors)) - ->will($this->returnValue($output)); + ->willReturn($output); $factory->expects($this->once()) ->method('create') ->with($expectedParameters) - ->will($this->returnValue($process)); + ->willReturn($process); $imp->setProcessBuilderFactory($factory); $imp->setProcessRunner($processRunner); @@ -198,37 +197,44 @@ public function testCommandRunsAProcess($parameters, $bypassErrors, $expectedPar } /** + * @param string $parameters + * @param bool $bypassErrors + * @param array $expectedParameters + * @param string $output + * @param int $count + * @param string|ListenerInterface $listeners + * * @dataProvider provideCommandWithListenersParameters */ - public function testCommandWithTemporaryListeners($parameters, $bypassErrors, $expectedParameters, $output, $count, $listeners) + public function testCommandWithTemporaryListeners(string $parameters, bool $bypassErrors, array $expectedParameters, string $output, int $count, $listeners) : void { $imp = Implementation::load('php'); - $factory = $this->getMockBuilder(\Alchemy\BinaryDriver\ProcessBuilderFactoryInterface::class)->getMock(); - $processRunner = $this->getMockBuilder(\Alchemy\BinaryDriver\ProcessRunnerInterface::class)->getMock(); + $factory = $this->createMock(\Alchemy\BinaryDriver\ProcessBuilderFactoryInterface::class); + $processRunner = $this->createMock(\Alchemy\BinaryDriver\ProcessRunnerInterface::class); - $process = $this->getMockBuilder(\Symfony\Component\Process\Process::class) - ->disableOriginalConstructor() - ->getMock(); + $process = $this->createMock(\Symfony\Component\Process\Process::class); $firstStorage = $secondStorage = null; $processRunner->expects($this->exactly(2)) ->method('run') ->with($this->equalTo($process), $this->isInstanceOf(\SplObjectStorage::class), $this->equalTo($bypassErrors)) - ->will($this->returnCallback(function ($process, $storage, $errors) use ($output, &$firstStorage, &$secondStorage) { - if (null === $firstStorage) { - $firstStorage = $storage; - } else { - $secondStorage = $storage; + ->willReturnCallback( + function ($process, $storage, $errors) use ($output, &$firstStorage, &$secondStorage) { + if (null === $firstStorage) { + $firstStorage = $storage; + } else { + $secondStorage = $storage; + } + + return $output; } - - return $output; - })); + ); $factory->expects($this->exactly(2)) ->method('create') ->with($expectedParameters) - ->will($this->returnValue($process)); + ->willReturn($process); $imp->setProcessBuilderFactory($factory); $imp->setProcessRunner($processRunner); @@ -239,41 +245,39 @@ public function testCommandWithTemporaryListeners($parameters, $bypassErrors, $e $this->assertCount(0, $secondStorage); } - public function provideCommandWithListenersParameters() + public function provideCommandWithListenersParameters() : array { - 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, []), - ); + return [ + ['-a', false, ['-a'], 'loubda', 2, [$this->getMockListener(), $this->getMockListener()]], + ['-a', false, ['-a'], 'loubda', 1, [$this->getMockListener()]], + ['-a', false, ['-a'], 'loubda', 1, $this->getMockListener()], + ['-a', false, ['-a'], 'loubda', 0, []], + ]; } - public function provideCommandParameters() + public function provideCommandParameters() : array { - 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'), - ); + return [ + ['-a', false, ['-a'], 'loubda'], + ['-a', true, ['-a'], 'loubda'], + ['-a -b', false, ['-a -b'], 'loubda'], + [['-a'], false, ['-a'], 'loubda'], + [['-a'], true, ['-a'], 'loubda'], + [['-a', '-b'], false, ['-a', '-b'], 'loubda'], + ]; } - public function testUnlistenUnregistersAListener() + public function testUnlistenUnregistersAListener() : void { $imp = Implementation::load('php'); - $listeners = $this->getMockBuilder(\Alchemy\BinaryDriver\Listeners\Listeners::class) - ->disableOriginalConstructor() - ->getMock(); + $listeners = $this->createMock(\Alchemy\BinaryDriver\Listeners\Listeners::class); - $listener = $this->getMockBuilder(\Alchemy\BinaryDriver\Listeners\ListenerInterface::class)->getMock(); + $listener = $this->createMock(\Alchemy\BinaryDriver\Listeners\ListenerInterface::class); $listeners->expects($this->once()) ->method('unregister') - ->with($this->equalTo($listener), $this->equalTo($imp)); + ->with($this->equalTo($listener)); $reflexion = new \ReflectionClass(\Alchemy\BinaryDriver\AbstractBinary::class); $prop = $reflexion->getProperty('listenersManager'); @@ -284,11 +288,11 @@ public function testUnlistenUnregistersAListener() } /** - * @return \PHPUnit_Framework_MockObject_MockObject + * @return MockObject */ - private function getMockListener() + private function getMockListener() : MockObject { - $listener = $this->getMockBuilder(ListenerInterface::class)->getMock(); + $listener = $this->createMock(ListenerInterface::class); $listener->expects($this->any()) ->method('forwardedEvents') ->willReturn([]); @@ -299,7 +303,7 @@ private function getMockListener() class Implementation extends AbstractBinary { - public function getName() + public function getName() : string { return 'Implementation'; } diff --git a/tests/Alchemy/Tests/BinaryDriver/AbstractProcessBuilderFactoryTest.php b/tests/Alchemy/Tests/BinaryDriver/AbstractProcessBuilderFactoryTest.php index 044f790..c909cd3 100644 --- a/tests/Alchemy/Tests/BinaryDriver/AbstractProcessBuilderFactoryTest.php +++ b/tests/Alchemy/Tests/BinaryDriver/AbstractProcessBuilderFactoryTest.php @@ -8,17 +8,18 @@ abstract class AbstractProcessBuilderFactoryTest extends TestCase { + /** + * @var string + */ public static $phpBinary; - private $original; /** * @return ProcessBuilderFactory */ - abstract protected function getProcessBuilderFactory($binary); + abstract protected function getProcessBuilderFactory($binary) : ProcessBuilderFactory; public function setUp() : void { - ProcessBuilderFactory::$emulateSfLTS = null; if (null === static::$phpBinary) { $this->markTestSkipped('Unable to detect php binary, skipping.'); return; @@ -33,16 +34,16 @@ public static function setUpBeforeClass() static::$phpBinary = $finder->find('php'); } - public function testThatBinaryIsSetOnConstruction() + public function testThatBinaryIsSetOnConstruction() : void { $factory = $this->getProcessBuilderFactory(static::$phpBinary); $this->assertEquals(static::$phpBinary, $factory->getBinary()); } - public function testGetSetBinary() + public function testGetSetBinary() : void { $finder = new ExecutableFinder(); - $phpUnit = $finder->find('phpunit'); + $phpUnit = $finder->find('php'); if (null === $phpUnit) { $this->markTestSkipped('Unable to detect phpunit binary, skipping'); @@ -53,11 +54,9 @@ public function testGetSetBinary() $this->assertEquals($phpUnit, $factory->getBinary()); } - /** - * @expectedException Alchemy\BinaryDriver\Exception\InvalidArgumentException - */ - public function testUseNonExistantBinary() + public function testUseNonExistantBinary() : void { + $this->expectException(\Alchemy\BinaryDriver\Exception\InvalidArgumentException::class); $factory = $this->getProcessBuilderFactory(static::$phpBinary); $factory->useBinary('itissureitdoesnotexist'); } @@ -65,7 +64,7 @@ public function testUseNonExistantBinary() /** * @requires OS Linux */ - public function testCreateShouldReturnAProcess() + public function testCreateShouldReturnAProcess() : void { $factory = $this->getProcessBuilderFactory(static::$phpBinary); $process = $factory->create(); @@ -77,7 +76,7 @@ public function testCreateShouldReturnAProcess() /** * @requires OS Linux */ - public function testCreateWithStringArgument() + public function testCreateWithStringArgument() : void { $factory = $this->getProcessBuilderFactory(static::$phpBinary); $process = $factory->create('-v'); @@ -89,7 +88,7 @@ public function testCreateWithStringArgument() /** * @requires OS Linux */ - public function testCreateWithArrayArgument() + public function testCreateWithArrayArgument() : void { $factory = $this->getProcessBuilderFactory(static::$phpBinary); $process = $factory->create(['-r', 'echo "Hello !";']); @@ -98,10 +97,10 @@ public function testCreateWithArrayArgument() $this->assertEquals("'" . static::$phpBinary . "' '-r' 'echo \"Hello !\";'", $process->getCommandLine()); } - public function testCreateWithTimeout() + public function testCreateWithTimeout() : void { $factory = $this->getProcessBuilderFactory(static::$phpBinary); - $factory->setTimeout(200); + $factory->setTimeout(200.0); $process = $factory->create(['-i']); $this->assertInstanceOf(\Symfony\Component\Process\Process::class, $process); diff --git a/tests/Alchemy/Tests/BinaryDriver/ConfigurationTest.php b/tests/Alchemy/Tests/BinaryDriver/ConfigurationTest.php index 5e5c4cb..86978fd 100644 --- a/tests/Alchemy/Tests/BinaryDriver/ConfigurationTest.php +++ b/tests/Alchemy/Tests/BinaryDriver/ConfigurationTest.php @@ -7,9 +7,9 @@ class ConfigurationTest extends TestCase { - public function testArrayAccessImplementation() + public function testArrayAccessImplementation() : void { - $configuration = new Configuration(array('key' => 'value')); + $configuration = new Configuration(['key' => 'value']); $this->assertTrue(isset($configuration['key'])); $this->assertEquals('value', $configuration['key']); @@ -23,16 +23,16 @@ public function testArrayAccessImplementation() $this->assertEquals('value2', $configuration['key2']); } - public function testGetOnNonExistentKeyShouldReturnDefaultValue() + public function testGetOnNonExistentKeyShouldReturnDefaultValue() : void { $conf = new Configuration(); $this->assertEquals('booba', $conf->get('hooba', 'booba')); $this->assertEquals(null, $conf->get('hooba')); } - public function testSetHasGetRemove() + public function testSetHasGetRemove() : void { - $configuration = new Configuration(array('key' => 'value')); + $configuration = new Configuration(['key' => 'value']); $this->assertTrue($configuration->has('key')); $this->assertEquals('value', $configuration->get('key')); @@ -46,13 +46,13 @@ public function testSetHasGetRemove() $this->assertEquals('value2', $configuration->get('key2')); } - public function testIterator() + public function testIterator() : void { - $data = array( + $data = [ 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3', - ); + ]; $captured = []; $conf = new Configuration($data); @@ -64,13 +64,13 @@ public function testIterator() $this->assertEquals($data, $captured); } - public function testAll() + public function testAll() : void { - $data = array( + $data = [ 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3', - ); + ]; $conf = new Configuration($data); $this->assertEquals($data, $conf->all()); diff --git a/tests/Alchemy/Tests/BinaryDriver/Listeners/DebugListenerTest.php b/tests/Alchemy/Tests/BinaryDriver/Listeners/DebugListenerTest.php index 25d5df8..26d2ce0 100644 --- a/tests/Alchemy/Tests/BinaryDriver/Listeners/DebugListenerTest.php +++ b/tests/Alchemy/Tests/BinaryDriver/Listeners/DebugListenerTest.php @@ -8,7 +8,7 @@ class DebugListenerTest extends TestCase { - public function testHandle() + public function testHandle() : void { $listener = new DebugListener(); @@ -21,13 +21,13 @@ public function testHandle() $listener->handle('unknown', "lalala"); $listener->handle(Process::OUT, "another output\n"); - $expected = array( + $expected = [ '[ERROR] first line', '[ERROR] second line', '[OUT] cool output', '[OUT] another output', '[OUT] ', - ); + ]; $this->assertEquals($expected, $lines); } diff --git a/tests/Alchemy/Tests/BinaryDriver/Listeners/ListenersTest.php b/tests/Alchemy/Tests/BinaryDriver/Listeners/ListenersTest.php index ef8713b..5bb3aab 100644 --- a/tests/Alchemy/Tests/BinaryDriver/Listeners/ListenersTest.php +++ b/tests/Alchemy/Tests/BinaryDriver/Listeners/ListenersTest.php @@ -9,7 +9,7 @@ class ListenersTest extends TestCase { - public function testRegister() + public function testRegister() : void { $listener = new MockListener(); @@ -38,7 +38,7 @@ public function testRegister() $this->assertEquals($data, $capturedData); } - public function testRegisterAndForwardThenUnregister() + public function testRegisterAndForwardThenUnregister() : void { $listener = new MockListener(); $target = new EventEmitter(); @@ -66,7 +66,7 @@ public function testRegisterAndForwardThenUnregister() $listener->handle($type, $data); $listener->handle($type, $data); - $listeners->unregister($listener, $target); + $listeners->unregister($listener); $listener->handle($type, $data); @@ -86,7 +86,7 @@ class MockListener extends EventEmitter implements ListenerInterface */ public function handle(string $type, string $data) : void { - $this->emit('received', array($type, $data)); + $this->emit('received', [$type, $data]); } /** diff --git a/tests/Alchemy/Tests/BinaryDriver/NONLTSProcessBuilderFactoryTest.php b/tests/Alchemy/Tests/BinaryDriver/NONLTSProcessBuilderFactoryTest.php deleted file mode 100644 index c84ed0e..0000000 --- a/tests/Alchemy/Tests/BinaryDriver/NONLTSProcessBuilderFactoryTest.php +++ /dev/null @@ -1,15 +0,0 @@ -createLoggerMock(); $runner = $this->getProcessRunner($logger); @@ -42,7 +43,7 @@ public function testRunSuccessFullProcess() $this->assertEquals('Kikoo Romain', $runner->run($process, new \SplObjectStorage(), false)); } - public function testRunSuccessFullProcessBypassingErrors() + public function testRunSuccessFullProcessBypassingErrors() : void { $logger = $this->createLoggerMock(); $runner = $this->getProcessRunner($logger); @@ -59,7 +60,7 @@ public function testRunSuccessFullProcessBypassingErrors() $this->assertEquals('Kikoo Romain', $runner->run($process, new \SplObjectStorage(), true)); } - public function testRunFailingProcess() + public function testRunFailingProcess() : void { $logger = $this->createLoggerMock(); $runner = $this->getProcessRunner($logger); @@ -81,15 +82,13 @@ public function testRunFailingProcess() } } - public function testRunFailingProcessWithException() + public function testRunFailingProcessWithException() : void { $logger = $this->createLoggerMock(); $runner = $this->getProcessRunner($logger); $exception = new ProcessRuntimeException('Process Failed'); - $process = $this->getMockBuilder('Symfony\Component\Process\Process') - ->disableOriginalConstructor() - ->getMock(); + $process = $this->createMock(Process::class); $process->expects($this->once()) ->method('run') ->will($this->throwException($exception)); @@ -109,7 +108,7 @@ public function testRunFailingProcessWithException() } } - public function testRunfailingProcessBypassingErrors() + public function testRunfailingProcessBypassingErrors() : void { $logger = $this->createLoggerMock(); $runner = $this->getProcessRunner($logger); @@ -126,15 +125,13 @@ public function testRunfailingProcessBypassingErrors() $this->assertSame('', $runner->run($process, new \SplObjectStorage(), true)); } - public function testRunFailingProcessWithExceptionBypassingErrors() + public function testRunFailingProcessWithExceptionBypassingErrors() : void { $logger = $this->createLoggerMock(); $runner = $this->getProcessRunner($logger); $exception = new ProcessRuntimeException('Process Failed'); - $process = $this->getMockBuilder('Symfony\Component\Process\Process') - ->disableOriginalConstructor() - ->getMock(); + $process = $this->createMock(Process::class); $process->expects($this->once()) ->method('run') ->will($this->throwException($exception)); @@ -149,20 +146,23 @@ public function testRunFailingProcessWithExceptionBypassingErrors() $this->assertSame('', $runner->run($process, new \SplObjectStorage(), true)); } - public function testRunSuccessFullProcessWithHandlers() + public function testRunSuccessFullProcessWithHandlers() : void { $logger = $this->createLoggerMock(); $runner = $this->getProcessRunner($logger); + /** @var null|\Closure $capturedCallback */ $capturedCallback = null; $process = $this->createProcessMock(1, true, '--helloworld--', "Kikoo Romain", null, true); $process->expects($this->once()) ->method('run') ->with($this->isInstanceOf('Closure')) - ->will($this->returnCallback(function ($callback) use (&$capturedCallback) { - $capturedCallback = $callback; - })); + ->willReturnCallback( + function ($callback) use (&$capturedCallback) { + $capturedCallback = $callback; + } + ); $logger ->expects($this->never()) @@ -187,6 +187,7 @@ public function testRunSuccessFullProcessWithHandlers() $type = 'err'; $data = 'data'; + $this->assertNotNull($capturedCallback); $capturedCallback($type, $data); $this->assertEquals($data, $capturedData); From 51dd3f474741385a3e6c100b99f2dad7641e3424 Mon Sep 17 00:00:00 2001 From: Artem Lopata Date: Fri, 16 Aug 2019 16:47:13 +0200 Subject: [PATCH 2/2] Do not use php 8.0 --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 46e9b71..276f27c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,7 @@ sudo: false php: - 7.1 - 7.2 - - nightly + - 7.3 matrix: include: