From c8a4cbcb8aae3126ef8faf492f15bac0f61d8365 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Chans=C3=A9aume?= Date: Fri, 4 Jul 2014 14:57:10 +0200 Subject: [PATCH] optimization --- core/lib/Thelia/Tests/Action/ContentTest.php | 27 ++------ core/lib/Thelia/Tests/Action/FolderTest.php | 26 +------- .../lib/Thelia/Tests/Action/I18nTestTrait.php | 65 +++++++++++++++++++ 3 files changed, 72 insertions(+), 46 deletions(-) create mode 100644 core/lib/Thelia/Tests/Action/I18nTestTrait.php diff --git a/core/lib/Thelia/Tests/Action/ContentTest.php b/core/lib/Thelia/Tests/Action/ContentTest.php index dfbabb21a..6c0c1275b 100644 --- a/core/lib/Thelia/Tests/Action/ContentTest.php +++ b/core/lib/Thelia/Tests/Action/ContentTest.php @@ -38,6 +38,8 @@ use Thelia\Tests\TestCaseWithURLToolSetup; */ class ContentTest extends TestCaseWithURLToolSetup { + use I18nTestTrait; + /** * @var EventDispatcherInterface */ @@ -322,7 +324,7 @@ class ContentTest extends TestCaseWithURLToolSetup $folder->setVisible(1); $folder->setPosition(1); - $this->setI18N($folder, "folder"); + $this->setI18n($folder); $folder->save(); @@ -334,7 +336,7 @@ class ContentTest extends TestCaseWithURLToolSetup $content->setVisible(1); $content->setPosition($i + 1); - $this->setI18N($content, "content"); + $this->setI18n($content); $contentFolders = $content->getContentFolders(); $collection = new Collection(); @@ -351,27 +353,6 @@ class ContentTest extends TestCaseWithURLToolSetup 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 */ diff --git a/core/lib/Thelia/Tests/Action/FolderTest.php b/core/lib/Thelia/Tests/Action/FolderTest.php index 3b2401159..5e56846d2 100644 --- a/core/lib/Thelia/Tests/Action/FolderTest.php +++ b/core/lib/Thelia/Tests/Action/FolderTest.php @@ -33,6 +33,7 @@ use Thelia\Tests\TestCaseWithURLToolSetup; class FolderTest extends TestCaseWithURLToolSetup { use RewrittenUrlTestTrait; + use I18nTestTrait; /** * @var EventDispatcherInterface @@ -299,7 +300,7 @@ class FolderTest extends TestCaseWithURLToolSetup $folder->setVisible(1); $folder->setPosition(1); - $this->setI18N($folder, "folder"); + $this->setI18n($folder); $folder->save(); @@ -311,7 +312,7 @@ class FolderTest extends TestCaseWithURLToolSetup $subFolder->setVisible(1); $subFolder->setPosition($i + 1); - $this->setI18N($subFolder, "sub folder"); + $this->setI18n($subFolder); $subFolder->save(); } @@ -322,27 +323,6 @@ class FolderTest extends TestCaseWithURLToolSetup 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 */ diff --git a/core/lib/Thelia/Tests/Action/I18nTestTrait.php b/core/lib/Thelia/Tests/Action/I18nTestTrait.php new file mode 100644 index 000000000..9c470589f --- /dev/null +++ b/core/lib/Thelia/Tests/Action/I18nTestTrait.php @@ -0,0 +1,65 @@ + + */ +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); + } + } + + } + +} \ No newline at end of file