From 72933a718f6c0ca073e3221cb4a9403243809462 Mon Sep 17 00:00:00 2001 From: Ismael Elias Date: Mon, 4 Jun 2018 22:54:48 -0300 Subject: [PATCH] Apply contribution guidelines to "Positive" rule Also does not allow validation of non-numeric values. Co-Authored-By: Henrique Moody --- docs/Positive.md | 7 +- library/Exceptions/PositiveException.php | 10 ++- library/Rules/Positive.php | 18 ++++- tests/integration/rules/positive.phpt | 38 +++++++++++ tests/unit/Rules/PositiveTest.php | 83 +++++++++++------------- 5 files changed, 106 insertions(+), 50 deletions(-) create mode 100644 tests/integration/rules/positive.phpt diff --git a/docs/Positive.md b/docs/Positive.md index 5f6de298..caecc6a8 100644 --- a/docs/Positive.md +++ b/docs/Positive.md @@ -2,16 +2,19 @@ - `Positive()` -Validates if a number is higher than zero +Validates whether the input is a positive number. ```php -v::numericVal()->positive()->validate(-15); // false +v::positive()->validate(1); // true +v::positive()->validate(0); // false +v::positive()->validate(-15); // false ``` ## Changelog Version | Description --------|------------- + 2.0.0 | Does not validate non-numeric values 0.3.9 | Created *** diff --git a/library/Exceptions/PositiveException.php b/library/Exceptions/PositiveException.php index 203cfb2d..73b8ebb7 100644 --- a/library/Exceptions/PositiveException.php +++ b/library/Exceptions/PositiveException.php @@ -13,8 +13,16 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; -class PositiveException extends ValidationException +/** + * @author Alexandre Gomes Gaigalas + * @author Henrique Moody + * @author Ismael Elias + */ +final class PositiveException extends ValidationException { + /** + * {@inheritdoc} + */ public static $defaultTemplates = [ self::MODE_DEFAULT => [ self::STANDARD => '{{name}} must be positive', diff --git a/library/Rules/Positive.php b/library/Rules/Positive.php index 03f2689d..ad392d36 100644 --- a/library/Rules/Positive.php +++ b/library/Rules/Positive.php @@ -13,10 +13,26 @@ declare(strict_types=1); namespace Respect\Validation\Rules; -class Positive extends AbstractRule +use function is_numeric; + +/** + * Validates whether the input is a positive number. + * + * @author Alexandre Gomes Gaigalas + * @author Henrique Moody + * @author Ismael Elias + */ +final class Positive extends AbstractRule { + /** + * {@inheritdoc} + */ public function validate($input): bool { + if (!is_numeric($input)) { + return false; + } + return $input > 0; } } diff --git a/tests/integration/rules/positive.phpt b/tests/integration/rules/positive.phpt new file mode 100644 index 00000000..e0c9272c --- /dev/null +++ b/tests/integration/rules/positive.phpt @@ -0,0 +1,38 @@ +--FILE-- +check(-10); +} catch (PositiveException $exception) { + echo $exception->getMessage().PHP_EOL; +} + +try { + v::not(v::positive())->check(16); +} catch (PositiveException $exception) { + echo $exception->getMessage().PHP_EOL; +} + +try { + v::positive()->assert('a'); +} catch (NestedValidationException $exception) { + echo $exception->getFullMessage().PHP_EOL; +} + +try { + v::not(v::positive())->assert('165'); +} catch (NestedValidationException $exception) { + echo $exception->getFullMessage().PHP_EOL; +} + +?> +--EXPECTF-- +-10 must be positive +16 must not be positive +- "a" must be positive +- "165" must not be positive diff --git a/tests/unit/Rules/PositiveTest.php b/tests/unit/Rules/PositiveTest.php index 27319ce2..82590e21 100644 --- a/tests/unit/Rules/PositiveTest.php +++ b/tests/unit/Rules/PositiveTest.php @@ -13,65 +13,56 @@ declare(strict_types=1); namespace Respect\Validation\Rules; -use PHPUnit\Framework\TestCase; +use Respect\Validation\Test\RuleTestCase; +use stdClass; /** - * @group rule + * @group rule + * * @covers \Respect\Validation\Rules\Positive - * @covers \Respect\Validation\Exceptions\PositiveException + * + * @author Alexandre Gomes Gaigalas + * @author Gabriel Caruso + * @author Henrique Moody + * @author Ismael Elias */ -class PositiveTest extends TestCase +final class PositiveTest extends RuleTestCase { - protected $object; - - protected function setUp(): void + /* + * {@inheritdoc} + */ + public function providerForValidInput(): array { - $this->object = new Positive(); - } + $rule = new Positive(); - /** - * @dataProvider providerForPositive - */ - public function testPositive($input): void - { - self::assertTrue($this->object->__invoke($input)); - $this->object->check($input); - $this->object->assert($input); - } - - /** - * @dataProvider providerForNotPositive - * @expectedException \Respect\Validation\Exceptions\PositiveException - */ - public function testNotPositive($input): void - { - self::assertFalse($this->object->__invoke($input)); - $this->object->assert($input); - } - - public function providerForPositive() - { return [ - [16], - ['165'], - [123456], - [1e10], + [$rule, 16], + [$rule, '165'], + [$rule, 123456], + [$rule, 1e10], ]; } - public function providerForNotPositive() + /* + * {@inheritdoc} + */ + public function providerForInvalidInput(): array { + $rule = new Positive(); + return [ - [''], - [null], - ['a'], - [' '], - ['Foo'], - ['-1.44'], - [-1e-5], - [0], - [-0], - [-10], + [$rule, ''], + [$rule, []], + [$rule, new stdClass()], + [$rule, null], + [$rule, 'a'], + [$rule, ' '], + [$rule, 'Foo'], + [$rule, '-1.44'], + [$rule, -1e-5], + [$rule, 0], + [$rule, -0], + [$rule, -10], ]; } }