mirror of
https://github.com/Respect/Validation.git
synced 2024-05-29 11:52:34 +02:00
Apply contribution guidelines to "FalseVal" rule
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
eb232247da
commit
0be63bd313
|
@ -13,8 +13,15 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class FalseValException extends ValidationException
|
||||
/**
|
||||
* @author Danilo Correa <danilosilva87@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
final class FalseValException extends ValidationException
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $defaultTemplates = [
|
||||
self::MODE_DEFAULT => [
|
||||
self::STANDARD => '{{name}} is not considered as "False"',
|
||||
|
|
|
@ -13,14 +13,23 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class FalseVal extends AbstractRule
|
||||
use const FILTER_NULL_ON_FAILURE;
|
||||
use const FILTER_VALIDATE_BOOLEAN;
|
||||
use function filter_var;
|
||||
|
||||
/**
|
||||
* Validates if a value is considered as false.
|
||||
*
|
||||
* @author Danilo Correa <danilosilva87@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
final class FalseVal extends AbstractRule
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($input): bool
|
||||
{
|
||||
if (false === $input) { // PHP 5.3 workaround
|
||||
return true;
|
||||
}
|
||||
|
||||
return false === filter_var($input, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
|
||||
}
|
||||
}
|
||||
|
|
38
tests/integration/rules/falseVal.phpt
Normal file
38
tests/integration/rules/falseVal.phpt
Normal file
|
@ -0,0 +1,38 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\FalseValException;
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::falseVal()->check(true);
|
||||
} catch (FalseValException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::falseVal())->check('false');
|
||||
} catch (FalseValException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::falseVal()->assert(1);
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::falseVal())->assert(0);
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
`TRUE` is not considered as "False"
|
||||
"false" is considered as "False"
|
||||
- 1 is not considered as "False"
|
||||
- 0 is considered as "False"
|
|
@ -13,69 +13,63 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Respect\Validation\Test\RuleTestCase;
|
||||
use stdClass;
|
||||
use const PHP_INT_MAX;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
* @covers \Respect\Validation\Exceptions\FalseValException
|
||||
* @group rule
|
||||
*
|
||||
* @covers \Respect\Validation\Rules\FalseVal
|
||||
*
|
||||
* @author Danilo Correa <danilosilva87@gmail.com>
|
||||
* @author Gabriel Caruso <carusogabriel34@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
class FalseValTest extends TestCase
|
||||
final class FalseValTest extends RuleTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider validFalseProvider
|
||||
*
|
||||
* @test
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function shouldValidatePatternAccordingToTheDefinedLocale($input): void
|
||||
public function providerForValidInput(): array
|
||||
{
|
||||
$rule = new FalseVal();
|
||||
$sut = new FalseVal();
|
||||
|
||||
self::assertTrue($rule->validate($input));
|
||||
}
|
||||
|
||||
public function validFalseProvider()
|
||||
{
|
||||
return [
|
||||
[false],
|
||||
[0],
|
||||
['0'],
|
||||
['false'],
|
||||
['off'],
|
||||
['no'],
|
||||
['FALSE'],
|
||||
['OFF'],
|
||||
['NO'],
|
||||
['False'],
|
||||
['Off'],
|
||||
['No'],
|
||||
'boolean false' => [$sut, false],
|
||||
'empty string' => [$sut, ''],
|
||||
'integer 0' => [$sut, 0],
|
||||
'0' => [$sut, '0'],
|
||||
'false' => [$sut, 'false'],
|
||||
'FALSE' => [$sut, 'FALSE'],
|
||||
'False' => [$sut, 'False'],
|
||||
'no' => [$sut, 'no'],
|
||||
'NO' => [$sut, 'NO'],
|
||||
'No' => [$sut, 'No'],
|
||||
'off' => [$sut, 'off'],
|
||||
'OFF' => [$sut, 'OFF'],
|
||||
'Off' => [$sut, 'Off'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider invalidFalseProvider
|
||||
*
|
||||
* @test
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function shouldNotValidatePatternAccordingToTheDefinedLocale($input): void
|
||||
public function providerForInvalidInput(): array
|
||||
{
|
||||
$rule = new FalseVal();
|
||||
$sut = new FalseVal();
|
||||
|
||||
self::assertFalse($rule->validate($input));
|
||||
}
|
||||
|
||||
public function invalidFalseProvider()
|
||||
{
|
||||
return [
|
||||
[true],
|
||||
[1],
|
||||
['1'],
|
||||
[0.5],
|
||||
[2],
|
||||
['true'],
|
||||
['on'],
|
||||
['yes'],
|
||||
['anything'],
|
||||
'boolean true' => [$sut, true],
|
||||
'integer bigger than 1' => [$sut, random_int(1, PHP_INT_MAX)],
|
||||
'integer-string bigger than 1' => [$sut, (string) random_int(1, PHP_INT_MAX)],
|
||||
'float bigger than 0' => [$sut, 0.5],
|
||||
'true' => [$sut, 'true'],
|
||||
'on' => [$sut, 'on'],
|
||||
'yes' => [$sut, 'yes'],
|
||||
'anything' => [$sut, 'anything'],
|
||||
'empty array' => [$sut, []],
|
||||
'object' => [$sut, new stdClass()],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue