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'); + } +}