Skip to content

Commit

Permalink
Consolidate HWFinder (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Dec 4, 2022
1 parent fc210d1 commit 1f1a2eb
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/HwFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,22 @@ public static function find(): ?int

if (is_resource($process)) {
// *nix (Linux, BSD and Mac)
$cores = (int) fgets($process);
$cores = self::countCpuCores(fgets($process));
pclose($process);

return $cores;
}

return null;
}

/**
* @internal
*
* @return positive-int|null
*/
public static function countCpuCores(string $process): ?int
{
return (int) $process;
}
}
49 changes: 49 additions & 0 deletions tests/HwFinderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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\CpuCounter\Test;

use Fidry\CpuCounter\HwFinder;
use PHPUnit\Framework\TestCase;

/**
* @covers \Fidry\CpuCounter\HwFinder
*
* @internal
*/
final class HwFinderTest extends TestCase
{
/**
* @dataProvider processProvider
*/
public function test_it_can_count_the_number_of_cpu_cores(
string $process,
?int $expected
): void {
$actual = HwFinder::countCpuCores($process);

self::assertSame($expected, $actual);
}

public static function processProvider(): iterable
{
// MyMachine™
yield 'example from an OSX machine' => [
<<<'EOF'
3

EOF,
3,
];
}
}

0 comments on commit 1f1a2eb

Please sign in to comment.