Skip to content

Commit

Permalink
Move the dummy core finder from the test to the source directory (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Dec 7, 2022
1 parent 6184d0e commit 4a0413c
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,23 @@

declare(strict_types=1);

namespace Fidry\CpuCoreCounter\Test\Finder;

use Fidry\CpuCoreCounter\Finder\CpuCoreFinder;
namespace Fidry\CpuCoreCounter\Finder;

/**
* 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.
*/
final class DummyCpuCoreFinder implements CpuCoreFinder
{
/**
* @var int|null
* @var positive-int
*/
private $count;

public function __construct(?int $count)
/**
* @param positive-int $count
*/
public function __construct(int $count)
{
$this->count = $count;
}
Expand Down
25 changes: 25 additions & 0 deletions src/Finder/NullCpuCoreFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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\Finder;

/**
* This finder returns whatever value you gave to it. This is useful for testing.
*/
final class NullCpuCoreFinder implements CpuCoreFinder
{
public function find(): ?int
{
return null;
}
}
9 changes: 5 additions & 4 deletions tests/CpuCoreCounterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
use Exception;
use Fidry\CpuCoreCounter\CpuCoreCounter;
use Fidry\CpuCoreCounter\Finder\CpuCoreFinder;
use Fidry\CpuCoreCounter\Finder\DummyCpuCoreFinder;
use Fidry\CpuCoreCounter\Finder\NullCpuCoreFinder;
use Fidry\CpuCoreCounter\NumberOfCpuCoreNotFound;
use Fidry\CpuCoreCounter\Test\Finder\DummyCpuCoreFinder;
use PHPUnit\Framework\TestCase;
use function get_class;
use function is_array;
Expand Down Expand Up @@ -117,7 +118,7 @@ public static function cpuCoreFinderProvider(): iterable

yield 'single finder does not find a value' => [
[
new DummyCpuCoreFinder(null),
new NullCpuCoreFinder(),
],
$defaultException,
];
Expand All @@ -140,9 +141,9 @@ public static function cpuCoreFinderProvider(): iterable

return [
[
new DummyCpuCoreFinder(null),
new NullCpuCoreFinder(),
$finder,
new DummyCpuCoreFinder(null),
new NullCpuCoreFinder(),
new DummyCpuCoreFinder(11),
],
[$finder, 7],
Expand Down
32 changes: 32 additions & 0 deletions tests/Finder/DummyCpuCoreFinderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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\Finder;

use Fidry\CpuCoreCounter\Finder\DummyCpuCoreFinder;
use PHPUnit\Framework\TestCase;

/**
* @covers \Fidry\CpuCoreCounter\Finder\DummyCpuCoreFinder
*
* @internal
*/
final class DummyCpuCoreFinderTest extends TestCase
{
public function test_it_returns_the_number_of_cores_given(): void
{
$finder = new DummyCpuCoreFinder(5);

self::assertSame(5, $finder->find());
}
}
32 changes: 32 additions & 0 deletions tests/Finder/NullCpuCoreFinderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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\Finder;

use Fidry\CpuCoreCounter\Finder\NullCpuCoreFinder;
use PHPUnit\Framework\TestCase;

/**
* @covers \Fidry\CpuCoreCounter\Finder\NullCpuCoreFinder
*
* @internal
*/
final class NullCpuCoreFinderTest extends TestCase
{
public function test_it_returns_null(): void
{
$finder = new NullCpuCoreFinder();

self::assertNull($finder->find());
}
}

0 comments on commit 4a0413c

Please sign in to comment.