Skip to content

Commit

Permalink
test: Introduce a value object to make it easier to write test cases (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Aug 2, 2024
1 parent 3a0bcfd commit e49b6b0
Show file tree
Hide file tree
Showing 2 changed files with 245 additions and 224 deletions.
99 changes: 99 additions & 0 deletions tests/AvailableCpuCoresScenario.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?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;

use Fidry\CpuCoreCounter\Finder\CpuCoreFinder;
use Fidry\CpuCoreCounter\Finder\DummyCpuCoreFinder;

/**
* @internal
* @readonly
*/
final class AvailableCpuCoresScenario
{
/** @var list<CpuCoreFinder> */
public $finders;
/** @var array<string, string|null> */
public $environmentVariables;
/** @var positive-int|0 */
public $reservedCpus;
/** @var positive-int */
public $limit;
/** @var float|null */
public $loadLimitPerCore;
/** @var float|null */
public $systemLoadAverage;
/** @var positive-int */
public $expected;

/**
* @param list<CpuCoreFinder> $finders
* @param array<string, string|int|null> $environmentVariables
* @param positive-int|0 $reservedCpus
* @param positive-int $limit
* @param positive-int $expected
*/
public function __construct(
array $finders,
array $environmentVariables,
int $reservedCpus,
?int $limit,
?float $loadLimitPerCore,
?float $systemLoadAverage,
int $expected
) {
$this->finders = $finders;
$this->environmentVariables = $environmentVariables;
$this->reservedCpus = $reservedCpus;
$this->limit = $limit;
$this->loadLimitPerCore = $loadLimitPerCore;
$this->systemLoadAverage = $systemLoadAverage;
$this->expected = $expected;
}

/**
* @param positive-int|null $coresCountFound
* @param array<string, string|int|null> $environmentVariables
* @param positive-int|0 $reservedCpus
* @param positive-int $limit
* @param positive-int $expected
*
* @return array{self}
*/
public static function create(
?int $coresCountFound,
array $environmentVariables,
?int $reservedCpus,
?int $limit,
?float $loadLimitPerCore,
?float $systemLoadAverage,
int $expected
): array {
$finders = null === $coresCountFound
? []
: [new DummyCpuCoreFinder($coresCountFound)];

return [
new self(
$finders,
$environmentVariables,
$reservedCpus ?? 0,
$limit,
$loadLimitPerCore,
$systemLoadAverage ?? 0.,
$expected
),
];
}
}
Loading

0 comments on commit e49b6b0

Please sign in to comment.