NumberHelperTest.php 2.9 KB

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