TranslateTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_View
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. // Call Zend_View_Helper_TranslateTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_TranslateTest::main");
  25. }
  26. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  27. /** Zend_View_Helper_Translate */
  28. require_once 'Zend/View/Helper/Translate.php';
  29. /** Zend_Registry */
  30. require_once 'Zend/Registry.php';
  31. /** Zend_Translate */
  32. require_once 'Zend/Translate.php';
  33. require_once 'Zend/Translate/Adapter/Array.php';
  34. /**
  35. * Test class for Zend_View_Helper_Translate.
  36. *
  37. * @category Zend
  38. * @package Zend_View
  39. * @subpackage UnitTests
  40. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. * @group Zend_View
  43. * @group Zend_View_Helper
  44. */
  45. class Zend_View_Helper_TranslateTest extends PHPUnit_Framework_TestCase
  46. {
  47. /**
  48. * @var Zend_View_Helper_Translate
  49. */
  50. public $helper;
  51. /**
  52. * @var string
  53. */
  54. public $basePath;
  55. /**
  56. * Runs the test methods of this class.
  57. *
  58. * @return void
  59. */
  60. public static function main()
  61. {
  62. require_once "PHPUnit/TextUI/TestRunner.php";
  63. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_TranslateTest");
  64. $result = PHPUnit_TextUI_TestRunner::run($suite);
  65. }
  66. public function clearRegistry()
  67. {
  68. $regKey = 'Zend_Translate';
  69. if (Zend_Registry::isRegistered($regKey)) {
  70. $registry = Zend_Registry::getInstance();
  71. unset($registry[$regKey]);
  72. }
  73. }
  74. /**
  75. * Sets up the fixture, for example, open a network connection.
  76. * This method is called before a test is executed.
  77. *
  78. * @return void
  79. */
  80. public function setUp()
  81. {
  82. $this->clearRegistry();
  83. $this->helper = new Zend_View_Helper_Translate();
  84. }
  85. /**
  86. * Tears down the fixture, for example, close a network connection.
  87. * This method is called after a test is executed.
  88. *
  89. * @return void
  90. */
  91. public function tearDown()
  92. {
  93. unset($this->helper);
  94. $this->clearRegistry();
  95. }
  96. public function testTranslationObjectPassedToConstructorUsedForTranslation()
  97. {
  98. $trans = new Zend_Translate('array', array('one' => 'eins', 'two %1\$s' => 'zwei %1\$s'), 'de');
  99. $helper = new Zend_View_Helper_Translate($trans);
  100. $this->assertEquals('eins', $helper->translate('one'));
  101. $this->assertEquals('three', $helper->translate('three'));
  102. }
  103. public function testLocalTranslationObjectUsedForTranslationsWhenPresent()
  104. {
  105. $trans = new Zend_Translate('array', array('one' => 'eins', 'two %1\$s' => 'zwei %1\$s'), 'de');
  106. $this->helper->setTranslator($trans);
  107. $this->assertEquals('eins', $this->helper->translate('one'));
  108. $this->assertEquals('three', $this->helper->translate('three'));
  109. }
  110. public function testTranslationObjectInRegistryUsedForTranslationsInAbsenceOfLocalTranslationObject()
  111. {
  112. $trans = new Zend_Translate('array', array('one' => 'eins', 'two %1\$s' => 'zwei %1\$s'), 'de');
  113. Zend_Registry::set('Zend_Translate', $trans);
  114. $this->assertEquals('eins', $this->helper->translate('one'));
  115. }
  116. public function testOriginalMessagesAreReturnedWhenNoTranslationObjectPresent()
  117. {
  118. $this->assertEquals('one', $this->helper->translate('one'));
  119. $this->assertEquals('three', $this->helper->translate('three'));
  120. }
  121. public function testPassingNonNullNonTranslationObjectToConstructorThrowsException()
  122. {
  123. try {
  124. $helper = new Zend_View_Helper_Translate('something');
  125. } catch (Zend_View_Exception $e) {
  126. $this->assertContains('must set an instance of Zend_Translate', $e->getMessage());
  127. }
  128. }
  129. public function testPassingNonTranslationObjectToSetTranslatorThrowsException()
  130. {
  131. try {
  132. $this->helper->setTranslator('something');
  133. } catch (Zend_View_Exception $e) {
  134. $this->assertContains('must set an instance of Zend_Translate', $e->getMessage());
  135. }
  136. }
  137. public function testRetrievingLocaleWhenNoTranslationObjectSetThrowsException()
  138. {
  139. try {
  140. $this->helper->getLocale();
  141. } catch (Zend_View_Exception $e) {
  142. $this->assertContains('must set an instance of Zend_Translate', $e->getMessage());
  143. }
  144. }
  145. public function testSettingLocaleWhenNoTranslationObjectSetThrowsException()
  146. {
  147. try {
  148. $this->helper->setLocale('de');
  149. } catch (Zend_View_Exception $e) {
  150. $this->assertContains('must set an instance of Zend_Translate', $e->getMessage());
  151. }
  152. }
  153. public function testCanSetLocale()
  154. {
  155. $trans = new Zend_Translate('array', array('one' => 'eins', 'two %1\$s' => 'zwei %1\$s'), 'de');
  156. $trans->addTranslation(array('one' => 'uno', 'two %1\$s' => 'duo %2\$s'), 'it');
  157. $trans->setLocale('de');
  158. $this->helper->setTranslator($trans);
  159. $this->assertEquals('eins', $this->helper->translate('one'));
  160. $new = $this->helper->setLocale('it');
  161. $this->assertTrue($new instanceof Zend_View_Helper_Translate);
  162. $this->assertEquals('it', $new->getLocale());
  163. $this->assertEquals('uno', $this->helper->translate('one'));
  164. }
  165. public function testHelperImplementsFluentInterface()
  166. {
  167. $trans = new Zend_Translate('array', array('one' => 'eins', 'two %1\$s' => 'zwei %1\$s'), 'de');
  168. $trans->addTranslation(array('one' => 'uno', 'two %1\$s' => 'duo %2\$s'), 'it');
  169. $trans->setLocale('de');
  170. $locale = $this->helper->translate()->setTranslator($trans)->getLocale();
  171. $this->assertEquals('de', $locale);
  172. }
  173. public function testCanTranslateWithOptions()
  174. {
  175. $trans = new Zend_Translate('array', array('one' => 'eins', "two %1\$s" => "zwei %1\$s",
  176. "three %1\$s %2\$s" => "drei %1\$s %2\$s"), 'de');
  177. $trans->addTranslation(array('one' => 'uno', "two %1\$s" => "duo %2\$s",
  178. "three %1\$s %2\$s" => "tre %1\$s %2\$s"), 'it');
  179. $trans->setLocale('de');
  180. $this->helper->setTranslator($trans);
  181. $this->assertEquals("drei 100 200", $this->helper->translate("three %1\$s %2\$s", "100", "200"));
  182. $this->assertEquals("tre 100 200", $this->helper->translate("three %1\$s %2\$s", "100", "200", 'it'));
  183. $this->assertEquals("drei 100 200", $this->helper->translate("three %1\$s %2\$s", array("100", "200")));
  184. $this->assertEquals("tre 100 200", $this->helper->translate("three %1\$s %2\$s", array("100", "200"), 'it'));
  185. }
  186. public function testTranslationObjectNullByDefault()
  187. {
  188. $this->assertNull($this->helper->getTranslator());
  189. }
  190. public function testLocalTranslationObjectIsPreferredOverRegistry()
  191. {
  192. $transReg = new Zend_Translate('array', array('one' => 'eins'));
  193. Zend_Registry::set('Zend_Translate', $transReg);
  194. $this->assertSame($transReg->getAdapter(), $this->helper->getTranslator());
  195. $transLoc = new Zend_Translate('array', array('one' => 'uno'));
  196. $this->helper->setTranslator($transLoc);
  197. $this->assertSame($transLoc->getAdapter(), $this->helper->getTranslator());
  198. $this->assertNotSame($transLoc->getAdapter(), $transReg->getAdapter());
  199. }
  200. public function testHelperObjectReturnedWhenNoArgumentsPassed()
  201. {
  202. $helper = $this->helper->translate();
  203. $this->assertSame($this->helper, $helper);
  204. $transLoc = new Zend_Translate('array', array('one' => 'eins'));
  205. $this->helper->setTranslator($transLoc);
  206. $helper = $this->helper->translate();
  207. $this->assertSame($this->helper, $helper);
  208. }
  209. /**
  210. * ZF-6724
  211. */
  212. public function testTranslationWithPercent()
  213. {
  214. $trans = new Zend_Translate('array', array('one' => 'eins', "two %1\$s" => "zwei %1\$s",
  215. "three %1\$s %2\$s" => "drei %1\$s %2\$s", 'vier%ig' => 'four%'), 'de');
  216. $trans->setLocale('de');
  217. $this->helper->setTranslator($trans);
  218. $this->assertEquals("four%", $this->helper->translate("vier%ig"));
  219. $this->assertEquals("zwei 100", $this->helper->translate("two %1\$s", "100"));
  220. }
  221. /**
  222. * ZF-7937
  223. */
  224. public function testTranslationWithoutTranslator()
  225. {
  226. $result = $this->helper->translate("test %1\$s", "100");
  227. $this->assertEquals('test 100', $result);
  228. }
  229. }
  230. // Call Zend_View_Helper_TranslateTest::main() if this source file is executed directly.
  231. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_TranslateTest::main") {
  232. Zend_View_Helper_TranslateTest::main();
  233. }