new types and their tests

This commit is contained in:
Etienne Roudeix
2013-06-24 13:17:38 +02:00
parent 00a8ad51b3
commit 29592da5b0
8 changed files with 268 additions and 13 deletions

View File

@@ -85,32 +85,32 @@ class Category extends BaseLoop {
new Argument( new Argument(
'id', 'id',
new TypeCollection( new TypeCollection(
new Type\AnyType() new Type\IntListType()
) )
), ),
new Argument( new Argument(
'parent', 'parent',
new TypeCollection( new TypeCollection(
new Type\AnyType() new Type\IntType()
) )
), ),
new Argument( new Argument(
'current', 'current',
new TypeCollection( new TypeCollection(
new Type\AnyType() new Type\IntType()
) )
), ),
new Argument( new Argument(
'not_empty', 'not_empty',
new TypeCollection( new TypeCollection(
new Type\AnyType() new Type\IntType()
), ),
0 0
), ),
new Argument( new Argument(
'visible', 'visible',
new TypeCollection( new TypeCollection(
new Type\AnyType() new Type\IntType()
), ),
1 1
), ),
@@ -123,7 +123,7 @@ class Category extends BaseLoop {
new Argument( new Argument(
'order', 'order',
new TypeCollection( new TypeCollection(
new Type\AnyType() new Type\EnumType('alpha', 'alpha_reverse', 'reverse')
) )
), ),
new Argument( new Argument(
@@ -136,20 +136,20 @@ class Category extends BaseLoop {
new Argument( new Argument(
'exclude', 'exclude',
new TypeCollection( new TypeCollection(
new Type\AnyType() new Type\IntListType()
) )
), ),
new Argument( new Argument(
'limit', 'limit',
new TypeCollection( new TypeCollection(
new Type\AnyType() new Type\IntType()
), ),
10 10
), ),
new Argument( new Argument(
'offset', 'offset',
new TypeCollection( new TypeCollection(
new Type\AnyType() new Type\IntType()
), ),
0 0
) )

View File

@@ -35,7 +35,7 @@ use Thelia\Type\TypeCollection;
*/ */
class ArgumentTest extends \PHPUnit_Framework_TestCase class ArgumentTest extends \PHPUnit_Framework_TestCase
{ {
function testArgumentCollectionConstruction() public function testArgumentCollectionConstruction()
{ {
$collection = new ArgumentCollection( $collection = new ArgumentCollection(
new Argument( new Argument(
@@ -87,7 +87,7 @@ class ArgumentTest extends \PHPUnit_Framework_TestCase
); );
} }
function testArgumentCollectionFetch() public function testArgumentCollectionFetch()
{ {
$collection = new ArgumentCollection( $collection = new ArgumentCollection(
new Argument( new Argument(

View File

@@ -33,7 +33,7 @@ use Thelia\Type\TypeCollection;
*/ */
class TypeTest extends \PHPUnit_Framework_TestCase class TypeTest extends \PHPUnit_Framework_TestCase
{ {
function testTypeCollectionConstruction() public function testTypeCollectionConstruction()
{ {
$collection = new TypeCollection( $collection = new TypeCollection(
new Type\AnyType(), new Type\AnyType(),
@@ -55,7 +55,7 @@ class TypeTest extends \PHPUnit_Framework_TestCase
); );
} }
function testTypeCollectionFetch() public function testTypeCollectionFetch()
{ {
$collection = new TypeCollection( $collection = new TypeCollection(
new Type\AnyType(), new Type\AnyType(),
@@ -79,4 +79,35 @@ class TypeTest extends \PHPUnit_Framework_TestCase
$collection->next(); $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'));
}
} }

View File

@@ -0,0 +1,50 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Type;
/**
*
* @author Etienne Roudeix <eroudeix@openstudio.fr>
*
*/
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);
}
}

View File

@@ -0,0 +1,42 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Type;
/**
*
* @author Etienne Roudeix <eroudeix@openstudio.fr>
*
*/
class FloatType implements TypeInterface
{
public function getType()
{
return 'Float type';
}
public function isValid($value)
{
return filter_var($value, FILTER_VALIDATE_FLOAT) === false ? false : true;
}
}

View File

@@ -0,0 +1,47 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Type;
/**
*
* @author Etienne Roudeix <eroudeix@openstudio.fr>
*
*/
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;
}
}

View File

@@ -0,0 +1,42 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Type;
/**
*
* @author Etienne Roudeix <eroudeix@openstudio.fr>
*
*/
class IntType implements TypeInterface
{
public function getType()
{
return 'Int type';
}
public function isValid($value)
{
return filter_var($value, FILTER_VALIDATE_INT) === false ? false : true;
}
}

View File

@@ -0,0 +1,43 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Type;
/**
*
* @author Etienne Roudeix <eroudeix@openstudio.fr>
*
*/
class JsonType implements TypeInterface
{
public function getType()
{
return 'Json type';
}
public function isValid($value)
{
json_decode($value);
return (json_last_error() == JSON_ERROR_NONE);
}
}