Apply contribution guidelines to "FloatVal" rule

Co-Authored-By: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Danilo Benevides 2018-06-04 13:29:55 -03:00 committed by Henrique Moody
parent b29bdbd111
commit c422dc39e4
No known key found for this signature in database
GPG key ID: 221E9281655813A6
5 changed files with 97 additions and 49 deletions

View file

@ -2,7 +2,7 @@
- `FloatVal()`
Validates a floating point number.
Validate whether the input value is float.
```php
v::floatVal()->validate(1.5); // true

View file

@ -13,8 +13,16 @@ declare(strict_types=1);
namespace Respect\Validation\Exceptions;
class FloatValException extends ValidationException
/**
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Danilo Benevides <danilobenevides01@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
final class FloatValException extends ValidationException
{
/**
* {@inheritdoc}
*/
public static $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be a float number',

View file

@ -13,8 +13,22 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
class FloatVal extends AbstractRule
use function filter_var;
use function is_float;
/**
* Validate whether the input value is float.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Danilo Benevides <danilobenevides01@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Jayson Reis <santosdosreis@gmail.com>
*/
final class FloatVal extends AbstractRule
{
/**
* {@inheritdoc}
*/
public function validate($input): bool
{
return is_float(filter_var($input, FILTER_VALIDATE_FLOAT));

View file

@ -0,0 +1,38 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\FloatValException;
use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Validator as v;
try {
v::floatVal()->check('a');
} catch (FloatValException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::not(v::floatVal())->check(165.0);
} catch (FloatValException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::floatVal()->assert('a');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::not(v::floatVal())->assert('165.7');
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
?>
--EXPECTF--
"a" must be a float number
165.0 must not be a float number
- "a" must be a float number
- "165.7" must not be a float number

View file

@ -13,65 +13,53 @@ 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\FloatVal
* @covers \Respect\Validation\Exceptions\FloatValException
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Danilo Benevides <danilobenevides01@gmail.com>
* @author Gabriel Caruso <carusogabriel34@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
class FloatValTest extends TestCase
final class FloatValTest extends RuleTestCase
{
protected $floatValidator;
protected function setUp(): void
/*
* {@inheritdoc}
*/
public function providerForValidInput(): array
{
$this->floatValidator = new FloatVal();
}
$rule = new FloatVal();
/**
* @dataProvider providerForFloat
*/
public function testFloatNumbersShouldPass($input): void
{
$this->floatValidator->assert($input);
self::assertTrue($this->floatValidator->__invoke($input));
$this->floatValidator->check($input);
}
/**
* @dataProvider providerForNotFloat
* @expectedException \Respect\Validation\Exceptions\FloatValException
*/
public function testNotFloatNumbersShouldFail($input): void
{
self::assertFalse($this->floatValidator->__invoke($input));
$this->floatValidator->assert($input);
}
public function providerForFloat()
{
return [
[165],
[1],
[0],
[0.0],
['1'],
['19347e12'],
[165.0],
['165.7'],
[1e12],
[$rule, 165],
[$rule, 1],
[$rule, 0],
[$rule, 0.0],
[$rule, '1'],
[$rule, '19347e12'],
[$rule, 165.0],
[$rule, '165.7'],
[$rule, 1e12],
];
}
public function providerForNotFloat()
/*
* {@inheritdoc}
*/
public function providerForInvalidInput(): array
{
$rule = new FloatVal();
return [
[''],
[null],
['a'],
[' '],
['Foo'],
[$rule, ''],
[$rule, null],
[$rule, 'a'],
[$rule, ' '],
[$rule, 'Foo'],
];
}
}