mirror of
https://github.com/Respect/Validation.git
synced 2024-06-27 01:30:05 +02:00
Apply contribution guidelines to "NotBlank" rule
This commit is contained in:
parent
0e67549dd9
commit
24040c9473
|
@ -13,10 +13,17 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class NotBlankException extends ValidationException
|
||||
/**
|
||||
* @author Danilo Correa <danilosilva87@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
final class NotBlankException extends ValidationException
|
||||
{
|
||||
public const NAMED = 'named';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $defaultTemplates = [
|
||||
self::MODE_DEFAULT => [
|
||||
self::STANDARD => 'The value must not be blank',
|
||||
|
@ -28,6 +35,9 @@ class NotBlankException extends ValidationException
|
|||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function chooseTemplate(): string
|
||||
{
|
||||
if ($this->getParam('input') || $this->getParam('name')) {
|
||||
|
|
|
@ -14,9 +14,23 @@ declare(strict_types=1);
|
|||
namespace Respect\Validation\Rules;
|
||||
|
||||
use stdClass;
|
||||
use function array_filter;
|
||||
use function is_array;
|
||||
use function is_numeric;
|
||||
use function is_string;
|
||||
use function trim;
|
||||
|
||||
class NotBlank extends AbstractRule
|
||||
/**
|
||||
* Validates if the given input is not a blank value (null, zeros, empty strings or empty arrays, recursively).
|
||||
*
|
||||
* @author Danilo Correa <danilosilva87@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
final class NotBlank extends AbstractRule
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($input): bool
|
||||
{
|
||||
if (is_numeric($input)) {
|
||||
|
|
51
tests/integration/rules/notBlank.phpt
Normal file
51
tests/integration/rules/notBlank.phpt
Normal file
|
@ -0,0 +1,51 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Exceptions\NotBlankException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::notBlank()->check(null);
|
||||
} catch (NotBlankException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::notBlank()->setName('Field')->check(null);
|
||||
} catch (NotBlankException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::notBlank())->check(1);
|
||||
} catch (NotBlankException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::notBlank()->assert('');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::notBlank()->setName('Field')->assert('');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::notBlank())->assert([1]);
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
The value must not be blank
|
||||
Field must not be blank
|
||||
1 must be blank
|
||||
- The value must not be blank
|
||||
- Field must not be blank
|
||||
- `{ 1 }` must be blank
|
|
@ -1,35 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
$notBlankValues = [
|
||||
'a',
|
||||
1,
|
||||
1.0,
|
||||
];
|
||||
|
||||
//Check the "pure" value
|
||||
foreach ($notBlankValues as $value) {
|
||||
v::notBlank()->assert($value);
|
||||
v::notBlank()->check($value);
|
||||
}
|
||||
|
||||
//Check the value inside an array
|
||||
foreach ($notBlankValues as $value) {
|
||||
v::notBlank()->assert([$value]);
|
||||
v::notBlank()->check([$value]);
|
||||
}
|
||||
|
||||
//Check the value inside an object
|
||||
foreach ($notBlankValues as $value) {
|
||||
$obj = new stdClass();
|
||||
$obj->testProp = $value;
|
||||
|
||||
v::notBlank()->assert($obj);
|
||||
v::notBlank()->check($obj);
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
|
@ -1,23 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\AllOfException;
|
||||
use Respect\Validation\Exceptions\NotBlankException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::notBlank()->check(null);
|
||||
} catch (NotBlankException $e) {
|
||||
echo $e->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::notBlank()->assert('');
|
||||
} catch (AllOfException $e) {
|
||||
echo $e->getFullMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
The value must not be blank
|
||||
- The value must not be blank
|
|
@ -1,23 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\AllOfException;
|
||||
use Respect\Validation\Exceptions\NotBlankException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::notBlank()->setName('Field')->check(null);
|
||||
} catch (NotBlankException $e) {
|
||||
echo $e->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::notBlank()->setName('Field')->assert('');
|
||||
} catch (AllOfException $e) {
|
||||
echo $e->getFullMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
Field must not be blank
|
||||
- Field must not be blank
|
|
@ -1,23 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\AllOfException;
|
||||
use Respect\Validation\Exceptions\NotBlankException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::not(v::notBlank())->check(1);
|
||||
} catch (NotBlankException $e) {
|
||||
echo $e->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::notBlank())->assert([1]);
|
||||
} catch (AllOfException $e) {
|
||||
echo $e->getFullMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
1 must be blank
|
||||
- `{ 1 }` must be blank
|
|
@ -13,98 +13,63 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Respect\Validation\Test\RuleTestCase;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
* @covers \Respect\Validation\Exceptions\NotBlankException
|
||||
* @group rule
|
||||
*
|
||||
* @covers \Respect\Validation\Rules\NotBlank
|
||||
*
|
||||
* @author Danilo Correa <danilosilva87@gmail.com>
|
||||
* @author Gabriel Caruso <carusogabriel34@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
class NotBlankTest extends TestCase
|
||||
final class NotBlankTest extends RuleTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider providerForNotBlank
|
||||
*
|
||||
* @test
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function shouldValidateWhenNotBlank($input): void
|
||||
{
|
||||
$rule = new NotBlank();
|
||||
|
||||
self::assertTrue($rule->validate($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForBlank
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function shouldNotValidateWhenBlank($input): void
|
||||
{
|
||||
$rule = new NotBlank();
|
||||
|
||||
self::assertFalse($rule->validate($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Respect\Validation\Exceptions\NotBlankException
|
||||
* @expectedExceptionMessage The value must not be blank
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function shouldThrowExceptionWhenFailure(): void
|
||||
{
|
||||
$rule = new NotBlank();
|
||||
$rule->check(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Respect\Validation\Exceptions\NotBlankException
|
||||
* @expectedExceptionMessage whatever must not be blank
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function shouldThrowExceptionWhenFailureAndDoesHaveAName(): void
|
||||
{
|
||||
$rule = new NotBlank();
|
||||
$rule->setName('whatever');
|
||||
$rule->check(0);
|
||||
}
|
||||
|
||||
public function providerForNotBlank()
|
||||
public function providerForValidInput(): array
|
||||
{
|
||||
$object = new stdClass();
|
||||
$object->foo = true;
|
||||
|
||||
$rule = new NotBlank();
|
||||
|
||||
return [
|
||||
[1],
|
||||
[' oi'],
|
||||
[[5]],
|
||||
[[1]],
|
||||
[$object],
|
||||
[$rule, 1],
|
||||
[$rule, ' oi'],
|
||||
[$rule, [5]],
|
||||
[$rule, [1]],
|
||||
[$rule, $object],
|
||||
];
|
||||
}
|
||||
|
||||
public function providerForBlank()
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerForInvalidInput(): array
|
||||
{
|
||||
$rule = new NotBlank();
|
||||
|
||||
return [
|
||||
[null],
|
||||
[''],
|
||||
[[]],
|
||||
[' '],
|
||||
[0],
|
||||
['0'],
|
||||
[0],
|
||||
['0.0'],
|
||||
[false],
|
||||
[['']],
|
||||
[[' ']],
|
||||
[[0]],
|
||||
[['0']],
|
||||
[[false]],
|
||||
[[[''], [0]]],
|
||||
[new stdClass()],
|
||||
[$rule, null],
|
||||
[$rule, ''],
|
||||
[$rule, []],
|
||||
[$rule, ' '],
|
||||
[$rule, 0],
|
||||
[$rule, '0'],
|
||||
[$rule, 0],
|
||||
[$rule, '0.0'],
|
||||
[$rule, false],
|
||||
[$rule, ['']],
|
||||
[$rule, [' ']],
|
||||
[$rule, [0]],
|
||||
[$rule, ['0']],
|
||||
[$rule, [false]],
|
||||
[$rule, [[''], [0]]],
|
||||
[$rule, new stdClass()],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue