NumberHelperTest.php 2.9 KB

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