SizeTest.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace Drupal\KernelTests\Core\Common;
  3. use Drupal\Component\Utility\Bytes;
  4. use Drupal\KernelTests\KernelTestBase;
  5. /**
  6. * Parse a predefined amount of bytes and compare the output with the expected
  7. * value.
  8. *
  9. * @group Common
  10. */
  11. class SizeTest extends KernelTestBase {
  12. /**
  13. * Checks that format_size() returns the expected string.
  14. *
  15. * @dataProvider providerTestCommonFormatSize
  16. */
  17. public function testCommonFormatSize($expected, $input) {
  18. $size = format_size($input, NULL);
  19. $this->assertEquals($expected, $size);
  20. }
  21. /**
  22. * Provides a list of byte size to test.
  23. */
  24. public function providerTestCommonFormatSize() {
  25. $kb = Bytes::KILOBYTE;
  26. return [
  27. ['0 bytes', 0],
  28. ['1 byte', 1],
  29. ['-1 bytes', -1],
  30. ['2 bytes', 2],
  31. ['-2 bytes', -2],
  32. ['1023 bytes', $kb - 1],
  33. ['1 KB', $kb],
  34. ['1 MB', pow($kb, 2)],
  35. ['1 GB', pow($kb, 3)],
  36. ['1 TB', pow($kb, 4)],
  37. ['1 PB', pow($kb, 5)],
  38. ['1 EB', pow($kb, 6)],
  39. ['1 ZB', pow($kb, 7)],
  40. ['1 YB', pow($kb, 8)],
  41. ['1024 YB', pow($kb, 9)],
  42. // Rounded to 1 MB - not 1000 or 1024 kilobytes
  43. ['1 MB', ($kb * $kb) - 1],
  44. ['-1 MB', -(($kb * $kb) - 1)],
  45. // Decimal Megabytes
  46. ['3.46 MB', 3623651],
  47. ['3.77 GB', 4053371676],
  48. // Decimal Petabytes
  49. ['59.72 PB', 67234178751368124],
  50. // Decimal Yottabytes
  51. ['194.67 YB', 235346823821125814962843827],
  52. ];
  53. }
  54. }