From 5362c307842d7e58fc531a0f8ce9fb66085a0ade Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Sat, 9 Jul 2016 11:03:34 +0200 Subject: [PATCH] Implemented scalar types for the app. --- src/engine/Types/Bool.php | 6 ++-- src/engine/Types/Email.php | 20 +++++++++++++ src/engine/Types/{Double.php => Float.php} | 8 +++-- src/engine/Types/Int.php | 6 ++-- src/engine/Types/NonEmptyString.php | 20 +++++++++++++ src/engine/Types/String.php | 6 ++-- src/engine/Types/Type.php | 11 +++++-- src/engine/Types/UnsignedInt.php | 6 ++-- src/engine/Types/Url.php | 20 +++++++++++++ test/php/engine/Types/BoolTest.php | 26 ++++++++++++++++ test/php/engine/Types/EmailTest.php | 31 ++++++++++++++++++++ test/php/engine/Types/FloatTest.php | 26 ++++++++++++++++ test/php/engine/Types/IntTest.php | 9 ++++-- test/php/engine/Types/NonEmptyStringTest.php | 31 ++++++++++++++++++++ test/php/engine/Types/StringTest.php | 26 ++++++++++++++++ test/php/engine/Types/UnsignedIntTest.php | 31 ++++++++++++++++++++ test/php/engine/Types/UrlTest.php | 31 ++++++++++++++++++++ 17 files changed, 296 insertions(+), 18 deletions(-) rename src/engine/Types/{Double.php => Float.php} (78%) create mode 100644 src/engine/Types/NonEmptyString.php create mode 100644 src/engine/Types/Url.php create mode 100644 test/php/engine/Types/BoolTest.php create mode 100644 test/php/engine/Types/EmailTest.php create mode 100644 test/php/engine/Types/FloatTest.php create mode 100644 test/php/engine/Types/NonEmptyStringTest.php create mode 100644 test/php/engine/Types/StringTest.php create mode 100644 test/php/engine/Types/UnsignedIntTest.php create mode 100644 test/php/engine/Types/UrlTest.php diff --git a/src/engine/Types/Bool.php b/src/engine/Types/Bool.php index baa628b3..d2d255e8 100644 --- a/src/engine/Types/Bool.php +++ b/src/engine/Types/Bool.php @@ -11,8 +11,10 @@ * @since v1.2.0 * ---------------------------------------------------------------------------- */ -namespace \EA\Engine\Types; +namespace EA\Engine\Types; class Bool extends Type { - + protected function _validate($value) { + return is_bool($value); + } } diff --git a/src/engine/Types/Email.php b/src/engine/Types/Email.php index e69de29b..5d338596 100644 --- a/src/engine/Types/Email.php +++ b/src/engine/Types/Email.php @@ -0,0 +1,20 @@ + + * @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 Email extends NonEmptyString { + protected function _validate($value) { + return parent::_validate($value) && filter_var($value, FILTER_VALIDATE_EMAIL); + } +} diff --git a/src/engine/Types/Double.php b/src/engine/Types/Float.php similarity index 78% rename from src/engine/Types/Double.php rename to src/engine/Types/Float.php index 30d29ebc..e26c695b 100644 --- a/src/engine/Types/Double.php +++ b/src/engine/Types/Float.php @@ -11,8 +11,10 @@ * @since v1.2.0 * ---------------------------------------------------------------------------- */ -namespace \EA\Engine\Types; - -class Double extends Type { +namespace EA\Engine\Types; +class Float extends Type { + protected function _validate($value) { + return is_float($value); + } } diff --git a/src/engine/Types/Int.php b/src/engine/Types/Int.php index 1b018b86..affa0d64 100644 --- a/src/engine/Types/Int.php +++ b/src/engine/Types/Int.php @@ -14,9 +14,7 @@ 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); - } + protected function _validate($value) { + return is_numeric($value) && !is_float($value); } } diff --git a/src/engine/Types/NonEmptyString.php b/src/engine/Types/NonEmptyString.php new file mode 100644 index 00000000..516e3934 --- /dev/null +++ b/src/engine/Types/NonEmptyString.php @@ -0,0 +1,20 @@ + + * @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 NonEmptyString extends String { + protected function _validate($value) { + return parent::_validate($value) && $value !== ''; + } +} diff --git a/src/engine/Types/String.php b/src/engine/Types/String.php index 7fddb205..2f86e930 100644 --- a/src/engine/Types/String.php +++ b/src/engine/Types/String.php @@ -11,8 +11,10 @@ * @since v1.2.0 * ---------------------------------------------------------------------------- */ -namespace \EA\Engine\Types; +namespace EA\Engine\Types; class String extends Type { - + protected function _validate($value) { + return is_string($value); + } } diff --git a/src/engine/Types/Type.php b/src/engine/Types/Type.php index 71f24276..660d285a 100644 --- a/src/engine/Types/Type.php +++ b/src/engine/Types/Type.php @@ -25,8 +25,11 @@ abstract class Type { * @param mixed $value The type value to be validated. */ public function __construct($value) { + if (!$this->_validate($value)) { + throw new \InvalidArgumentException(__CLASS__ . ': Invalid argument value provided (' . $value . ')'); + } + $this->value = $value; - $this->_validate($value); } /** @@ -40,8 +43,10 @@ abstract class Type { /** * Implement the validation logic in the children classes. + * + * @param mixed $value The value to be validated. * - * @throws \InvalidArgumentException If the validation fails. + * @return bool Returns whether the value is valid or not. */ - abstract protected function _validate(); + abstract protected function _validate($value); } diff --git a/src/engine/Types/UnsignedInt.php b/src/engine/Types/UnsignedInt.php index e55b3fea..3af061b8 100644 --- a/src/engine/Types/UnsignedInt.php +++ b/src/engine/Types/UnsignedInt.php @@ -11,8 +11,10 @@ * @since v1.2.0 * ---------------------------------------------------------------------------- */ -namespace \EA\Engine\Types; +namespace EA\Engine\Types; class UnsignedInt extends Int { - + protected function _validate($value) { + return parent::_validate($value) && $value > -1; + } } diff --git a/src/engine/Types/Url.php b/src/engine/Types/Url.php new file mode 100644 index 00000000..367d055d --- /dev/null +++ b/src/engine/Types/Url.php @@ -0,0 +1,20 @@ + + * @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 Url extends NonEmptyString { + protected function _validate($value) { + return parent::_validate($value) && filter_var($value, FILTER_VALIDATE_URL); + } +} diff --git a/test/php/engine/Types/BoolTest.php b/test/php/engine/Types/BoolTest.php new file mode 100644 index 00000000..da790590 --- /dev/null +++ b/test/php/engine/Types/BoolTest.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 BoolTest extends \PHPUnit_Framework_TestCase { + public function testBoolType() { + $type = new Bool(true); + $this->assertEquals(true, $type->get()); + } + + public function testBoolTypeThrowsExceptionWithInvalidValue() { + $this->setExpectedException('\InvalidArgumentException'); + new Bool(null); + } +} diff --git a/test/php/engine/Types/EmailTest.php b/test/php/engine/Types/EmailTest.php new file mode 100644 index 00000000..9820aad5 --- /dev/null +++ b/test/php/engine/Types/EmailTest.php @@ -0,0 +1,31 @@ + + * @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 EmailTest extends \PHPUnit_Framework_TestCase { + public function testEmailType() { + $type = new Email('john@doe.com'); + $this->assertEquals('john@doe.com', $type->get()); + } + + public function testEmailTypeThrowsExceptionWithInvalidEmail() { + $this->setExpectedException('\InvalidArgumentException'); + new Email('abcdef'); + } + + public function testEmailTypeThrowsExceptionWithInvalidValue() { + $this->setExpectedException('\InvalidArgumentException'); + new Email(null); + } +} diff --git a/test/php/engine/Types/FloatTest.php b/test/php/engine/Types/FloatTest.php new file mode 100644 index 00000000..234b27ea --- /dev/null +++ b/test/php/engine/Types/FloatTest.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 FloatTest extends \PHPUnit_Framework_TestCase { + public function testFloatType() { + $type = new Float(100.00); + $this->assertEquals(100.00, $type->get()); + } + + public function testFloatTypeThrowsExceptionWithInvalidValue() { + $this->setExpectedException('\InvalidArgumentException'); + new Float(null); + } +} diff --git a/test/php/engine/Types/IntTest.php b/test/php/engine/Types/IntTest.php index f0a53d43..eaebd519 100644 --- a/test/php/engine/Types/IntTest.php +++ b/test/php/engine/Types/IntTest.php @@ -13,13 +13,18 @@ namespace EA\Engine\Types; -class AuthorizationTest extends \PHPUnit_Framework_TestCase { +class IntTest extends \PHPUnit_Framework_TestCase { public function testIntType() { $type = new Int(1); $this->assertEquals(1, $type->get()); } - public function testIntTypeThrowsExceptionWithInvalidValue() { + public function testIntTypeThrowsExceptionWithFloat() { + $this->setExpectedException('\InvalidArgumentException'); + new Int(100.00); + } + + public function testIntTypeThrowsExceptionWithWithString() { $this->setExpectedException('\InvalidArgumentException'); new Int('invalid'); } diff --git a/test/php/engine/Types/NonEmptyStringTest.php b/test/php/engine/Types/NonEmptyStringTest.php new file mode 100644 index 00000000..95f3f633 --- /dev/null +++ b/test/php/engine/Types/NonEmptyStringTest.php @@ -0,0 +1,31 @@ + + * @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 NonEmptyStringTest extends \PHPUnit_Framework_TestCase { + public function testNonEmptyStringType() { + $type = new NonEmptyString('Hello!'); + $this->assertEquals('Hello!', $type->get()); + } + + public function testNonEmptyStringTypeThrowsExceptionWithEmptyString() { + $this->setExpectedException('\InvalidArgumentException'); + new NonEmptyString(''); + } + + public function testNonEmptyStringTypeThrowsExceptionWithInvalidValue() { + $this->setExpectedException('\InvalidArgumentException'); + new NonEmptyString(null); + } +} diff --git a/test/php/engine/Types/StringTest.php b/test/php/engine/Types/StringTest.php new file mode 100644 index 00000000..adea4ba3 --- /dev/null +++ b/test/php/engine/Types/StringTest.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 StringTest extends \PHPUnit_Framework_TestCase { + public function testStringType() { + $type = new String('Hello!'); + $this->assertEquals('Hello!', $type->get()); + } + + public function testStringTypeThrowsExceptionWithInvalidValue() { + $this->setExpectedException('\InvalidArgumentException'); + new String(null); + } +} diff --git a/test/php/engine/Types/UnsignedIntTest.php b/test/php/engine/Types/UnsignedIntTest.php new file mode 100644 index 00000000..ebadd997 --- /dev/null +++ b/test/php/engine/Types/UnsignedIntTest.php @@ -0,0 +1,31 @@ + + * @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 UnsignedIntTest extends \PHPUnit_Framework_TestCase { + public function testUnsignedIntType() { + $type = new UnsignedInt(1); + $this->assertEquals(1, $type->get()); + } + + public function testUnsignedIntTypeThrowsExceptionWithNegative() { + $this->setExpectedException('\InvalidArgumentException'); + new UnsignedInt(-1); + } + + public function testUnsignedIntTypeThrowsExceptionWithWithString() { + $this->setExpectedException('\InvalidArgumentException'); + new UnsignedInt('invalid'); + } +} diff --git a/test/php/engine/Types/UrlTest.php b/test/php/engine/Types/UrlTest.php new file mode 100644 index 00000000..0d3eb85a --- /dev/null +++ b/test/php/engine/Types/UrlTest.php @@ -0,0 +1,31 @@ + + * @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 UrlTest extends \PHPUnit_Framework_TestCase { + public function testUrlType() { + $type = new Url('http://localhost'); + $this->assertEquals('http://localhost', $type->get()); + } + + public function testUrlTypeThrowsExceptionWithInvalidUrl() { + $this->setExpectedException('\InvalidArgumentException'); + new Url('abcdef'); + } + + public function testUrlTypeThrowsExceptionWithInvalidValue() { + $this->setExpectedException('\InvalidArgumentException'); + new Url(null); + } +}