TranslateTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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-2014 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-2014 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. /**
  40. * @var array
  41. */
  42. protected $_translationOptions = array(
  43. 'data' => array(
  44. 'message1' => 'message1',
  45. 'message2' => 'message2',
  46. 'message3' => 'message3'
  47. )
  48. );
  49. /**
  50. * @var Zend_Loader_Autoloader
  51. */
  52. protected $autoloader;
  53. /**
  54. * @var Zend_Application
  55. */
  56. protected $application;
  57. /**
  58. * @var Zend_Application_Bootstrap_Bootstrap
  59. */
  60. protected $bootstrap;
  61. public static function main()
  62. {
  63. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  64. $result = PHPUnit_TextUI_TestRunner::run($suite);
  65. }
  66. public function setUp()
  67. {
  68. // Store original autoloaders
  69. $this->loaders = spl_autoload_functions();
  70. if (!is_array($this->loaders)) {
  71. // spl_autoload_functions does not return empty array when no
  72. // autoloaders registered...
  73. $this->loaders = array();
  74. }
  75. Zend_Loader_Autoloader::resetInstance();
  76. $this->autoloader = Zend_Loader_Autoloader::getInstance();
  77. $this->application = new Zend_Application('testing');
  78. $this->bootstrap = new Zend_Application_Bootstrap_Bootstrap($this->application);
  79. Zend_Registry::_unsetInstance();
  80. }
  81. public function tearDown()
  82. {
  83. // Restore original autoloaders
  84. $loaders = spl_autoload_functions();
  85. foreach ($loaders as $loader) {
  86. spl_autoload_unregister($loader);
  87. }
  88. foreach ($this->loaders as $loader) {
  89. spl_autoload_register($loader);
  90. }
  91. // Reset autoloader instance so it doesn't affect other tests
  92. Zend_Loader_Autoloader::resetInstance();
  93. }
  94. public function testInitializationInitializesTranslateObject()
  95. {
  96. $resource = new Zend_Application_Resource_Translate($this->_translationOptions);
  97. $resource->setBootstrap($this->bootstrap);
  98. $resource->init();
  99. $this->assertTrue($resource->getTranslate() instanceof Zend_Translate);
  100. }
  101. public function testInitializationReturnsLocaleObject()
  102. {
  103. $resource = new Zend_Application_Resource_Translate($this->_translationOptions);
  104. $resource->setBootstrap($this->bootstrap);
  105. $test = $resource->init();
  106. $this->assertTrue($test instanceof Zend_Translate);
  107. }
  108. public function testOptionsPassedToResourceAreUsedToSetLocaleState()
  109. {
  110. $resource = new Zend_Application_Resource_Translate($this->_translationOptions);
  111. $resource->setBootstrap($this->bootstrap);
  112. $resource->init();
  113. $translate = $resource->getTranslate();
  114. $this->assertTrue(Zend_Registry::isRegistered('Zend_Translate'));
  115. $this->assertSame(Zend_Registry::get('Zend_Translate'), $translate);
  116. }
  117. public function testResourceThrowsExceptionWithoutData()
  118. {
  119. try {
  120. $resource = new Zend_Application_Resource_Translate();
  121. $resource->getTranslate();
  122. $this->fail('Expected Zend_Application_Resource_Exception');
  123. } catch (Zend_Application_Resource_Exception $e) {
  124. $this->assertTrue($e instanceof Zend_Application_Resource_Exception);
  125. }
  126. }
  127. /**
  128. * @group ZF-7352
  129. */
  130. public function testTranslationIsAddedIfRegistryKeyExistsAlready()
  131. {
  132. $options1 = array('foo' => 'bar');
  133. $options2 = array_merge_recursive($this->_translationOptions,
  134. array('data' => array('message4' => 'bericht4')));
  135. $translate = new Zend_Translate(Zend_Translate::AN_ARRAY, $options1);
  136. Zend_Registry::set('Zend_Translate', $translate);
  137. $resource = new Zend_Application_Resource_Translate($options2);
  138. $this->assertTrue($translate === $resource->getTranslate());
  139. $this->assertEquals('bar', $translate->translate('foo'));
  140. $this->assertEquals('bericht4', $translate->translate('message4'));
  141. $this->assertEquals('shouldNotExist', $translate->translate('shouldNotExist'));
  142. }
  143. /**
  144. * @group ZF-10034
  145. */
  146. public function testSetCacheFromCacheManager()
  147. {
  148. $configCache = array(
  149. 'translate' => array(
  150. 'frontend' => array(
  151. 'name' => 'Core',
  152. 'options' => array(
  153. 'lifetime' => 120,
  154. 'automatic_serialization' => true
  155. )
  156. ),
  157. 'backend' => array(
  158. 'name' => 'Black Hole'
  159. )
  160. )
  161. );
  162. $this->bootstrap->registerPluginResource('cachemanager', $configCache);
  163. $options = $this->_translationOptions;
  164. $options['cache'] = 'translate';
  165. $resource = new Zend_Application_Resource_Translate($options);
  166. $resource->setBootstrap($this->bootstrap);
  167. $resource->init();
  168. $this->assertTrue(Zend_Translate::getCache() instanceof Zend_Cache_Core);
  169. Zend_Translate::removeCache();
  170. }
  171. /**
  172. * @group ZF-10352
  173. */
  174. public function testToUseTheSameKeyAsTheOptionsZendTranslate()
  175. {
  176. $options = array(
  177. 'adapter' => 'array',
  178. 'content' => array(
  179. 'm1' => 'message1',
  180. 'm2' => 'message2'
  181. ),
  182. 'locale' => 'auto'
  183. );
  184. $resource = new Zend_Application_Resource_Translate($options);
  185. $translator = $resource->init();
  186. $this->assertEquals(new Zend_Translate($options), $translator);
  187. $this->assertEquals('message2', $translator->_('m2'));
  188. }
  189. /**
  190. * @group ZF-10352
  191. * @expectedException Zend_Application_Resource_Exception
  192. */
  193. public function testToUseTheTwoKeysContentAndDataShouldThrowsException()
  194. {
  195. $options = array(
  196. 'adapter' => 'array',
  197. 'content' => array(
  198. 'm1' => 'message1',
  199. 'm2' => 'message2'
  200. ),
  201. 'data' => array(
  202. 'm3' => 'message3',
  203. 'm4' => 'message4'
  204. ),
  205. 'locale' => 'auto'
  206. );
  207. $resource = new Zend_Application_Resource_Translate($options);
  208. $translator = $resource->init();
  209. }
  210. /**
  211. * @group GH-103
  212. */
  213. public function testLogFactory()
  214. {
  215. $options = $this->_translationOptions;
  216. $options['log'][0] = new Zend_Log_Writer_Mock();
  217. $options['logUntranslated'] = true;
  218. $options['locale'] = 'en';
  219. $resource = new Zend_Application_Resource_Translate($options);
  220. $resource->setBootstrap($this->bootstrap);
  221. $resource->init()->translate('untranslated');
  222. $event = current($options['log'][0]->events);
  223. $this->assertTrue(is_array($event));
  224. $this->assertTrue(array_key_exists('message', $event));
  225. $this->assertEquals(
  226. "Untranslated message within 'en': untranslated",
  227. $event['message']
  228. );
  229. }
  230. }
  231. if (PHPUnit_MAIN_METHOD == 'Zend_Application_Resource_TranslateTest::main') {
  232. Zend_Application_Resource_TranslateTest::main();
  233. }