PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/test/testsuite/generator/behavior/i18n/I18nBehaviorObjectBuilderModifierTest.php

https://github.com/mattleff/propel
PHP | 289 lines | 247 code | 26 blank | 16 comment | 1 complexity | 5c4259bf0c4fe9b3e7bcb01f7726457b MD5 | raw file
  1. <?php
  2. /*
  3. * $Id: VersionableBehaviorTest.php 1460 2010-01-17 22:36:48Z francois $
  4. * This file is part of the Propel package.
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * @license MIT License
  9. */
  10. require_once 'PHPUnit/Framework.php';
  11. require_once dirname(__FILE__) . '/../../../../../generator/lib/util/PropelQuickBuilder.php';
  12. require_once dirname(__FILE__) . '/../../../../../generator/lib/behavior/i18n/I18nBehavior.php';
  13. require_once dirname(__FILE__) . '/../../../../../runtime/lib/Propel.php';
  14. /**
  15. * Tests for I18nBehavior class object modifier
  16. *
  17. * @author François Zaninotto
  18. * @version $Revision: 2251 $
  19. * @package generator.behavior.i18n
  20. */
  21. class I18nBehaviorObjectBuilderModifierTest extends PHPUnit_Framework_TestCase
  22. {
  23. public function setUp()
  24. {
  25. if (!class_exists('I18nBehaviorTest1')) {
  26. $schema = <<<EOF
  27. <database name="i18n_behavior_test_1">
  28. <table name="i18n_behavior_test_1">
  29. <column name="id" primaryKey="true" type="INTEGER" autoIncrement="true" />
  30. <column name="foo" type="INTEGER" />
  31. <column name="bar" type="VARCHAR" size="100" />
  32. <behavior name="i18n">
  33. <parameter name="i18n_columns" value="bar" />
  34. </behavior>
  35. </table>
  36. <table name="i18n_behavior_test_2">
  37. <column name="id" primaryKey="true" type="INTEGER" autoIncrement="true" />
  38. <column name="foo" type="INTEGER" />
  39. <column name="bar1" type="VARCHAR" size="100" />
  40. <column name="bar2" type="LONGVARCHAR" lazyLoad="true" />
  41. <column name="bar3" type="TIMESTAMP" />
  42. <column name="bar4" type="LONGVARCHAR" description="This is the Bar4 column" />
  43. <behavior name="i18n">
  44. <parameter name="i18n_columns" value="bar1,bar2,bar3,bar4" />
  45. <parameter name="default_locale" value="fr_FR" />
  46. <parameter name="locale_alias" value="culture" />
  47. </behavior>
  48. </table>
  49. </database>
  50. EOF;
  51. PropelQuickBuilder::buildSchema($schema);
  52. }
  53. }
  54. public function testPostDeleteEmulatesOnDeleteCascade()
  55. {
  56. I18nBehaviorTest1Query::create()->deleteAll();
  57. I18nBehaviorTest1I18nQuery::create()->deleteAll();
  58. $o = new I18nBehaviorTest1();
  59. $o->setFoo(123);
  60. $o->setLocale('en_EN');
  61. $o->setBar('hello');
  62. $o->setLocale('fr_FR');
  63. $o->setBar('bonjour');
  64. $o->save();
  65. $this->assertEquals(2, I18nBehaviorTest1I18nQuery::create()->count());
  66. $o->clearI18nBehaviorTest1I18ns();
  67. $o->delete();
  68. $this->assertEquals(0, I18nBehaviorTest1I18nQuery::create()->count());
  69. }
  70. public function testGetTranslationReturnsTranslationObject()
  71. {
  72. $o = new I18nBehaviorTest1();
  73. $translation = $o->getTranslation();
  74. $this->assertTrue($translation instanceof I18nBehaviorTest1I18n);
  75. }
  76. public function testGetTranslationOnNewObjectReturnsNewTranslation()
  77. {
  78. $o = new I18nBehaviorTest1();
  79. $translation = $o->getTranslation();
  80. $this->assertTrue($translation->isNew());
  81. }
  82. public function testGetTranslationOnPersistedObjectReturnsNewTranslation()
  83. {
  84. $o = new I18nBehaviorTest1();
  85. $o->save();
  86. $translation = $o->getTranslation();
  87. $this->assertTrue($translation->isNew());
  88. }
  89. public function testGetTranslationOnPersistedObjectWithTranslationReturnsExistingTranslation()
  90. {
  91. $o = new I18nBehaviorTest1();
  92. $translation = new I18nBehaviorTest1I18n();
  93. $o->addI18nBehaviorTest1I18n($translation);
  94. $o->save();
  95. $translation = $o->getTranslation();
  96. $this->assertFalse($translation->isNew());
  97. }
  98. public function testGetTranslationAcceptsALocaleParameter()
  99. {
  100. $o = new I18nBehaviorTest1();
  101. $translation1 = new I18nBehaviorTest1I18n();
  102. $translation1->setLocale('en_EN');
  103. $o->addI18nBehaviorTest1I18n($translation1);
  104. $translation2 = new I18nBehaviorTest1I18n();
  105. $translation2->setLocale('fr_FR');
  106. $o->addI18nBehaviorTest1I18n($translation2);
  107. $o->save();
  108. $this->assertEquals($translation1, $o->getTranslation('en_EN'));
  109. $this->assertEquals($translation2, $o->getTranslation('fr_FR'));
  110. }
  111. public function testGetTranslationSetsTheLocaleOnTheTranslation()
  112. {
  113. $o = new I18nBehaviorTest1();
  114. $o->save();
  115. $translation = $o->getTranslation();
  116. $this->assertEquals('en_EN', $translation->getLocale());
  117. $o = new I18nBehaviorTest2();
  118. $o->save();
  119. $translation = $o->getTranslation();
  120. $this->assertEquals('fr_FR', $translation->getLocale());
  121. }
  122. public function testGetTranslationUsesInternalCollectionIfAvailable()
  123. {
  124. $o = new I18nBehaviorTest1();
  125. $translation1 = new I18nBehaviorTest1I18n();
  126. $translation1->setLocale('en_EN');
  127. $o->addI18nBehaviorTest1I18n($translation1);
  128. $translation2 = new I18nBehaviorTest1I18n();
  129. $translation2->setLocale('fr_FR');
  130. $o->addI18nBehaviorTest1I18n($translation2);
  131. $translation = $o->getTranslation('en_EN');
  132. $this->assertEquals($translation1, $translation);
  133. }
  134. public function testRemoveTranslation()
  135. {
  136. $o = new I18nBehaviorTest1();
  137. $translation1 = new I18nBehaviorTest1I18n();
  138. $translation1->setLocale('en_EN');
  139. $o->addI18nBehaviorTest1I18n($translation1);
  140. $translation2 = new I18nBehaviorTest1I18n();
  141. $translation2->setLocale('fr_FR');
  142. $translation2->setBar('bonjour');
  143. $o->addI18nBehaviorTest1I18n($translation2);
  144. $o->save();
  145. $this->assertEquals(2, $o->countI18nBehaviorTest1I18ns());
  146. $o->removeTranslation('fr_FR');
  147. $this->assertEquals(1, $o->countI18nBehaviorTest1I18ns());
  148. $translation = $o->getTranslation('fr_FR');
  149. $this->assertNotEquals($translation->getBar(), $translation2->getBar());
  150. }
  151. public function testLocaleSetterAndGetterExist()
  152. {
  153. $this->assertTrue(method_exists('I18nBehaviorTest1', 'setLocale'));
  154. $this->assertTrue(method_exists('I18nBehaviorTest1', 'getLocale'));
  155. }
  156. public function testGetLocaleReturnsDefaultLocale()
  157. {
  158. $o = new I18nBehaviorTest1();
  159. $this->assertEquals('en_EN', $o->getLocale());
  160. $o = new I18nBehaviorTest2();
  161. $this->assertEquals('fr_FR', $o->getLocale());
  162. }
  163. public function testSetLocale()
  164. {
  165. $o = new I18nBehaviorTest1();
  166. $o->setLocale('fr_FR');
  167. $this->assertEquals('fr_FR', $o->getLocale());
  168. }
  169. public function testSetLocaleUsesDefaultLocale()
  170. {
  171. $o = new I18nBehaviorTest1();
  172. $o->setLocale('fr_FR');
  173. $o->setLocale();
  174. $this->assertEquals('en_EN', $o->getLocale());
  175. }
  176. public function testLocaleSetterAndGetterAliasesExist()
  177. {
  178. $this->assertTrue(method_exists('I18nBehaviorTest2', 'setCulture'));
  179. $this->assertTrue(method_exists('I18nBehaviorTest2', 'getCulture'));
  180. }
  181. public function testGetLocaleAliasReturnsDefaultLocale()
  182. {
  183. $o = new I18nBehaviorTest2();
  184. $this->assertEquals('fr_FR', $o->getCulture());
  185. }
  186. public function testSetLocaleAlias()
  187. {
  188. $o = new I18nBehaviorTest2();
  189. $o->setCulture('en_EN');
  190. $this->assertEquals('en_EN', $o->getCulture());
  191. }
  192. public function testGetCurrentTranslationUsesDefaultLocale()
  193. {
  194. $o = new I18nBehaviorTest1();
  195. $t = $o->getCurrentTranslation();
  196. $this->assertEquals('en_EN', $t->getLocale());
  197. $o = new I18nBehaviorTest2();
  198. $t = $o->getCurrentTranslation();
  199. $this->assertEquals('fr_FR', $t->getLocale());
  200. }
  201. public function testGetCurrentTranslationUsesCurrentLocale()
  202. {
  203. $o = new I18nBehaviorTest1();
  204. $o->setLocale('fr_FR');
  205. $this->assertEquals('fr_FR', $o->getCurrentTranslation()->getLocale());
  206. $o->setLocale('pt_PT');
  207. $this->assertEquals('pt_PT', $o->getCurrentTranslation()->getLocale());
  208. }
  209. public function testI18nColumnGetterUsesCurrentTranslation()
  210. {
  211. $o = new I18nBehaviorTest1();
  212. $t1 = $o->getCurrentTranslation();
  213. $t1->setBar('hello');
  214. $o->setLocale('fr_FR');
  215. $t2 = $o->getCurrentTranslation();
  216. $t2->setBar('bonjour');
  217. //$o->save();
  218. $o->setLocale('en_EN');
  219. $this->assertEquals('hello', $o->getBar());
  220. $o->setLocale('fr_FR');
  221. $this->assertEquals('bonjour', $o->getBar());
  222. }
  223. public function testI18nColumnSetterUsesCurrentTranslation()
  224. {
  225. $o = new I18nBehaviorTest1();
  226. $o->setBar('hello');
  227. $o->setLocale('fr_FR');
  228. $o->setBar('bonjour');
  229. $o->setLocale('en_EN');
  230. $this->assertEquals('hello', $o->getBar());
  231. $o->setLocale('fr_FR');
  232. $this->assertEquals('bonjour', $o->getBar());
  233. }
  234. public function testTranslationsArePersisted()
  235. {
  236. $o = new I18nBehaviorTest1();
  237. $o->save();
  238. $count = I18nBehaviorTest1I18nQuery::create()
  239. ->filterById($o->getId())
  240. ->count();
  241. $this->assertEquals(0, $count);
  242. $o->setBar('hello');
  243. $o->setLocale('fr_FR');
  244. $o->setBar('bonjour');
  245. $o->save();
  246. $count = I18nBehaviorTest1I18nQuery::create()
  247. ->filterById($o->getId())
  248. ->count();
  249. $this->assertEquals(2, $count);
  250. }
  251. public function testClearRemovesExistingTranlsations()
  252. {
  253. $o = new I18nBehaviorTest1();
  254. $translation1 = new I18nBehaviorTest1I18n();
  255. $translation1->setBar('baz');
  256. $translation1->setLocale('fr_FR');
  257. $o->addI18nBehaviorTest1I18n($translation1);
  258. $o->clear();
  259. $this->assertEquals('en_EN', $o->getLocale());
  260. $t1 = $o->getTranslation('fr_FR');
  261. $this->assertEquals('', $t1->getBar());
  262. }
  263. }