mirror of
https://github.com/Respect/Validation.git
synced 2024-06-04 14:52:17 +02:00
Apply contribution guidelines to "Even" rule
Also add an extra validation to the rule, not allowing non-integers to be considered as even numbers. Co-Authored-By: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
3d2136e2ad
commit
51ec5e1b95
|
@ -2,7 +2,7 @@
|
|||
|
||||
- `Even()`
|
||||
|
||||
Validates an even number.
|
||||
Validates whether the input is an even number or not.
|
||||
|
||||
```php
|
||||
v::intVal()->even()->validate(2); // true
|
||||
|
|
|
@ -13,8 +13,18 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class EvenException extends ValidationException
|
||||
/**
|
||||
* Exceptions to be thrown by the Even Rule.
|
||||
*
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Jean Pimentel <jeanfap@gmail.com>
|
||||
* @author Paul Karikari <paulkarikari1@gmail.com>
|
||||
*/
|
||||
final class EvenException extends ValidationException
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $defaultTemplates = [
|
||||
self::MODE_DEFAULT => [
|
||||
self::STANDARD => '{{name}} must be an even number',
|
||||
|
|
|
@ -13,10 +13,27 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class Even extends AbstractRule
|
||||
use const FILTER_VALIDATE_INT;
|
||||
use function filter_var;
|
||||
|
||||
/**
|
||||
* Validates whether the input is an even number or not.
|
||||
*
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Jean Pimentel <jeanfap@gmail.com>
|
||||
* @author Paul Karikari <paulkarikari1@gmail.com>
|
||||
*/
|
||||
final class Even extends AbstractRule
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($input): bool
|
||||
{
|
||||
if (false === filter_var($input, FILTER_VALIDATE_INT)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return 0 === (int) $input % 2;
|
||||
}
|
||||
}
|
||||
|
|
38
tests/integration/rules/even.phpt
Normal file
38
tests/integration/rules/even.phpt
Normal file
|
@ -0,0 +1,38 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\EvenException;
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::even()->check(-1);
|
||||
} catch (EvenException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::even()->assert(5);
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::even())->check(6);
|
||||
} catch (EvenException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::even())->assert(8);
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
-1 must be an even number
|
||||
- 5 must be an even number
|
||||
6 must not be an even number
|
||||
- 8 must not be an even number
|
|
@ -13,60 +13,45 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Respect\Validation\Test\RuleTestCase;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
* @group rule
|
||||
*
|
||||
* @covers \Respect\Validation\Rules\Even
|
||||
* @covers \Respect\Validation\Exceptions\EvenException
|
||||
*
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Jean Pimentel <jeanfap@gmail.com>
|
||||
* @author Paul Karikari <paulkarikari1@gmail.com>
|
||||
*/
|
||||
class EvenTest extends TestCase
|
||||
final class EvenTest extends RuleTestCase
|
||||
{
|
||||
protected $evenValidator;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->evenValidator = new Even();
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForEven
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function testEvenNumbersShouldPass($input): void
|
||||
{
|
||||
self::assertTrue($this->evenValidator->validate($input));
|
||||
$this->evenValidator->check($input);
|
||||
$this->evenValidator->assert($input);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForNotEven
|
||||
* @expectedException \Respect\Validation\Exceptions\EvenException
|
||||
*/
|
||||
public function testNotEvenNumbersShouldFail($input): void
|
||||
{
|
||||
self::assertFalse($this->evenValidator->validate($input));
|
||||
$this->evenValidator->assert($input);
|
||||
}
|
||||
|
||||
public function providerForEven()
|
||||
public function providerForValidInput(): array
|
||||
{
|
||||
return [
|
||||
[''],
|
||||
[-2],
|
||||
[-0],
|
||||
[0],
|
||||
[32],
|
||||
[new Even(), 2],
|
||||
[new Even(), -2],
|
||||
[new Even(), 0],
|
||||
[new Even(), 32],
|
||||
];
|
||||
}
|
||||
|
||||
public function providerForNotEven()
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerForInvalidInput(): array
|
||||
{
|
||||
return [
|
||||
[-5],
|
||||
[-1],
|
||||
[1],
|
||||
[13],
|
||||
[new Even(), ''],
|
||||
[new Even(), INF],
|
||||
[new Even(), 2.2],
|
||||
[new Even(), -5],
|
||||
[new Even(), -1],
|
||||
[new Even(), 1],
|
||||
[new Even(), 13],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue