changes tests

create argument helpers
start loop auto pagination
This commit is contained in:
Etienne Roudeix
2013-06-26 14:45:19 +02:00
parent 0be18e763a
commit 80380878f5
13 changed files with 352 additions and 133 deletions

View File

@@ -26,6 +26,7 @@ namespace Thelia\Core\Template\Element;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Thelia\Core\Template\Loop\Argument\ArgumentCollection; use Thelia\Core\Template\Loop\Argument\ArgumentCollection;
use Thelia\Core\Template\Loop\Argument\Argument;
/** /**
* *
@@ -44,6 +45,14 @@ abstract class BaseLoop
*/ */
public $dispatcher; public $dispatcher;
protected function getDefaultArgs()
{
return array(
Argument::createIntTypeArgument('offset'),
Argument::createIntTypeArgument('limit'),
);
}
/** /**
* @param \Symfony\Component\HttpFoundation\Request $request * @param \Symfony\Component\HttpFoundation\Request $request
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher
@@ -54,6 +63,11 @@ abstract class BaseLoop
$this->dispatcher = $dispatcher; $this->dispatcher = $dispatcher;
} }
public function getArgs()
{
return $this->defineArgs()->addArguments($this->getDefaultArgs());
}
/** /**
* *
* this function have to be implement in your own loop class. * this function have to be implement in your own loop class.
@@ -99,6 +113,6 @@ abstract class BaseLoop
* *
* @return ArgumentCollection * @return ArgumentCollection
*/ */
abstract public function defineArgs(); abstract protected function defineArgs();
} }

View File

@@ -22,6 +22,9 @@
/*************************************************************************************/ /*************************************************************************************/
namespace Thelia\Core\Template\Loop\Argument; namespace Thelia\Core\Template\Loop\Argument;
use Thelia\Type\TypeCollection;
use Thelia\Type;
/** /**
* *
* @author Etienne Roudeix <eroudeix@openstudio.fr> * @author Etienne Roudeix <eroudeix@openstudio.fr>
@@ -42,4 +45,43 @@ class Argument
$this->mandatory = $mandatory ? true : false; $this->mandatory = $mandatory ? true : false;
$this->default = $default; $this->default = $default;
} }
public static function createAnyTypeArgument($name, $default=null, $mandatory=false, $empty=true)
{
return new Argument(
$name,
new TypeCollection(
new Type\AnyType()
),
$default,
$mandatory,
$empty
);
}
public static function createIntTypeArgument($name, $default=null, $mandatory=false, $empty=true)
{
return new Argument(
$name,
new TypeCollection(
new Type\IntType()
),
$default,
$mandatory,
$empty
);
}
public static function createIntListTypeArgument($name, $default=null, $mandatory=false, $empty=true)
{
return new Argument(
$name,
new TypeCollection(
new Type\IntListType()
),
$default,
$mandatory,
$empty
);
}
} }

View File

@@ -35,9 +35,7 @@ class ArgumentCollection implements \Iterator
public function __construct() public function __construct()
{ {
foreach(func_get_args() as $argument) { $this->addArguments(func_get_args());
$this->addArgument($argument);
}
} }
public function isEmpty() public function isEmpty()
@@ -45,6 +43,20 @@ class ArgumentCollection implements \Iterator
return count($this->arguments) == 0; return count($this->arguments) == 0;
} }
/**
* @param array $argumentList
*
* @return ArgumentCollection
*/
public function addArguments(array $argumentList)
{
foreach($argumentList as $argument) {
$this->addArgument($argument);
}
return $this;
}
/** /**
* @param Argument $argument * @param Argument $argument
* *

View File

@@ -79,80 +79,23 @@ class Category extends BaseLoop {
public $limit; public $limit;
public $offset; public $offset;
public function defineArgs() protected function defineArgs()
{ {
return new ArgumentCollection( return new ArgumentCollection(
new Argument( Argument::createIntListTypeArgument('id'),
'id', Argument::createIntTypeArgument('parent'),
new TypeCollection( Argument::createIntTypeArgument('current'),
new Type\IntListType() Argument::createIntTypeArgument('not_empty', 0),
) Argument::createIntTypeArgument('visible', 1),
), Argument::createAnyTypeArgument('link'),
new Argument(
'parent',
new TypeCollection(
new Type\IntType()
)
),
new Argument(
'current',
new TypeCollection(
new Type\IntType()
)
),
new Argument(
'not_empty',
new TypeCollection(
new Type\IntType()
),
0
),
new Argument(
'visible',
new TypeCollection(
new Type\IntType()
),
1
),
new Argument(
'link',
new TypeCollection(
new Type\AnyType()
)
),
new Argument( new Argument(
'order', 'order',
new TypeCollection( new TypeCollection(
new Type\EnumType('alpha', 'alpha_reverse', 'reverse') new Type\EnumType('alpha', 'alpha_reverse', 'reverse')
) )
), ),
new Argument( Argument::createIntTypeArgument('random', 0),
'random', Argument::createIntListTypeArgument('exclude')
new TypeCollection(
new Type\AnyType()
),
0
),
new Argument(
'exclude',
new TypeCollection(
new Type\IntListType()
)
),
new Argument(
'limit',
new TypeCollection(
new Type\IntType()
),
10
),
new Argument(
'offset',
new TypeCollection(
new Type\IntType()
),
0
)
); );
} }
@@ -187,11 +130,11 @@ class Category extends BaseLoop {
$search->filterByLink($this->link); $search->filterByLink($this->link);
} }
if($this->limit > -1) { /*if($this->limit > -1) {
$search->limit($this->limit); $search->limit($this->limit);
} }*/
$search->filterByVisible($this->visible); $search->filterByVisible($this->visible);
$search->offset($this->offset); //$search->offset($this->offset);
switch($this->order) { switch($this->order) {
@@ -221,7 +164,8 @@ class Category extends BaseLoop {
*/ */
$search->joinWithI18n($this->request->getSession()->get('locale', 'en_US'), \Criteria::INNER_JOIN); $search->joinWithI18n($this->request->getSession()->get('locale', 'en_US'), \Criteria::INNER_JOIN);
$categories = $search->find(); //$categories = $search->find();
$categories = $search->paginate($page = 2, $maxPerPage = 2);
$loopResult = new LoopResult(); $loopResult = new LoopResult();

View File

@@ -177,8 +177,8 @@ class TheliaLoop implements SmartyPluginInterface
* *
* @param string $name * @param string $name
* @return \Thelia\Core\Template\Element\BaseLoop * @return \Thelia\Core\Template\Element\BaseLoop
* @throws \Thelia\Tpex\Exception\InvalidElementException * @throws InvalidElementException
* @throws \Thelia\Tpex\Exception\ElementNotFoundException * @throws ElementNotFoundException
*/ */
protected function createLoopInstance($name) protected function createLoopInstance($name)
{ {
@@ -204,8 +204,8 @@ class TheliaLoop implements SmartyPluginInterface
/** /**
* Returns the value of a loop argument. * Returns the value of a loop argument.
* *
* @param unknown $loop a BaseLoop instance * @param BaseLoop $loop a BaseLoop instance
* @param unknown $smartyParam * @param $smartyParam
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
protected function getLoopArgument(BaseLoop $loop, $smartyParam) protected function getLoopArgument(BaseLoop $loop, $smartyParam)
@@ -218,7 +218,7 @@ class TheliaLoop implements SmartyPluginInterface
$faultActor = array(); $faultActor = array();
$faultDetails = array(); $faultDetails = array();
$argumentsCollection = $loop->defineArgs(); $argumentsCollection = $loop->getArgs();
foreach( $argumentsCollection as $argument ) { foreach( $argumentsCollection as $argument ) {
$value = isset($smartyParam[$argument->name]) ? $smartyParam[$argument->name] : null; $value = isset($smartyParam[$argument->name]) ? $smartyParam[$argument->name] : null;
@@ -275,7 +275,7 @@ class TheliaLoop implements SmartyPluginInterface
* "myLoop" => "My\Own\Loop" * "myLoop" => "My\Own\Loop"
* ); * );
* *
* @param array $loops * @param array $loopDefinition
* @throws \InvalidArgumentException if loop name already exists * @throws \InvalidArgumentException if loop name already exists
*/ */
public function setLoopList(array $loopDefinition) public function setLoopList(array $loopDefinition)

View File

@@ -113,17 +113,11 @@ class ArgumentTest extends \PHPUnit_Framework_TestCase
$arguments = \PHPUnit_Framework_Assert::readAttribute($collection, 'arguments'); $arguments = \PHPUnit_Framework_Assert::readAttribute($collection, 'arguments');
$collection->rewind(); foreach($collection as $key => $argument) {
while ($collection->valid()) {
$argument = $collection->current();
$this->assertEquals( $this->assertEquals(
$argument, $argument,
$arguments[$collection->key()] $arguments[$key]
); );
$collection->next();
} }
} }
} }

View File

@@ -0,0 +1,40 @@
<?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\Tests\Type;
use Thelia\Type\AnyType;
/**
*
* @author Etienne Roudeix <eroudeix@openstudio.fr>
*
*/
class AnyTypeTest extends \PHPUnit_Framework_TestCase
{
public function testAnyType()
{
$anyType = new AnyType();
$this->assertTrue($anyType->isValid(md5(rand(1000, 10000))));
}
}

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\Tests\Type;
use Thelia\Type\EnumType;
/**
*
* @author Etienne Roudeix <eroudeix@openstudio.fr>
*
*/
class EnumTypeTest extends \PHPUnit_Framework_TestCase
{
public function testEnumType()
{
$enumType = new EnumType(array("cat", "dog"));
$this->assertTrue($enumType->isValid('cat'));
$this->assertTrue($enumType->isValid('dog'));
$this->assertFalse($enumType->isValid('monkey'));
$this->assertFalse($enumType->isValid('catdog'));
}
}

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\Tests\Type;
use Thelia\Type\FloatType;
/**
*
* @author Etienne Roudeix <eroudeix@openstudio.fr>
*
*/
class FloatTypeTest extends \PHPUnit_Framework_TestCase
{
public function testFloatType()
{
$floatType = new FloatType();
$this->assertTrue($floatType->isValid('1.1'));
$this->assertTrue($floatType->isValid(2.2));
$this->assertFalse($floatType->isValid('foo'));
}
}

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\Tests\Type;
use Thelia\Type\IntListType;
/**
*
* @author Etienne Roudeix <eroudeix@openstudio.fr>
*
*/
class IntListTypeTest extends \PHPUnit_Framework_TestCase
{
public function testIntListType()
{
$intListType = new IntListType();
$this->assertTrue($intListType->isValid('1'));
$this->assertTrue($intListType->isValid('1,2,3'));
$this->assertFalse($intListType->isValid('1,2,3.3'));
}
}

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\Tests\Type;
use Thelia\Type\IntType;
/**
*
* @author Etienne Roudeix <eroudeix@openstudio.fr>
*
*/
class IntTypeTest extends \PHPUnit_Framework_TestCase
{
public function testIntType()
{
$intType = new IntType();
$this->assertTrue($intType->isValid('1'));
$this->assertTrue($intType->isValid(2));
$this->assertFalse($intType->isValid('3.3'));
}
}

View File

@@ -0,0 +1,41 @@
<?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\Tests\Type;
use Thelia\Type\JsonType;
/**
*
* @author Etienne Roudeix <eroudeix@openstudio.fr>
*
*/
class JsonTypeTest extends \PHPUnit_Framework_TestCase
{
public function testJsonType()
{
$jsonType = new JsonType();
$this->assertTrue($jsonType->isValid('{"k0":"v0","k1":"v1","k2":"v2"}'));
$this->assertFalse($jsonType->isValid('1,2,3'));
}
}

View File

@@ -37,18 +37,18 @@ class TypeTest extends \PHPUnit_Framework_TestCase
{ {
$collection = new TypeCollection( $collection = new TypeCollection(
new Type\AnyType(), new Type\AnyType(),
new Type\AnyType() new Type\IntType()
); );
$collection->addType( $collection->addType(
new Type\AnyType() new Type\FloatType()
); );
$this->assertAttributeEquals( $this->assertAttributeEquals(
array( array(
new Type\AnyType(), new Type\AnyType(),
new Type\AnyType(), new Type\IntType(),
new Type\AnyType(), new Type\FloatType(),
), ),
'types', 'types',
$collection $collection
@@ -59,55 +59,18 @@ class TypeTest extends \PHPUnit_Framework_TestCase
{ {
$collection = new TypeCollection( $collection = new TypeCollection(
new Type\AnyType(), new Type\AnyType(),
new Type\AnyType(), new Type\IntType(),
new Type\AnyType() new Type\FloatType()
); );
$types = \PHPUnit_Framework_Assert::readAttribute($collection, 'types'); $types = \PHPUnit_Framework_Assert::readAttribute($collection, 'types');
$collection->rewind(); foreach($collection as $key => $type) {
while ($collection->valid()) {
$type = $collection->current();
$this->assertEquals( $this->assertEquals(
$type, $type,
$types[$collection->key()] $types[$key]
); );
$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'));
}
} }