NumberHelperTest.php 3.1 KB

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