TranslateTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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_Application
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2012 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. if (!defined('PHPUnit_MAIN_METHOD')) {
  23. define('PHPUnit_MAIN_METHOD', 'Zend_Application_Resource_TranslateTest::main');
  24. }
  25. /**
  26. * Zend_Loader_Autoloader
  27. */
  28. require_once 'Zend/Loader/Autoloader.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Application
  32. * @subpackage UnitTests
  33. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. * @group Zend_Application
  36. */
  37. class Zend_Application_Resource_TranslateTest extends PHPUnit_Framework_TestCase
  38. {
  39. private $_translationOptions = array('data' => array(
  40. 'message1' => 'message1',
  41. 'message2' => 'message2',
  42. 'message3' => 'message3'
  43. ));
  44. public static function main()
  45. {
  46. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  47. $result = PHPUnit_TextUI_TestRunner::run($suite);
  48. }
  49. public function setUp()
  50. {
  51. // Store original autoloaders
  52. $this->loaders = spl_autoload_functions();
  53. if (!is_array($this->loaders)) {
  54. // spl_autoload_functions does not return empty array when no
  55. // autoloaders registered...
  56. $this->loaders = array();
  57. }
  58. Zend_Loader_Autoloader::resetInstance();
  59. $this->autoloader = Zend_Loader_Autoloader::getInstance();
  60. $this->application = new Zend_Application('testing');
  61. $this->bootstrap = new Zend_Application_Bootstrap_Bootstrap($this->application);
  62. Zend_Registry::_unsetInstance();
  63. }
  64. public function tearDown()
  65. {
  66. // Restore original autoloaders
  67. $loaders = spl_autoload_functions();
  68. foreach ($loaders as $loader) {
  69. spl_autoload_unregister($loader);
  70. }
  71. foreach ($this->loaders as $loader) {
  72. spl_autoload_register($loader);
  73. }
  74. // Reset autoloader instance so it doesn't affect other tests
  75. Zend_Loader_Autoloader::resetInstance();
  76. }
  77. public function testInitializationInitializesTranslateObject()
  78. {
  79. $resource = new Zend_Application_Resource_Translate($this->_translationOptions);
  80. $resource->setBootstrap($this->bootstrap);
  81. $resource->init();
  82. $this->assertTrue($resource->getTranslate() instanceof Zend_Translate);
  83. }
  84. public function testInitializationReturnsLocaleObject()
  85. {
  86. $resource = new Zend_Application_Resource_Translate($this->_translationOptions);
  87. $resource->setBootstrap($this->bootstrap);
  88. $test = $resource->init();
  89. $this->assertTrue($test instanceof Zend_Translate);
  90. }
  91. public function testOptionsPassedToResourceAreUsedToSetLocaleState()
  92. {
  93. $resource = new Zend_Application_Resource_Translate($this->_translationOptions);
  94. $resource->setBootstrap($this->bootstrap);
  95. $resource->init();
  96. $translate = $resource->getTranslate();
  97. $this->assertTrue(Zend_Registry::isRegistered('Zend_Translate'));
  98. $this->assertSame(Zend_Registry::get('Zend_Translate'), $translate);
  99. }
  100. public function testResourceThrowsExceptionWithoutData()
  101. {
  102. try {
  103. $resource = new Zend_Application_Resource_Translate();
  104. $resource->getTranslate();
  105. $this->fail('Expected Zend_Application_Resource_Exception');
  106. } catch (Zend_Application_Resource_Exception $e) {
  107. $this->assertTrue($e instanceof Zend_Application_Resource_Exception);
  108. }
  109. }
  110. /**
  111. * @group ZF-7352
  112. */
  113. public function testTranslationIsAddedIfRegistryKeyExistsAlready()
  114. {
  115. $options1 = array('foo' => 'bar');
  116. $options2 = array_merge_recursive($this->_translationOptions,
  117. array('data' => array('message4' => 'bericht4')));
  118. $translate = new Zend_Translate(Zend_Translate::AN_ARRAY, $options1);
  119. Zend_Registry::set('Zend_Translate', $translate);
  120. $resource = new Zend_Application_Resource_Translate($options2);
  121. $this->assertTrue($translate === $resource->getTranslate());
  122. $this->assertEquals('bar', $translate->translate('foo'));
  123. $this->assertEquals('bericht4', $translate->translate('message4'));
  124. $this->assertEquals('shouldNotExist', $translate->translate('shouldNotExist'));
  125. }
  126. /**
  127. * @group ZF-10034
  128. */
  129. public function testSetCacheFromCacheManager()
  130. {
  131. $configCache = array(
  132. 'translate' => array(
  133. 'frontend' => array(
  134. 'name' => 'Core',
  135. 'options' => array(
  136. 'lifetime' => 120,
  137. 'automatic_serialization' => true
  138. )
  139. ),
  140. 'backend' => array(
  141. 'name' => 'Black Hole'
  142. )
  143. )
  144. );
  145. $this->bootstrap->registerPluginResource('cachemanager', $configCache);
  146. $options = $this->_translationOptions;
  147. $options['cache'] = 'translate';
  148. $resource = new Zend_Application_Resource_Translate($options);
  149. $resource->setBootstrap($this->bootstrap);
  150. $resource->init();
  151. $this->assertTrue(Zend_Translate::getCache() instanceof Zend_Cache_Core);
  152. Zend_Translate::removeCache();
  153. }
  154. /**
  155. * @group ZF-10352
  156. */
  157. public function testToUseTheSameKeyAsTheOptionsZendTranslate()
  158. {
  159. $options = array(
  160. 'adapter' => 'array',
  161. 'content' => array(
  162. 'm1' => 'message1',
  163. 'm2' => 'message2'
  164. ),
  165. 'locale' => 'auto'
  166. );
  167. $resource = new Zend_Application_Resource_Translate($options);
  168. $translator = $resource->init();
  169. $this->assertEquals(new Zend_Translate($options), $translator);
  170. $this->assertEquals('message2', $translator->_('m2'));
  171. }
  172. /**
  173. * @group ZF-10352
  174. * @expectedException Zend_Application_Resource_Exception
  175. */
  176. public function testToUseTheTwoKeysContentAndDataShouldThrowsException()
  177. {
  178. $options = array(
  179. 'adapter' => 'array',
  180. 'content' => array(
  181. 'm1' => 'message1',
  182. 'm2' => 'message2'
  183. ),
  184. 'data' => array(
  185. 'm3' => 'message3',
  186. 'm4' => 'message4'
  187. ),
  188. 'locale' => 'auto'
  189. );
  190. $resource = new Zend_Application_Resource_Translate($options);
  191. $translator = $resource->init();
  192. }
  193. }
  194. if (PHPUnit_MAIN_METHOD == 'Zend_Application_Resource_TranslateTest::main') {
  195. Zend_Application_Resource_TranslateTest::main();
  196. }