Skip to content

Commit

Permalink
[9.x] Fix ExcludeIf constructor (#41931)
Browse files Browse the repository at this point in the history
* Fix ExcludeIf constructor

* Fix StyleCI
  • Loading branch information
seriquynh authored Apr 12, 2022
1 parent bd3ac84 commit 0d49faf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/Illuminate/Validation/Rules/ExcludeIf.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand Down
20 changes: 13 additions & 7 deletions tests/Validation/ValidationExcludeIfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Validation\Rules\ExcludeIf;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use stdClass;

class ValidationExcludeIfTest extends TestCase
{
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 0d49faf

Please sign in to comment.