optimization

This commit is contained in:
Julien Chanséaume
2014-07-04 14:57:10 +02:00
parent 71f4380dff
commit c8a4cbcb8a
3 changed files with 72 additions and 46 deletions

View File

@@ -38,6 +38,8 @@ use Thelia\Tests\TestCaseWithURLToolSetup;
*/ */
class ContentTest extends TestCaseWithURLToolSetup class ContentTest extends TestCaseWithURLToolSetup
{ {
use I18nTestTrait;
/** /**
* @var EventDispatcherInterface * @var EventDispatcherInterface
*/ */
@@ -322,7 +324,7 @@ class ContentTest extends TestCaseWithURLToolSetup
$folder->setVisible(1); $folder->setVisible(1);
$folder->setPosition(1); $folder->setPosition(1);
$this->setI18N($folder, "folder"); $this->setI18n($folder);
$folder->save(); $folder->save();
@@ -334,7 +336,7 @@ class ContentTest extends TestCaseWithURLToolSetup
$content->setVisible(1); $content->setVisible(1);
$content->setPosition($i + 1); $content->setPosition($i + 1);
$this->setI18N($content, "content"); $this->setI18n($content);
$contentFolders = $content->getContentFolders(); $contentFolders = $content->getContentFolders();
$collection = new Collection(); $collection = new Collection();
@@ -351,27 +353,6 @@ class ContentTest extends TestCaseWithURLToolSetup
return self::$folderForPositionTest; return self::$folderForPositionTest;
} }
/**
* Set the title of te object in all locales
*
* @param mixed $object
* @param string $title
*/
public function setI18N(&$object, $title)
{
$localeList = LangQuery::create()
->select("Locale")
->find()
->toArray();
foreach ($localeList as $locale) {
$object->setLocale($locale);
$object->setTitle($locale . ' : ' . $title);
}
}
/** /**
* @return \Thelia\Model\Folder * @return \Thelia\Model\Folder
*/ */

View File

@@ -33,6 +33,7 @@ use Thelia\Tests\TestCaseWithURLToolSetup;
class FolderTest extends TestCaseWithURLToolSetup class FolderTest extends TestCaseWithURLToolSetup
{ {
use RewrittenUrlTestTrait; use RewrittenUrlTestTrait;
use I18nTestTrait;
/** /**
* @var EventDispatcherInterface * @var EventDispatcherInterface
@@ -299,7 +300,7 @@ class FolderTest extends TestCaseWithURLToolSetup
$folder->setVisible(1); $folder->setVisible(1);
$folder->setPosition(1); $folder->setPosition(1);
$this->setI18N($folder, "folder"); $this->setI18n($folder);
$folder->save(); $folder->save();
@@ -311,7 +312,7 @@ class FolderTest extends TestCaseWithURLToolSetup
$subFolder->setVisible(1); $subFolder->setVisible(1);
$subFolder->setPosition($i + 1); $subFolder->setPosition($i + 1);
$this->setI18N($subFolder, "sub folder"); $this->setI18n($subFolder);
$subFolder->save(); $subFolder->save();
} }
@@ -322,27 +323,6 @@ class FolderTest extends TestCaseWithURLToolSetup
return self::$folderIdForPositionTest; return self::$folderIdForPositionTest;
} }
/**
* Set the title of te object in all locales
*
* @param mixed $object
* @param string $title
*/
public function setI18N(&$object, $title)
{
$localeList = LangQuery::create()
->select("Locale")
->find()
->toArray();
foreach ($localeList as $locale) {
$object->setLocale($locale);
$object->setTitle($locale . ' : ' . $title);
}
}
/** /**
* @return \Thelia\Model\Folder * @return \Thelia\Model\Folder
*/ */

View File

@@ -0,0 +1,65 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Tests\Action;
use Thelia\Model\LangQuery;
/**
* Class I18NTestTrait
* @package Thelia\Tests\Action
* @author Julien Chanséaume <jchanseaume@openstudio.fr>
*/
trait I18nTestTrait
{
/** @var array list of available locale */
protected static $localeList = null;
/**
* populate a list of field for each locale for an object
*
* @param mixed $object the object to populate
* @param array $fields list of field to populate
* @param array $localeList list of locale to use populate the object
*/
protected function setI18n(&$object, $fields = array("title"), $localeList = null)
{
if (null === $localeList) {
if (null === self::$localeList) {
self::$localeList = LangQuery::create()
->select("Locale")
->find()
->toArray();
}
$localeList = self::$localeList;
}
foreach ($localeList as $locale) {
$object->setLocale($locale);
foreach ($fields as $name) {
$func = "set" . ucfirst(strtolower($name));
$object->$func($locale . ' : ' . $name);
}
}
}
}