diff --git a/src/Validator.php b/src/Validator.php index 14795f65..6addfabe 100755 --- a/src/Validator.php +++ b/src/Validator.php @@ -14,6 +14,15 @@ abstract class Validator { + + const TYPE_BOOLEAN = "boolean"; + const TYPE_INTEGER = "integer"; + const TYPE_FLOAT = "double"; /* gettype() returns "double" for historical reasons */ + const TYPE_STRING = "string"; + const TYPE_ARRAY = "array"; + const TYPE_OBJECT = "object"; + const TYPE_MIXED = "mixed"; + /** * Get Description * @@ -23,6 +32,15 @@ abstract class Validator */ abstract public function getDescription(); + /** + * Is array + * + * Returns true if an array or false if not. + * + * @return bool + */ + abstract public function isArray(); + /** * Is valid * @@ -32,4 +50,13 @@ abstract public function getDescription(); * @return bool */ abstract public function isValid($value); + + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + abstract public function getType(); } diff --git a/src/Validator/ArrayList.php b/src/Validator/ArrayList.php index 8f68379d..34bc8239 100644 --- a/src/Validator/ArrayList.php +++ b/src/Validator/ArrayList.php @@ -52,6 +52,30 @@ public function getDescription() return 'Value must be an array and ' . $this->validator->getDescription(); } + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return true; + } + + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType(): string + { + return $this->validator->getType(); + } + /** * Is valid * diff --git a/src/Validator/Assoc.php b/src/Validator/Assoc.php index 6a55eec0..de87e0c0 100644 --- a/src/Validator/Assoc.php +++ b/src/Validator/Assoc.php @@ -35,6 +35,30 @@ public function getDescription() return 'Value must be a valid object.'; } + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return true; + } + + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType(): string + { + return self::TYPE_ARRAY; + } + /** * Is valid * diff --git a/src/Validator/Boolean.php b/src/Validator/Boolean.php index 4d39d279..ab1d6c2b 100644 --- a/src/Validator/Boolean.php +++ b/src/Validator/Boolean.php @@ -51,6 +51,30 @@ public function getDescription() return 'Value must be a boolean'; } + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return false; + } + + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType(): string + { + return self::TYPE_BOOLEAN; + } + /** * Is valid * diff --git a/src/Validator/Domain.php b/src/Validator/Domain.php deleted file mode 100644 index c6ec477f..00000000 --- a/src/Validator/Domain.php +++ /dev/null @@ -1,57 +0,0 @@ - - * @license The MIT License (MIT) - */ - -namespace Utopia\Validator; - -use Utopia\Validator; - -/** - * Domain - * - * Validate that an variable is a valid domain address - * - * @package Utopia\Validator - */ -class Domain extends Validator -{ - /** - * Get Description - * - * Returns validator description - * - * @return string - */ - public function getDescription() - { - return 'Value must be a valid domain'; - } - - /** - * Is valid - * - * Validation will pass when $value is valid domain. - * - * Validates domain names against RFC 1034, RFC 1035, RFC 952, RFC 1123, RFC 2732, RFC 2181, and RFC 1123. - * Also specifically validate hostnames (they must start with an alphanumberic character and contain only alphanumerics or hyphens). - * - * @param mixed $value - * @return bool - */ - public function isValid($value) - { - if (\filter_var($value, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME) === false) { - return false; - } - - return true; - } -} diff --git a/src/Validator/Email.php b/src/Validator/Email.php deleted file mode 100644 index 016a8ccb..00000000 --- a/src/Validator/Email.php +++ /dev/null @@ -1,54 +0,0 @@ - - * @license The MIT License (MIT) - */ - -namespace Utopia\Validator; - -use Utopia\Validator; - -/** - * Email - * - * Validate that an variable is a valid email address - * - * @package Utopia\Validator - */ -class Email extends Validator -{ - /** - * Get Description - * - * Returns validator description - * - * @return string - */ - public function getDescription() - { - return 'Value must be a valid email address'; - } - - /** - * Is valid - * - * Validation will pass when $value is valid email address. - * - * @param mixed $value - * @return bool - */ - public function isValid($value) - { - if (!\filter_var($value, FILTER_VALIDATE_EMAIL)) { - return false; - } - - return true; - } -} diff --git a/src/Validator/FloatValidator.php b/src/Validator/FloatValidator.php index 5bea4f7b..8244a86b 100755 --- a/src/Validator/FloatValidator.php +++ b/src/Validator/FloatValidator.php @@ -35,6 +35,30 @@ public function getDescription() return 'Value must be a valid float'; } + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return false; + } + + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType(): string + { + return self::TYPE_FLOAT; + } + /** * Is valid * diff --git a/src/Validator/HexColor.php b/src/Validator/HexColor.php index 39de9a93..77c63990 100644 --- a/src/Validator/HexColor.php +++ b/src/Validator/HexColor.php @@ -24,6 +24,30 @@ public function getDescription() return 'Value must be a valid Hex color code'; } + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return false; + } + + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType(): string + { + return self::TYPE_STRING; + } + /** * @param mixed $value * @return bool diff --git a/src/Validator/Host.php b/src/Validator/Host.php deleted file mode 100644 index 8f0cc37c..00000000 --- a/src/Validator/Host.php +++ /dev/null @@ -1,70 +0,0 @@ - - * @license The MIT License (MIT) - */ - -namespace Utopia\Validator; - -use Utopia\Validator; - -/** - * Host - * - * Validate that a host is allowed from given whitelisted hosts list - * - * @package Utopia\Validator - */ -class Host extends Validator -{ - protected $whitelist = []; - - /** - * @param array $whitelist - */ - public function __construct(array $whitelist) - { - $this->whitelist = $whitelist; - } - - /** - * Get Description - * - * Returns validator description - * - * @return string - */ - public function getDescription() - { - return 'URL host must be one of: ' . \implode(', ', $this->whitelist); - } - - /** - * Is valid - * - * Validation will pass when $value starts with one of the given hosts - * - * @param mixed $value - * @return bool - */ - public function isValid($value) - { - $urlValidator = new URL(); - - if (!$urlValidator->isValid($value)) { - return false; - } - - if (\in_array(\parse_url($value, PHP_URL_HOST), $this->whitelist)) { - return true; - } - - return false; - } -} diff --git a/src/Validator/IP.php b/src/Validator/IP.php deleted file mode 100644 index 2cb04303..00000000 --- a/src/Validator/IP.php +++ /dev/null @@ -1,100 +0,0 @@ - - * @license The MIT License (MIT) - */ - -namespace Utopia\Validator; - -use Exception; -use Utopia\Validator; - -/** - * IP - * - * Validate that an variable is a valid IP address - * - * @package Utopia\Validator - */ -class IP extends Validator -{ - const ALL = 'all'; - const V4 = 'ipv4'; - const V6 = 'ipv6'; - - /** - * @var string - */ - protected $type = self::ALL; - - /** - * Constructor - * - * Set a the type of IP check. - * - * @param string $type - */ - public function __construct(string $type = self::ALL) - { - if (!in_array($type, [self::ALL, self::V4, self::V6])) { - throw new Exception('Unsupported IP type'); - } - - $this->type = $type; - } - - /** - * Get Description - * - * Returns validator description - * - * @return string - */ - public function getDescription() - { - return 'Value must be a valid IP address'; - } - - /** - * Is valid - * - * Validation will pass when $value is valid IP address. - * - * @param mixed $value - * @return bool - */ - public function isValid($value) - { - switch ($this->type) { - case self::ALL: - if (\filter_var($value, FILTER_VALIDATE_IP)) { - return true; - } - break; - - case self::V4: - if (\filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { - return true; - } - break; - - case self::V6: - if (\filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { - return true; - } - break; - - default: - return false; - break; - } - - return false; - } -} diff --git a/src/Validator/Integer.php b/src/Validator/Integer.php index b7db4b13..01f4ad43 100755 --- a/src/Validator/Integer.php +++ b/src/Validator/Integer.php @@ -35,6 +35,30 @@ public function getDescription() return 'Value must be a valid integer'; } + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return false; + } + + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType(): string + { + return self::TYPE_INTEGER; + } + /** * Is valid * diff --git a/src/Validator/JSON.php b/src/Validator/JSON.php index 810a6f43..05fdd279 100644 --- a/src/Validator/JSON.php +++ b/src/Validator/JSON.php @@ -24,6 +24,30 @@ public function getDescription() return 'Value must be a valid JSON string'; } + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return false; + } + + /** + * Get Type + * + * Returns validator type + * + * @return string + */ + public function getType(): string + { + return self::TYPE_OBJECT; + } + /** * @param mixed $value * @return bool diff --git a/src/Validator/Length.php b/src/Validator/Length.php deleted file mode 100755 index 8db4f427..00000000 --- a/src/Validator/Length.php +++ /dev/null @@ -1,76 +0,0 @@ - - * @license The MIT License (MIT) - */ - -namespace Utopia\Validator; - -use Utopia\Validator; - -/** - * Length - * - * Validates that an length of a string. - * - * @package Utopia\Validator - */ -class Length extends Validator -{ - /** - * @var int - */ - protected $min; - - /** - * @var int - */ - protected $max; - - /** - * @param int $min - * @param int $max - */ - public function __construct($min, $max) - { - $this->min = $min; - $this->max = $max; - } - - /** - * Get Description - * - * Returns validator description - * - * @return string - */ - public function getDescription() - { - return 'Value must be between ' . \number_format($this->min) . ' and ' . \number_format($this->max) . ' chars'; - } - - /** - * Is valid - * - * Validation will pass when $value number is bigger or equal than $min number and lower or equal than $max. - * - * @param mixed $value - * @return bool - */ - public function isValid($value) - { - $length = \mb_strlen($value); - - if ($this->min <= $length && $this->max >= $length) { - return true; - } - - return false; - } -} diff --git a/src/Validator/Mock.php b/src/Validator/Mock.php deleted file mode 100755 index eea088e8..00000000 --- a/src/Validator/Mock.php +++ /dev/null @@ -1,50 +0,0 @@ - - * @license The MIT License (MIT) - */ - -namespace Utopia\Validator; - -use Utopia\Validator; - -/** - * Null - * - * Validate that always validate data as valid - * - * @package Utopia\Validator - */ -class Mock extends Validator -{ - /** - * Get Description - * - * Returns validator description - * - * @return string - */ - public function getDescription() - { - return 'Every input is valid'; - } - - /** - * Is valid - * - * Validation will pass when $value is numeric. - * - * @param mixed $value - * @return bool - */ - public function isValid($value) - { - return true; - } -} diff --git a/src/Validator/Multiple.php b/src/Validator/Multiple.php deleted file mode 100755 index 6f806411..00000000 --- a/src/Validator/Multiple.php +++ /dev/null @@ -1,101 +0,0 @@ - - * @license The MIT License (MIT) - */ - -namespace Utopia\Validator; - -use Utopia\Validator; - -/** - * Multiple - * - * Multiple validator is a container of multiple validations each acting as a rule. - * - * @package Utopia\Validator - */ -class Multiple extends Validator -{ - /** - * @var Validator[] - */ - protected $rules = []; - - /** - * Constructor - * - * Multiple constructor can get any number of arguments containing Validator instances using PHP func_get_args function. - * - * Example: - * - * $multiple = new Multiple($validator1, $validator2, $validator3); - */ - public function __construct() - { - // array of all method arguments - $rules = \func_get_args(); - - foreach ($rules as $rule) { - $this->addRule($rule); - } - } - - /** - * Add rule - * - * Add a new rule to the end of the rules containing array - * - * @param Validator $rule - * @return $this - */ - public function addRule(Validator $rule) - { - $this->rules[] = $rule; - - return $this; - } - - /** - * Get Description - * - * Returns validator description - * - * @return string - */ - public function getDescription() - { - $description = 'Value must be:' . "\n"; - - foreach ($this->rules as $key => $rule) { - $description .= (++$key) . '. ' . $rule->getDescription() . " \n"; - } - - return $description; - } - - /** - * Is valid - * - * Validation will pass when all rules are valid if only one of the rules is invalid validation will fail. - * - * @param mixed $value - * @return bool - */ - public function isValid($value) - { - foreach ($this->rules as $rule) { /* @var $rule Validator */ - if (false === $rule->isValid($value)) { - return false; - } - } - - return true; - } -} diff --git a/src/Validator/Numeric.php b/src/Validator/Numeric.php index d1e3c0d0..53e7887a 100755 --- a/src/Validator/Numeric.php +++ b/src/Validator/Numeric.php @@ -35,6 +35,30 @@ public function getDescription() return 'Value must be a valid number'; } + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return false; + } + + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType(): string + { + return self::TYPE_MIXED; + } + /** * Is valid * diff --git a/src/Validator/Range.php b/src/Validator/Range.php index b323ce7c..e816e8af 100755 --- a/src/Validator/Range.php +++ b/src/Validator/Range.php @@ -71,6 +71,30 @@ public function getDescription() return 'Value must be in range between ' . \number_format($this->min) . ' and ' . \number_format($this->max); } + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return false; + } + + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType(): string + { + return self::TYPE_INTEGER; + } + /** * Is valid * diff --git a/src/Validator/Text.php b/src/Validator/Text.php index afe9f279..72aed3a3 100644 --- a/src/Validator/Text.php +++ b/src/Validator/Text.php @@ -57,6 +57,30 @@ public function getDescription() return $message; } + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return false; + } + + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType(): string + { + return self::TYPE_STRING; + } + /** * Is valid * diff --git a/src/Validator/URL.php b/src/Validator/URL.php deleted file mode 100644 index 3f37b41e..00000000 --- a/src/Validator/URL.php +++ /dev/null @@ -1,54 +0,0 @@ - - * @license The MIT License (MIT) - */ - -namespace Utopia\Validator; - -use Utopia\Validator; - -/** - * URL - * - * Validate that an variable is a valid URL - * - * @package Utopia\Validator - */ -class URL extends Validator -{ - /** - * Get Description - * - * Returns validator description - * - * @return string - */ - public function getDescription() - { - return 'Value must be a valid URL'; - } - - /** - * Is valid - * - * Validation will pass when $value is valid URL. - * - * @param mixed $value - * @return bool - */ - public function isValid($value) - { - if (\filter_var($value, FILTER_VALIDATE_URL) === false) { - return false; - } - - return true; - } -} diff --git a/src/Validator/WhiteList.php b/src/Validator/WhiteList.php index e277c6ee..66cf22b0 100755 --- a/src/Validator/WhiteList.php +++ b/src/Validator/WhiteList.php @@ -33,6 +33,11 @@ class WhiteList extends Validator */ protected $strict; + /** + * @var string + */ + protected $type; + /** * Constructor * @@ -40,11 +45,13 @@ class WhiteList extends Validator * * @param array $list * @param bool $strict disable type check and be case insensetive + * @param string $type of $list items */ - public function __construct(array $list, $strict = false) + public function __construct(array $list, bool $strict = false, string $type = self::TYPE_STRING) { - $this->list = $list; - $this->strict = $strict; + $this->list = $list; + $this->strict = $strict; + $this->type = $type; if (!$this->strict) { foreach ($list as $key => &$value) { @@ -75,6 +82,30 @@ public function getDescription() return 'Value must be one of (' . \implode(', ', $this->list) . ')'; } + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return true; + } + + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType(): string + { + return $this->type; + } + /** * Is valid * diff --git a/tests/Validator/ArrayListTest.php b/tests/Validator/ArrayListTest.php index 12ddbb36..f3b56411 100755 --- a/tests/Validator/ArrayListTest.php +++ b/tests/Validator/ArrayListTest.php @@ -51,5 +51,8 @@ public function testIsValid() $this->assertEquals(false, $this->arrayList2->isValid('string')); $this->assertEquals(true, $this->arrayList2->isValid([1, 2, 3])); $this->assertEquals(false, $this->arrayList2->isValid(1, '2', 3)); + $this->assertEquals($this->arrayList1->getType(), \Utopia\Validator::TYPE_STRING); + $this->assertEquals($this->arrayList2->getType(), \Utopia\Validator::TYPE_MIXED); + $this->assertEquals($this->arrayList1->isArray(), true); } } diff --git a/tests/Validator/AssocTest.php b/tests/Validator/AssocTest.php index bd332f30..0c17c5ce 100755 --- a/tests/Validator/AssocTest.php +++ b/tests/Validator/AssocTest.php @@ -42,5 +42,7 @@ public function testIsValid() $this->assertEquals(true, $this->assoc->isValid(["1" => 'a', "0" => 'b', "2" => 'c'])); $this->assertEquals(true, $this->assoc->isValid(["a" => 'a', "b" => 'b', "c" => 'c'])); $this->assertEquals(true, $this->assoc->isValid([])); + $this->assertEquals($this->assoc->getType(), \Utopia\Validator::TYPE_ARRAY); + $this->assertEquals($this->assoc->isArray(), true); } } diff --git a/tests/Validator/BooleanTest.php b/tests/Validator/BooleanTest.php index 604d94a8..ed2a1027 100755 --- a/tests/Validator/BooleanTest.php +++ b/tests/Validator/BooleanTest.php @@ -60,5 +60,7 @@ public function testIsValid() $this->assertEquals(false, $this->boolean->isValid(['string', 'string'])); $this->assertEquals(false, $this->boolean->isValid('string')); $this->assertEquals(false, $this->boolean->isValid(1.2)); + $this->assertEquals($this->boolean->getType(), \Utopia\Validator::TYPE_BOOLEAN); + $this->assertEquals($this->boolean->isArray(), false); } } diff --git a/tests/Validator/DomainTest.php b/tests/Validator/DomainTest.php deleted file mode 100755 index 7daeb453..00000000 --- a/tests/Validator/DomainTest.php +++ /dev/null @@ -1,49 +0,0 @@ - - * @version 1.0 RC4 - * @license The MIT License (MIT) - */ - -namespace Utopia\Validator; - -use PHPUnit\Framework\TestCase; - -class DomainTest extends TestCase -{ - /** - * @var Domain - */ - protected $domain = null; - - public function setUp():void - { - $this->domain = new Domain(); - } - - public function tearDown():void - { - $this->domain = null; - } - - public function testIsValid() - { - // Assertions - $this->assertEquals(true, $this->domain->isValid('example.com')); - $this->assertEquals(true, $this->domain->isValid('subdomain.example.com')); - $this->assertEquals(true, $this->domain->isValid('localhost')); - $this->assertEquals(true, $this->domain->isValid('appwrite.io')); - $this->assertEquals(true, $this->domain->isValid('appwrite.org')); - $this->assertEquals(false, $this->domain->isValid(false)); - $this->assertEquals(false, $this->domain->isValid(['string', 'string'])); - //$this->assertEquals(false, $this->domain->isValid('string')); - //$this->assertEquals(false, $this->domain->isValid(1)); - //$this->assertEquals(false, $this->domain->isValid(1.2)); - } -} diff --git a/tests/Validator/EmailTest.php b/tests/Validator/EmailTest.php deleted file mode 100755 index a23083c4..00000000 --- a/tests/Validator/EmailTest.php +++ /dev/null @@ -1,70 +0,0 @@ - - * @version 1.0 RC4 - * @license The MIT License (MIT) - */ - -namespace Utopia\Validator; - -use PHPUnit\Framework\TestCase; - -class EmailTest extends TestCase -{ - /** - * @var Email - */ - protected $email = null; - - public function setUp():void - { - $this->email = new Email(); - } - - public function tearDown():void - { - $this->email = null; - } - - public function testIsValid() - { - // Assertions - $this->assertEquals(true, $this->email->isValid('email@domain.com')); - $this->assertEquals(true, $this->email->isValid('firstname.lastname@domain.com')); - $this->assertEquals(true, $this->email->isValid('email@subdomain.domain.com')); - $this->assertEquals(true, $this->email->isValid('firstname+lastname@domain.com')); - $this->assertEquals(true, $this->email->isValid('email@[123.123.123.123]')); - $this->assertEquals(true, $this->email->isValid('"email"@domain.com')); - $this->assertEquals(true, $this->email->isValid('1234567890@domain.com')); - $this->assertEquals(true, $this->email->isValid('email@domain-one.com')); - $this->assertEquals(true, $this->email->isValid('_______@domain.com')); - $this->assertEquals(true, $this->email->isValid('email@domain.name')); - $this->assertEquals(true, $this->email->isValid('email@domain.co.jp')); - $this->assertEquals(true, $this->email->isValid('firstname-lastname@domain.com')); - $this->assertEquals(false, $this->email->isValid(false)); - $this->assertEquals(false, $this->email->isValid(['string', 'string'])); - $this->assertEquals(false, $this->email->isValid(1)); - $this->assertEquals(false, $this->email->isValid(1.2)); - $this->assertEquals(false, $this->email->isValid('plainaddress')); // Missing @ sign and domain - $this->assertEquals(false, $this->email->isValid('@domain.com')); // Missing username - $this->assertEquals(false, $this->email->isValid('#@%^%#$@#$@#.com')); // Garbage - $this->assertEquals(false, $this->email->isValid('Joe Smith ')); // Encoded html within email is invalid - $this->assertEquals(false, $this->email->isValid('email.domain.com')); // Missing @ - $this->assertEquals(false, $this->email->isValid('email@domain@domain.com')); // Two @ sign - $this->assertEquals(false, $this->email->isValid('.email@domain.com')); // Leading dot in address is not allowed - $this->assertEquals(false, $this->email->isValid('email.@domain.com')); // Trailing dot in address is not allowed - $this->assertEquals(false, $this->email->isValid('email..email@domain.com')); // Multiple dots - $this->assertEquals(false, $this->email->isValid('あいうえお@domain.com')); // Unicode char as address - $this->assertEquals(false, $this->email->isValid('email@domain.com (Joe Smith)')); // Text followed email is not allowed - $this->assertEquals(false, $this->email->isValid('email@domain')); // Missing top level domain (.com/.net/.org/etc) - $this->assertEquals(false, $this->email->isValid('email@-domain.com')); // Leading dash in front of domain is invalid - $this->assertEquals(false, $this->email->isValid('email@111.222.333.44444')); // Invalid IP format - $this->assertEquals(false, $this->email->isValid('email@domain..com')); // Multiple dot in the domain portion is invalid - } -} diff --git a/tests/Validator/FloatValidatorTest.php b/tests/Validator/FloatValidatorTest.php index d008e76e..817b1ab9 100755 --- a/tests/Validator/FloatValidatorTest.php +++ b/tests/Validator/FloatValidatorTest.php @@ -43,5 +43,7 @@ public function testIsValid() $this->assertEquals($this->validator->isValid(true), false); $this->assertEquals($this->validator->isValid('23.5'), false); $this->assertEquals($this->validator->isValid('23'), false); + $this->assertEquals($this->validator->getType(), \Utopia\Validator::TYPE_FLOAT); + $this->assertEquals($this->validator->isArray(), false); } } diff --git a/tests/Validator/HexColorTest.php b/tests/Validator/HexColorTest.php index f8f1fc2a..3a65e59a 100755 --- a/tests/Validator/HexColorTest.php +++ b/tests/Validator/HexColorTest.php @@ -44,5 +44,7 @@ public function testIsValid() $this->assertEquals($this->hexColor->isValid('ffffff'), true); $this->assertEquals($this->hexColor->isValid('fff'), true); $this->assertEquals($this->hexColor->isValid('000000'), true); + $this->assertEquals($this->hexColor->getType(), \Utopia\Validator::TYPE_STRING); + $this->assertEquals($this->hexColor->isArray(), false); } } diff --git a/tests/Validator/HostTest.php b/tests/Validator/HostTest.php deleted file mode 100755 index d90dc026..00000000 --- a/tests/Validator/HostTest.php +++ /dev/null @@ -1,44 +0,0 @@ - - * @version 1.0 RC4 - * @license The MIT License (MIT) - */ - -namespace Utopia\Validator; - -use PHPUnit\Framework\TestCase; - -class HostTest extends TestCase -{ - /** - * @var Host - */ - protected $host = null; - - public function setUp():void - { - $this->host = new Host(['appwrite.io', 'subdomain.appwrite.test', 'localhost']); - } - - public function tearDown():void - { - $this->host = null; - } - - public function testIsValid() - { - // Assertions - $this->assertEquals($this->host->isValid('https://appwrite.io/link'), true); - $this->assertEquals($this->host->isValid('https://localhost'), true); - $this->assertEquals($this->host->isValid('localhost'), false); - $this->assertEquals($this->host->isValid('http://subdomain.appwrite.test/path'), true); - $this->assertEquals($this->host->isValid('http://test.subdomain.appwrite.test/path'), false); - } -} diff --git a/tests/Validator/IPTest.php b/tests/Validator/IPTest.php deleted file mode 100755 index e1936089..00000000 --- a/tests/Validator/IPTest.php +++ /dev/null @@ -1,80 +0,0 @@ - - * @version 1.0 RC4 - * @license The MIT License (MIT) - */ - -namespace Utopia\Validator; - -use PHPUnit\Framework\TestCase; - -class IPTest extends TestCase -{ - public function tearDown():void - { - $this->validator = null; - } - - public function testIsValidIP() - { - $validator = new IP(); - - // Assertions - $this->assertEquals($validator->isValid('2001:0db8:85a3:08d3:1319:8a2e:0370:7334'), true); - $this->assertEquals($validator->isValid('109.67.204.101'), true); - $this->assertEquals($validator->isValid(23.5), false); - $this->assertEquals($validator->isValid('23.5'), false); - $this->assertEquals($validator->isValid(null), false); - $this->assertEquals($validator->isValid(true), false); - $this->assertEquals($validator->isValid(false), false); - } - - public function testIsValidIPALL() - { - $validator = new IP(IP::ALL); - - // Assertions - $this->assertEquals($validator->isValid('2001:0db8:85a3:08d3:1319:8a2e:0370:7334'), true); - $this->assertEquals($validator->isValid('109.67.204.101'), true); - $this->assertEquals($validator->isValid(23.5), false); - $this->assertEquals($validator->isValid('23.5'), false); - $this->assertEquals($validator->isValid(null), false); - $this->assertEquals($validator->isValid(true), false); - $this->assertEquals($validator->isValid(false), false); - } - - public function testIsValidIPV4() - { - $validator = new IP(IP::V4); - - // Assertions - $this->assertEquals($validator->isValid('2001:0db8:85a3:08d3:1319:8a2e:0370:7334'), false); - $this->assertEquals($validator->isValid('109.67.204.101'), true); - $this->assertEquals($validator->isValid(23.5), false); - $this->assertEquals($validator->isValid('23.5'), false); - $this->assertEquals($validator->isValid(null), false); - $this->assertEquals($validator->isValid(true), false); - $this->assertEquals($validator->isValid(false), false); - } - - public function testIsValidIPV6() - { - $validator = new IP(IP::V6); - - // Assertions - $this->assertEquals($validator->isValid('2001:0db8:85a3:08d3:1319:8a2e:0370:7334'), true); - $this->assertEquals($validator->isValid('109.67.204.101'), false); - $this->assertEquals($validator->isValid(23.5), false); - $this->assertEquals($validator->isValid('23.5'), false); - $this->assertEquals($validator->isValid(null), false); - $this->assertEquals($validator->isValid(true), false); - $this->assertEquals($validator->isValid(false), false); - } -} diff --git a/tests/Validator/IntegerTest.php b/tests/Validator/IntegerTest.php index dd8b8017..efe68f3e 100755 --- a/tests/Validator/IntegerTest.php +++ b/tests/Validator/IntegerTest.php @@ -42,5 +42,7 @@ public function testIsValid() $this->assertEquals($this->validator->isValid(null), false); $this->assertEquals($this->validator->isValid(true), false); $this->assertEquals($this->validator->isValid(false), false); + $this->assertEquals($this->validator->getType(), \Utopia\Validator::TYPE_INTEGER); + $this->assertEquals($this->validator->isArray(), false); } } diff --git a/tests/Validator/JSONTest.php b/tests/Validator/JSONTest.php index 22d58009..695bdf79 100755 --- a/tests/Validator/JSONTest.php +++ b/tests/Validator/JSONTest.php @@ -47,5 +47,7 @@ public function testIsValid() $this->assertEquals(true, $this->json->isValid(['test'])); $this->assertEquals(true, $this->json->isValid(['test' => 'demo'])); $this->assertEquals(true, $this->json->isValid('{"test": "demo"}')); + $this->assertEquals($this->json->getType(), \Utopia\Validator::TYPE_OBJECT); + $this->assertEquals($this->json->isArray(), false); } } diff --git a/tests/Validator/MultipleTest.php b/tests/Validator/MultipleTest.php deleted file mode 100755 index a2340504..00000000 --- a/tests/Validator/MultipleTest.php +++ /dev/null @@ -1,45 +0,0 @@ - - * @version 1.0 RC4 - * @license The MIT License (MIT) - */ - -namespace Utopia\Validator; - -use PHPUnit\Framework\TestCase; - -class MultipleTest extends TestCase -{ - /** - * @var Multiple - */ - protected $multiple = null; - - public function setUp():void - { - $this->multiple = new Multiple(new Range(10, 20)); - $this->multiple->addRule(new Numeric()); - } - - public function tearDown():void - { - $this->multiple = null; - } - - public function testIsValid() - { - // Assertions - $this->assertEquals($this->multiple->isValid('42'), false); - $this->assertEquals($this->multiple->isValid(20), true); - $this->assertEquals($this->multiple->isValid(1), false); - $this->assertEquals($this->multiple->isValid(-1), false); - $this->assertEquals($this->multiple->isValid('-1'), false); - } -} diff --git a/tests/Validator/NumericTest.php b/tests/Validator/NumericTest.php index 56ea214f..948b0aff 100755 --- a/tests/Validator/NumericTest.php +++ b/tests/Validator/NumericTest.php @@ -43,5 +43,7 @@ public function testIsValid() $this->assertEquals($this->numeric->isValid(9.1), true); $this->assertEquals($this->numeric->isValid('not numeric'), false); $this->assertEquals($this->numeric->isValid([]), false); + $this->assertEquals($this->numeric->getType(), \Utopia\Validator::TYPE_MIXED); + $this->assertEquals($this->numeric->isArray(), false); } } diff --git a/tests/Validator/RangeTest.php b/tests/Validator/RangeTest.php index b55e863d..88268b33 100755 --- a/tests/Validator/RangeTest.php +++ b/tests/Validator/RangeTest.php @@ -44,5 +44,7 @@ public function testIsValid() $this->assertEquals($this->range->isValid(-1), false); $this->assertEquals($this->range->getMin(), 0); $this->assertEquals($this->range->getMax(), 5); + $this->assertEquals($this->range->isArray(), false); + $this->assertEquals($this->range->getType(), \Utopia\Validator::TYPE_INTEGER); } } diff --git a/tests/Validator/TextTest.php b/tests/Validator/TextTest.php new file mode 100755 index 00000000..f99dbaa8 --- /dev/null +++ b/tests/Validator/TextTest.php @@ -0,0 +1,48 @@ + + * @version 1.0 RC4 + * @license The MIT License (MIT) + */ + +namespace Utopia\Validator; + +use PHPUnit\Framework\TestCase; + +class TextTest extends TestCase +{ + /** + * @var Domain + */ + protected $text = null; + + public function setUp():void + { + $this->text = new Text(10); + } + + public function tearDown():void + { + $this->text = null; + } + + public function testIsValid() + { + // Assertions + $this->assertEquals(true, $this->text->isValid('text')); + $this->assertEquals(true, $this->text->isValid('7')); + $this->assertEquals(true, $this->text->isValid('7.9')); + $this->assertEquals(true, $this->text->isValid('["seven"]')); + $this->assertEquals(false, $this->text->isValid(["seven"])); + $this->assertEquals(false, $this->text->isValid(["seven", 8, 9.0])); + $this->assertEquals(false, $this->text->isValid(false)); + $this->assertEquals(false, $this->text->isArray()); + $this->assertEquals(\Utopia\Validator::TYPE_STRING, $this->text->getType()); + } +} diff --git a/tests/Validator/WhiteListTest.php b/tests/Validator/WhiteListTest.php index b9ff86cb..fcf2e97d 100755 --- a/tests/Validator/WhiteListTest.php +++ b/tests/Validator/WhiteListTest.php @@ -29,6 +29,8 @@ public function testIsValid() $this->assertEquals($whiteList->isValid(3), true); $this->assertEquals($whiteList->isValid(5), false); $this->assertEquals($whiteList->getList(), ['string1', 'string2', 3, 4]); + $this->assertEquals($whiteList->getType(), \Utopia\Validator::TYPE_STRING); //string by default + $this->assertEquals($whiteList->isArray(), true); $whiteList = new WhiteList(['string1', 'string2', 3, 4], false);