Skip to content

Commit b90c12f

Browse files
authored
fix: improve validation in Priority::withFairnessWeight and add unit tests (#719)
1 parent 1ec2e61 commit b90c12f

2 files changed

Lines changed: 68 additions & 3 deletions

File tree

src/Common/Priority.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,11 @@ public function withFairnessKey(string $value): self
136136
*/
137137
public function withFairnessWeight(float $value): self
138138
{
139-
$value < 0.001 or $value > 1000.0 and throw new \InvalidArgumentException(
140-
'FairnessWeight must be in the range [0.001, 1000].',
141-
);
139+
if ($value < 0.001 || $value > 1000.0) {
140+
throw new \InvalidArgumentException(
141+
'FairnessWeight must be in the range [0.001, 1000].',
142+
);
143+
}
142144
$clone = clone $this;
143145
$clone->fairnessWeight = $value;
144146
return $clone;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Temporal\Tests\Unit\Common;
6+
7+
use PHPUnit\Framework\Attributes\DataProvider;
8+
use PHPUnit\Framework\TestCase;
9+
use Temporal\Common\Priority;
10+
11+
final class PriorityTestCase extends TestCase
12+
{
13+
public function testWithFairnessWeightValidValue(): void
14+
{
15+
$priority = Priority::new()->withFairnessWeight(1.0);
16+
17+
$this->assertSame(1.0, $priority->fairnessWeight);
18+
}
19+
20+
public function testWithFairnessWeightMinBoundary(): void
21+
{
22+
$priority = Priority::new()->withFairnessWeight(0.001);
23+
24+
$this->assertSame(0.001, $priority->fairnessWeight);
25+
}
26+
27+
public function testWithFairnessWeightMaxBoundary(): void
28+
{
29+
$priority = Priority::new()->withFairnessWeight(1000.0);
30+
31+
$this->assertSame(1000.0, $priority->fairnessWeight);
32+
}
33+
34+
#[DataProvider('invalidFairnessWeightProvider')]
35+
public function testWithFairnessWeightThrowsOnInvalidValue(float $value): void
36+
{
37+
$this->expectException(\InvalidArgumentException::class);
38+
$this->expectExceptionMessage('FairnessWeight must be in the range [0.001, 1000].');
39+
40+
Priority::new()->withFairnessWeight($value);
41+
}
42+
43+
public static function invalidFairnessWeightProvider(): iterable
44+
{
45+
return [
46+
'zero' => [0.0],
47+
'below minimum' => [0.0009],
48+
'negative' => [-1.0],
49+
'above maximum' => [1000.1],
50+
'large value' => [9999.0],
51+
];
52+
}
53+
54+
public function testWithFairnessWeightIsImmutable(): void
55+
{
56+
$original = Priority::new();
57+
$modified = $original->withFairnessWeight(5.0);
58+
59+
$this->assertNotSame($original, $modified);
60+
$this->assertSame(0.0, $original->fairnessWeight);
61+
$this->assertSame(5.0, $modified->fairnessWeight);
62+
}
63+
}

0 commit comments

Comments
 (0)