diff --git a/core/lib/Drupal/Component/Utility/Environment.php b/core/lib/Drupal/Component/Utility/Environment.php index 356a515..a2183cc 100644 --- a/core/lib/Drupal/Component/Utility/Environment.php +++ b/core/lib/Drupal/Component/Utility/Environment.php @@ -54,25 +54,21 @@ public static function checkMemoryLimit($required, $memory_limit = NULL) { * bytes with optional SI or IEC binary unit prefix (e.g. 2, 3K, 5MB, 10G, * 6GiB, 8bytes, 9mbytes). If no value is passed, the current PHP * memory_limit will be used. Defaults to NULL. + * @param string $used_memory + * (optional) The current memory usage, expressed as a number of bytes + * with optional SI or IEC binary unit prefix. If no value is passed, the + * current PHP memory usage as returned by memory_get_usage will be used. + * Defaults to NULL. * * @return bool * TRUE if there is sufficient memory to allow the operation, or FALSE * otherwise. */ - public static function checkAvailableMemory($required, $memory_limit = NULL) { - return static::checkMemoryLimit(static::getUsedMemory() + $required, $memory_limit); - } - - /** - * Returns the current memory in use. - * - * Wrapper of memory_get_usage(), to allow unit testing. - * - * @return int - * The amount of memory, in bytes, currently in use. - */ - protected static function getUsedMemory() { - return memory_get_usage(TRUE); + public static function checkAvailableMemory($required, $memory_limit = NULL, $used_memory = NULL) { + if (!$used_memory) { + $used_memory = memory_get_usage(TRUE); + } + return static::checkMemoryLimit(Bytes::toInt($used_memory) + Bytes::toInt($required), $memory_limit); } } diff --git a/core/modules/system/tests/src/Unit/Plugin/ImageToolkit/GdToolkitTest.php b/core/modules/system/tests/src/Unit/Plugin/ImageToolkit/GdToolkitTest.php index 23edaee5..207053e 100644 --- a/core/modules/system/tests/src/Unit/Plugin/ImageToolkit/GdToolkitTest.php +++ b/core/modules/system/tests/src/Unit/Plugin/ImageToolkit/GdToolkitTest.php @@ -112,7 +112,7 @@ public function testLoad() { $this->setToolkitMock(['checkAvailableMemory']); $this->toolkit->expects($this->any()) ->method('checkAvailableMemory') - ->will($this->returnValue(TRUE)); + ->will($this->returnValue(-1)); $this->image = new Image($this->toolkit, $this->source); $this->assertTrue(is_resource($this->image->getToolkit()->getResource())); @@ -130,7 +130,7 @@ public function testNotEnoughMemoryOnLoad() { $this->setToolkitMock(['checkAvailableMemory']); $this->toolkit->expects($this->any()) ->method('checkAvailableMemory') - ->will($this->returnValue(FALSE)); + ->will($this->returnValue(1024)); // Getting image from file validates the Image object, since GD toolkit is // lazy loading the resource. @@ -156,7 +156,7 @@ public function testNotEnoughMemoryOnCreateNew() { $this->setToolkitOperationMock('CreateNew', $this->toolkit); $this->toolkit->expects($this->any()) ->method('checkAvailableMemory') - ->will($this->onConsecutiveCalls(TRUE, FALSE)); + ->will($this->onConsecutiveCalls(-1, 1024)); $this->toolkit->expects($this->any()) ->method('getToolkitOperation') ->will($this->returnValue($this->toolkitOperation)); @@ -185,7 +185,7 @@ public function testNotEnoughMemoryOnRotate() { $this->setToolkitOperationMock('Rotate', $this->toolkit); $this->toolkit->expects($this->any()) ->method('checkAvailableMemory') - ->will($this->onConsecutiveCalls(TRUE, FALSE)); + ->will($this->onConsecutiveCalls(-1, 1024)); $this->toolkit->expects($this->any()) ->method('getToolkitOperation') ->will($this->returnValue($this->toolkitOperation)); diff --git a/core/tests/Drupal/Tests/Component/Utility/BytesTest.php b/core/tests/Drupal/Tests/Component/Utility/BytesTest.php index 4e7f671..ecda509 100644 --- a/core/tests/Drupal/Tests/Component/Utility/BytesTest.php +++ b/core/tests/Drupal/Tests/Component/Utility/BytesTest.php @@ -99,7 +99,7 @@ public function providerTestToString() { [pow(Bytes::KILOBYTE, 6), '1 EB'], [pow(Bytes::KILOBYTE, 7), '1 ZB'], [pow(Bytes::KILOBYTE, 8), '1 YB'], - [23476892, '23476892'], + [23476892, '22.39 MB'], [79691776, '76 MB'], [81862076662, '76.24 GB'], ]; diff --git a/core/tests/Drupal/Tests/Component/Utility/EnvironmentTest.php b/core/tests/Drupal/Tests/Component/Utility/EnvironmentTest.php index 181457a..16a5f87 100644 --- a/core/tests/Drupal/Tests/Component/Utility/EnvironmentTest.php +++ b/core/tests/Drupal/Tests/Component/Utility/EnvironmentTest.php @@ -42,37 +42,6 @@ public function testCheckMemoryLimit($required, $custom_memory_limit, $expected) } /** - * Tests \Drupal\Component\Utility\Environment::checkAvailableMemory(). - * - * @dataProvider providerTestCheckAvailableMemory - * @covers ::checkAvailableMemory - * - * @param string $custom_memory_limit - * A custom memory limit argument for - * \Drupal\Component\Utility\Environment::checkAvailableMemory(). - * @param string $used_memory - * A mocked value for used memory. - * @param string $required - * The required memory argument for - * \Drupal\Component\Utility\Environment::checkAvailableMemory(). - * @param bool $expected - * The expected return value from - * \Drupal\Component\Utility\Environment::checkAvailableMemory(). - */ - public function testCheckAvailableMemory($custom_memory_limit, $used_memory, $required, $expected) { - $mock_builder = $this->getMockBuilder('Drupal\Component\Utility\Environment'); - $environment = $mock_builder - ->setMethods('getUsedMemory') - ->getMock(); - $environment->expects($this->any()) - ->method('getUsedMemory') - ->will($this->returnValue(Bytes::toInt($usedMemory))); - - $actual = $environment::checkAvailableMemory($required, $custom_memory_limit); - $this->assertEquals($expected, $actual); - } - - /** * Provides data for testCheckMemoryLimit(). * * @return array @@ -97,12 +66,35 @@ public function providerTestCheckMemoryLimit() { } /** + * Tests \Drupal\Component\Utility\Environment::checkAvailableMemory(). + * + * @dataProvider providerTestCheckAvailableMemory + * @covers ::checkAvailableMemory + * + * @param string $custom_memory_limit + * A custom memory limit argument for + * \Drupal\Component\Utility\Environment::checkAvailableMemory(). + * @param string $used_memory + * A mocked value for used memory. + * @param string $required + * The required memory argument for + * \Drupal\Component\Utility\Environment::checkAvailableMemory(). + * @param bool $expected + * The expected return value from + * \Drupal\Component\Utility\Environment::checkAvailableMemory(). + */ + public function testCheckAvailableMemory($custom_memory_limit, $used_memory, $required, $expected) { + $actual = Environment::checkAvailableMemory($required, $custom_memory_limit, $used_memory); + $this->assertEquals($expected, $actual); + } + + /** * Provides data for testCheckAvailableMemory(). * * @return array[] * An array of arrays, each containing the arguments for * \Drupal\Component\Utility\Environment::checkAvailableMemory(): - * memory_limit, mocked memory in use, required memory and the expected + * memory_limit, used_memory, required memory and the expected * return value. */ public function providerTestCheckAvailableMemory() {