respect-validation/tests/unit/Rules/IntValTest.php
bmorg 34cbed2c9b
Allow leading zeros when using "IntVal" rule
There is some confusion about integer literals (as we type them into
source code) and integer values (the actual value they represent).

When casting the integer 08 (without quotes), PHP triggers an error as
integers starting with 0 should have base 8. However, when casting the
string '08' as an integer PHP returns the integer 8.

This commit will change the behavior of the "IntVal" rule, allowing it
to accept any integer type and any representation of an integer as a
string.

Reviewed-by: Emmerson Siqueira <emmersonsiqueira@gmail.com>
Reviewed-by: Wesley Victhor Mendes Santiago <w.v.mendes.s@gmail.com>
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
2020-07-21 21:40:38 +02:00

79 lines
1.7 KiB
PHP

<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/
declare(strict_types=1);
namespace Respect\Validation\Rules;
use Respect\Validation\Test\RuleTestCase;
use stdClass;
use const PHP_INT_MAX;
/**
* @group rule
*
* @covers \Respect\Validation\Rules\IntVal
*
* @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>
*/
final class IntValTest extends RuleTestCase
{
/**
* {@inheritDoc}
*/
public function providerForValidInput(): array
{
$rule = new IntVal();
return [
[$rule, 16],
[$rule, '165'],
[$rule, 123456],
[$rule, PHP_INT_MAX],
[$rule, '06'],
[$rule, '09'],
[$rule, '0'],
[$rule, '00'],
[$rule, 0b101010],
[$rule, 0x2a],
[$rule, '089'],
];
}
/**
* {@inheritDoc}
*/
public function providerForInvalidInput(): array
{
$rule = new IntVal();
return [
[$rule, ''],
[$rule, new stdClass()],
[$rule, []],
[$rule, null],
[$rule, 'a'],
[$rule, '1.0'],
[$rule, 1.0],
[$rule, ' '],
[$rule, true],
[$rule, false],
[$rule, 'Foo'],
[$rule, '1.44'],
[$rule, 1e-5],
[$rule, '089ab'],
];
}
}