PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/View/Helper/CurrencyTest.php

https://github.com/MontmereLimited/zf2
PHP | 176 lines | 105 code | 23 blank | 48 comment | 7 complexity | 8e411c5873a0677781df717a0fd35a82 MD5 | raw file
  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace ZendTest\View\Helper;
  25. use Zend\Cache\StorageFactory as CacheFactory,
  26. Zend\Cache\Storage\Adapter as CacheAdapter,
  27. Zend\Currency,
  28. Zend\View\Helper;
  29. /**
  30. * Test class for Zend_View_Helper_Currency
  31. *
  32. * @category Zend
  33. * @package Zend_View
  34. * @subpackage UnitTests
  35. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. * @group Zend_View
  38. * @group Zend_View_Helper
  39. */
  40. class CurrencyTest extends \PHPUnit_Framework_TestCase
  41. {
  42. /**
  43. * @var Zend_View_Helper_Currency
  44. */
  45. public $helper;
  46. public function clearRegistry()
  47. {
  48. $regKey = 'Zend_Currency';
  49. if (\Zend\Registry::isRegistered($regKey)) {
  50. $registry = \Zend\Registry::getInstance();
  51. unset($registry[$regKey]);
  52. }
  53. }
  54. /**
  55. * Sets up the fixture, for example, open a network connection.
  56. * This method is called before a test is executed.
  57. *
  58. * @return void
  59. */
  60. public function setUp()
  61. {
  62. $this->clearRegistry();
  63. $this->_cache = CacheFactory::adapterFactory('memory', array('memory_limit' => 0));
  64. Currency\Currency::setCache($this->_cache);
  65. $this->helper = new Helper\Currency('de_AT');
  66. }
  67. /**
  68. * Tears down the fixture, for example, close a network connection.
  69. * This method is called after a test is executed.
  70. *
  71. * @return void
  72. */
  73. public function tearDown()
  74. {
  75. unset($this->helper);
  76. $this->_cache->clear(CacheAdapter::MATCH_ALL);
  77. $this->clearRegistry();
  78. }
  79. public function testCurrencyObjectPassedToConstructor()
  80. {
  81. $curr = new Currency\Currency('de_AT');
  82. $helper = new Helper\Currency($curr);
  83. $this->assertEquals('€ 1.234,56', $helper->__invoke(1234.56));
  84. $this->assertEquals('€ 0,12', $helper->__invoke(0.123));
  85. }
  86. public function testLocalCurrencyObjectUsedWhenPresent()
  87. {
  88. $curr = new Currency\Currency('de_AT');
  89. $this->helper->setCurrency($curr);
  90. $this->assertEquals('€ 1.234,56', $this->helper->__invoke(1234.56));
  91. $this->assertEquals('€ 0,12', $this->helper->__invoke(0.123));
  92. }
  93. public function testCurrencyObjectInRegistryUsedInAbsenceOfLocalCurrencyObject()
  94. {
  95. $curr = new Currency\Currency('de_AT');
  96. \Zend\Registry::set('Zend_Currency', $curr);
  97. $this->assertEquals('€ 1.234,56', $this->helper->__invoke(1234.56));
  98. }
  99. public function testPassingNonNullNonCurrencyObjectToConstructorThrowsException()
  100. {
  101. try {
  102. $helper = new Helper\Currency('something');
  103. } catch (\Exception $e) {
  104. if (substr($e->getMessage(), 0, 15) == 'No region found') {
  105. $this->assertContains('within the locale', $e->getMessage());
  106. } else {
  107. $this->assertContains('not found', $e->getMessage());
  108. }
  109. }
  110. }
  111. public function testPassingNonCurrencyObjectToSetCurrencyThrowsException()
  112. {
  113. try {
  114. $this->helper->setCurrency('something');
  115. } catch (\Exception $e) {
  116. if (substr($e->getMessage(), 0, 15) == 'No region found') {
  117. $this->assertContains('within the locale', $e->getMessage());
  118. } else {
  119. $this->assertContains('not found', $e->getMessage());
  120. }
  121. }
  122. }
  123. public function testCanOutputCurrencyWithOptions()
  124. {
  125. $curr = new Currency\Currency('de_AT');
  126. $this->helper->setCurrency($curr);
  127. $this->assertEquals("€ 1.234,56", $this->helper->__invoke(1234.56, "de_AT"));
  128. }
  129. public function testCurrencyObjectNullByDefault()
  130. {
  131. $this->assertNotNull($this->helper->getCurrency());
  132. }
  133. public function testLocalCurrencyObjectIsPreferredOverRegistry()
  134. {
  135. $currReg = new Currency\Currency('de_AT');
  136. \Zend\Registry::set('Zend_Currency', $currReg);
  137. $this->helper = new Helper\Currency();
  138. $this->assertSame($currReg, $this->helper->getCurrency());
  139. $currLoc = new Currency\Currency('en_US');
  140. $this->helper->setCurrency($currLoc);
  141. $this->assertSame($currLoc, $this->helper->getCurrency());
  142. $this->assertNotSame($currLoc, $currReg);
  143. }
  144. public function testHelperObjectReturnedWhenNoArgumentsPassed()
  145. {
  146. $helper = $this->helper->__invoke();
  147. $this->assertSame($this->helper, $helper);
  148. $currLoc = new Currency\Currency('de_AT');
  149. $this->helper->setCurrency($currLoc);
  150. $helper = $this->helper->__invoke();
  151. $this->assertSame($this->helper, $helper);
  152. }
  153. }