FilterTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. use PrivateBin\Filter;
  3. use Eris\Generator;
  4. class FilterTest extends PHPUnit_Framework_TestCase
  5. {
  6. use Eris\TestTrait;
  7. public function testFilterStripsSlashesDeeply()
  8. {
  9. $this->assertEquals(
  10. array("f'oo", "b'ar", array("fo'o", "b'ar")),
  11. Filter::stripslashesDeep(array("f\\'oo", "b\\'ar", array("fo\\'o", "b\\'ar")))
  12. );
  13. }
  14. public function testFilterMakesTimesHumanlyReadable()
  15. {
  16. $this->forAll(
  17. Generator\nat(),
  18. Generator\oneOf(
  19. 'sec', 'second', 'seconds'
  20. )
  21. )->then(
  22. function ($int, $unit)
  23. {
  24. $suffix = $int === 1 ? '' : 's';
  25. $this->assertEquals($int . ' second' . $suffix, Filter::formatHumanReadableTime($int . $unit));
  26. }
  27. );
  28. $this->forAll(
  29. Generator\nat(),
  30. Generator\oneOf(
  31. 'min', 'minute', 'minutes'
  32. )
  33. )->then(
  34. function ($int, $unit)
  35. {
  36. $suffix = $int === 1 ? '' : 's';
  37. $this->assertEquals($int . ' minute' . $suffix, Filter::formatHumanReadableTime($int . $unit));
  38. }
  39. );
  40. $this->forAll(
  41. Generator\nat(),
  42. Generator\oneOf(
  43. 'hour', 'hours', 'day', 'days', 'week', 'weeks',
  44. 'month', 'months', 'year', 'years'
  45. )
  46. )->then(
  47. function ($int, $unit)
  48. {
  49. $suffix = $int === 1 ? '' : 's';
  50. $this->assertEquals($int . ' ' . rtrim($unit, 's') . $suffix, Filter::formatHumanReadableTime($int . $unit));
  51. }
  52. );
  53. }
  54. /**
  55. * @expectedException Exception
  56. * @expectedExceptionCode 30
  57. */
  58. public function testFilterFailTimesHumanlyReadable()
  59. {
  60. Filter::formatHumanReadableTime('five_minutes');
  61. }
  62. public function testFilterMakesSizesHumanlyReadable()
  63. {
  64. $this->assertEquals('1 B', Filter::formatHumanReadableSize(1));
  65. $this->assertEquals('1 000 B', Filter::formatHumanReadableSize(1000));
  66. $this->assertEquals('1.00 KiB', Filter::formatHumanReadableSize(1024));
  67. $this->assertEquals('1.21 KiB', Filter::formatHumanReadableSize(1234));
  68. $exponent = 1024;
  69. $this->assertEquals('1 000.00 KiB', Filter::formatHumanReadableSize(1000 * $exponent));
  70. $this->assertEquals('1.00 MiB', Filter::formatHumanReadableSize(1024 * $exponent));
  71. $this->assertEquals('1.21 MiB', Filter::formatHumanReadableSize(1234 * $exponent));
  72. $exponent *= 1024;
  73. $this->assertEquals('1 000.00 MiB', Filter::formatHumanReadableSize(1000 * $exponent));
  74. $this->assertEquals('1.00 GiB', Filter::formatHumanReadableSize(1024 * $exponent));
  75. $this->assertEquals('1.21 GiB', Filter::formatHumanReadableSize(1234 * $exponent));
  76. $exponent *= 1024;
  77. $this->assertEquals('1 000.00 GiB', Filter::formatHumanReadableSize(1000 * $exponent));
  78. $this->assertEquals('1.00 TiB', Filter::formatHumanReadableSize(1024 * $exponent));
  79. $this->assertEquals('1.21 TiB', Filter::formatHumanReadableSize(1234 * $exponent));
  80. $exponent *= 1024;
  81. $this->assertEquals('1 000.00 TiB', Filter::formatHumanReadableSize(1000 * $exponent));
  82. $this->assertEquals('1.00 PiB', Filter::formatHumanReadableSize(1024 * $exponent));
  83. $this->assertEquals('1.21 PiB', Filter::formatHumanReadableSize(1234 * $exponent));
  84. $exponent *= 1024;
  85. $this->assertEquals('1 000.00 PiB', Filter::formatHumanReadableSize(1000 * $exponent));
  86. $this->assertEquals('1.00 EiB', Filter::formatHumanReadableSize(1024 * $exponent));
  87. $this->assertEquals('1.21 EiB', Filter::formatHumanReadableSize(1234 * $exponent));
  88. $exponent *= 1024;
  89. $this->assertEquals('1 000.00 EiB', Filter::formatHumanReadableSize(1000 * $exponent));
  90. $this->assertEquals('1.00 ZiB', Filter::formatHumanReadableSize(1024 * $exponent));
  91. $this->assertEquals('1.21 ZiB', Filter::formatHumanReadableSize(1234 * $exponent));
  92. $exponent *= 1024;
  93. $this->assertEquals('1 000.00 ZiB', Filter::formatHumanReadableSize(1000 * $exponent));
  94. $this->assertEquals('1.00 YiB', Filter::formatHumanReadableSize(1024 * $exponent));
  95. $this->assertEquals('1.21 YiB', Filter::formatHumanReadableSize(1234 * $exponent));
  96. }
  97. public function testSlowEquals()
  98. {
  99. $this->assertTrue(Filter::slowEquals('foo', 'foo'), 'same string');
  100. $this->assertFalse(Filter::slowEquals('foo', true), 'string and boolean');
  101. $this->assertFalse(Filter::slowEquals('foo', 0), 'string and integer');
  102. $this->assertFalse(Filter::slowEquals('123foo', 123), 'string and integer');
  103. $this->assertFalse(Filter::slowEquals('123foo', '123'), 'different strings');
  104. $this->assertFalse(Filter::slowEquals('6', ' 6'), 'strings with space');
  105. $this->assertFalse(Filter::slowEquals('4.2', '4.20'), 'floats as strings');
  106. $this->assertFalse(Filter::slowEquals('1e3', '1000'), 'integers as strings');
  107. $this->assertFalse(Filter::slowEquals('9223372036854775807', '9223372036854775808'), 'large integers as strings');
  108. $this->assertFalse(Filter::slowEquals('61529519452809720693702583126814', '61529519452809720000000000000000'), 'larger integers as strings');
  109. }
  110. }