NumberHelperTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace Tools\TestCase\View\Helper;
  3. use Tools\View\Helper\NumberHelper;
  4. use Tools\TestSuite\TestCase;
  5. use Cake\View\View;
  6. use Cake\Core\Configure;
  7. use Tools\Utility\Number;
  8. /**
  9. * Number Test Case
  10. */
  11. class NumberHelperTest extends TestCase {
  12. public function setUp() {
  13. parent::setUp();
  14. Configure::write('Localization', array(
  15. 'decimals' => ',',
  16. 'thousands' => '.'
  17. ));
  18. Number::config();
  19. $this->Number = new NumberHelper(new View(null));
  20. }
  21. /**
  22. * Test format
  23. *
  24. * @return void
  25. */
  26. public function testFormat() {
  27. $is = $this->Number->format('22');
  28. $expected = '22';
  29. $this->assertEquals($expected, $is);
  30. $is = $this->Number->format('22.01');
  31. $expected = '22,01';
  32. $this->assertEquals($expected, $is);
  33. $is = $this->Number->format('22', ['places' => 2]);
  34. $expected = '22,00';
  35. $this->assertEquals($expected, $is);
  36. $is = $this->Number->format('22.30', array('places' => 1));
  37. $expected = '22,3';
  38. $this->assertEquals($expected, $is);
  39. $is = $this->Number->format('22.30', array('precision' => -1));
  40. $expected = '22';
  41. $this->assertEquals($expected, $is);
  42. $is = $this->Number->format('22.30', array('places' => 3));
  43. $expected = '22,300';
  44. $this->assertEquals($expected, $is);
  45. $is = $this->Number->format('abc', array('places' => 2));
  46. $expected = '0,00';
  47. $this->assertEquals($expected, $is);
  48. $is = $this->Number->format('22.3', array('places' => 2, 'before' => 'EUR '));
  49. $expected = 'EUR 22,30';
  50. $this->assertEquals($expected, $is);
  51. $is = $this->Number->format('22.3', array('places' => 2, 'after' => ' EUR'));
  52. $expected = '22,30 EUR';
  53. $this->assertEquals($expected, $is);
  54. $is = $this->Number->format('22.3', array('places' => 2, 'after' => 'x', 'before' => 'v'));
  55. $expected = 'v22,30x';
  56. $this->assertEquals($expected, $is);
  57. $is = $this->Number->format('22.3', array('places' => 2, 'locale' => 'en-US'));
  58. $expected = '22.30';
  59. $this->assertEquals($expected, $is);
  60. }
  61. /**
  62. * NumberHelperTest::testCurrency()
  63. *
  64. * @return void
  65. */
  66. public function testCurrency() {
  67. $is = Number::defaultCurrency();
  68. $this->assertEquals('EUR', $is);
  69. $is = $this->Number->currency(22.2);
  70. $this->assertEquals('22,20 €', $is);
  71. }
  72. /**
  73. * NumberHelperTest::testToReadableSize()
  74. *
  75. * @return void
  76. */
  77. public function testToReadableSize() {
  78. $is = $this->Number->toReadableSize(1206);
  79. $this->assertEquals('1,18 KB', $is);
  80. $is = $this->Number->toReadableSize(1024 * 1024 * 1024);
  81. $this->assertEquals('1 GB', $is);
  82. $is = $this->Number->toReadableSize(1024 * 1024 * 1024 * 1024 * 2.5);
  83. $this->assertEquals('2,5 TB', $is);
  84. }
  85. public function tearDown() {
  86. parent::tearDown();
  87. unset($this->Number);
  88. }
  89. }