From 29592da5b0ad3b89ae61731aea050f9689bb51b1 Mon Sep 17 00:00:00 2001 From: Etienne Roudeix Date: Mon, 24 Jun 2013 13:17:38 +0200 Subject: [PATCH] new types and their tests --- .../Thelia/Core/Template/Loop/Category.php | 18 +++---- .../Template/Loop/Argument/ArgumentTest.php | 4 +- core/lib/Thelia/Tests/Type/TypeTest.php | 35 ++++++++++++- core/lib/Thelia/Type/EnumType.php | 50 +++++++++++++++++++ core/lib/Thelia/Type/FloatType.php | 42 ++++++++++++++++ core/lib/Thelia/Type/IntListType.php | 47 +++++++++++++++++ core/lib/Thelia/Type/IntType.php | 42 ++++++++++++++++ core/lib/Thelia/Type/JsonType.php | 43 ++++++++++++++++ 8 files changed, 268 insertions(+), 13 deletions(-) create mode 100755 core/lib/Thelia/Type/EnumType.php create mode 100755 core/lib/Thelia/Type/FloatType.php create mode 100755 core/lib/Thelia/Type/IntListType.php create mode 100755 core/lib/Thelia/Type/IntType.php create mode 100755 core/lib/Thelia/Type/JsonType.php diff --git a/core/lib/Thelia/Core/Template/Loop/Category.php b/core/lib/Thelia/Core/Template/Loop/Category.php index 2541fc982..dadb69505 100755 --- a/core/lib/Thelia/Core/Template/Loop/Category.php +++ b/core/lib/Thelia/Core/Template/Loop/Category.php @@ -85,32 +85,32 @@ class Category extends BaseLoop { new Argument( 'id', new TypeCollection( - new Type\AnyType() + new Type\IntListType() ) ), new Argument( 'parent', new TypeCollection( - new Type\AnyType() + new Type\IntType() ) ), new Argument( 'current', new TypeCollection( - new Type\AnyType() + new Type\IntType() ) ), new Argument( 'not_empty', new TypeCollection( - new Type\AnyType() + new Type\IntType() ), 0 ), new Argument( 'visible', new TypeCollection( - new Type\AnyType() + new Type\IntType() ), 1 ), @@ -123,7 +123,7 @@ class Category extends BaseLoop { new Argument( 'order', new TypeCollection( - new Type\AnyType() + new Type\EnumType('alpha', 'alpha_reverse', 'reverse') ) ), new Argument( @@ -136,20 +136,20 @@ class Category extends BaseLoop { new Argument( 'exclude', new TypeCollection( - new Type\AnyType() + new Type\IntListType() ) ), new Argument( 'limit', new TypeCollection( - new Type\AnyType() + new Type\IntType() ), 10 ), new Argument( 'offset', new TypeCollection( - new Type\AnyType() + new Type\IntType() ), 0 ) diff --git a/core/lib/Thelia/Tests/Core/Template/Loop/Argument/ArgumentTest.php b/core/lib/Thelia/Tests/Core/Template/Loop/Argument/ArgumentTest.php index e5a240b8b..7c5ce5d16 100755 --- a/core/lib/Thelia/Tests/Core/Template/Loop/Argument/ArgumentTest.php +++ b/core/lib/Thelia/Tests/Core/Template/Loop/Argument/ArgumentTest.php @@ -35,7 +35,7 @@ use Thelia\Type\TypeCollection; */ class ArgumentTest extends \PHPUnit_Framework_TestCase { - function testArgumentCollectionConstruction() + public function testArgumentCollectionConstruction() { $collection = new ArgumentCollection( new Argument( @@ -87,7 +87,7 @@ class ArgumentTest extends \PHPUnit_Framework_TestCase ); } - function testArgumentCollectionFetch() + public function testArgumentCollectionFetch() { $collection = new ArgumentCollection( new Argument( diff --git a/core/lib/Thelia/Tests/Type/TypeTest.php b/core/lib/Thelia/Tests/Type/TypeTest.php index fa6e4ea31..4b7c65df8 100755 --- a/core/lib/Thelia/Tests/Type/TypeTest.php +++ b/core/lib/Thelia/Tests/Type/TypeTest.php @@ -33,7 +33,7 @@ use Thelia\Type\TypeCollection; */ class TypeTest extends \PHPUnit_Framework_TestCase { - function testTypeCollectionConstruction() + public function testTypeCollectionConstruction() { $collection = new TypeCollection( new Type\AnyType(), @@ -55,7 +55,7 @@ class TypeTest extends \PHPUnit_Framework_TestCase ); } - function testTypeCollectionFetch() + public function testTypeCollectionFetch() { $collection = new TypeCollection( new Type\AnyType(), @@ -79,4 +79,35 @@ class TypeTest extends \PHPUnit_Framework_TestCase $collection->next(); } } + + public function testTypes() + { + $anyType = new Type\AnyType(); + $this->assertTrue($anyType->isValid(md5(rand(1000, 10000)))); + + $intType = new Type\IntType(); + $this->assertTrue($intType->isValid('1')); + $this->assertTrue($intType->isValid(2)); + $this->assertFalse($intType->isValid('3.3')); + + $floatType = new Type\FloatType(); + $this->assertTrue($floatType->isValid('1.1')); + $this->assertTrue($floatType->isValid(2.2)); + $this->assertFalse($floatType->isValid('foo')); + + $enumType = new Type\EnumType(array("cat", "dog")); + $this->assertTrue($enumType->isValid('cat')); + $this->assertTrue($enumType->isValid('dog')); + $this->assertFalse($enumType->isValid('monkey')); + $this->assertFalse($enumType->isValid('catdog')); + + $intListType = new Type\IntListType(); + $this->assertTrue($intListType->isValid('1')); + $this->assertTrue($intListType->isValid('1,2,3')); + $this->assertFalse($intListType->isValid('1,2,3.3')); + + $jsonType = new Type\JsonType(); + $this->assertTrue($jsonType->isValid('{"k0":"v0","k1":"v1","k2":"v2"}')); + $this->assertFalse($jsonType->isValid('1,2,3')); + } } diff --git a/core/lib/Thelia/Type/EnumType.php b/core/lib/Thelia/Type/EnumType.php new file mode 100755 index 000000000..d0a1c9b89 --- /dev/null +++ b/core/lib/Thelia/Type/EnumType.php @@ -0,0 +1,50 @@ +. */ +/* */ +/*************************************************************************************/ +namespace Thelia\Type; + +/** + * + * @author Etienne Roudeix + * + */ + +class EnumType implements TypeInterface +{ + protected $values = array(); + + public function __construct($values = array()) + { + if(is_array($values)) + $this->values = $values; + } + + public function getType() + { + return 'Enum type'; + } + + public function isValid($value) + { + return in_array($value, $this->values); + } +} diff --git a/core/lib/Thelia/Type/FloatType.php b/core/lib/Thelia/Type/FloatType.php new file mode 100755 index 000000000..e341b540f --- /dev/null +++ b/core/lib/Thelia/Type/FloatType.php @@ -0,0 +1,42 @@ +. */ +/* */ +/*************************************************************************************/ +namespace Thelia\Type; + +/** + * + * @author Etienne Roudeix + * + */ + +class FloatType implements TypeInterface +{ + public function getType() + { + return 'Float type'; + } + + public function isValid($value) + { + return filter_var($value, FILTER_VALIDATE_FLOAT) === false ? false : true; + } +} diff --git a/core/lib/Thelia/Type/IntListType.php b/core/lib/Thelia/Type/IntListType.php new file mode 100755 index 000000000..74b0a6790 --- /dev/null +++ b/core/lib/Thelia/Type/IntListType.php @@ -0,0 +1,47 @@ +. */ +/* */ +/*************************************************************************************/ +namespace Thelia\Type; + +/** + * + * @author Etienne Roudeix + * + */ + +class IntListType implements TypeInterface +{ + public function getType() + { + return 'Int list type'; + } + + public function isValid($values) + { + foreach(explode(',', $values) as $value) { + if(filter_var($value, FILTER_VALIDATE_INT) === false) + return false; + } + + return true; + } +} diff --git a/core/lib/Thelia/Type/IntType.php b/core/lib/Thelia/Type/IntType.php new file mode 100755 index 000000000..d077a6c89 --- /dev/null +++ b/core/lib/Thelia/Type/IntType.php @@ -0,0 +1,42 @@ +. */ +/* */ +/*************************************************************************************/ +namespace Thelia\Type; + +/** + * + * @author Etienne Roudeix + * + */ + +class IntType implements TypeInterface +{ + public function getType() + { + return 'Int type'; + } + + public function isValid($value) + { + return filter_var($value, FILTER_VALIDATE_INT) === false ? false : true; + } +} diff --git a/core/lib/Thelia/Type/JsonType.php b/core/lib/Thelia/Type/JsonType.php new file mode 100755 index 000000000..805977a32 --- /dev/null +++ b/core/lib/Thelia/Type/JsonType.php @@ -0,0 +1,43 @@ +. */ +/* */ +/*************************************************************************************/ +namespace Thelia\Type; + +/** + * + * @author Etienne Roudeix + * + */ + +class JsonType implements TypeInterface +{ + public function getType() + { + return 'Json type'; + } + + public function isValid($value) + { + json_decode($value); + return (json_last_error() == JSON_ERROR_NONE); + } +}