Apply contribution guidelines to "Infinite" rule

Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Danilo Benevides 2018-08-06 21:46:24 -03:00 committed by Henrique Moody
parent f01972e208
commit 94d2e87b2a
No known key found for this signature in database
GPG key ID: 221E9281655813A6
4 changed files with 80 additions and 56 deletions

View file

@ -14,12 +14,13 @@ declare(strict_types=1);
namespace Respect\Validation\Exceptions;
/**
* @author Danilo Benevides <danilobenevides01@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
class InfiniteException extends ValidationException
final class InfiniteException extends ValidationException
{
/**
* @var array
* {@inheritdoc}
*/
public static $defaultTemplates = [
self::MODE_DEFAULT => [

View file

@ -13,10 +13,16 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use function is_infinite;
use function is_numeric;
/**
* Validates if the input is an infinite number
*
* @author Danilo Benevides <danilobenevides01@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
class Infinite extends AbstractRule
final class Infinite extends AbstractRule
{
/**
* {@inheritdoc}

View file

@ -0,0 +1,38 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\InfiniteException;
use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Validator as v;
try {
v::infinite()->check(-9);
} catch (InfiniteException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::not(v::infinite())->check(INF);
} catch (InfiniteException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::infinite()->assert(new stdClass());
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::not(v::infinite())->assert(INF * -1);
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
?>
--EXPECTF--
-9 must be an infinite number
`INF` must not be an infinite number
- `[object] (stdClass: { })` must be an infinite number
- `-INF` must not be an infinite number

View file

@ -13,74 +13,53 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use PHPUnit\Framework\TestCase;
use Respect\Validation\Test\RuleTestCase;
use stdClass;
use const INF;
use const PHP_INT_MAX;
/**
* @group rule
* @covers \Respect\Validation\Exceptions\InfiniteException
* @group rule
*
* @covers \Respect\Validation\Rules\Infinite
*
* @author Danilo Benevides <danilobenevides01@gmail.com>
* @author Gabriel Caruso <carusogabriel34@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
class InfiniteTest extends TestCase
final class InfiniteTest extends RuleTestCase
{
protected $rule;
protected function setUp(): void
{
$this->rule = new Infinite();
}
/**
* @dataProvider providerForInfinite
*
* @test
* {@inheritdoc}
*/
public function shouldValidateInfiniteNumbers($input): void
public function providerForValidInput(): array
{
self::assertTrue($this->rule->validate($input));
}
$rule = new Infinite();
/**
* @dataProvider providerForNonInfinite
*
* @test
*/
public function shouldNotValidateNonInfiniteNumbers($input): void
{
self::assertFalse($this->rule->validate($input));
}
/**
* @expectedException \Respect\Validation\Exceptions\InfiniteException
* @expectedExceptionMessage 123456 must be an infinite number
*
* @test
*/
public function shouldThrowInfiniteExceptionWhenChecking(): void
{
$this->rule->check(123456);
}
public function providerForInfinite()
{
return [
[INF],
[INF * -1],
[$rule, INF],
[$rule, INF * -1],
];
}
public function providerForNonInfinite()
/**
* {@inheritdoc}
*/
public function providerForInvalidInput(): array
{
$rule = new Infinite();
return [
[' '],
[[]],
[new \stdClass()],
[null],
['123456'],
[-9],
[0],
[16],
[2],
[PHP_INT_MAX],
[$rule, ' '],
[$rule, []],
[$rule, new stdClass()],
[$rule, null],
[$rule, '123456'],
[$rule, -9],
[$rule, 0],
[$rule, 16],
[$rule, 2],
[$rule, PHP_INT_MAX],
];
}
}