Apply contribution guidelines to "CallableType" rule

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Henrique Moody 2018-03-26 22:15:06 +02:00
parent 47eaea6f28
commit 34d42a0d70
No known key found for this signature in database
GPG key ID: 221E9281655813A6
9 changed files with 77 additions and 182 deletions

View file

@ -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

View file

@ -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',
],
];
}

View file

@ -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}

View 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

View file

@ -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--

View file

@ -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

View file

@ -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--

View file

@ -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

View file

@ -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],
];
}
}