/tests/Zend/Filter/NormalizedToLocalizedTest.php

https://github.com/Exercise/zf2 · PHP · 237 lines · 157 code · 25 blank · 55 comment · 0 complexity · 1ccb7760e001b177d3c93e16df4558be 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_Filter
  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$
  21. */
  22. namespace ZendTest\Filter;
  23. use Zend\Filter\NormalizedToLocalized as NormalizedToLocalizedFilter;
  24. /**
  25. * @category Zend
  26. * @package Zend_Filter
  27. * @subpackage UnitTests
  28. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. * @group Zend_Filter
  31. */
  32. class NormalizedToLocalizedTest extends \PHPUnit_Framework_TestCase
  33. {
  34. /**
  35. * Ensures that the filter follows expected behavior
  36. *
  37. * @return void
  38. */
  39. public function testNumberLocalization()
  40. {
  41. $filter = new NormalizedToLocalizedFilter(array('locale' => 'de'));
  42. $valuesExpected = array(
  43. 1 => '0',
  44. 2 => 0,
  45. 3 => '1234',
  46. 4 => 1234,
  47. 5 => '1.234',
  48. 6 => '1234.56',
  49. 7 => 1234.56,
  50. 8 => '-1234',
  51. 9 => -1234,
  52. 10 => '-1234.56',
  53. 11 => -1234.56
  54. );
  55. $valuesReceived = array(
  56. 1 => '0',
  57. 2 => '0',
  58. 3 => '1.234',
  59. 4 => '1.234',
  60. 5 => '1,234',
  61. 6 => '1.234,56',
  62. 7 => '1.234,56',
  63. 8 => '-1.234',
  64. 9 => '-1.234',
  65. 10 => '-1.234,56',
  66. 11 => '-1.234,56'
  67. );
  68. foreach ($valuesExpected as $key => $value) {
  69. $this->assertEquals($valuesReceived[$key], $filter($value), 'failed filter of ' . var_export($value, 1));
  70. }
  71. }
  72. /**
  73. * Ensures that the filter follows expected behavior
  74. *
  75. * @return void
  76. */
  77. public function testDateLocalizationWithoutParameters()
  78. {
  79. $filter = new NormalizedToLocalizedFilter(array('locale' => 'de', 'date_format' => 'HH:mm:ss'));
  80. $valuesExpected[1] = array(
  81. 'hour' => '11',
  82. 'minute' => '22',
  83. 'second' => '33');
  84. $valuesReceived[1] = '11:22:33';
  85. foreach ($valuesExpected as $key => $value) {
  86. $this->assertEquals($valuesReceived[$key], $filter($value), 'failed filter of ' . var_export($value, 1));
  87. }
  88. $filter = new NormalizedToLocalizedFilter(array('locale' => 'de', 'date_format' => 'dd.MM.yyyy'));
  89. $valuesExpected[1] = array(
  90. 'date_format' => 'dd.MM.yyyy',
  91. 'locale' => 'de',
  92. 'day' => '20',
  93. 'month' => '04',
  94. 'year' => '2009');
  95. $valuesReceived[1] = '20.04.2009';
  96. $valuesExpected[2] = array(
  97. 'date_format' => null,
  98. 'locale' => 'de',
  99. 'day' => '20',
  100. 'month' => '04',
  101. 'year' => '2009');
  102. $valuesReceived[2] = '20.04.2009';
  103. $valuesExpected[3] = array(
  104. 'date_format' => 'dd.MM.yyyy',
  105. 'day' => '20',
  106. 'month' => '04',
  107. 'year' => '2009');
  108. $valuesReceived[3] = '20.04.2009';
  109. $valuesExpected[4] = array(
  110. 'day' => '20',
  111. 'month' => '04',
  112. 'year' => '2009');
  113. $valuesReceived[4] = '20.04.2009';
  114. foreach ($valuesExpected as $key => $value) {
  115. $this->assertEquals($valuesReceived[$key], $filter($value), 'failed filter of ' . var_export($value, 1));
  116. }
  117. }
  118. /**
  119. * Ensures that the filter follows expected behavior
  120. *
  121. * @return void
  122. */
  123. public function testDateLocalizationWithParameters()
  124. {
  125. $filter = new NormalizedToLocalizedFilter(array('locale' => 'de', 'date_format' => 'yyyy.dd.MM'));
  126. // Note that any non date array key like date_format or locale does not
  127. // change filter parameters... only day, month and year are used
  128. $valuesExpected[1] = array(
  129. 'date_format' => 'yyyy.dd.MM',
  130. 'locale' => 'de',
  131. 'day' => '20',
  132. 'month' => '04',
  133. 'year' => '2009');
  134. $valuesReceived[1] = '2009.20.04';
  135. $valuesExpected[2] = array(
  136. 'day' => '20',
  137. 'month' => '04',
  138. 'year' => '2009');
  139. $valuesReceived[2] = '2009.20.04';
  140. $valuesExpected[3] = array(
  141. 'locale' => 'de',
  142. 'day' => '20',
  143. 'month' => '04',
  144. 'year' => '2009');
  145. $valuesReceived[3] = '2009.20.04';
  146. $valuesExpected[4] = array(
  147. 'date_format' => null,
  148. 'day' => '20',
  149. 'month' => '04',
  150. 'year' => '2009');
  151. $valuesReceived[4] = '2009.20.04';
  152. foreach ($valuesExpected as $key => $value) {
  153. $this->assertEquals($valuesReceived[$key], $filter($value), 'failed filter of ' . var_export($value, 1));
  154. }
  155. }
  156. /**
  157. * Ensures that the filter follows expected behavior
  158. *
  159. * @return void
  160. */
  161. public function testLocalizationToInteger()
  162. {
  163. $filter = new NormalizedToLocalizedFilter(array('locale' => 'de', 'precision' => 0));
  164. $valuesExpected = array(
  165. 1 => '1234.56',
  166. 2 => 1234.56,
  167. 3 => '1.234',
  168. 4 => 1.234,
  169. 5 => '1234',
  170. 6 => 1234
  171. );
  172. $valuesReceived = array(
  173. 1 => '1.235',
  174. 2 => '1.235',
  175. 3 => '1',
  176. 4 => '1',
  177. 5 => '1.234',
  178. 6 => '1.234'
  179. );
  180. foreach ($valuesExpected as $key => $value) {
  181. $this->assertEquals($valuesReceived[$key], $filter($value), 'failed filter of ' . var_export($value, 1));
  182. }
  183. }
  184. /**
  185. * Ensures that the filter follows expected behavior
  186. *
  187. * @return void
  188. */
  189. public function testLocalizationToFloat()
  190. {
  191. $filter = new NormalizedToLocalizedFilter(array('locale' => 'de', 'precision' => 2));
  192. $valuesExpected = array(
  193. 1 => '1234.5678',
  194. 2 => 1234.5678,
  195. 3 => '1.234',
  196. 4 => 1.234,
  197. 5 => '1234',
  198. 6 => 1234
  199. );
  200. $valuesReceived = array(
  201. 1 => '1.234,57',
  202. 2 => '1.234,57',
  203. 3 => '1,23',
  204. 4 => '1,23',
  205. 5 => '1.234,00',
  206. 6 => '1.234,00'
  207. );
  208. foreach ($valuesExpected as $key => $value) {
  209. $this->assertEquals($valuesReceived[$key], $filter($value), 'failed filter of ' . var_export($value, 1));
  210. }
  211. }
  212. }