NumberFormatterTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Intl\Tests\NumberFormatter;
  11. use Symfony\Component\Intl\Globals\IntlGlobals;
  12. use Symfony\Component\Intl\NumberFormatter\NumberFormatter;
  13. /**
  14. * Note that there are some values written like -2147483647 - 1. This is the lower 32bit int max and is a known
  15. * behavior of PHP.
  16. */
  17. class NumberFormatterTest extends AbstractNumberFormatterTest
  18. {
  19. /**
  20. * @expectedException \Symfony\Component\Intl\Exception\MethodArgumentValueNotImplementedException
  21. */
  22. public function testConstructorWithUnsupportedLocale()
  23. {
  24. new NumberFormatter('pt_BR');
  25. }
  26. /**
  27. * @expectedException \Symfony\Component\Intl\Exception\MethodArgumentValueNotImplementedException
  28. */
  29. public function testConstructorWithUnsupportedStyle()
  30. {
  31. new NumberFormatter('en', NumberFormatter::PATTERN_DECIMAL);
  32. }
  33. /**
  34. * @expectedException \Symfony\Component\Intl\Exception\MethodArgumentNotImplementedException
  35. */
  36. public function testConstructorWithPatternDifferentThanNull()
  37. {
  38. new NumberFormatter('en', NumberFormatter::DECIMAL, '');
  39. }
  40. /**
  41. * @expectedException \Symfony\Component\Intl\Exception\MethodArgumentValueNotImplementedException
  42. */
  43. public function testSetAttributeWithUnsupportedAttribute()
  44. {
  45. $formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL);
  46. $formatter->setAttribute(NumberFormatter::LENIENT_PARSE, null);
  47. }
  48. /**
  49. * @expectedException \Symfony\Component\Intl\Exception\MethodArgumentValueNotImplementedException
  50. */
  51. public function testSetAttributeInvalidRoundingMode()
  52. {
  53. $formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL);
  54. $formatter->setAttribute(NumberFormatter::ROUNDING_MODE, null);
  55. }
  56. public function testConstructWithoutLocale()
  57. {
  58. $this->assertInstanceOf(
  59. '\Symfony\Component\Intl\NumberFormatter\NumberFormatter',
  60. $this->getNumberFormatter(null, NumberFormatter::DECIMAL)
  61. );
  62. }
  63. public function testCreate()
  64. {
  65. $this->assertInstanceOf(
  66. '\Symfony\Component\Intl\NumberFormatter\NumberFormatter',
  67. NumberFormatter::create('en', NumberFormatter::DECIMAL)
  68. );
  69. }
  70. /**
  71. * @expectedException \RuntimeException
  72. */
  73. public function testFormatWithCurrencyStyle()
  74. {
  75. parent::testFormatWithCurrencyStyle();
  76. }
  77. /**
  78. * @dataProvider formatTypeInt32Provider
  79. * @expectedException \Symfony\Component\Intl\Exception\MethodArgumentValueNotImplementedException
  80. */
  81. public function testFormatTypeInt32($formatter, $value, $expected, $message = '')
  82. {
  83. parent::testFormatTypeInt32($formatter, $value, $expected, $message);
  84. }
  85. /**
  86. * @dataProvider formatTypeInt32WithCurrencyStyleProvider
  87. * @expectedException \Symfony\Component\Intl\Exception\NotImplementedException
  88. */
  89. public function testFormatTypeInt32WithCurrencyStyle($formatter, $value, $expected, $message = '')
  90. {
  91. parent::testFormatTypeInt32WithCurrencyStyle($formatter, $value, $expected, $message);
  92. }
  93. /**
  94. * @dataProvider formatTypeInt64Provider
  95. * @expectedException \Symfony\Component\Intl\Exception\MethodArgumentValueNotImplementedException
  96. */
  97. public function testFormatTypeInt64($formatter, $value, $expected)
  98. {
  99. parent::testFormatTypeInt64($formatter, $value, $expected);
  100. }
  101. /**
  102. * @dataProvider formatTypeInt64WithCurrencyStyleProvider
  103. * @expectedException \Symfony\Component\Intl\Exception\NotImplementedException
  104. */
  105. public function testFormatTypeInt64WithCurrencyStyle($formatter, $value, $expected)
  106. {
  107. parent::testFormatTypeInt64WithCurrencyStyle($formatter, $value, $expected);
  108. }
  109. /**
  110. * @dataProvider formatTypeDoubleProvider
  111. * @expectedException \Symfony\Component\Intl\Exception\MethodArgumentValueNotImplementedException
  112. */
  113. public function testFormatTypeDouble($formatter, $value, $expected)
  114. {
  115. parent::testFormatTypeDouble($formatter, $value, $expected);
  116. }
  117. /**
  118. * @dataProvider formatTypeDoubleWithCurrencyStyleProvider
  119. * @expectedException \Symfony\Component\Intl\Exception\NotImplementedException
  120. */
  121. public function testFormatTypeDoubleWithCurrencyStyle($formatter, $value, $expected)
  122. {
  123. parent::testFormatTypeDoubleWithCurrencyStyle($formatter, $value, $expected);
  124. }
  125. /**
  126. * @expectedException \Symfony\Component\Intl\Exception\MethodNotImplementedException
  127. */
  128. public function testGetPattern()
  129. {
  130. $formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL);
  131. $formatter->getPattern();
  132. }
  133. public function testGetErrorCode()
  134. {
  135. $formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL);
  136. $this->assertEquals(IntlGlobals::U_ZERO_ERROR, $formatter->getErrorCode());
  137. }
  138. /**
  139. * @expectedException \Symfony\Component\Intl\Exception\MethodNotImplementedException
  140. */
  141. public function testParseCurrency()
  142. {
  143. $formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL);
  144. $formatter->parseCurrency(null, $currency);
  145. }
  146. /**
  147. * @expectedException \Symfony\Component\Intl\Exception\MethodNotImplementedException
  148. */
  149. public function testSetPattern()
  150. {
  151. $formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL);
  152. $formatter->setPattern(null);
  153. }
  154. /**
  155. * @expectedException \Symfony\Component\Intl\Exception\MethodNotImplementedException
  156. */
  157. public function testSetSymbol()
  158. {
  159. $formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL);
  160. $formatter->setSymbol(null, null);
  161. }
  162. /**
  163. * @expectedException \Symfony\Component\Intl\Exception\MethodNotImplementedException
  164. */
  165. public function testSetTextAttribute()
  166. {
  167. $formatter = $this->getNumberFormatter('en', NumberFormatter::DECIMAL);
  168. $formatter->setTextAttribute(null, null);
  169. }
  170. protected function getNumberFormatter($locale = 'en', $style = null, $pattern = null)
  171. {
  172. return new NumberFormatter($locale, $style, $pattern);
  173. }
  174. protected function getIntlErrorMessage()
  175. {
  176. return IntlGlobals::getErrorMessage();
  177. }
  178. protected function getIntlErrorCode()
  179. {
  180. return IntlGlobals::getErrorCode();
  181. }
  182. protected function isIntlFailure($errorCode)
  183. {
  184. return IntlGlobals::isFailure($errorCode);
  185. }
  186. }