BytesTest.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace Drupal\Tests\Component\Utility;
  3. use Drupal\Component\Utility\Bytes;
  4. use PHPUnit\Framework\TestCase;
  5. /**
  6. * Tests bytes size parsing helper methods.
  7. *
  8. * @group Utility
  9. *
  10. * @coversDefaultClass \Drupal\Component\Utility\Bytes
  11. */
  12. class BytesTest extends TestCase {
  13. /**
  14. * Tests \Drupal\Component\Utility\Bytes::toInt().
  15. *
  16. * @param int $size
  17. * The value for the size argument for
  18. * \Drupal\Component\Utility\Bytes::toInt().
  19. * @param int $expected_int
  20. * The expected return value from
  21. * \Drupal\Component\Utility\Bytes::toInt().
  22. *
  23. * @dataProvider providerTestToInt
  24. * @covers ::toInt
  25. */
  26. public function testToInt($size, $expected_int) {
  27. $this->assertEquals($expected_int, Bytes::toInt($size));
  28. }
  29. /**
  30. * Provides data for testToInt.
  31. *
  32. * @return array
  33. * An array of arrays, each containing the argument for
  34. * \Drupal\Component\Utility\Bytes::toInt(): size, and the expected return
  35. * value.
  36. */
  37. public function providerTestToInt() {
  38. return [
  39. ['1', 1],
  40. ['1 byte', 1],
  41. ['1 KB' , Bytes::KILOBYTE],
  42. ['1 MB' , pow(Bytes::KILOBYTE, 2)],
  43. ['1 GB' , pow(Bytes::KILOBYTE, 3)],
  44. ['1 TB' , pow(Bytes::KILOBYTE, 4)],
  45. ['1 PB' , pow(Bytes::KILOBYTE, 5)],
  46. ['1 EB' , pow(Bytes::KILOBYTE, 6)],
  47. ['1 ZB' , pow(Bytes::KILOBYTE, 7)],
  48. ['1 YB' , pow(Bytes::KILOBYTE, 8)],
  49. ['23476892 bytes', 23476892],
  50. // 76 MB.
  51. ['76MRandomStringThatShouldBeIgnoredByParseSize.', 79691776],
  52. // 76.24 GB (with typo).
  53. ['76.24 Giggabyte', 81862076662],
  54. ];
  55. }
  56. }