Apply contribution guidelines to "NullType" rule

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Henrique Moody 2018-03-26 22:44:18 +02:00
commit ad9cab5daf
No known key found for this signature in database
GPG key ID: 221E9281655813A6
9 changed files with 85 additions and 110 deletions

View file

@ -2,7 +2,7 @@
- `NullType()`
Validates if the input is null.
Validates whether the input is [null](http://php.net/types.null).
```php
v::nullType()->validate(null); // true

View file

@ -13,8 +13,17 @@ declare(strict_types=1);
namespace Respect\Validation\Exceptions;
class NullTypeException extends ValidationException
/**
* Exception class for NullType.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
final class NullTypeException extends ValidationException
{
/**
* {@inheritdoc}
*/
public static $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be null',

View file

@ -13,8 +13,19 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
class NullType extends NotEmpty
use function is_null;
/**
* Validates whether the input is null.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
final class NullType extends NotEmpty
{
/**
* {@inheritdoc}
*/
public function validate($input): bool
{
return is_null($input);

View file

@ -0,0 +1,37 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Exceptions\NullTypeException;
use Respect\Validation\Validator as v;
try {
v::nullType()->check('');
} catch (NullTypeException $exception) {
echo $exception->getMainMessage().PHP_EOL;
}
try {
v::not(v::nullType())->check(null);
} catch (NullTypeException $exception) {
echo $exception->getMainMessage().PHP_EOL;
}
try {
v::nullType()->assert(false);
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
try {
v::not(v::nullType())->assert(null);
} catch (NestedValidationException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
?>
--EXPECTF--
"" must be null
`NULL` must not be null
- `FALSE` must be null
- `NULL` must not be null

View file

@ -1,11 +0,0 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Validator as v;
v::nullType()->assert(null);
v::nullType()->check(null);
?>
--EXPECTF--

View file

@ -1,23 +0,0 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\AllOfException;
use Respect\Validation\Exceptions\NullTypeException;
use Respect\Validation\Validator as v;
try {
v::nullType()->check(1);
} catch (NullTypeException $e) {
echo $e->getMainMessage().PHP_EOL;
}
try {
v::nullType()->assert('');
} catch (AllOfException $e) {
echo $e->getFullMessage().PHP_EOL;
}
?>
--EXPECTF--
1 must be null
- "" must be null

View file

@ -1,23 +0,0 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\AllOfException;
use Respect\Validation\Exceptions\NullTypeException;
use Respect\Validation\Validator as v;
try {
v::nullType()->setName('Field')->check('');
} catch (NullTypeException $e) {
echo $e->getMainMessage().PHP_EOL;
}
try {
v::nullType()->setName('Field')->assert('');
} catch (AllOfException $e) {
echo $e->getFullMessage().PHP_EOL;
}
?>
--EXPECTF--
Field must be null
- Field must be null

View file

@ -1,23 +0,0 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\AllOfException;
use Respect\Validation\Exceptions\NullTypeException;
use Respect\Validation\Validator as v;
try {
v::not(v::nullType())->check(null);
} catch (NullTypeException $e) {
echo $e->getMainMessage().PHP_EOL;
}
try {
v::not(v::nullType())->assert(null);
} catch (AllOfException $e) {
echo $e->getFullMessage().PHP_EOL;
}
?>
--EXPECTF--
`NULL` must not be null
- `NULL` must not be null

View file

@ -13,47 +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\NullType
* @covers \Respect\Validation\Exceptions\NullTypeException
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Gabriel Caruso <carusogabriel34@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Nick Lombard <github@jigsoft.co.za>
*/
class NullTypeTest extends TestCase
final class NullTypeTest extends RuleTestCase
{
protected $object;
protected function setUp(): void
/**
* {@inheritdoc}
*/
public function providerForValidInput(): array
{
$this->object = new NullType();
}
$rule = new NullType();
public function testNullValue(): void
{
$this->object->assert(null);
self::assertTrue($this->object->__invoke(null));
$this->object->check(null);
return [
[$rule, null],
];
}
/**
* @dataProvider providerForNotNull
* @expectedException \Respect\Validation\Exceptions\NullTypeException
* {@inheritdoc}
*/
public function testNotNull($input): void
public function providerForInvalidInput(): array
{
self::assertFalse($this->object->__invoke($input));
$this->object->assert($input);
}
$rule = new NullType();
public function providerForNotNull()
{
return [
[''],
[0],
['w poiur'],
[' '],
['Foo'],
[$rule, ''],
[$rule, false],
[$rule, []],
[$rule, 0],
[$rule, 'w poiur'],
];
}
}