diff --git a/docs/ObjectType.md b/docs/ObjectType.md index b78f9d4c..1d481e0b 100644 --- a/docs/ObjectType.md +++ b/docs/ObjectType.md @@ -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 diff --git a/library/Exceptions/ObjectTypeException.php b/library/Exceptions/ObjectTypeException.php index e99701c8..5fdfe868 100644 --- a/library/Exceptions/ObjectTypeException.php +++ b/library/Exceptions/ObjectTypeException.php @@ -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 + * @author Henrique Moody + */ +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', ], ]; } diff --git a/library/Rules/ObjectType.php b/library/Rules/ObjectType.php index 8440b1a5..23eb2c38 100644 --- a/library/Rules/ObjectType.php +++ b/library/Rules/ObjectType.php @@ -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 + * @author Henrique Moody + */ +final class ObjectType extends AbstractRule { + /** + * {@inheritdoc} + */ public function validate($input): bool { return is_object($input); diff --git a/tests/integration/rules/objectType.phpt b/tests/integration/rules/objectType.phpt new file mode 100644 index 00000000..b67454db --- /dev/null +++ b/tests/integration/rules/objectType.phpt @@ -0,0 +1,37 @@ +--FILE-- +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 diff --git a/tests/integration/rules/objectType_1.phpt b/tests/integration/rules/objectType_1.phpt deleted file mode 100644 index 0edd9e2b..00000000 --- a/tests/integration/rules/objectType_1.phpt +++ /dev/null @@ -1,10 +0,0 @@ ---FILE-- -assert(new stdClass()); -v::objectType()->check(new stdClass()); -?> ---EXPECTF-- diff --git a/tests/integration/rules/objectType_2.phpt b/tests/integration/rules/objectType_2.phpt deleted file mode 100644 index 5271d883..00000000 --- a/tests/integration/rules/objectType_2.phpt +++ /dev/null @@ -1,29 +0,0 @@ ---FILE-- -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 diff --git a/tests/integration/rules/objectType_3.phpt b/tests/integration/rules/objectType_3.phpt deleted file mode 100644 index efb2cbcc..00000000 --- a/tests/integration/rules/objectType_3.phpt +++ /dev/null @@ -1,15 +0,0 @@ ---FILE-- -check(new stdClass()); -} catch (ObjectTypeException $exception) { - echo $exception->getMainMessage(); -} -?> ---EXPECTF-- -`[object] (stdClass: { })` must not be an object diff --git a/tests/integration/rules/objectType_4.phpt b/tests/integration/rules/objectType_4.phpt deleted file mode 100644 index fbdf7cdd..00000000 --- a/tests/integration/rules/objectType_4.phpt +++ /dev/null @@ -1,11 +0,0 @@ ---FILE-- -check(''); -v::not(v::objectType())->check(true); -v::not(v::objectType())->check(0); -?> ---EXPECTF-- \ No newline at end of file diff --git a/tests/unit/Rules/ObjectTypeTest.php b/tests/unit/Rules/ObjectTypeTest.php index 2784c21d..bcb46e57 100644 --- a/tests/unit/Rules/ObjectTypeTest.php +++ b/tests/unit/Rules/ObjectTypeTest.php @@ -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 + * @author Gabriel Caruso + * @author Henrique Moody */ -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], ]; } }