mirror of
https://github.com/Respect/Validation.git
synced 2024-06-08 08:42:15 +02:00
Apply contribution guidelines to "Negative" rule
Also does not allow validation of non-numeric values. Co-Authored-By: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
72933a718f
commit
c2f6876e4f
|
@ -2,7 +2,7 @@
|
|||
|
||||
- `Negative()`
|
||||
|
||||
Validates if a number is lower than zero
|
||||
Validates whether the input is a negative number.
|
||||
|
||||
```php
|
||||
v::numericVal()->negative()->validate(-15); // true
|
||||
|
@ -12,6 +12,7 @@ v::numericVal()->negative()->validate(-15); // true
|
|||
|
||||
Version | Description
|
||||
--------|-------------
|
||||
2.0.0 | Does not validate non-numeric values
|
||||
0.3.9 | Created
|
||||
|
||||
***
|
||||
|
|
|
@ -13,8 +13,16 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class NegativeException extends ValidationException
|
||||
/**
|
||||
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Ismael Elias <ismael.esq@hotmail.com>
|
||||
*/
|
||||
final class NegativeException extends ValidationException
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $defaultTemplates = [
|
||||
self::MODE_DEFAULT => [
|
||||
self::STANDARD => '{{name}} must be negative',
|
||||
|
|
|
@ -13,10 +13,26 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class Negative extends AbstractRule
|
||||
use function is_numeric;
|
||||
|
||||
/**
|
||||
* Validates whether the input is a negative number.
|
||||
*
|
||||
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Ismael Elias <ismael.esq@hotmail.com>
|
||||
*/
|
||||
final class Negative extends AbstractRule
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($input): bool
|
||||
{
|
||||
if (!is_numeric($input)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $input < 0;
|
||||
}
|
||||
}
|
||||
|
|
38
tests/integration/rules/negative.phpt
Normal file
38
tests/integration/rules/negative.phpt
Normal file
|
@ -0,0 +1,38 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\NegativeException;
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::negative()->check(16);
|
||||
} catch (NegativeException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::negative())->check(-10);
|
||||
} catch (NegativeException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::negative()->assert('a');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::negative())->assert('-144');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
16 must be negative
|
||||
-10 must not be negative
|
||||
- "a" must be negative
|
||||
- "-144" must not be negative
|
|
@ -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\Negative
|
||||
* @covers \Respect\Validation\Exceptions\NegativeException
|
||||
*
|
||||
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
* @author Gabriel Caruso <carusogabriel34@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Ismael Elias <ismael.esq@hotmail.com>
|
||||
*/
|
||||
class NegativeTest extends TestCase
|
||||
final class NegativeTest extends RuleTestCase
|
||||
{
|
||||
protected $negativeValidator;
|
||||
|
||||
protected function setUp(): void
|
||||
/*
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerForValidInput(): array
|
||||
{
|
||||
$this->negativeValidator = new Negative();
|
||||
}
|
||||
$rule = new Negative();
|
||||
|
||||
/**
|
||||
* @dataProvider providerForNegative
|
||||
*/
|
||||
public function testNegativeShouldPass($input): void
|
||||
{
|
||||
$this->negativeValidator->assert($input);
|
||||
self::assertTrue($this->negativeValidator->__invoke($input));
|
||||
$this->negativeValidator->check($input);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForNotNegative
|
||||
* @expectedException \Respect\Validation\Exceptions\NegativeException
|
||||
*/
|
||||
public function testNotNegativeNumbersShouldThrowNegativeException($input): void
|
||||
{
|
||||
self::assertFalse($this->negativeValidator->__invoke($input));
|
||||
$this->negativeValidator->assert($input);
|
||||
}
|
||||
|
||||
public function providerForNegative()
|
||||
{
|
||||
return [
|
||||
['-1.44'],
|
||||
[-1e-5],
|
||||
[-10],
|
||||
[$rule, '-1.44'],
|
||||
[$rule, -1e-5],
|
||||
[$rule, -10],
|
||||
];
|
||||
}
|
||||
|
||||
public function providerForNotNegative()
|
||||
/*
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerForInvalidInput(): array
|
||||
{
|
||||
$rule = new Negative();
|
||||
|
||||
return [
|
||||
[''],
|
||||
[0],
|
||||
[-0],
|
||||
[null],
|
||||
['a'],
|
||||
[' '],
|
||||
['Foo'],
|
||||
[16],
|
||||
['165'],
|
||||
[123456],
|
||||
[1e10],
|
||||
[$rule, ''],
|
||||
[$rule, []],
|
||||
[$rule, new stdClass()],
|
||||
[$rule, 0],
|
||||
[$rule, -0],
|
||||
[$rule, null],
|
||||
[$rule, 'a'],
|
||||
[$rule, ' '],
|
||||
[$rule, 'Foo'],
|
||||
[$rule, 16],
|
||||
[$rule, '165'],
|
||||
[$rule, 123456],
|
||||
[$rule, 1e10],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue