CurrencyTest.php 6.3 KB

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