Skip to content

Commit

Permalink
Inject ProcessExecutor to the finders (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Dec 17, 2022
1 parent f6b95e1 commit b830f3c
Show file tree
Hide file tree
Showing 19 changed files with 386 additions and 85 deletions.
15 changes: 5 additions & 10 deletions infection.json5
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
"timeout": 10,

"mutators": {
"global-ignore": [
"Fidry\\CpuCoreCounter\\Finder\\NProcFinder::getCommand"
],
"@default": true,
"ArrayItemRemoval": {
"ignore": [
Expand All @@ -23,21 +26,13 @@
"CastString": {
"ignore": [
// I can't find a case in practice where this would happen
"Fidry\\CpuCoreCounter\\Executor\\ProcOpen::execute"
]
},
"FalseValue": {
"ignore": [
// This is from thecodingmachine/safe – leave it alone
"Fidry\\CpuCoreCounter\\Executor\\ShellExec::execute"
"Fidry\\CpuCoreCounter\\Executor\\ProcOpenExecutor::execute"
]
},
"FunctionCallRemoval": {
"ignore": [
// I can't find a case in practice where this would happen
"Fidry\\CpuCoreCounter\\Executor\\ProcOpen::execute",
// This is from thecodingmachine/safe – leave it alone
"Fidry\\CpuCoreCounter\\Executor\\ShellExec::execute"
"Fidry\\CpuCoreCounter\\Executor\\ProcOpenExecutor::execute",
]
},
"PublicVisibility": false
Expand Down
11 changes: 2 additions & 9 deletions src/Executor/ProcOpen.php → src/Executor/ProcOpenExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@
use function proc_open;
use function stream_get_contents;

final class ProcOpen
final class ProcOpenExecutor implements ProcessExecutor
{
/**
* @return array{string, string}|null STDOUT & STDERR tuple
*/
public static function execute(string $command): ?array
public function execute(string $command): ?array
{
if (!function_exists('proc_open')) {
return null;
Expand Down Expand Up @@ -56,8 +53,4 @@ public static function execute(string $command): ?array

return [$stdout, $stderr];
}

private function __construct()
{
}
}
22 changes: 22 additions & 0 deletions src/Executor/ProcessExecutor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Fidry CPUCounter Config package.
*
* (c) Théo FIDRY <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Fidry\CpuCoreCounter\Executor;

interface ProcessExecutor
{
/**
* @return array{string, string}|null STDOUT & STDERR tuple
*/
public function execute(string $command): ?array;
}
11 changes: 8 additions & 3 deletions src/Finder/NProcFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Fidry\CpuCoreCounter\Finder;

use Fidry\CpuCoreCounter\Executor\ProcessExecutor;
use function sprintf;

/**
Expand All @@ -31,8 +32,12 @@ final class NProcFinder extends ProcOpenBasedFinder
/**
* @param bool $all If disabled will give the number of cores available for the current process only.
*/
public function __construct(bool $all = true)
{
public function __construct(
bool $all = true,
?ProcessExecutor $executor = null
) {
parent::__construct($executor);

$this->all = $all;
}

Expand All @@ -46,6 +51,6 @@ public function toString(): string

protected function getCommand(): string
{
return 'nproc'.($this->all ? ' --all' : '').' 2>&1';
return 'nproc'.($this->all ? ' --all' : '');
}
}
21 changes: 16 additions & 5 deletions src/Finder/ProcOpenBasedFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

namespace Fidry\CpuCoreCounter\Finder;

use Fidry\CpuCoreCounter\Executor\ProcOpen;
use Fidry\CpuCoreCounter\Executor\ProcessExecutor;
use Fidry\CpuCoreCounter\Executor\ProcOpenExecutor;
use function filter_var;
use function function_exists;
use function is_int;
Expand All @@ -24,14 +25,24 @@

abstract class ProcOpenBasedFinder implements CpuCoreFinder
{
/**
* @var ProcessExecutor
*/
private $executor;

public function __construct(?ProcessExecutor $executor = null)
{
$this->executor = $executor ?? new ProcOpenExecutor();
}

public function diagnose(): string
{
if (!function_exists('proc_open')) {
return 'The function "proc_open" is not available.';
}

$command = $this->getCommand();
$output = ProcOpen::execute($command);
$output = $this->executor->execute($command);

if (null === $output) {
return sprintf(
Expand Down Expand Up @@ -63,7 +74,7 @@ public function diagnose(): string
*/
public function find(): ?int
{
$output = ProcOpen::execute($this->getCommand());
$output = $this->executor->execute($this->getCommand());

if (null === $output) {
return null;
Expand All @@ -74,15 +85,15 @@ public function find(): ?int

return $failed
? null
: static::countCpuCores($stdout);
: $this->countCpuCores($stdout);
}

/**
* @internal
*
* @return positive-int|null
*/
public static function countCpuCores(string $process): ?int
protected function countCpuCores(string $process): ?int
{
$cpuCount = filter_var($process, FILTER_VALIDATE_INT);

Expand Down
2 changes: 1 addition & 1 deletion src/Finder/WmicLogicalFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function toString(): string
return 'WmicLogicalFinder';
}

public static function countCpuCores(string $process): ?int
protected function countCpuCores(string $process): ?int
{
if (0 === preg_match(self::CPU_CORE_COUNT_REGEX, $process, $matches)) {
return parent::countCpuCores($process);
Expand Down
2 changes: 1 addition & 1 deletion src/Finder/WmicPhysicalFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function toString(): string
return 'WmicPhysicalFinder';
}

public static function countCpuCores(string $process): ?int
protected function countCpuCores(string $process): ?int
{
if (0 === preg_match(self::CPU_CORE_COUNT_REGEX, $process, $matches)) {
return parent::countCpuCores($process);
Expand Down
37 changes: 37 additions & 0 deletions tests/Executor/DummyExecutor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the Fidry CPUCounter Config package.
*
* (c) Théo FIDRY <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Fidry\CpuCoreCounter\Test\Executor;

use Fidry\CpuCoreCounter\Executor\ProcessExecutor;

final class DummyExecutor implements ProcessExecutor
{
/**
* @var array{string, string}|null
*/
private $output;

/**
* @param array{string, string}|null $output
*/
public function setOutput(?array $output): void
{
$this->output = $output;
}

public function execute(string $command): ?array
{
return $this->output ?? null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,38 @@

namespace Fidry\CpuCoreCounter\Test\Executor;

use Fidry\CpuCoreCounter\Executor\ProcOpen;
use Fidry\CpuCoreCounter\Executor\ProcOpenExecutor;
use PHPUnit\Framework\TestCase;
use const PHP_EOL;

/**
* @covers \Fidry\CpuCoreCounter\Executor\ProcOpen
* @covers \Fidry\CpuCoreCounter\Executor\ProcOpenExecutor
*
* @internal
*/
final class ProcOpenTest extends TestCase
final class ProcOpenExecutorTest extends TestCase
{
/**
* @var ProcOpenExecutor
*/
private $executor;

protected function setUp(): void
{
$this->executor = new ProcOpenExecutor();
}

protected function tearDown(): void
{
unset($this->executor);
}

public function test_it_can_execute_a_command_writing_output_to_the_stdout(): void
{
$command = 'echo "Hello world!"';

$expected = ['Hello world!'.PHP_EOL, ''];
$actual = ProcOpen::execute($command);
$actual = $this->executor->execute($command);

self::assertSame($expected, $actual);
}
Expand All @@ -39,7 +54,7 @@ public function test_it_can_execute_a_command_writing_output_to_the_stderr_inste
$command = 'echo "Hello world!" 1>&2';

$expected = ['', 'Hello world!'.PHP_EOL];
$actual = ProcOpen::execute($command);
$actual = $this->executor->execute($command);

self::assertSame($expected, $actual);
}
Expand All @@ -49,7 +64,7 @@ public function test_it_can_execute_a_command_writing_output_to_the_stdout_inste
$command = 'echoerr() { echo "$@" 1>&2; }; echoerr "Hello world!" 2>&1';

$expected = ['Hello world!'.PHP_EOL, ''];
$actual = \Fidry\CpuCoreCounter\Executor\ProcOpen::execute($command);
$actual = $this->executor->execute($command);

self::assertSame($expected, $actual);
}
Expand Down
23 changes: 23 additions & 0 deletions tests/Finder/DummyProcOpenBasedFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,38 @@

namespace Fidry\CpuCoreCounter\Test\Finder;

use Fidry\CpuCoreCounter\Executor\ProcessExecutor;
use Fidry\CpuCoreCounter\Finder\ProcOpenBasedFinder;

final class DummyProcOpenBasedFinder extends ProcOpenBasedFinder
{
/**
* @var callable
*/
private $extraParse;

/**
* @param callable(?int):int|null $extraParse
*/
public function __construct(
?callable $extraParse = null,
?ProcessExecutor $executor = null
) {
parent::__construct($executor);

$this->extraParse = $extraParse ?? static function (?int $value): ?int { return $value; };
}

protected function getCommand(): string
{
return '';
}

protected function countCpuCores(string $process): ?int
{
return ($this->extraParse)(parent::countCpuCores($process));
}

public function toString(): string
{
return 'DummyProcOpenBasedFinder';
Expand Down
5 changes: 3 additions & 2 deletions tests/Finder/HwLogicalFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Fidry\CpuCoreCounter\Test\Finder;

use Fidry\CpuCoreCounter\Executor\ProcessExecutor;
use Fidry\CpuCoreCounter\Finder\HwLogicalFinder;
use Fidry\CpuCoreCounter\Finder\ProcOpenBasedFinder;

Expand All @@ -23,8 +24,8 @@
*/
final class HwLogicalFinderTest extends ProcOpenBasedFinderTestCase
{
protected function getFinder(): ProcOpenBasedFinder
protected function createFinder(ProcessExecutor $executor): ProcOpenBasedFinder
{
return new HwLogicalFinder();
return new HwLogicalFinder($executor);
}
}
5 changes: 3 additions & 2 deletions tests/Finder/HwPhysicalFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Fidry\CpuCoreCounter\Test\Finder;

use Fidry\CpuCoreCounter\Executor\ProcessExecutor;
use Fidry\CpuCoreCounter\Finder\HwPhysicalFinder;
use Fidry\CpuCoreCounter\Finder\ProcOpenBasedFinder;

Expand All @@ -23,8 +24,8 @@
*/
final class HwPhysicalFinderTest extends ProcOpenBasedFinderTestCase
{
protected function getFinder(): ProcOpenBasedFinder
protected function createFinder(ProcessExecutor $executor): ProcOpenBasedFinder
{
return new HwPhysicalFinder();
return new HwPhysicalFinder($executor);
}
}
Loading

0 comments on commit b830f3c

Please sign in to comment.