Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moving towards php 7.1 and new PHPUnit #31

Open
wants to merge 2 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ sudo: false
php:
- 7.1
- 7.2
- nightly
- 7.3

matrix:
include:
Expand Down
3 changes: 1 addition & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
</testsuites>
<filter>
<whitelist>
<directory>vendor</directory>
<directory>tests</directory>
<directory>src</directory>
</whitelist>
</filter>

Expand Down
10 changes: 5 additions & 5 deletions src/Alchemy/BinaryDriver/AbstractBinary.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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'));
Expand Down
31 changes: 15 additions & 16 deletions src/Alchemy/BinaryDriver/BinaryDriverTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

/**
Expand All @@ -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');
Expand All @@ -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,
Expand All @@ -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);
}
}
4 changes: 2 additions & 2 deletions src/Alchemy/BinaryDriver/BinaryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Alchemy/BinaryDriver/Listeners/DebugListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
5 changes: 3 additions & 2 deletions src/Alchemy/BinaryDriver/Listeners/Listeners.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Alchemy\BinaryDriver\Listeners;

use Alchemy\BinaryDriver\Exception\InvalidArgumentException;
use SplObjectStorage;
use Evenement\EventEmitter;

Expand All @@ -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
{
Expand All @@ -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
*/
Expand Down
94 changes: 7 additions & 87 deletions src/Alchemy/BinaryDriver/ProcessBuilderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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
*/
Expand All @@ -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;
}
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public function getProcessBuilderFactory() : ProcessBuilderFactoryInterface;
* Set a process builder factory
*
* @param ProcessBuilderFactoryInterface $factory
*
* @return ProcessBuilderFactoryAwareInterface
*/
public function setProcessBuilderFactory(ProcessBuilderFactoryInterface $factory) : ProcessBuilderFactoryAwareInterface;
}
6 changes: 3 additions & 3 deletions src/Alchemy/BinaryDriver/ProcessBuilderFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
7 changes: 5 additions & 2 deletions src/Alchemy/BinaryDriver/ProcessRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
{
Expand Down
Loading