diff --git a/docs/Odd.md b/docs/Odd.md index 6e1f5825..05433686 100644 --- a/docs/Odd.md +++ b/docs/Odd.md @@ -2,18 +2,20 @@ - `Odd()` -Validates an odd number. +Validates whether the input is an odd number or not. ```php -v::intVal()->odd()->validate(3); // true +v::odd()->validate(0); // false +v::odd()->validate(3); // true ``` -Using `int()` before `odd()` is a best practice. +Using `intVal()` before `odd()` is a best practice. ## Changelog Version | Description --------|------------- + 2.0.0 | Only validates integers 0.3.9 | Created *** diff --git a/library/Exceptions/OddException.php b/library/Exceptions/OddException.php index 8b01b35f..3bd1d503 100644 --- a/library/Exceptions/OddException.php +++ b/library/Exceptions/OddException.php @@ -13,8 +13,16 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; -class OddException extends ValidationException +/** + * @author Danilo Benevides + * @author Henrique Moody + * @author Jean Pimentel + */ +final class OddException extends ValidationException { + /** + * {@inheritdoc} + */ public static $defaultTemplates = [ self::MODE_DEFAULT => [ self::STANDARD => '{{name}} must be an odd number', diff --git a/library/Rules/Odd.php b/library/Rules/Odd.php index 1d4fd05b..dd7f7d47 100644 --- a/library/Rules/Odd.php +++ b/library/Rules/Odd.php @@ -13,10 +13,28 @@ declare(strict_types=1); namespace Respect\Validation\Rules; -class Odd extends AbstractRule +use function filter_var; +use function is_numeric; + +/** + * Validates whether the input is an odd number or not. + * + * @author Danilo Benevides + * @author Henrique Moody + * @author Jean Pimentel + */ +final class Odd extends AbstractRule { public function validate($input): bool { + if (!is_numeric($input)) { + return false; + } + + if (!filter_var($input, FILTER_VALIDATE_INT)) { + return false; + } + return 0 !== (int) $input % 2; } } diff --git a/tests/integration/rules/odd.phpt b/tests/integration/rules/odd.phpt new file mode 100644 index 00000000..e3845c6b --- /dev/null +++ b/tests/integration/rules/odd.phpt @@ -0,0 +1,38 @@ +--FILE-- +check(2); +} catch (OddException $exception) { + echo $exception->getMessage().PHP_EOL; +} + +try { + v::not(v::odd())->check(7); +} catch (OddException $exception) { + echo $exception->getMessage().PHP_EOL; +} + +try { + v::odd()->assert(2); +} catch (NestedValidationException $exception) { + echo $exception->getFullMessage().PHP_EOL; +} + +try { + v::not(v::odd())->assert(9); +} catch (NestedValidationException $exception) { + echo $exception->getFullMessage().PHP_EOL; +} + +?> +--EXPECTF-- +2 must be an odd number +7 must not be an odd number +- 2 must be an odd number +- 9 must not be an odd number diff --git a/tests/unit/Rules/OddTest.php b/tests/unit/Rules/OddTest.php index 5438ba7b..aef3908d 100644 --- a/tests/unit/Rules/OddTest.php +++ b/tests/unit/Rules/OddTest.php @@ -13,60 +13,54 @@ declare(strict_types=1); namespace Respect\Validation\Rules; -use PHPUnit\Framework\TestCase; +use Respect\Validation\Test\RuleTestCase; +use stdClass; +use function tmpfile; /** - * @group rule + * @group rule * @covers \Respect\Validation\Rules\Odd - * @covers \Respect\Validation\Exceptions\OddException + * + * @author Danilo Benevides + * @author Gabriel Caruso + * @author Henrique Moody + * @author Jean Pimentel */ -class OddTest extends TestCase +final class OddTest extends RuleTestCase { - protected $object; - - protected function setUp(): void + /* + * {@inheritdoc} + */ + public function providerForValidInput(): array { - $this->object = new Odd(); - } + $rule = new Odd(); - /** - * @dataProvider providerForOdd - */ - public function testOdd($input): void - { - $this->object->assert($input); - self::assertTrue($this->object->__invoke($input)); - $this->object->check($input); - } - - /** - * @dataProvider providerForNotOdd - * @expectedException \Respect\Validation\Exceptions\OddException - */ - public function testNotOdd($input): void - { - self::assertFalse($this->object->__invoke($input)); - $this->object->assert($input); - } - - public function providerForOdd() - { return [ - [-5], - [-1], - [1], - [13], + [$rule, -5], + [$rule, -1], + [$rule, 1], + [$rule, 13], ]; } - public function providerForNotOdd() + /* + * {@inheritdoc} + */ + public function providerForInvalidInput(): array { + $rule = new Odd(); + return [ - [''], - [-2], - [-0], - [0], - [32], + [$rule, []], + [$rule, new stdClass()], + [$rule, tmpfile()], + [$rule, true], + [$rule, false], + [$rule, ''], + [$rule, -2], + [$rule, -0], + [$rule, 0], + [$rule, 32], ]; } }