NumberHelperTest.php 2.8 KB

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