TranslateTest.php 7.3 KB

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