From 0d49faf8273ffce80650ba046061aa0fc82d5733 Mon Sep 17 00:00:00 2001 From: Quynh Nguyen Date: Tue, 12 Apr 2022 20:40:16 +0700 Subject: [PATCH] [9.x] Fix ExcludeIf constructor (#41931) * Fix ExcludeIf constructor * Fix StyleCI --- src/Illuminate/Validation/Rules/ExcludeIf.php | 4 +++- tests/Validation/ValidationExcludeIfTest.php | 20 ++++++++++++------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/Illuminate/Validation/Rules/ExcludeIf.php b/src/Illuminate/Validation/Rules/ExcludeIf.php index 2a8b75d1ab2a..e72f35e5766d 100644 --- a/src/Illuminate/Validation/Rules/ExcludeIf.php +++ b/src/Illuminate/Validation/Rules/ExcludeIf.php @@ -18,10 +18,12 @@ class ExcludeIf * * @param callable|bool $condition * @return void + * + * @throws \InvalidArgumentException */ public function __construct($condition) { - if (! is_string($condition)) { + if (is_callable($condition) || is_bool($condition)) { $this->condition = $condition; } else { throw new InvalidArgumentException('The provided condition must be a callable or boolean.'); diff --git a/tests/Validation/ValidationExcludeIfTest.php b/tests/Validation/ValidationExcludeIfTest.php index 1d8d13833cc4..4f68623740ce 100644 --- a/tests/Validation/ValidationExcludeIfTest.php +++ b/tests/Validation/ValidationExcludeIfTest.php @@ -6,6 +6,7 @@ use Illuminate\Validation\Rules\ExcludeIf; use InvalidArgumentException; use PHPUnit\Framework\TestCase; +use stdClass; class ValidationExcludeIfTest extends TestCase { @@ -34,13 +35,18 @@ public function testItClousureReturnsFormatsAStringVersionOfTheRule() public function testItOnlyCallableAndBooleanAreAcceptableArgumentsOfTheRule() { - $rule = new ExcludeIf(false); - - $rule = new ExcludeIf(true); - - $this->expectException(InvalidArgumentException::class); - - $rule = new ExcludeIf('phpinfo'); + new ExcludeIf(false); + new ExcludeIf(true); + new ExcludeIf(fn () => true); + + foreach ([1, 1.1, 'foobar', new stdClass] as $condition) { + try { + new ExcludeIf($condition); + $this->fail('The ExcludeIf constructor must not accept '.gettype($condition)); + } catch (InvalidArgumentException $exception) { + $this->assertEquals('The provided condition must be a callable or boolean.', $exception->getMessage()); + } + } } public function testItReturnedRuleIsNotSerializable()