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

Throw exception upon failure instead of arbitrary default value #21

Merged
merged 3 commits into from
Dec 4, 2022
Merged
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
8 changes: 6 additions & 2 deletions src/CpuCoreCounter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ final class CpuCoreCounter
private int $count;

/**
* @param list<CpuCoreFinder> $finders
* @param list<CpuCoreFinder>|null $finders
*/
public function __construct(?array $finders = null)
{
$this->finders = $finders ?? self::getDefaultFinders();
}

/**
* @throws NumberOfCpuCoreNotFound
*
* @return positive-int
*/
public function getCount(): int
Expand All @@ -47,6 +49,8 @@ public function getCount(): int
}

/**
* @throws NumberOfCpuCoreNotFound
*
* @return positive-int
*/
private function findCount(): int
Expand All @@ -63,7 +67,7 @@ private function findCount(): int
}
}

return 2;
throw NumberOfCpuCoreNotFound::create();
}

/**
Expand Down
26 changes: 26 additions & 0 deletions src/NumberOfCpuCoreNotFound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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;

use RuntimeException;

final class NumberOfCpuCoreNotFound extends RuntimeException
{
public static function create(): self
{
return new self(
'Could not find the number of CPU cores available.',
);
}
}
29 changes: 24 additions & 5 deletions tests/CpuCoreCounterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@

namespace Fidry\CpuCounter\Test;

use Exception;
use Fidry\CpuCounter\CpuCoreCounter;
use Fidry\CpuCounter\NumberOfCpuCoreNotFound;
use PHPUnit\Framework\TestCase;
use function get_class;
use function is_int;

/**
* @covers \Fidry\CpuCounter\CpuCoreCounter
Expand All @@ -34,23 +38,38 @@ public function test_it_can_get_the_number_of_cpu_cores(): void
* @dataProvider cpuCoreFinderProvider
*
* @param list<CpuCoreCounter> $finders
* @param int|Exception $expected
*/
public function test_it_can_get_the_number_of_cpu_cores_based_on_the_registered_finders(
array $finders,
int $expected
$expected
): void {
$counter = new CpuCoreCounter($finders);

$actual = $counter->getCount();
if (is_int($expected)) {
$actual = $counter->getCount();

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

return;
}

// Sanity check
self::assertTrue($expected instanceof Exception);

$this->expectException(get_class($expected));
$this->expectExceptionMessage($expected->getMessage());

$counter->getCount();
}

public static function cpuCoreFinderProvider(): iterable
{
$defaultException = NumberOfCpuCoreNotFound::create();

yield 'no finder' => [
[],
2,
$defaultException,
];

yield 'single finder finds a value' => [
Expand All @@ -64,7 +83,7 @@ public static function cpuCoreFinderProvider(): iterable
[
new DummyCpuCoreFinder(null),
],
2,
$defaultException,
];

yield 'multiple finders find a value' => [
Expand Down