LocaleTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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_LocaleTest::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_LocaleTest extends PHPUnit_Framework_TestCase
  38. {
  39. public static function main()
  40. {
  41. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  42. $result = PHPUnit_TextUI_TestRunner::run($suite);
  43. }
  44. public function setUp()
  45. {
  46. // Store original autoloaders
  47. $this->loaders = spl_autoload_functions();
  48. if (!is_array($this->loaders)) {
  49. // spl_autoload_functions does not return empty array when no
  50. // autoloaders registered...
  51. $this->loaders = array();
  52. }
  53. Zend_Loader_Autoloader::resetInstance();
  54. $this->autoloader = Zend_Loader_Autoloader::getInstance();
  55. $this->application = new Zend_Application('testing');
  56. $this->bootstrap = new Zend_Application_Bootstrap_Bootstrap($this->application);
  57. Zend_Registry::_unsetInstance();
  58. }
  59. public function tearDown()
  60. {
  61. // Restore original autoloaders
  62. $loaders = spl_autoload_functions();
  63. foreach ($loaders as $loader) {
  64. spl_autoload_unregister($loader);
  65. }
  66. foreach ($this->loaders as $loader) {
  67. spl_autoload_register($loader);
  68. }
  69. // Reset autoloader instance so it doesn't affect other tests
  70. Zend_Loader_Autoloader::resetInstance();
  71. }
  72. public function testInitializationInitializesLocaleObject()
  73. {
  74. $resource = new Zend_Application_Resource_Locale(array());
  75. $resource->init();
  76. $this->assertTrue($resource->getLocale() instanceof Zend_Locale);
  77. }
  78. public function testInitializationReturnsLocaleObject()
  79. {
  80. $resource = new Zend_Application_Resource_Locale(array());
  81. $resource->setBootstrap($this->bootstrap);
  82. $test = $resource->init();
  83. $this->assertTrue($test instanceof Zend_Locale);
  84. }
  85. public function testOptionsPassedToResourceAreUsedToSetLocaleState()
  86. {
  87. $options = array(
  88. 'default' => 'kok_IN',
  89. 'registry_key' => 'Foo_Bar',
  90. 'force' => true
  91. );
  92. $resource = new Zend_Application_Resource_Locale($options);
  93. $resource->setBootstrap($this->bootstrap);
  94. $resource->init();
  95. $locale = $resource->getLocale();
  96. $this->assertEquals('kok_IN', $locale->__toString());
  97. $this->assertTrue(Zend_Registry::isRegistered('Foo_Bar'));
  98. $this->assertSame(Zend_Registry::get('Foo_Bar'), $locale);
  99. }
  100. public function testOptionsPassedToResourceAreUsedToSetLocaleState1()
  101. {
  102. $options = array(
  103. 'default' => 'kok_IN',
  104. 'force' => true
  105. );
  106. $resource = new Zend_Application_Resource_Locale($options);
  107. $resource->setBootstrap($this->bootstrap);
  108. $resource->init();
  109. $locale = $resource->getLocale();
  110. // This test will fail if your configured locale is kok_IN
  111. $this->assertEquals('kok_IN', $locale->__toString());
  112. $this->assertSame(Zend_Registry::get('Zend_Locale'), $locale);
  113. }
  114. /**
  115. * @group ZF-7058
  116. */
  117. public function testSetCache()
  118. {
  119. $cache = Zend_Cache::factory('Core', 'Black Hole', array(
  120. 'lifetime' => 120,
  121. 'automatic_serialization' => true
  122. ));
  123. $config = array(
  124. 'default' => 'fr_FR',
  125. 'cache' => $cache,
  126. );
  127. $resource = new Zend_Application_Resource_Locale($config);
  128. $resource->init();
  129. $backend = Zend_Locale::getCache()->getBackend();
  130. $this->assertTrue($backend instanceof Zend_Cache_Backend_BlackHole);
  131. Zend_Locale::removeCache();
  132. }
  133. /**
  134. * @group ZF-7058
  135. */
  136. public function testSetCacheFromCacheManager()
  137. {
  138. $configCache = array(
  139. 'memory' => array(
  140. 'frontend' => array(
  141. 'name' => 'Core',
  142. 'options' => array(
  143. 'lifetime' => 120,
  144. 'automatic_serialization' => true
  145. )
  146. ),
  147. 'backend' => array(
  148. 'name' => 'Black Hole'
  149. )
  150. )
  151. );
  152. $this->bootstrap->registerPluginResource('cachemanager', $configCache);
  153. $this->assertFalse(Zend_Locale::hasCache());
  154. $config = array(
  155. 'bootstrap' => $this->bootstrap,
  156. 'cache' => 'memory',
  157. );
  158. $resource = new Zend_Application_Resource_Locale($config);
  159. $resource->init();
  160. $this->assertTrue(Zend_Locale::hasCache());
  161. Zend_Locale::removeCache();
  162. }
  163. }
  164. if (PHPUnit_MAIN_METHOD == 'Zend_Application_Resource_LocaleTest::main') {
  165. Zend_Application_Resource_LocaleTest::main();
  166. }