Skip to content

Commit 3a23cbf

Browse files
authored
Add test for CpuCoreCounter (#192)
1 parent 436871f commit 3a23cbf

5 files changed

+92
-3
lines changed

phpunit.xml.dist

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
</php>
1414

1515
<extensions>
16+
<extension class="Webmozarts\Console\Parallelization\PHPUnit\ResetCpuCounterListener"/>
1617
<extension class="Webmozarts\StrictPHPUnit\StrictPHPUnitExtension"/>
1718
</extensions>
1819

src/CpuCoreCounter.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace Webmozarts\Console\Parallelization;
1515

1616
use Webmozart\Assert\Assert;
17-
use function getenv;
1817

1918
/**
2019
* @internal
@@ -37,7 +36,7 @@ public static function getNumberOfCpuCores(): int
3736
return self::$count = 1;
3837
}
3938

40-
$count = getenv('WEBMOZARTS_CONSOLE_PARALLELIZATION_CPU_COUNT');
39+
$count = $_ENV['WEBMOZARTS_CONSOLE_PARALLELIZATION_CPU_COUNT'];
4140

4241
if (false !== $count) {
4342
Assert::numeric($count);

tests/CpuCoreCounterTest.php

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Webmozarts Console Parallelization package.
5+
*
6+
* (c) Webmozarts GmbH <office@webmozarts.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Webmozarts\Console\Parallelization;
15+
16+
use PHPUnit\Framework\TestCase;
17+
18+
/**
19+
* @covers \Webmozarts\Console\Parallelization\CpuCoreCounter
20+
*
21+
* @internal
22+
*/
23+
final class CpuCoreCounterTest extends TestCase
24+
{
25+
public function test_can_get_the_number_of_cpu_cores(): void
26+
{
27+
$cpuCoresCount = CpuCoreCounter::getNumberOfCpuCores();
28+
29+
self::assertGreaterThan(0, $cpuCoresCount);
30+
}
31+
32+
public function test_can_get_the_number_of_cpu_cores_defined(): void
33+
{
34+
$cleanUp = EnvironmentVariables::setVariables([
35+
'WEBMOZARTS_CONSOLE_PARALLELIZATION_CPU_COUNT' => '7',
36+
]);
37+
38+
$cpuCoresCount = CpuCoreCounter::getNumberOfCpuCores();
39+
40+
$cleanUp();
41+
42+
self::assertSame(7, $cpuCoresCount);
43+
}
44+
}

tests/EnvironmentVariables.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private static function setVariable(string $name, string $value): callable
5757
if (array_key_exists($name, $_ENV)) {
5858
$previousValue = $_ENV[$name];
5959

60-
$restoreEnv = static fn () => $_SERVER[$name] = $previousValue;
60+
$restoreEnv = static fn () => $_ENV[$name] = $previousValue;
6161
} else {
6262
$restoreEnv = static function () use ($name): void {
6363
unset($_ENV[$name]);
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Webmozarts Console Parallelization package.
5+
*
6+
* (c) Webmozarts GmbH <office@webmozarts.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Webmozarts\Console\Parallelization\PHPUnit;
15+
16+
use PHPUnit\Runner\AfterTestHook;
17+
use ReflectionClass;
18+
use ReflectionProperty;
19+
use Webmozarts\Console\Parallelization\CpuCoreCounter;
20+
21+
final class ResetCpuCounterListener implements AfterTestHook
22+
{
23+
private static ?ReflectionProperty $countReflection;
24+
25+
public function executeAfterTest(string $test, float $time): void
26+
{
27+
self::resetCounter();
28+
}
29+
30+
private static function getCountReflection(): ReflectionProperty
31+
{
32+
if (!isset(self::$countReflection)) {
33+
self::$countReflection = (new ReflectionClass(CpuCoreCounter::class))->getProperty('count');
34+
self::$countReflection->setAccessible(true);
35+
}
36+
37+
return self::$countReflection;
38+
}
39+
40+
private static function resetCounter(): void
41+
{
42+
$countReflection = self::getCountReflection();
43+
$countReflection->setValue(new CpuCoreCounter(), null);
44+
}
45+
}

0 commit comments

Comments
 (0)