From 5d09cdde1e56427aee90e264963ca27e60894e02 Mon Sep 17 00:00:00 2001 From: "cuong.tt" Date: Tue, 10 Jun 2025 08:32:02 +0700 Subject: [PATCH 1/4] Validate that 'outOf' is greater than 1 in Lottery class and add a corresponding test --- src/Illuminate/Support/Lottery.php | 4 ++++ tests/Support/LotteryTest.php | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/src/Illuminate/Support/Lottery.php b/src/Illuminate/Support/Lottery.php index 1f8c80588345..ed917371f90e 100644 --- a/src/Illuminate/Support/Lottery.php +++ b/src/Illuminate/Support/Lottery.php @@ -53,6 +53,10 @@ public function __construct($chances, $outOf = null) throw new RuntimeException('Float must not be greater than 1.'); } + if ($outOf !== null && $outOf < 1) { + throw new RuntimeException('The opportunities to win must be greater than 1.'); + } + $this->chances = $chances; $this->outOf = $outOf; diff --git a/tests/Support/LotteryTest.php b/tests/Support/LotteryTest.php index 7dc3a31d856f..f3b6cfc19c96 100644 --- a/tests/Support/LotteryTest.php +++ b/tests/Support/LotteryTest.php @@ -154,6 +154,14 @@ public function testItThrowsForFloatsOverOne() new Lottery(1.1); } + public function testItThrowsForOutOfLessThanOne() + { + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('The opportunities to win must be greater than 1.'); + + new Lottery(1, 0); + } + public function testItCanWinWithFloat() { $wins = false; From 53e7d9236b0a59ca98c73dc8f4f796c72c2d7fe0 Mon Sep 17 00:00:00 2001 From: "cuong.tt" Date: Tue, 10 Jun 2025 08:51:38 +0700 Subject: [PATCH 2/4] Update message --- src/Illuminate/Support/Lottery.php | 2 +- tests/Support/LotteryTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Support/Lottery.php b/src/Illuminate/Support/Lottery.php index ed917371f90e..ec75e22ffce0 100644 --- a/src/Illuminate/Support/Lottery.php +++ b/src/Illuminate/Support/Lottery.php @@ -54,7 +54,7 @@ public function __construct($chances, $outOf = null) } if ($outOf !== null && $outOf < 1) { - throw new RuntimeException('The opportunities to win must be greater than 1.'); + throw new RuntimeException('The opportunities to win must be greater or equals to 1.'); } $this->chances = $chances; diff --git a/tests/Support/LotteryTest.php b/tests/Support/LotteryTest.php index f3b6cfc19c96..8dc0910c41fd 100644 --- a/tests/Support/LotteryTest.php +++ b/tests/Support/LotteryTest.php @@ -157,7 +157,7 @@ public function testItThrowsForFloatsOverOne() public function testItThrowsForOutOfLessThanOne() { $this->expectException(RuntimeException::class); - $this->expectExceptionMessage('The opportunities to win must be greater than 1.'); + $this->expectExceptionMessage('The opportunities to win must be greater or equals to 1.'); new Lottery(1, 0); } From 66db9f14a8818bd1c8a9aee439ba61e4236116fa Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 10 Jun 2025 09:24:59 -0500 Subject: [PATCH 3/4] Update LotteryTest.php --- tests/Support/LotteryTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Support/LotteryTest.php b/tests/Support/LotteryTest.php index 8dc0910c41fd..e81bb8fe136f 100644 --- a/tests/Support/LotteryTest.php +++ b/tests/Support/LotteryTest.php @@ -157,7 +157,6 @@ public function testItThrowsForFloatsOverOne() public function testItThrowsForOutOfLessThanOne() { $this->expectException(RuntimeException::class); - $this->expectExceptionMessage('The opportunities to win must be greater or equals to 1.'); new Lottery(1, 0); } From aa5b32a5f74887752ecb3e4c05d12f470cc53f63 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 10 Jun 2025 09:27:03 -0500 Subject: [PATCH 4/4] Update Lottery.php --- src/Illuminate/Support/Lottery.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Support/Lottery.php b/src/Illuminate/Support/Lottery.php index ec75e22ffce0..183cf957c578 100644 --- a/src/Illuminate/Support/Lottery.php +++ b/src/Illuminate/Support/Lottery.php @@ -45,7 +45,7 @@ class Lottery * Create a new Lottery instance. * * @param int|float $chances - * @param int|null $outOf + * @param int<1, max>|null $outOf */ public function __construct($chances, $outOf = null) { @@ -54,7 +54,7 @@ public function __construct($chances, $outOf = null) } if ($outOf !== null && $outOf < 1) { - throw new RuntimeException('The opportunities to win must be greater or equals to 1.'); + throw new RuntimeException('Lottery "out of" value must be greater than or equal to 1.'); } $this->chances = $chances;