Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.x] Type hinted arguments for Illuminate\Validation\Rules\RequiredIf #37688

Merged
merged 10 commits into from
Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Illuminate/Validation/Rules/RequiredIf.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ class RequiredIf
*/
public function __construct($condition)
{
$this->condition = $condition;
if (! is_string($condition) && (is_bool($condition) || is_callable($condition))) {
$this->condition = $condition;
} else {
throw new \InvalidArgumentException("Condition type must be 'callable' or 'bool'.");
}
}

/**
Expand Down
26 changes: 26 additions & 0 deletions tests/Validation/ValidationRequiredIfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,30 @@ public function testItClousureReturnsFormatsAStringVersionOfTheRule()

$this->assertSame('', (string) $rule);
}

public function testItOnlyCallableAndBooleanAreAcceptableArgumentsOfTheRule()
Copy link
Contributor

@nuernbergerA nuernbergerA Jun 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test would lead to false/positives.

expectException will succeed if one of the 3 rules hit
it would be better to test with a data provider or separate tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh my bad, will fix it.

{
$rule = new RequiredIf(false);

$rule = new RequiredIf(true);

$this->expectException(\InvalidArgumentException::class);

$rule = new RequiredIf('phpinfo');

$rule = new RequiredIf(12.3);

$rule = new RequiredIf(new stdClass());
}

public function testItReturnedRuleIsNotSerializable()
{
$this->expectException(\Exception::class);

$rule = serialize(new RequiredIf(function () {
return true;
}));

$rule = serialize(new RequiredIf());
}
}