CurrencyTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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_View
  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: TranslateTest.php 18387 2010-09-23 21:00:00Z thomas $
  21. */
  22. // Call Zend_View_Helper_CurrencyTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_CurrencyTest::main");
  25. }
  26. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  27. /** Zend_View_Helper_Currency */
  28. require_once 'Zend/View/Helper/Currency.php';
  29. /** Zend_Registry */
  30. require_once 'Zend/Registry.php';
  31. /** Zend_Currency */
  32. require_once 'Zend/Currency.php';
  33. /**
  34. * Test class for Zend_View_Helper_Currency
  35. *
  36. * @category Zend
  37. * @package Zend_View
  38. * @subpackage UnitTests
  39. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  40. * @license http://framework.zend.com/license/new-bsd New BSD License
  41. * @group Zend_View
  42. * @group Zend_View_Helper
  43. */
  44. class Zend_View_Helper_CurrencyTest extends PHPUnit_Framework_TestCase
  45. {
  46. /**
  47. * @var Zend_View_Helper_Currency
  48. */
  49. public $helper;
  50. /**
  51. * Runs the test methods of this class.
  52. *
  53. * @return void
  54. */
  55. public static function main()
  56. {
  57. require_once "PHPUnit/TextUI/TestRunner.php";
  58. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_CurrencyTest");
  59. $result = PHPUnit_TextUI_TestRunner::run($suite);
  60. }
  61. public function clearRegistry()
  62. {
  63. $regKey = 'Zend_Currency';
  64. if (Zend_Registry::isRegistered($regKey)) {
  65. $registry = Zend_Registry::getInstance();
  66. unset($registry[$regKey]);
  67. }
  68. }
  69. /**
  70. * Sets up the fixture, for example, open a network connection.
  71. * This method is called before a test is executed.
  72. *
  73. * @return void
  74. */
  75. public function setUp()
  76. {
  77. $this->clearRegistry();
  78. require_once 'Zend/Cache.php';
  79. $this->_cache = Zend_Cache::factory('Core', 'File',
  80. array('lifetime' => 120, 'automatic_serialization' => true),
  81. array('cache_dir' => dirname(__FILE__) . '/../../_files/'));
  82. Zend_Currency::setCache($this->_cache);
  83. $this->helper = new Zend_View_Helper_Currency('de_AT');
  84. }
  85. /**
  86. * Tears down the fixture, for example, close a network connection.
  87. * This method is called after a test is executed.
  88. *
  89. * @return void
  90. */
  91. public function tearDown()
  92. {
  93. unset($this->helper);
  94. $this->_cache->clean(Zend_Cache::CLEANING_MODE_ALL);
  95. $this->clearRegistry();
  96. }
  97. public function testCurrencyObjectPassedToConstructor()
  98. {
  99. $curr = new Zend_Currency('de_AT');
  100. $helper = new Zend_View_Helper_Currency($curr);
  101. $this->assertEquals('€ 1.234,56', $helper->currency(1234.56));
  102. $this->assertEquals('€ 0,12', $helper->currency(0.123));
  103. }
  104. public function testLocalCurrencyObjectUsedWhenPresent()
  105. {
  106. $curr = new Zend_Currency('de_AT');
  107. $this->helper->setCurrency($curr);
  108. $this->assertEquals('€ 1.234,56', $this->helper->currency(1234.56));
  109. $this->assertEquals('€ 0,12', $this->helper->currency(0.123));
  110. }
  111. public function testCurrencyObjectInRegistryUsedInAbsenceOfLocalCurrencyObject()
  112. {
  113. $curr = new Zend_Currency('de_AT');
  114. Zend_Registry::set('Zend_Currency', $curr);
  115. $this->assertEquals('€ 1.234,56', $this->helper->currency(1234.56));
  116. }
  117. public function testPassingNonNullNonCurrencyObjectToConstructorThrowsException()
  118. {
  119. try {
  120. $helper = new Zend_View_Helper_Currency('something');
  121. } catch (Exception $e) {
  122. if (substr($e->getMessage(), 0, 15) == 'No region found') {
  123. $this->assertContains('within the locale', $e->getMessage());
  124. } else {
  125. $this->assertContains('not found', $e->getMessage());
  126. }
  127. }
  128. }
  129. public function testPassingNonCurrencyObjectToSetCurrencyThrowsException()
  130. {
  131. try {
  132. $this->helper->setCurrency('something');
  133. } catch (Exception $e) {
  134. if (substr($e->getMessage(), 0, 15) == 'No region found') {
  135. $this->assertContains('within the locale', $e->getMessage());
  136. } else {
  137. $this->assertContains('not found', $e->getMessage());
  138. }
  139. }
  140. }
  141. public function testCanOutputCurrencyWithOptions()
  142. {
  143. $curr = new Zend_Currency('de_AT');
  144. $this->helper->setCurrency($curr);
  145. $this->assertEquals("€ 1.234,56", $this->helper->currency(1234.56, "de_AT"));
  146. }
  147. public function testCurrencyObjectNullByDefault()
  148. {
  149. $this->assertNotNull($this->helper->getCurrency());
  150. }
  151. public function testLocalCurrencyObjectIsPreferredOverRegistry()
  152. {
  153. $currReg = new Zend_Currency('de_AT');
  154. Zend_Registry::set('Zend_Currency', $currReg);
  155. $this->helper = new Zend_View_Helper_Currency();
  156. $this->assertSame($currReg, $this->helper->getCurrency());
  157. $currLoc = new Zend_Currency('en_US');
  158. $this->helper->setCurrency($currLoc);
  159. $this->assertSame($currLoc, $this->helper->getCurrency());
  160. $this->assertNotSame($currLoc, $currReg);
  161. }
  162. public function testHelperObjectReturnedWhenNoArgumentsPassed()
  163. {
  164. $helper = $this->helper->currency();
  165. $this->assertSame($this->helper, $helper);
  166. $currLoc = new Zend_Currency('de_AT');
  167. $this->helper->setCurrency($currLoc);
  168. $helper = $this->helper->currency();
  169. $this->assertSame($this->helper, $helper);
  170. }
  171. }
  172. // Call Zend_View_Helper_TranslateTest::main() if this source file is executed directly.
  173. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_TranslateTest::main") {
  174. Zend_View_Helper_TranslateTest::main();
  175. }