Apply contribution guidelines to "ObjectType" rule

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Henrique Moody 2018-03-26 23:37:42 +02:00
parent 34d42a0d70
commit e203f8aec5
No known key found for this signature in database
GPG key ID: 221E9281655813A6
9 changed files with 89 additions and 108 deletions

View file

@ -2,7 +2,7 @@
- `ObjectType()` - `ObjectType()`
Validates if the input is an object. Validates whether the input is an [object](http://php.net/types.object).
```php ```php
v::objectType()->validate(new stdClass); // true v::objectType()->validate(new stdClass); // true

View file

@ -13,14 +13,23 @@ declare(strict_types=1);
namespace Respect\Validation\Exceptions; 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 = [ public static $defaultTemplates = [
self::MODE_DEFAULT => [ self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be an object', self::STANDARD => '{{name}} must be of type object',
], ],
self::MODE_NEGATIVE => [ self::MODE_NEGATIVE => [
self::STANDARD => '{{name}} must not be an object', self::STANDARD => '{{name}} must not be of type object',
], ],
]; ];
} }

View file

@ -13,8 +13,19 @@ declare(strict_types=1);
namespace Respect\Validation\Rules; 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 public function validate($input): bool
{ {
return is_object($input); return is_object($input);

View 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

View file

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

View file

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

View file

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

View file

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

View file

@ -13,59 +13,48 @@ declare(strict_types=1);
namespace Respect\Validation\Rules; 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\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)); $rule = new ObjectType();
$this->object->assert($input);
$this->object->check($input);
}
/**
* @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 [ return [
[new \stdClass()], [$rule, new stdClass()],
[new \ArrayObject()], [$rule, new ArrayObject()],
]; ];
} }
public function providerForNotObject() /**
* {@inheritdoc}
*/
public function providerForInvalidInput(): array
{ {
$rule = new ObjectType();
return [ return [
[''], [$rule, ''],
[null], [$rule, null],
[121], [$rule, 121],
[[]], [$rule, []],
['Foo'], [$rule, 'Foo'],
[false], [$rule, false],
]; ];
} }
} }