wpConvertHrToBytes.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * Tests for wp_convert_hr_to_bytes().
  4. *
  5. * @group load.php
  6. * @covers ::wp_convert_hr_to_bytes
  7. */
  8. class Tests_Load_wpConvertHrToBytes extends WP_UnitTestCase {
  9. /**
  10. * Tests converting (PHP ini) byte values to integer byte values.
  11. *
  12. * @ticket 32075
  13. *
  14. * @dataProvider data_wp_convert_hr_to_bytes
  15. *
  16. * @param int|string $value The value passed to wp_convert_hr_to_bytes().
  17. * @param int $expected The expected output of wp_convert_hr_to_bytes().
  18. */
  19. function test_wp_convert_hr_to_bytes( $value, $expected ) {
  20. $this->assertSame( $expected, wp_convert_hr_to_bytes( $value ) );
  21. }
  22. /**
  23. * Data provider for test_wp_convert_hr_to_bytes().
  24. *
  25. * @return array {
  26. * @type array {
  27. * @type int|string $value The value passed to wp_convert_hr_to_bytes().
  28. * @type int $expected The expected output of wp_convert_hr_to_bytes().
  29. * }
  30. * }
  31. */
  32. function data_wp_convert_hr_to_bytes() {
  33. $array = array(
  34. // Integer input.
  35. array( -1, -1 ), // = no memory limit.
  36. array( 8388608, 8388608 ), // 8M.
  37. // String input (memory limit shorthand values).
  38. array( '32k', 32768 ),
  39. array( '64K', 65536 ),
  40. array( '128m', 134217728 ),
  41. array( '256M', 268435456 ),
  42. array( '1g', 1073741824 ),
  43. array( '128m ', 134217728 ), // Leading/trailing whitespace gets trimmed.
  44. array( '1024', 1024 ), // No letter will be interpreted as integer value.
  45. // Edge cases.
  46. array( 'g', 0 ),
  47. array( 'g1', 0 ),
  48. array( 'null', 0 ),
  49. array( 'off', 0 ),
  50. );
  51. // Test for running into maximum integer size limit on 32bit systems.
  52. if ( 2147483647 === PHP_INT_MAX ) {
  53. $array[] = array( '2G', 2147483647 );
  54. $array[] = array( '4G', 2147483647 );
  55. } else {
  56. $array[] = array( '2G', 2147483648 );
  57. $array[] = array( '4G', 4294967296 );
  58. }
  59. return $array;
  60. }
  61. }