mirror of
https://github.com/Respect/Validation.git
synced 2024-06-04 23:02:16 +02:00
Apply contribution guidelines to "CallableType" rule
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
47eaea6f28
commit
34d42a0d70
|
@ -2,7 +2,7 @@
|
|||
|
||||
- `CallableType()`
|
||||
|
||||
Validates if the input is a callable value.
|
||||
Validates whether the pseudo-type of the input is [callable](http://php.net/types.callable).
|
||||
|
||||
```php
|
||||
v::callableType()->validate(function () {}); // true
|
||||
|
|
|
@ -14,19 +14,21 @@ declare(strict_types=1);
|
|||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
/**
|
||||
* Exception class for CallableType rule.
|
||||
*
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
class CallableTypeException extends ValidationException
|
||||
final class CallableTypeException extends ValidationException
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $defaultTemplates = [
|
||||
self::MODE_DEFAULT => [
|
||||
self::STANDARD => '{{name}} must be a callable',
|
||||
self::STANDARD => '{{name}} must be callable',
|
||||
],
|
||||
self::MODE_NEGATIVE => [
|
||||
self::STANDARD => '{{name}} must not be a callable',
|
||||
self::STANDARD => '{{name}} must not be callable',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
|
|
@ -13,10 +13,14 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use function is_callable;
|
||||
|
||||
/**
|
||||
* Validates whether the pseudo-type of the input is callable.
|
||||
*
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
class CallableType extends AbstractRule
|
||||
final class CallableType extends AbstractRule
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
|
|
38
tests/integration/rules/callableType.phpt
Normal file
38
tests/integration/rules/callableType.phpt
Normal file
|
@ -0,0 +1,38 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\CallableTypeException;
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::callableType()->check([]);
|
||||
} catch (CallableTypeException $exception) {
|
||||
echo $exception->getMainMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::callableType())->check('trim');
|
||||
} catch (CallableTypeException $exception) {
|
||||
echo $exception->getMainMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::callableType()->assert(true);
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::callableType())->assert(function (): void {
|
||||
});
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
`{ }` must be callable
|
||||
"trim" must not be callable
|
||||
- `TRUE` must be callable
|
||||
- `[object] (Closure: { })` must not be callable
|
|
@ -1,12 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
v::callableType()->validate(function (): void {
|
||||
});
|
||||
v::callableType()->validate('trim');
|
||||
v::callableType()->validate(v::callableType(), 'validate');
|
||||
?>
|
||||
--EXPECTF--
|
|
@ -1,44 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\CallableTypeException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::callableType()->check([]);
|
||||
} catch (CallableTypeException $exception) {
|
||||
echo $exception->getMainMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::callableType()->check('oneInexistentFunction');
|
||||
} catch (CallableTypeException $exception) {
|
||||
echo $exception->getMainMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::callableType()->check(100);
|
||||
} catch (CallableTypeException $exception) {
|
||||
echo $exception->getMainMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::callableType()->check(null);
|
||||
} catch (CallableTypeException $exception) {
|
||||
echo $exception->getMainMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::callableType()->check('');
|
||||
} catch (CallableTypeException $exception) {
|
||||
echo $exception->getMainMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
`{ }` must be a callable
|
||||
"oneInexistentFunction" must be a callable
|
||||
100 must be a callable
|
||||
`NULL` must be a callable
|
||||
"" must be a callable
|
|
@ -1,38 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\CallableTypeException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::not(v::callableType())->check([]);
|
||||
} catch (CallableTypeException $exception) {
|
||||
echo $exception->getFullMessage();
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::callableType())->check('oneInexistentFunction');
|
||||
} catch (CallableTypeException $exception) {
|
||||
echo $exception->getFullMessage();
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::callableType())->check(100);
|
||||
} catch (CallableTypeException $exception) {
|
||||
echo $exception->getFullMessage();
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::callableType())->check(null);
|
||||
} catch (CallableTypeException $exception) {
|
||||
echo $exception->getFullMessage();
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::callableType())->check('');
|
||||
} catch (CallableTypeException $exception) {
|
||||
echo $exception->getFullMessage();
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
|
@ -1,38 +0,0 @@
|
|||
--SKIPIF--
|
||||
<?php
|
||||
if (defined('HHVM_VERSION')) {
|
||||
die('skip: Not working on hhvm because of dynamic callable id');
|
||||
}
|
||||
?>
|
||||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\CallableTypeException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
$x = function (): void {
|
||||
};
|
||||
v::not(v::callableType())->check($x);
|
||||
} catch (CallableTypeException $e) {
|
||||
echo $e->getMainMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::callableType())->check('trim');
|
||||
} catch (CallableTypeException $e) {
|
||||
echo $e->getMainMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::callableType())->check(v::callableType(), 'validate');
|
||||
} catch (CallableTypeException $e) {
|
||||
echo $e->getMainMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
`[object] (Closure: { })` must not be a callable
|
||||
"trim" must not be a callable
|
||||
`[object] (Respect\Validation\Validator: { })` must not be a callable
|
|
@ -13,66 +13,49 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Respect\Validation\Test\RuleTestCase;
|
||||
use stdClass;
|
||||
use const INF;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
* @group rule
|
||||
*
|
||||
* @covers \Respect\Validation\Rules\CallableType
|
||||
* @covers \Respect\Validation\Exceptions\CallableTypeException
|
||||
*
|
||||
* @author Gabriel Caruso <carusogabriel34@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
class CallableTypeTest extends TestCase
|
||||
final class CallableTypeTest extends RuleTestCase
|
||||
{
|
||||
protected $rule;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->rule = new CallableType();
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForCallable
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function testShouldValidateCallableTypeNumbers($input): void
|
||||
public function providerForValidInput(): array
|
||||
{
|
||||
self::assertTrue($this->rule->validate($input));
|
||||
}
|
||||
$rule = new CallableType();
|
||||
|
||||
/**
|
||||
* @dataProvider providerForNonCallable
|
||||
*/
|
||||
public function testShouldNotValidateNonCallableTypeNumbers($input): void
|
||||
{
|
||||
self::assertFalse($this->rule->validate($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Respect\Validation\Exceptions\CallableTypeException
|
||||
* @expectedExceptionMessage "testShouldThrowCallableTypeExceptionWhenChecking" must be a callable
|
||||
*/
|
||||
public function testShouldThrowCallableTypeExceptionWhenChecking(): void
|
||||
{
|
||||
$this->rule->check(__FUNCTION__);
|
||||
}
|
||||
|
||||
public function providerForCallable()
|
||||
{
|
||||
return [
|
||||
[function (): void {
|
||||
[$rule, function (): void {
|
||||
}],
|
||||
['trim'],
|
||||
[__METHOD__],
|
||||
[[$this, __FUNCTION__]],
|
||||
[$rule, 'trim'],
|
||||
[$rule, __METHOD__],
|
||||
[$rule, [$this, __FUNCTION__]],
|
||||
];
|
||||
}
|
||||
|
||||
public function providerForNonCallable()
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerForInvalidInput(): array
|
||||
{
|
||||
$rule = new CallableType();
|
||||
|
||||
return [
|
||||
[' '],
|
||||
[INF],
|
||||
[[]],
|
||||
[new \stdClass()],
|
||||
[null],
|
||||
[$rule, ' '],
|
||||
[$rule, INF],
|
||||
[$rule, []],
|
||||
[$rule, new stdClass()],
|
||||
[$rule, null],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue