CurrencyTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. $this->helper = new Zend_View_Helper_Currency();
  79. }
  80. /**
  81. * Tears down the fixture, for example, close a network connection.
  82. * This method is called after a test is executed.
  83. *
  84. * @return void
  85. */
  86. public function tearDown()
  87. {
  88. unset($this->helper);
  89. $this->clearRegistry();
  90. }
  91. public function testCurrencyObjectPassedToConstructor()
  92. {
  93. $curr = new Zend_Currency('de_AT');
  94. $helper = new Zend_View_Helper_Currency($curr);
  95. $this->assertEquals('€ 1.234,56', $helper->currency(1234.56));
  96. $this->assertEquals('€ 0,12', $helper->currency(0.123));
  97. }
  98. public function testLocalCurrencyObjectUsedWhenPresent()
  99. {
  100. $curr = new Zend_Currency('de_AT');
  101. $this->helper->setCurrency($curr);
  102. $this->assertEquals('€ 1.234,56', $this->helper->currency(1234.56));
  103. $this->assertEquals('€ 0,12', $this->helper->currency(0.123));
  104. }
  105. public function testCurrencyObjectInRegistryUsedInAbsenceOfLocalCurrencyObject()
  106. {
  107. $curr = new Zend_Currency('de_AT');
  108. Zend_Registry::set('Zend_Currency', $curr);
  109. $this->assertEquals('€ 1.234,56', $this->helper->currency(1234.56));
  110. }
  111. public function testPassingNonNullNonCurrencyObjectToConstructorThrowsException()
  112. {
  113. try {
  114. $helper = new Zend_View_Helper_Currency('something');
  115. } catch (Exception $e) {
  116. $this->assertContains('not found', $e->getMessage());
  117. }
  118. }
  119. public function testPassingNonCurrencyObjectToSetCurrencyThrowsException()
  120. {
  121. try {
  122. $this->helper->setCurrency('something');
  123. } catch (Exception $e) {
  124. $this->assertContains('not found', $e->getMessage());
  125. }
  126. }
  127. public function testCanOutputCurrencyWithOptions()
  128. {
  129. $curr = new Zend_Currency('de_AT');
  130. $this->helper->setCurrency($curr);
  131. $this->assertEquals("€ 1.234,56", $this->helper->currency(1234.56, "de_AT"));
  132. }
  133. public function testCurrencyObjectNullByDefault()
  134. {
  135. $this->assertNotNull($this->helper->getCurrency());
  136. }
  137. public function testLocalCurrencyObjectIsPreferredOverRegistry()
  138. {
  139. $currReg = new Zend_Currency('de_AT');
  140. Zend_Registry::set('Zend_Currency', $currReg);
  141. $this->helper = new Zend_View_Helper_Currency();
  142. $this->assertSame($currReg, $this->helper->getCurrency());
  143. $currLoc = new Zend_Currency('en_US');
  144. $this->helper->setCurrency($currLoc);
  145. $this->assertSame($currLoc, $this->helper->getCurrency());
  146. $this->assertNotSame($currLoc, $currReg);
  147. }
  148. public function testHelperObjectReturnedWhenNoArgumentsPassed()
  149. {
  150. $helper = $this->helper->currency();
  151. $this->assertSame($this->helper, $helper);
  152. $currLoc = new Zend_Currency('de_AT');
  153. $this->helper->setCurrency($currLoc);
  154. $helper = $this->helper->currency();
  155. $this->assertSame($this->helper, $helper);
  156. }
  157. }
  158. // Call Zend_View_Helper_TranslateTest::main() if this source file is executed directly.
  159. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_TranslateTest::main") {
  160. Zend_View_Helper_TranslateTest::main();
  161. }