NumTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * Tests Num
  4. *
  5. * @group ko7
  6. * @group ko7.core
  7. * @group ko7.core.num
  8. * @package KO7
  9. * @category Tests
  10. *
  11. * @author BRMatt <matthew@sigswitch.com>
  12. * @copyright (c) 2007-2016 Kohana Team
  13. * @copyright (c) since 2016 Koseven Team
  14. * @license https://koseven.dev/LICENSE
  15. */
  16. class KO7_NumTest extends Unittest_TestCase
  17. {
  18. protected $default_locale;
  19. /**
  20. * SetUp test enviroment
  21. */
  22. // @codingStandardsIgnoreStart
  23. public function setUp(): void
  24. // @codingStandardsIgnoreEnd
  25. {
  26. parent::setUp();
  27. $this->default_locale = setlocale(LC_ALL, 0);
  28. setlocale(LC_ALL, 'en_US.utf8');
  29. }
  30. /**
  31. * Tear down environment
  32. */
  33. // @codingStandardsIgnoreStart
  34. public function tearDown(): void
  35. // @codingStandardsIgnoreEnd
  36. {
  37. parent::tearDown();
  38. setlocale(LC_ALL, $this->default_locale);
  39. }
  40. /**
  41. * Provides test data for test_bytes()
  42. *
  43. * @return array
  44. */
  45. public function provider_bytes()
  46. {
  47. return [
  48. [204800.0, '200K'],
  49. [5242880.0, '5MiB'],
  50. [1000.0, 1000],
  51. [2684354560.0, '2.5GB'],
  52. ];
  53. }
  54. /**
  55. * Tests Num::bytes()
  56. *
  57. * @test
  58. * @covers Num::bytes
  59. * @dataProvider provider_bytes
  60. * @param integer Expected Value
  61. * @param string Input value
  62. */
  63. public function test_bytes($expected, $size)
  64. {
  65. $this->assertSame($expected, Num::bytes($size));
  66. }
  67. /**
  68. * Provides test data for test_ordinal()
  69. * @return array
  70. */
  71. public function provider_ordinal()
  72. {
  73. return [
  74. [0, 'th'],
  75. [1, 'st'],
  76. [21, 'st'],
  77. [112, 'th'],
  78. [23, 'rd'],
  79. [42, 'nd'],
  80. ];
  81. }
  82. /**
  83. *
  84. * @test
  85. * @dataProvider provider_ordinal
  86. * @param integer $number
  87. * @param <type> $expected
  88. */
  89. public function test_ordinal($number, $expected)
  90. {
  91. $this->assertSame($expected, Num::ordinal($number));
  92. }
  93. /**
  94. * Provides test data for test_format()
  95. * @return array
  96. */
  97. public function provider_format()
  98. {
  99. return [
  100. // English
  101. [10000, 2, FALSE, '10,000.00'],
  102. [10000, 2, TRUE, '10,000.00'],
  103. // Additional dp's should be removed
  104. [123.456, 2, FALSE, '123.46'],
  105. [123.456, 2, TRUE, '123.46'],
  106. ];
  107. }
  108. /**
  109. * @TODO test locales
  110. * @test
  111. * @dataProvider provider_format
  112. * @param integer $number
  113. * @param integer $places
  114. * @param boolean $monetary
  115. * @param string $expected
  116. */
  117. public function test_format($number, $places, $monetary, $expected)
  118. {
  119. $this->assertSame($expected, Num::format($number, $places, $monetary));
  120. }
  121. /**
  122. * Provides data for test_round()
  123. * @return array
  124. */
  125. function provider_round()
  126. {
  127. return [
  128. [5.5, 0, [
  129. 6.0,
  130. 5.0,
  131. 6.0,
  132. 5.0,
  133. ]],
  134. [42.5, 0, [
  135. 43.0,
  136. 42.0,
  137. 42.0,
  138. 43.0,
  139. ]],
  140. [10.4, 0, [
  141. 10.0,
  142. 10.0,
  143. 10.0,
  144. 10.0,
  145. ]],
  146. [10.8, 0, [
  147. 11.0,
  148. 11.0,
  149. 11.0,
  150. 11.0,
  151. ]],
  152. [-5.5, 0, [
  153. -6.0,
  154. -5.0,
  155. -6.0,
  156. -5.0,
  157. ]],
  158. [-10.5, 0, [
  159. -11.0,
  160. -10.0,
  161. -10.0,
  162. -11.0,
  163. ]],
  164. [26.12375, 4, [
  165. 26.1238,
  166. 26.1237,
  167. 26.1238,
  168. 26.1237,
  169. ]],
  170. [26.12325, 4, [
  171. 26.1233,
  172. 26.1232,
  173. 26.1232,
  174. 26.1233,
  175. ]],
  176. ];
  177. }
  178. /**
  179. * @test
  180. * @dataProvider provider_round
  181. * @param number $input
  182. * @param integer $precision
  183. * @param integer $mode
  184. * @param number $expected
  185. */
  186. function test_round($input, $precision, $expected)
  187. {
  188. foreach ([Num::ROUND_HALF_UP, Num::ROUND_HALF_DOWN, Num::ROUND_HALF_EVEN, Num::ROUND_HALF_ODD] as $i => $mode)
  189. {
  190. $this->assertSame($expected[$i], Num::round($input, $precision, $mode, FALSE));
  191. }
  192. }
  193. }