mirror of
https://github.com/Respect/Validation.git
synced 2024-06-29 10:40:24 +02:00
Apply contribution guidelines to "ObjectType" rule
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
34d42a0d70
commit
e203f8aec5
|
@ -2,7 +2,7 @@
|
|||
|
||||
- `ObjectType()`
|
||||
|
||||
Validates if the input is an object.
|
||||
Validates whether the input is an [object](http://php.net/types.object).
|
||||
|
||||
```php
|
||||
v::objectType()->validate(new stdClass); // true
|
||||
|
|
|
@ -13,14 +13,23 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class ObjectTypeException extends ValidationException
|
||||
/**
|
||||
* Exception class for ObjectType rule.
|
||||
*
|
||||
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
final class ObjectTypeException extends ValidationException
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $defaultTemplates = [
|
||||
self::MODE_DEFAULT => [
|
||||
self::STANDARD => '{{name}} must be an object',
|
||||
self::STANDARD => '{{name}} must be of type object',
|
||||
],
|
||||
self::MODE_NEGATIVE => [
|
||||
self::STANDARD => '{{name}} must not be an object',
|
||||
self::STANDARD => '{{name}} must not be of type object',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
|
|
@ -13,8 +13,19 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class ObjectType extends AbstractRule
|
||||
use function is_object;
|
||||
|
||||
/**
|
||||
* Validates whether the input is an object.
|
||||
*
|
||||
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
final class ObjectType extends AbstractRule
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($input): bool
|
||||
{
|
||||
return is_object($input);
|
||||
|
|
37
tests/integration/rules/objectType.phpt
Normal file
37
tests/integration/rules/objectType.phpt
Normal file
|
@ -0,0 +1,37 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Exceptions\ObjectTypeException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::objectType()->check([]);
|
||||
} catch (ObjectTypeException $exception) {
|
||||
echo $exception->getMainMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::objectType())->check(new stdClass());
|
||||
} catch (ObjectTypeException $exception) {
|
||||
echo $exception->getMainMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::objectType()->assert('test');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::objectType())->assert(new ArrayObject());
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
`{ }` must be of type object
|
||||
`[object] (stdClass: { })` must not be of type object
|
||||
- "test" must be of type object
|
||||
- `[traversable] (ArrayObject: { })` must not be of type object
|
|
@ -1,10 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
v::objectType()->assert(new stdClass());
|
||||
v::objectType()->check(new stdClass());
|
||||
?>
|
||||
--EXPECTF--
|
|
@ -1,29 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\ObjectTypeException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::objectType()->check('');
|
||||
} catch (ObjectTypeException $exception) {
|
||||
echo $exception->getMainMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::objectType()->check(true);
|
||||
} catch (ObjectTypeException $exception) {
|
||||
echo $exception->getMainMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::objectType()->check(0);
|
||||
} catch (ObjectTypeException $exception) {
|
||||
echo $exception->getMainMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
"" must be an object
|
||||
`TRUE` must be an object
|
||||
0 must be an object
|
|
@ -1,15 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\ObjectTypeException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::not(v::objectType())->check(new stdClass());
|
||||
} catch (ObjectTypeException $exception) {
|
||||
echo $exception->getMainMessage();
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
`[object] (stdClass: { })` must not be an object
|
|
@ -1,11 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
v::not(v::objectType())->check('');
|
||||
v::not(v::objectType())->check(true);
|
||||
v::not(v::objectType())->check(0);
|
||||
?>
|
||||
--EXPECTF--
|
|
@ -13,59 +13,48 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use ArrayObject;
|
||||
use Respect\Validation\Test\RuleTestCase;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
* @group rule
|
||||
*
|
||||
* @covers \Respect\Validation\Rules\ObjectType
|
||||
* @covers \Respect\Validation\Exceptions\ObjectTypeException
|
||||
*
|
||||
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
* @author Gabriel Caruso <carusogabriel34@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
class ObjectTypeTest extends TestCase
|
||||
final class ObjectTypeTest extends RuleTestCase
|
||||
{
|
||||
protected $object;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->object = new ObjectType();
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForObject
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function testObject($input): void
|
||||
public function providerForValidInput(): array
|
||||
{
|
||||
self::assertTrue($this->object->__invoke($input));
|
||||
$this->object->assert($input);
|
||||
$this->object->check($input);
|
||||
}
|
||||
$rule = new ObjectType();
|
||||
|
||||
/**
|
||||
* @dataProvider providerForNotObject
|
||||
* @expectedException \Respect\Validation\Exceptions\ObjectTypeException
|
||||
*/
|
||||
public function testNotObject($input): void
|
||||
{
|
||||
self::assertFalse($this->object->__invoke($input));
|
||||
$this->object->assert($input);
|
||||
}
|
||||
|
||||
public function providerForObject()
|
||||
{
|
||||
return [
|
||||
[new \stdClass()],
|
||||
[new \ArrayObject()],
|
||||
[$rule, new stdClass()],
|
||||
[$rule, new ArrayObject()],
|
||||
];
|
||||
}
|
||||
|
||||
public function providerForNotObject()
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerForInvalidInput(): array
|
||||
{
|
||||
$rule = new ObjectType();
|
||||
|
||||
return [
|
||||
[''],
|
||||
[null],
|
||||
[121],
|
||||
[[]],
|
||||
['Foo'],
|
||||
[false],
|
||||
[$rule, ''],
|
||||
[$rule, null],
|
||||
[$rule, 121],
|
||||
[$rule, []],
|
||||
[$rule, 'Foo'],
|
||||
[$rule, false],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue