FilterTest.php 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. use PrivateBin\Filter;
  3. class FilterTest extends PHPUnit_Framework_TestCase
  4. {
  5. public function testFilterMakesTimesHumanlyReadable()
  6. {
  7. $this->assertEquals('5 minutes', Filter::formatHumanReadableTime('5min'));
  8. $this->assertEquals('90 seconds', Filter::formatHumanReadableTime('90sec'));
  9. $this->assertEquals('1 week', Filter::formatHumanReadableTime('1week'));
  10. $this->assertEquals('6 months', Filter::formatHumanReadableTime('6months'));
  11. }
  12. /**
  13. * @expectedException Exception
  14. * @expectedExceptionCode 30
  15. */
  16. public function testFilterFailTimesHumanlyReadable()
  17. {
  18. Filter::formatHumanReadableTime('five_minutes');
  19. }
  20. public function testFilterMakesSizesHumanlyReadable()
  21. {
  22. $this->assertEquals('1 B', Filter::formatHumanReadableSize(1));
  23. $this->assertEquals('1 000 B', Filter::formatHumanReadableSize(1000));
  24. $this->assertEquals('1.00 KiB', Filter::formatHumanReadableSize(1024));
  25. $this->assertEquals('1.21 KiB', Filter::formatHumanReadableSize(1234));
  26. $exponent = 1024;
  27. $this->assertEquals('1 000.00 KiB', Filter::formatHumanReadableSize(1000 * $exponent));
  28. $this->assertEquals('1.00 MiB', Filter::formatHumanReadableSize(1024 * $exponent));
  29. $this->assertEquals('1.21 MiB', Filter::formatHumanReadableSize(1234 * $exponent));
  30. $exponent *= 1024;
  31. $this->assertEquals('1 000.00 MiB', Filter::formatHumanReadableSize(1000 * $exponent));
  32. $this->assertEquals('1.00 GiB', Filter::formatHumanReadableSize(1024 * $exponent));
  33. $this->assertEquals('1.21 GiB', Filter::formatHumanReadableSize(1234 * $exponent));
  34. $exponent *= 1024;
  35. $this->assertEquals('1 000.00 GiB', Filter::formatHumanReadableSize(1000 * $exponent));
  36. $this->assertEquals('1.00 TiB', Filter::formatHumanReadableSize(1024 * $exponent));
  37. $this->assertEquals('1.21 TiB', Filter::formatHumanReadableSize(1234 * $exponent));
  38. $exponent *= 1024;
  39. $this->assertEquals('1 000.00 TiB', Filter::formatHumanReadableSize(1000 * $exponent));
  40. $this->assertEquals('1.00 PiB', Filter::formatHumanReadableSize(1024 * $exponent));
  41. $this->assertEquals('1.21 PiB', Filter::formatHumanReadableSize(1234 * $exponent));
  42. $exponent *= 1024;
  43. $this->assertEquals('1 000.00 PiB', Filter::formatHumanReadableSize(1000 * $exponent));
  44. $this->assertEquals('1.00 EiB', Filter::formatHumanReadableSize(1024 * $exponent));
  45. $this->assertEquals('1.21 EiB', Filter::formatHumanReadableSize(1234 * $exponent));
  46. $exponent *= 1024;
  47. $this->assertEquals('1 000.00 EiB', Filter::formatHumanReadableSize(1000 * $exponent));
  48. $this->assertEquals('1.00 ZiB', Filter::formatHumanReadableSize(1024 * $exponent));
  49. $this->assertEquals('1.21 ZiB', Filter::formatHumanReadableSize(1234 * $exponent));
  50. $exponent *= 1024;
  51. $this->assertEquals('1 000.00 ZiB', Filter::formatHumanReadableSize(1000 * $exponent));
  52. $this->assertEquals('1.00 YiB', Filter::formatHumanReadableSize(1024 * $exponent));
  53. $this->assertEquals('1.21 YiB', Filter::formatHumanReadableSize(1234 * $exponent));
  54. }
  55. public function testSlowEquals()
  56. {
  57. $this->assertTrue(Filter::slowEquals('foo', 'foo'), 'same string');
  58. $this->assertFalse(Filter::slowEquals('foo', true), 'string and boolean');
  59. $this->assertFalse(Filter::slowEquals('foo', 0), 'string and integer');
  60. $this->assertFalse(Filter::slowEquals('123foo', 123), 'string and integer');
  61. $this->assertFalse(Filter::slowEquals('123foo', '123'), 'different strings');
  62. $this->assertFalse(Filter::slowEquals('6', ' 6'), 'strings with space');
  63. $this->assertFalse(Filter::slowEquals('4.2', '4.20'), 'floats as strings');
  64. $this->assertFalse(Filter::slowEquals('1e3', '1000'), 'integers as strings');
  65. $this->assertFalse(Filter::slowEquals('9223372036854775807', '9223372036854775808'), 'large integers as strings');
  66. $this->assertFalse(Filter::slowEquals('61529519452809720693702583126814', '61529519452809720000000000000000'), 'larger integers as strings');
  67. }
  68. }