num.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * Part of the Fuel framework.
  4. *
  5. * @package Fuel
  6. * @version 1.5
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2013 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Fuel\Core;
  13. /**
  14. * Numeric helper tests
  15. *
  16. * @package Fuel
  17. * @category Core
  18. * @author Chase "Syntaqx" Hutchins
  19. *
  20. * @group Core
  21. * @group Num
  22. */
  23. class Test_Num extends TestCase
  24. {
  25. /**
  26. * @see Num::bytes
  27. */
  28. public function test_bytes()
  29. {
  30. $output = Num::bytes('200K');
  31. $expected = '204800';
  32. $this->assertEquals($expected, $output);
  33. }
  34. /**
  35. * @see Num::bytes
  36. * @expectedException Exception
  37. */
  38. public function test_bytes_exception()
  39. {
  40. $output = Num::bytes('invalid');
  41. }
  42. /**
  43. * @see Num::format_bytes
  44. */
  45. public function test_format_bytes()
  46. {
  47. $output = Num::format_bytes('204800');
  48. $expected = '200 kB';
  49. $this->assertEquals($expected, $output);
  50. $output = Num::format_bytes('invalid');
  51. $this->assertFalse($output);
  52. }
  53. /**
  54. * @see Num::quantity
  55. */
  56. public function test_quantity()
  57. {
  58. // Return the same
  59. $output = Num::quantity('100');
  60. $expected = '100';
  61. $this->assertEquals($expected, $output);
  62. $output = Num::quantity('7500');
  63. $expected = '8K';
  64. $this->assertEquals($expected, $output);
  65. $output = Num::quantity('1500000');
  66. $expected = '2M';
  67. $this->assertEquals($expected, $output);
  68. $output = Num::quantity('1000000000');
  69. $expected = '1B';
  70. $this->assertEquals($expected, $output);
  71. // Get current localized decimal separator
  72. $locale_conv = localeconv();
  73. $decimal_point = isset($locale_conv['decimal_point']) ? $locale_conv['decimal_point'] : '.';
  74. $output = Num::quantity('7500', 1);
  75. $expected = '7'.$decimal_point.'5K';
  76. $this->assertEquals($expected, $output);
  77. }
  78. /**
  79. * @see Num::format
  80. */
  81. public function test_format()
  82. {
  83. $output = Num::format('1234567890', '(000) 000-0000');
  84. $expected = '(123) 456-7890';
  85. $this->assertEquals($expected, $output);
  86. $output = Num::format(null, '(000) 000-0000');
  87. $this->assertNull($output);
  88. $output = Num::format('1234567890', null);
  89. $expected = '1234567890';
  90. $this->assertEquals($expected, $output);
  91. }
  92. /**
  93. * @see Num::mask_string
  94. */
  95. public function test_mask_string()
  96. {
  97. $output = Num::mask_string('1234567812345678', '**** - **** - **** - 0000', ' -');
  98. $expected = '**** - **** - **** - 5678';
  99. $this->assertEquals($expected, $output);
  100. // Return the same
  101. $output = Num::mask_string('1234567812345678');
  102. $expected = '1234567812345678';
  103. $this->assertEquals($expected, $output);
  104. }
  105. /**
  106. * @see Num::format_phone
  107. */
  108. public function test_format_phone()
  109. {
  110. $output = Num::format_phone('1234567890');
  111. $expected = '(123) 456-7890';
  112. $this->assertEquals($expected, $output);
  113. }
  114. /**
  115. * @see Num::smart_format_phone
  116. */
  117. public function test_smart_format_phone()
  118. {
  119. $output = Num::smart_format_phone('1234567');
  120. $expected = '123-4567';
  121. $this->assertEquals($expected, $output);
  122. // Return the same
  123. $output = Num::smart_format_phone('123456');
  124. $expected = '123456';
  125. $this->assertEquals($expected, $output);
  126. }
  127. /**
  128. * @see Num::format_exp
  129. */
  130. public function test_format_exp()
  131. {
  132. $output = Num::format_exp('1234');
  133. $expected = '12-34';
  134. $this->assertEquals($expected, $output);
  135. }
  136. /**
  137. * @see Num::mask_credit_card
  138. */
  139. public function test_mask_credit_card()
  140. {
  141. $output = Num::mask_credit_card('1234567812345678');
  142. $expected = '**** **** **** 5678';
  143. $this->assertEquals($expected, $output);
  144. }
  145. }
  146. /* End of file num.php */