From 64cc62ea9cd0c5d6be32d55848d80f045d5b6a59 Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Sat, 9 Jul 2016 10:33:13 +0200 Subject: [PATCH] Implemented Int type. --- src/engine/Types/Int.php | 8 ++++++-- src/engine/Types/Type.php | 4 ++-- test/php/engine/Types/IntTest.php | 26 ++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 test/php/engine/Types/IntTest.php diff --git a/src/engine/Types/Int.php b/src/engine/Types/Int.php index b3bf8557..1b018b86 100644 --- a/src/engine/Types/Int.php +++ b/src/engine/Types/Int.php @@ -11,8 +11,12 @@ * @since v1.2.0 * ---------------------------------------------------------------------------- */ -namespace \EA\Engine\Types; +namespace EA\Engine\Types; class Int extends Type { - + protected function _validate() { + if (!is_numeric($this->value) && (int)$this->value !== (float)$this->value) { + throw new \InvalidArgumentException('Invalid type value provided (expected int): ' . $this->value); + } + } } diff --git a/src/engine/Types/Type.php b/src/engine/Types/Type.php index 343857ca..71f24276 100644 --- a/src/engine/Types/Type.php +++ b/src/engine/Types/Type.php @@ -11,7 +11,7 @@ * @since v1.2.0 * ---------------------------------------------------------------------------- */ -namespace \EA\Engine\Types; +namespace EA\Engine\Types; /** * Abstract Type Class @@ -25,8 +25,8 @@ abstract class Type { * @param mixed $value The type value to be validated. */ public function __construct($value) { - $this->_validate($value); $this->value = $value; + $this->_validate($value); } /** diff --git a/test/php/engine/Types/IntTest.php b/test/php/engine/Types/IntTest.php new file mode 100644 index 00000000..f0a53d43 --- /dev/null +++ b/test/php/engine/Types/IntTest.php @@ -0,0 +1,26 @@ + + * @copyright Copyright (c) 2013 - 2016, Alex Tselegidis + * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link http://easyappointments.org + * @since v1.2.0 + * ---------------------------------------------------------------------------- */ + +namespace EA\Engine\Types; + +class AuthorizationTest extends \PHPUnit_Framework_TestCase { + public function testIntType() { + $type = new Int(1); + $this->assertEquals(1, $type->get()); + } + + public function testIntTypeThrowsExceptionWithInvalidValue() { + $this->setExpectedException('\InvalidArgumentException'); + new Int('invalid'); + } +}