PageRenderTime 25ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

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