NumberHelperTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace Tools\Test\TestCase\View\Helper;
  3. use Cake\Core\Configure;
  4. use Cake\View\View;
  5. use Shim\TestSuite\TestCase;
  6. use Tools\I18n\Number;
  7. use Tools\View\Helper\NumberHelper;
  8. class NumberHelperTest extends TestCase {
  9. /**
  10. * @var \Tools\View\Helper\NumberHelper|\Tools\Utility\Number
  11. */
  12. protected $Number;
  13. /**
  14. * @var string
  15. */
  16. protected $locale;
  17. /**
  18. * @return void
  19. */
  20. public function setUp(): void {
  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(): void {
  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->assertSame($expected, $is);
  57. $is = $this->Number->format(22.01);
  58. $expected = '22,01';
  59. $this->assertSame($expected, $is);
  60. $is = $this->Number->format(22, ['places' => 2]);
  61. $expected = '22,00';
  62. $this->assertSame($expected, $is);
  63. $is = $this->Number->format(22.30, ['places' => 1]);
  64. $expected = '22,3';
  65. $this->assertSame($expected, $is);
  66. $is = $this->Number->format('22.30', ['precision' => -1]);
  67. $expected = '22,3'; // Why not 22 ?
  68. $this->assertSame($expected, $is);
  69. $is = $this->Number->format(22.30, ['places' => 3]);
  70. $expected = '22,300';
  71. $this->assertSame($expected, $is);
  72. $is = $this->Number->format(22.3, ['places' => 2, 'before' => 'EUR ']);
  73. $expected = 'EUR 22,30';
  74. $this->assertSame($expected, $is);
  75. $is = $this->Number->format(22.3, ['places' => 2, 'after' => ' EUR']);
  76. $expected = '22,30 EUR';
  77. $this->assertSame($expected, $is);
  78. $is = $this->Number->format(22.3, ['places' => 2, 'after' => 'x', 'before' => 'v']);
  79. $expected = 'v22,30x';
  80. $this->assertSame($expected, $is);
  81. $is = $this->Number->format(22.3, ['places' => 2, 'locale' => 'en-US']);
  82. $expected = '22.30';
  83. $this->assertSame($expected, $is);
  84. }
  85. /**
  86. * @return void
  87. */
  88. public function testCurrency() {
  89. $is = Number::getDefaultCurrency();
  90. $this->assertSame('EUR', $is);
  91. $is = $this->Number->currency(22.2);
  92. $this->assertSame('22,20 €', $is);
  93. }
  94. /**
  95. * @return void
  96. */
  97. public function testToReadableSize() {
  98. $is = $this->Number->toReadableSize(1206);
  99. $this->assertSame('1,18 KB', $is);
  100. $is = $this->Number->toReadableSize(1024 * 1024 * 1024);
  101. $this->assertSame('1 GB', $is);
  102. $is = $this->Number->toReadableSize(1024 * 1024 * 1024 * 1024 * 2.5);
  103. $this->assertSame('2,5 TB', $is);
  104. }
  105. }