diff --git a/src/Mock/Generation/Value/Primitive/RandomNumberGenerator.php b/src/Mock/Generation/Value/Primitive/RandomNumberGenerator.php index 7f11ddb..c27fc2d 100644 --- a/src/Mock/Generation/Value/Primitive/RandomNumberGenerator.php +++ b/src/Mock/Generation/Value/Primitive/RandomNumberGenerator.php @@ -32,8 +32,8 @@ public function generateValue(TypeInterface $type): ?float private function generateFloatValue(NumberType $type): float { - $minimum = $type->minimum ?? 0; - $maximum = $type->maximum ?? mt_getrandmax(); + $minimum = (float) ($type->minimum ?? -mt_getrandmax() / 2); + $maximum = (float) ($type->maximum ?? mt_getrandmax() / 2); $range = $maximum - $minimum; $uniformValue = $this->uniformRandomValue($type->exclusiveMinimum, $type->exclusiveMaximum); @@ -47,11 +47,14 @@ private function generateFloatValue(NumberType $type): float return $value; } - private function uniformRandomValue(bool $exclusiveMinimum, bool $exclusiveMaximum): float + private function uniformRandomValue(bool $exclusiveMinimum = false, bool $exclusiveMaximum = false): float { $minimum = $exclusiveMinimum ? 1 : 0; $maximum = mt_getrandmax() - ($exclusiveMaximum ? 1 : 0); - return (float) random_int($minimum, $maximum) / mt_getrandmax(); + $value1 = (float) random_int($minimum, $maximum) / mt_getrandmax(); + $value2 = (float) random_int($minimum, $maximum) / mt_getrandmax(); + + return $value1 * $value2; } } diff --git a/tests/Unit/Mock/Generation/Value/Combined/AnyOfValueGeneratorTest.php b/tests/Unit/Mock/Generation/Value/Combined/AnyOfValueGeneratorTest.php index 7bff8bd..1b46d09 100644 --- a/tests/Unit/Mock/Generation/Value/Combined/AnyOfValueGeneratorTest.php +++ b/tests/Unit/Mock/Generation/Value/Combined/AnyOfValueGeneratorTest.php @@ -33,9 +33,6 @@ class AnyOfValueGeneratorTest extends TestCase 'property1' => 'value1', 'property2' => 'value2', ]; - private const COMMON_PROPERTY_PAIR = [ - 'commonProperty' => 'commonPropertyValue', - ]; protected function setUp(): void { diff --git a/tests/Unit/Mock/Generation/Value/Primitive/RandomNumberGeneratorTest.php b/tests/Unit/Mock/Generation/Value/Primitive/RandomNumberGeneratorTest.php index 5ad5154..2ecd9d8 100644 --- a/tests/Unit/Mock/Generation/Value/Primitive/RandomNumberGeneratorTest.php +++ b/tests/Unit/Mock/Generation/Value/Primitive/RandomNumberGeneratorTest.php @@ -24,6 +24,21 @@ class RandomNumberGeneratorTest extends TestCase private const MULTIPLE_OF = 2.5; private const VALUE_DEVIATION = 0.001; + /** @test */ + public function generateValue_numberType_randomNumberValueReturned(): void + { + $generator = $this->createRandomNumberGenerator(); + $type = new NumberType(); + + $test = function () use ($generator, $type) { + $value = $generator->generateValue($type); + + return abs($value) - floor(abs($value)); + }; + + $this->expectClosureOccasionallyReturnsValueGreaterThan($test, self::VALUE_DEVIATION); + } + /** @test */ public function generateValue_numberTypeWithLimits_limitedNumberValueReturned(): void { diff --git a/tests/Utility/TestCase/ProbabilityTestCaseTrait.php b/tests/Utility/TestCase/ProbabilityTestCaseTrait.php index 087d3eb..42456bd 100644 --- a/tests/Utility/TestCase/ProbabilityTestCaseTrait.php +++ b/tests/Utility/TestCase/ProbabilityTestCaseTrait.php @@ -29,4 +29,17 @@ protected function expectClosureOccasionallyReturnsNull(\Closure $test): void Assert::assertNull($value); } + + protected function expectClosureOccasionallyReturnsValueGreaterThan(\Closure $test, $expectedValue): void + { + for ($i = 0; $i < 100; $i++) { + $value = $test(); + + if ($value > $expectedValue) { + break; + } + } + + Assert::assertGreaterThan($expectedValue, $value); + } }