From 7a6740d11f299e119c708047626a2df190f085b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= <5175937+theofidry@users.noreply.github.com> Date: Thu, 8 Dec 2022 00:03:52 +0100 Subject: [PATCH] Introduce a diagnosis tool (#60) Introduce a way to diagnose a finder, i.e. get information about what a specific finder may find. Those are then aggregated and but run all by a diagnoser. The idea of this tool is to provide a quick insight on how each finder will operator. For example on my machine: ``` $ make diagnose ./bin/diagnose.php Running diagnosis... CpuInfoFinder: -------------------------------------------- The file "/proc/cpuinfo" could not be found. -------------------------------------------- DummyCpuCoreFinder(value=1): ---------------- Will return "1". ---------------- HwLogicalFinder: --------------------------------------------------------------------------------- Executed the command "sysctl -n hw.logicalcpu 2>&1" and got the following output: 8 --------------------------------------------------------------------------------- HwPhysicalFinder: ---------------------------------------------------------------------------------- Executed the command "sysctl -n hw.physicalcpu 2>&1" and got the following output: 8 ---------------------------------------------------------------------------------- NProcFinder(all=true): ---------------------------------------------------- The command "nproc --all" gave the following output: 8 ---------------------------------------------------- NProcFinder(all=false): ---------------------------------------------- The command "nproc" gave the following output: 8 ---------------------------------------------- NullCpuCoreFinder: ------------------- Will return "null". ------------------- WindowsWmicPhysicalFinder: -------------------------------------------------------------------------------------------------------------------------------------- Executed the command "wmic cpu get NumberOfProcessors 2>&1" which exited with a non-success exit code (127) with the following output: sh: wmic: command not found -------------------------------------------------------------------------------------------------------------------------------------- WindowsWmicLogicalFinder: --------------------------------------------------------------------------------------------------------------------------------------------- Executed the command "wmic cpu get NumberOfLogicalProcessors 2>&1" which exited with a non-success exit code (127) with the following output: sh: wmic: command not found --------------------------------------------------------------------------------------------------------------------------------------------- ``` It also allows to execute each finders: ``` $ make execute ./bin/execute.php Executing finders... CpuInfoFinder: NULL DummyCpuCoreFinder(value=1): 1 HwLogicalFinder: 8 HwPhysicalFinder: 8 NProcFinder(all=true): 8 NProcFinder(all=false): 8 NullCpuCoreFinder: NULL WindowsWmicPhysicalFinder: NULL WindowsWmicLogicalFinder: NULL ``` With this, it should be easy to test it on a specific system. --- Makefile | 10 ++ bin/diagnose.php | 21 +++ bin/execute.php | 21 +++ e2e/execute-finders.php | 29 +--- e2e/expected-output | 8 +- src/Diagnoser.php | 99 +++++++++++ src/Finder/CpuCoreFinder.php | 15 ++ src/Finder/CpuInfoFinder.php | 36 ++++ src/Finder/DummyCpuCoreFinder.php | 22 +++ src/Finder/FinderRegistry.php | 42 +++++ src/Finder/NProcFinder.php | 68 +++++++- src/Finder/NullCpuCoreFinder.php | 14 ++ src/Finder/PopenBasedFinder.php | 53 ++++++ tests/AutoReview/TODO | 2 +- tests/DiagnoserTest.php | 154 ++++++++++++++++++ tests/Finder/CpuInfoFinderTest.php | 17 +- tests/Finder/DummyCpuCoreFinderTest.php | 7 + tests/Finder/HwLogicalFinderTest.php | 7 + tests/Finder/HwPhysicalFinderTest.php | 7 + tests/Finder/LabeledFinder.php | 63 ------- tests/Finder/LabeledFinderTest.php | 46 ------ tests/Finder/NProcFinderTest.php | 24 +++ tests/Finder/NullCpuCoreFinderTest.php | 7 + tests/Finder/PopenBasedFinderTest.php | 7 + tests/Finder/WindowsWmicLogicalFinderTest.php | 7 + .../Finder/WindowsWmicPhysicalFinderTest.php | 7 + 26 files changed, 652 insertions(+), 141 deletions(-) create mode 100755 bin/diagnose.php create mode 100755 bin/execute.php create mode 100644 src/Diagnoser.php create mode 100644 src/Finder/FinderRegistry.php create mode 100644 tests/DiagnoserTest.php delete mode 100644 tests/Finder/LabeledFinder.php delete mode 100644 tests/Finder/LabeledFinderTest.php diff --git a/Makefile b/Makefile index 26bafe2..c7a846e 100644 --- a/Makefile +++ b/Makefile @@ -49,6 +49,16 @@ help: default: ## Runs the default task: CS fix and all the tests default: security cs autoreview test +.PHONY: diagnose +diagnose: ## Executes a diagnosis for all the finders +diagnose: vendor + ./bin/diagnose.php + +.PHONY: execute +execute: ## Executes all the finders +execute: vendor + ./bin/execute.php + .PHONY: phive phive: ## Updates a (registered) tool. E.g. make phive TOOL=infection phive: $(PHIVE_BIN) diff --git a/bin/diagnose.php b/bin/diagnose.php new file mode 100755 index 0000000..ec589e3 --- /dev/null +++ b/bin/diagnose.php @@ -0,0 +1,21 @@ +#!/usr/bin/env php + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +use Fidry\CpuCoreCounter\Diagnoser; +use Fidry\CpuCoreCounter\Finder\FinderRegistry; + +require_once __DIR__.'/../vendor/autoload.php'; + +echo 'Running diagnosis...'.PHP_EOL.PHP_EOL; +echo Diagnoser::diagnose(FinderRegistry::getAllVariants()).PHP_EOL; diff --git a/bin/execute.php b/bin/execute.php new file mode 100755 index 0000000..edadebb --- /dev/null +++ b/bin/execute.php @@ -0,0 +1,21 @@ +#!/usr/bin/env php + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +use Fidry\CpuCoreCounter\Diagnoser; +use Fidry\CpuCoreCounter\Finder\FinderRegistry; + +require_once __DIR__.'/../vendor/autoload.php'; + +echo 'Executing finders...'.PHP_EOL.PHP_EOL; +echo Diagnoser::execute(FinderRegistry::getAllVariants()).PHP_EOL; diff --git a/e2e/execute-finders.php b/e2e/execute-finders.php index 1e0aa09..d66d1d3 100644 --- a/e2e/execute-finders.php +++ b/e2e/execute-finders.php @@ -11,42 +11,23 @@ declare(strict_types=1); -use Fidry\CpuCoreCounter\Finder\CpuInfoFinder; -use Fidry\CpuCoreCounter\Finder\DummyCpuCoreFinder; -use Fidry\CpuCoreCounter\Finder\HwLogicalFinder; -use Fidry\CpuCoreCounter\Finder\HwPhysicalFinder; -use Fidry\CpuCoreCounter\Finder\NProcFinder; -use Fidry\CpuCoreCounter\Finder\NullCpuCoreFinder; -use Fidry\CpuCoreCounter\Finder\WindowsWmicLogicalFinder; -use Fidry\CpuCoreCounter\Finder\WindowsWmicPhysicalFinder; -use Fidry\CpuCoreCounter\Test\Finder\LabeledFinder; +use Fidry\CpuCoreCounter\Finder\CpuCoreFinder; +use Fidry\CpuCoreCounter\Finder\FinderRegistry; require_once __DIR__.'/../vendor/autoload.php'; -$finders = [ - new LabeledFinder(new CpuInfoFinder()), - new LabeledFinder(new DummyCpuCoreFinder(11)), - new LabeledFinder(new HwLogicalFinder()), - new LabeledFinder(new HwPhysicalFinder()), - new LabeledFinder(new NProcFinder(), 'NProcFinder{all=true}'), - new LabeledFinder(new NProcFinder(false), 'NProcFinder{all=false}'), - new LabeledFinder(new NullCpuCoreFinder()), - new LabeledFinder(new WindowsWmicLogicalFinder()), - new LabeledFinder(new WindowsWmicPhysicalFinder()), -]; - $results = array_map( - static function (LabeledFinder $finder): string { + static function (CpuCoreFinder $finder): string { return implode( '', [ - $finder->getLabel(), + $finder->toString(), ': ', null !== $finder->find() ? '.' : 'F', ] ); }, - $finders + FinderRegistry::getAllVariants() ); echo implode(PHP_EOL, $results).PHP_EOL; diff --git a/e2e/expected-output b/e2e/expected-output index 51d48f9..bfe9400 100644 --- a/e2e/expected-output +++ b/e2e/expected-output @@ -1,9 +1,9 @@ CpuInfoFinder: F -DummyCpuCoreFinder: . +DummyCpuCoreFinder(value=1): . HwLogicalFinder: . HwPhysicalFinder: . -NProcFinder{all=true}: . -NProcFinder{all=false}: . +NProcFinder(all=true): . +NProcFinder(all=false): . NullCpuCoreFinder: F -WindowsWmicLogicalFinder: F WindowsWmicPhysicalFinder: F +WindowsWmicLogicalFinder: F diff --git a/src/Diagnoser.php b/src/Diagnoser.php new file mode 100644 index 0000000..8495cbe --- /dev/null +++ b/src/Diagnoser.php @@ -0,0 +1,99 @@ + + * + * 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; + +use Fidry\CpuCoreCounter\Finder\CpuCoreFinder; +use function array_map; +use function explode; +use function implode; +use function max; +use function str_repeat; +use const PHP_EOL; + +/** + * Utility to debug. + * + * @private + */ +final class Diagnoser +{ + /** + * Provides an aggregated diagnosis based on each finders diagnosis. + * + * @param list $finders + */ + public static function diagnose(array $finders): string + { + $diagnoses = array_map( + static fn (CpuCoreFinder $finder): string => self::diagnoseFinder($finder), + $finders + ); + + return implode(PHP_EOL, $diagnoses); + } + + /** + * Executes each finders. + * + * @param list $finders + */ + public static function execute(array $finders): string + { + $diagnoses = array_map( + static function (CpuCoreFinder $finder): string { + $coresCount = $finder->find(); + + return implode( + '', + [ + $finder->toString(), + ': ', + null === $coresCount ? 'NULL' : $coresCount, + ] + ); + }, + $finders + ); + + return implode(PHP_EOL, $diagnoses); + } + + private static function diagnoseFinder(CpuCoreFinder $finder): string + { + $diagnosis = $finder->diagnose(); + + $maxLineLength = max( + array_map( + 'strlen', + explode(PHP_EOL, $diagnosis) + ) + ); + + $separator = str_repeat('-', $maxLineLength); + + return implode( + '', + [ + $finder->toString().':'.PHP_EOL, + $separator.PHP_EOL, + $diagnosis.PHP_EOL, + $separator.PHP_EOL, + ] + ); + } + + private function __construct() + { + } +} diff --git a/src/Finder/CpuCoreFinder.php b/src/Finder/CpuCoreFinder.php index d62c469..edb40e8 100644 --- a/src/Finder/CpuCoreFinder.php +++ b/src/Finder/CpuCoreFinder.php @@ -16,7 +16,22 @@ interface CpuCoreFinder { /** + * Provides an explanation which may offer some insight as to what the finder + * will be able to find. + * + * This is practical to have an idea of what each finder will find collect + * information for the unit tests, since integration tests are quite complicated + * as dependent on complex infrastructures. + */ + public function diagnose(): string; + + /** + * Find the number of CPU cores. If it could not find it, returns null. The + * means used to find the cores are at the implementation discretion. + * * @return positive-int|null */ public function find(): ?int; + + public function toString(): string; } diff --git a/src/Finder/CpuInfoFinder.php b/src/Finder/CpuInfoFinder.php index 61aeadc..6171410 100644 --- a/src/Finder/CpuInfoFinder.php +++ b/src/Finder/CpuInfoFinder.php @@ -15,7 +15,11 @@ use function file_get_contents; use function is_file; +use function sprintf; +use function strrpos; +use function substr; use function substr_count; +use const PHP_EOL; /** * Find the number of CPU cores looking up at the cpuinfo file which is available @@ -28,6 +32,32 @@ final class CpuInfoFinder implements CpuCoreFinder { private const CPU_INFO_PATH = '/proc/cpuinfo'; + public function diagnose(): string + { + if (!is_file(self::CPU_INFO_PATH)) { + return sprintf( + 'The file "%s" could not be found.', + self::CPU_INFO_PATH + ); + } + + $cpuInfo = file_get_contents(self::CPU_INFO_PATH); + + if (false === $cpuInfo) { + return sprintf( + 'Could not get the content of the file "%s".', + self::CPU_INFO_PATH + ); + } + + return sprintf( + 'Found the file "%s" with the content:%s%s', + self::CPU_INFO_PATH, + PHP_EOL, + $cpuInfo + ); + } + /** * @return positive-int|null */ @@ -38,6 +68,12 @@ public function find(): ?int return null === $cpuInfo ? null : self::countCpuCores($cpuInfo); } + public function toString(): string + { + /** @phpstan-ignore-next-line */ + return substr(__CLASS__, strrpos(__CLASS__, '\\') + 1); + } + private static function getCpuInfo(): ?string { if (!is_file(self::CPU_INFO_PATH)) { diff --git a/src/Finder/DummyCpuCoreFinder.php b/src/Finder/DummyCpuCoreFinder.php index a70566f..050b8b0 100644 --- a/src/Finder/DummyCpuCoreFinder.php +++ b/src/Finder/DummyCpuCoreFinder.php @@ -13,6 +13,10 @@ namespace Fidry\CpuCoreCounter\Finder; +use function sprintf; +use function strrpos; +use function substr; + /** * This finder returns whatever value you gave to it. This is useful for testing * or as a fallback to avoid to catch the NumberOfCpuCoreNotFound exception. @@ -24,6 +28,14 @@ final class DummyCpuCoreFinder implements CpuCoreFinder */ private $count; + public function diagnose(): string + { + return sprintf( + 'Will return "%d".', + $this->count + ); + } + /** * @param positive-int $count */ @@ -36,4 +48,14 @@ public function find(): ?int { return $this->count; } + + public function toString(): string + { + return sprintf( + '%s(value=%d)', + /** @phpstan-ignore-next-line */ + substr(__CLASS__, strrpos(__CLASS__, '\\') + 1), + $this->count + ); + } } diff --git a/src/Finder/FinderRegistry.php b/src/Finder/FinderRegistry.php new file mode 100644 index 0000000..9bd0dcb --- /dev/null +++ b/src/Finder/FinderRegistry.php @@ -0,0 +1,42 @@ + + * + * 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\Finder; + +/** + * @private + */ +final class FinderRegistry +{ + /** + * @return list List of all the known finders with all their variants. + */ + public static function getAllVariants(): array + { + return [ + new CpuInfoFinder(), + new DummyCpuCoreFinder(1), + new HwLogicalFinder(), + new HwPhysicalFinder(), + new NProcFinder(true), + new NProcFinder(false), + new NullCpuCoreFinder(), + new WindowsWmicPhysicalFinder(), + new WindowsWmicLogicalFinder(), + ]; + } + + private function __construct() + { + } +} diff --git a/src/Finder/NProcFinder.php b/src/Finder/NProcFinder.php index 5ac08fb..b0b636e 100644 --- a/src/Finder/NProcFinder.php +++ b/src/Finder/NProcFinder.php @@ -18,8 +18,12 @@ use function filter_var; use function function_exists; use function is_int; +use function sprintf; +use function strrpos; +use function substr; use function trim; use const FILTER_VALIDATE_INT; +use const PHP_EOL; /** * The number of (logical) cores. @@ -29,6 +33,8 @@ */ final class NProcFinder implements CpuCoreFinder { + private const DETECT_NPROC_COMMAND = 'command -v nproc'; + /** * @var bool */ @@ -42,6 +48,54 @@ public function __construct(bool $all = true) $this->all = $all; } + public function diagnose(): string + { + if (!function_exists('shell_exec')) { + return 'The function "shell_exec" is not available.'; + } + + try { + $commandNproc = ShellExec::execute(self::DETECT_NPROC_COMMAND); + } catch (ExecException $nprocCommandNotFound) { + return sprintf( + 'The command nproc was not detected. The command "%s" failed: %s', + self::DETECT_NPROC_COMMAND, + $nprocCommandNotFound->getMessage() + ); + } + + if ('' === trim($commandNproc)) { + return sprintf( + 'The command nproc was not detected. The command "%s" gave an empty output.', + self::DETECT_NPROC_COMMAND + ); + } + + // Redirect the STDERR to the STDOUT since popen cannot capture the + // STDERR. + // We could use proc_open but this would be a greater difference between + // the command we really execute when using the finder and what we will + // diagnose. + $nprocCommand = 'nproc'.($this->all ? ' --all' : '').' 2>&1'; + + try { + $nproc = ShellExec::execute($nprocCommand); + } catch (ExecException $nprocFailed) { + return sprintf( + 'The command "%s" failed: %s', + $nprocCommand, + $nprocFailed->getMessage() + ); + } + + return sprintf( + 'The command "%s" gave the following output:%s%s', + $nprocCommand, + PHP_EOL, + $nproc + ); + } + /** * @return positive-int|null */ @@ -60,6 +114,16 @@ public function find(): ?int return self::countCpuCores($nproc); } + public function toString(): string + { + return sprintf( + '%s(all=%s)', + /** @phpstan-ignore-next-line */ + substr(__CLASS__, strrpos(__CLASS__, '\\') + 1), + $this->all ? 'true' : 'false' + ); + } + private static function supportsNproc(): bool { if (!function_exists('shell_exec')) { @@ -67,8 +131,8 @@ private static function supportsNproc(): bool } try { - $commandNproc = ShellExec::execute('command -v nproc'); - } catch (ExecException $noNprocCommand) { + $commandNproc = ShellExec::execute(self::DETECT_NPROC_COMMAND); + } catch (ExecException $nprocCommandNotFound) { return false; } diff --git a/src/Finder/NullCpuCoreFinder.php b/src/Finder/NullCpuCoreFinder.php index 96c7b82..6dc8fd8 100644 --- a/src/Finder/NullCpuCoreFinder.php +++ b/src/Finder/NullCpuCoreFinder.php @@ -13,13 +13,27 @@ namespace Fidry\CpuCoreCounter\Finder; +use function strrpos; +use function substr; + /** * This finder returns whatever value you gave to it. This is useful for testing. */ final class NullCpuCoreFinder implements CpuCoreFinder { + public function diagnose(): string + { + return 'Will return "null".'; + } + public function find(): ?int { return null; } + + public function toString(): string + { + /** @phpstan-ignore-next-line */ + return substr(__CLASS__, strrpos(__CLASS__, '\\') + 1); + } } diff --git a/src/Finder/PopenBasedFinder.php b/src/Finder/PopenBasedFinder.php index 50a2f99..9c5203c 100644 --- a/src/Finder/PopenBasedFinder.php +++ b/src/Finder/PopenBasedFinder.php @@ -20,10 +20,55 @@ use function is_resource; use function pclose; use function popen; +use function sprintf; +use function strrpos; +use function substr; use const FILTER_VALIDATE_INT; +use const PHP_EOL; abstract class PopenBasedFinder implements CpuCoreFinder { + public function diagnose(): string + { + if (!function_exists('popen')) { + return 'The function "popen" is not available.'; + } + + // Redirect the STDERR to the STDOUT since popen cannot capture the + // STDERR. + // We could use proc_open but this would be a greater difference between + // the command we really execute when using the finder and what we will + // diagnose. + $command = $this->getCommand().' 2>&1'; + + $process = popen($command, 'rb'); + + if (!is_resource($process)) { + return sprintf( + 'Could not execute the function "popen" with the command "%s".', + $command, + ); + } + + $processResult = fgets($process); + $exitCode = pclose($process); + + return 0 === $exitCode + ? sprintf( + 'Executed the command "%s" and got the following output:%s%s', + $command, + PHP_EOL, + $processResult + ) + : sprintf( + 'Executed the command "%s" which exited with a non-success exit code (%d) with the following output:%s%s', + $command, + $exitCode, + PHP_EOL, + $processResult + ); + } + /** * @return positive-int|null */ @@ -47,6 +92,14 @@ public function find(): ?int : self::countCpuCores($processResult); } + public function toString(): string + { + $class = static::class; + + /** @phpstan-ignore-next-line */ + return substr($class, strrpos($class, '\\') + 1); + } + /** * @internal * diff --git a/tests/AutoReview/TODO b/tests/AutoReview/TODO index 9fa49e5..d68ef57 100644 --- a/tests/AutoReview/TODO +++ b/tests/AutoReview/TODO @@ -1,4 +1,4 @@ Move MakefileTest here -Add test to ensure all finders are listed in execute-finders +Add test to ensure all finders are listed in FinderRegistry Add test to ensure each class has a test case Add test to ensure the test class matches its corresponding class diff --git a/tests/DiagnoserTest.php b/tests/DiagnoserTest.php new file mode 100644 index 0000000..08c65d3 --- /dev/null +++ b/tests/DiagnoserTest.php @@ -0,0 +1,154 @@ + + * + * 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; + +use Fidry\CpuCoreCounter\Diagnoser; +use Fidry\CpuCoreCounter\Finder\CpuInfoFinder; +use Fidry\CpuCoreCounter\Finder\DummyCpuCoreFinder; +use Fidry\CpuCoreCounter\Finder\NullCpuCoreFinder; +use PHPUnit\Framework\TestCase; + +/** + * @covers \Fidry\CpuCoreCounter\Diagnoser + * + * @internal + */ +final class DiagnoserTest extends TestCase +{ + /** + * @dataProvider diagnosisProvider + * + * @param list $finders + */ + public function test_it_can_run_a_diagnosis(array $finders, string $expected): void + { + $actual = Diagnoser::diagnose($finders); + + self::assertSame($expected, $actual); + } + + public static function diagnosisProvider(): iterable + { + yield 'no finder' => [ + [], + '', + ]; + + yield 'single finder' => [ + [new NullCpuCoreFinder()], + <<<'EOF' +NullCpuCoreFinder: +------------------- +Will return "null". +------------------- + +EOF + ]; + + yield 'multiple finders' => [ + [ + new NullCpuCoreFinder(), + new NullCpuCoreFinder(), + ], + <<<'EOF' +NullCpuCoreFinder: +------------------- +Will return "null". +------------------- + +NullCpuCoreFinder: +------------------- +Will return "null". +------------------- + +EOF + ]; + + yield 'multiple finders with variable outputs' => [ + [ + new NullCpuCoreFinder(), + new DummyCpuCoreFinder(1000000), + new NullCpuCoreFinder(), + ], + <<<'EOF' +NullCpuCoreFinder: +------------------- +Will return "null". +------------------- + +DummyCpuCoreFinder(value=1000000): +---------------------- +Will return "1000000". +---------------------- + +NullCpuCoreFinder: +------------------- +Will return "null". +------------------- + +EOF + ]; + } + + /** + * @dataProvider findersToExecuteProvider + * + * @param list $finders + */ + public function test_it_can_execute_finders(array $finders, string $expected): void + { + $actual = Diagnoser::execute($finders); + + self::assertSame($expected, $actual); + } + + public static function findersToExecuteProvider(): iterable + { + yield 'no finder' => [ + [], + '', + ]; + + yield 'single finder' => [ + [new NullCpuCoreFinder()], + <<<'EOF' +NullCpuCoreFinder: NULL +EOF + ]; + + yield 'multiple finders' => [ + [ + new NullCpuCoreFinder(), + new NullCpuCoreFinder(), + ], + <<<'EOF' +NullCpuCoreFinder: NULL +NullCpuCoreFinder: NULL +EOF + ]; + + yield 'multiple finders with variable outputs' => [ + [ + new NullCpuCoreFinder(), + new DummyCpuCoreFinder(2), + new NullCpuCoreFinder(), + ], + <<<'EOF' +NullCpuCoreFinder: NULL +DummyCpuCoreFinder(value=2): 2 +NullCpuCoreFinder: NULL +EOF + ]; + } +} diff --git a/tests/Finder/CpuInfoFinderTest.php b/tests/Finder/CpuInfoFinderTest.php index 644dcb8..d8391d4 100644 --- a/tests/Finder/CpuInfoFinderTest.php +++ b/tests/Finder/CpuInfoFinderTest.php @@ -23,6 +23,21 @@ */ final class CpuInfoFinderTest extends TestCase { + /** + * @var CpuInfoFinder + */ + private $finder; + + protected function setUp(): void + { + $this->finder = new CpuInfoFinder(); + } + + public function test_it_can_describe_itself(): void + { + self::assertSame('CpuInfoFinder', $this->finder->toString()); + } + /** * @dataProvider cpuInfoProvider */ @@ -30,7 +45,7 @@ public function test_it_can_count_the_number_of_cpu_cores( string $cpuInfo, ?int $expected ): void { - $actual = CpuInfoFinder::countCpuCores($cpuInfo); + $actual = $this->finder::countCpuCores($cpuInfo); self::assertSame($expected, $actual); } diff --git a/tests/Finder/DummyCpuCoreFinderTest.php b/tests/Finder/DummyCpuCoreFinderTest.php index 42f535a..d52faad 100644 --- a/tests/Finder/DummyCpuCoreFinderTest.php +++ b/tests/Finder/DummyCpuCoreFinderTest.php @@ -29,4 +29,11 @@ public function test_it_returns_the_number_of_cores_given(): void self::assertSame(5, $finder->find()); } + + public function test_it_can_describe_itself(): void + { + $finder = new DummyCpuCoreFinder(5); + + self::assertSame('DummyCpuCoreFinder(value=5)', $finder->toString()); + } } diff --git a/tests/Finder/HwLogicalFinderTest.php b/tests/Finder/HwLogicalFinderTest.php index 0a71015..9278957 100644 --- a/tests/Finder/HwLogicalFinderTest.php +++ b/tests/Finder/HwLogicalFinderTest.php @@ -23,6 +23,13 @@ */ final class HwLogicalFinderTest extends PopenBasedFinderTestCase { + public function test_it_can_describe_itself(): void + { + $finder = new HwLogicalFinder(); + + self::assertSame('HwLogicalFinder', $finder->toString()); + } + protected function getFinder(): PopenBasedFinder { return new HwLogicalFinder(); diff --git a/tests/Finder/HwPhysicalFinderTest.php b/tests/Finder/HwPhysicalFinderTest.php index 6d72c19..72d6057 100644 --- a/tests/Finder/HwPhysicalFinderTest.php +++ b/tests/Finder/HwPhysicalFinderTest.php @@ -23,6 +23,13 @@ */ final class HwPhysicalFinderTest extends PopenBasedFinderTestCase { + public function test_it_can_describe_itself(): void + { + $finder = new HwPhysicalFinder(); + + self::assertSame('HwPhysicalFinder', $finder->toString()); + } + protected function getFinder(): PopenBasedFinder { return new HwPhysicalFinder(); diff --git a/tests/Finder/LabeledFinder.php b/tests/Finder/LabeledFinder.php deleted file mode 100644 index c97d2a9..0000000 --- a/tests/Finder/LabeledFinder.php +++ /dev/null @@ -1,63 +0,0 @@ - - * - * 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\Finder; - -use Fidry\CpuCoreCounter\Finder\CpuCoreFinder; -use function get_class; - -final class LabeledFinder implements CpuCoreFinder -{ - /** - * @var string - */ - private $label; - - /** - * @var CpuCoreFinder - */ - private $decoratedFinder; - - public function __construct( - CpuCoreFinder $decoratedFinder, - ?string $label = null - ) { - $this->decoratedFinder = $decoratedFinder; - $this->label = $label ?? self::getShortClassName($decoratedFinder); - } - - public function getLabel(): string - { - return $this->label; - } - - public function find(): ?int - { - return $this->decoratedFinder->find(); - } - - /** - * Strips the namespace off a fully-qualified class name. E.g.: - * "Acme\Foo\Bar" -> "Bar". - */ - private static function getShortClassName(CpuCoreFinder $finder): string - { - $className = get_class($finder); - - if (false !== ($pos = mb_strrpos($className, '\\'))) { - return substr($className, $pos + 1); - } - - return $className; - } -} diff --git a/tests/Finder/LabeledFinderTest.php b/tests/Finder/LabeledFinderTest.php deleted file mode 100644 index 8a48861..0000000 --- a/tests/Finder/LabeledFinderTest.php +++ /dev/null @@ -1,46 +0,0 @@ - - * - * 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\Finder; - -use Fidry\CpuCoreCounter\Finder\DummyCpuCoreFinder; -use PHPUnit\Framework\TestCase; - -/** - * @covers \Fidry\CpuCoreCounter\Test\Finder\LabeledFinder - * - * @internal - */ -final class LabeledFinderTest extends TestCase -{ - public function test_it_decorates_a_finder_with_its_class_short_name_as_a_label(): void - { - $finder = new LabeledFinder( - new DummyCpuCoreFinder(7) - ); - - self::assertSame('DummyCpuCoreFinder', $finder->getLabel()); - self::assertSame(7, $finder->find()); - } - - public function test_it_decorates_a_finder_with_the_given_label_when_specified(): void - { - $finder = new LabeledFinder( - new DummyCpuCoreFinder(7), - 'Foo' - ); - - self::assertSame('Foo', $finder->getLabel()); - self::assertSame(7, $finder->find()); - } -} diff --git a/tests/Finder/NProcFinderTest.php b/tests/Finder/NProcFinderTest.php index b319f78..1d0bca6 100644 --- a/tests/Finder/NProcFinderTest.php +++ b/tests/Finder/NProcFinderTest.php @@ -13,6 +13,7 @@ namespace Fidry\CpuCoreCounter\Test\Finder; +use Fidry\CpuCoreCounter\Finder\CpuCoreFinder; use Fidry\CpuCoreCounter\Finder\NProcFinder; use PHPUnit\Framework\TestCase; @@ -23,6 +24,29 @@ */ final class NProcFinderTest extends TestCase { + /** + * @dataProvider finderProvider + */ + public function test_it_can_describe_itself(CpuCoreFinder $finder, string $expected): void + { + $actual = $finder->toString(); + + self::assertSame($expected, $actual); + } + + public static function finderProvider(): iterable + { + yield [ + new NProcFinder(true), + 'NProcFinder(all=true)', + ]; + + yield [ + new NProcFinder(false), + 'NProcFinder(all=false)', + ]; + } + /** * @dataProvider nprocProvider */ diff --git a/tests/Finder/NullCpuCoreFinderTest.php b/tests/Finder/NullCpuCoreFinderTest.php index a96c4de..13954ca 100644 --- a/tests/Finder/NullCpuCoreFinderTest.php +++ b/tests/Finder/NullCpuCoreFinderTest.php @@ -29,4 +29,11 @@ public function test_it_returns_null(): void self::assertNull($finder->find()); } + + public function test_it_can_describe_itself(): void + { + $finder = new NullCpuCoreFinder(); + + self::assertSame('NullCpuCoreFinder', $finder->toString()); + } } diff --git a/tests/Finder/PopenBasedFinderTest.php b/tests/Finder/PopenBasedFinderTest.php index 00065ca..c1c51f5 100644 --- a/tests/Finder/PopenBasedFinderTest.php +++ b/tests/Finder/PopenBasedFinderTest.php @@ -79,4 +79,11 @@ public static function popenFgetsProvider(): iterable null, ]; } + + public function test_it_can_describe_itself(): void + { + $finder = new DummyPopenBasedFinder(); + + self::assertSame('DummyPopenBasedFinder', $finder->toString()); + } } diff --git a/tests/Finder/WindowsWmicLogicalFinderTest.php b/tests/Finder/WindowsWmicLogicalFinderTest.php index 8ef8235..14ef2ff 100644 --- a/tests/Finder/WindowsWmicLogicalFinderTest.php +++ b/tests/Finder/WindowsWmicLogicalFinderTest.php @@ -23,6 +23,13 @@ */ final class WindowsWmicLogicalFinderTest extends PopenBasedFinderTestCase { + public function test_it_can_describe_itself(): void + { + $finder = new WindowsWmicLogicalFinder(); + + self::assertSame('WindowsWmicLogicalFinder', $finder->toString()); + } + protected function getFinder(): PopenBasedFinder { return new WindowsWmicLogicalFinder(); diff --git a/tests/Finder/WindowsWmicPhysicalFinderTest.php b/tests/Finder/WindowsWmicPhysicalFinderTest.php index dfa2f19..106c12e 100644 --- a/tests/Finder/WindowsWmicPhysicalFinderTest.php +++ b/tests/Finder/WindowsWmicPhysicalFinderTest.php @@ -23,6 +23,13 @@ */ final class WindowsWmicPhysicalFinderTest extends PopenBasedFinderTestCase { + public function test_it_can_describe_itself(): void + { + $finder = new WindowsWmicPhysicalFinder(); + + self::assertSame('WindowsWmicPhysicalFinder', $finder->toString()); + } + protected function getFinder(): PopenBasedFinder { return new WindowsWmicPhysicalFinder();