EnvironmentTest.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace Drupal\Tests\Component\Utility;
  3. use Drupal\Component\Utility\Environment;
  4. use PHPUnit\Framework\TestCase;
  5. /**
  6. * Test PHP Environment helper methods.
  7. *
  8. * @group Utility
  9. *
  10. * @coversDefaultClass \Drupal\Component\Utility\Environment
  11. */
  12. class EnvironmentTest extends TestCase {
  13. /**
  14. * Tests \Drupal\Component\Utility\Environment::checkMemoryLimit().
  15. *
  16. * @dataProvider providerTestCheckMemoryLimit
  17. * @covers ::checkMemoryLimit
  18. *
  19. * @param string $required
  20. * The required memory argument for
  21. * \Drupal\Component\Utility\Environment::checkMemoryLimit().
  22. * @param string $custom_memory_limit
  23. * The custom memory limit argument for
  24. * \Drupal\Component\Utility\Environment::checkMemoryLimit().
  25. * @param bool $expected
  26. * The expected return value from
  27. * \Drupal\Component\Utility\Environment::checkMemoryLimit().
  28. */
  29. public function testCheckMemoryLimit($required, $custom_memory_limit, $expected) {
  30. $actual = Environment::checkMemoryLimit($required, $custom_memory_limit);
  31. $this->assertEquals($expected, $actual);
  32. }
  33. /**
  34. * Provides data for testCheckMemoryLimit().
  35. *
  36. * @return array
  37. * An array of arrays, each containing the arguments for
  38. * \Drupal\Component\Utility\Environment::checkMemoryLimit():
  39. * required and memory_limit, and the expected return value.
  40. */
  41. public function providerTestCheckMemoryLimit() {
  42. $memory_limit = ini_get('memory_limit');
  43. $twice_avail_memory = ($memory_limit * 2) . 'MB';
  44. return [
  45. // Minimal amount of memory should be available.
  46. ['30MB', NULL, TRUE],
  47. // Exceed a custom (unlimited) memory limit.
  48. [$twice_avail_memory, -1, TRUE],
  49. // Exceed a custom memory limit.
  50. ['30MB', '16MB', FALSE],
  51. // Available = required.
  52. ['30MB', '30MB', TRUE],
  53. ];
  54. }
  55. }