2
0

TranslateTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. if (PHPUnit_MAIN_METHOD == 'Zend_Application_Resource_TranslateTest::main') {
  132. Zend_Application_Resource_TranslateTest::main();
  133. }