Skip to content

Commit

Permalink
Add coverage for memory limit increases.
Browse files Browse the repository at this point in the history
Include tests for common memory limits.

Refs #7550
  • Loading branch information
markstory committed Dec 28, 2015
1 parent a77d0c4 commit 0c541b7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/Error/BaseErrorHandler.php
Expand Up @@ -76,8 +76,8 @@ public function register()
if ($megabytes === null) {
$megabytes = 4;
}
if ($megabytes !== false && $megabytes > 0) {
static::increaseMemoryLimit($megabytes * 1024);
if ($megabytes > 0) {
$this->increaseMemoryLimit($megabytes * 1024);
}
$error = error_get_last();
if (!is_array($error)) {
Expand All @@ -93,7 +93,7 @@ public function register()
}
$this->handleFatalError(
$error['type'],
$error['message'],
$error['message'],
$error['file'],
$error['line']
);
Expand Down Expand Up @@ -222,30 +222,30 @@ public function handleFatalError($code, $description, $file, $line)
/**
* Increases the PHP "memory_limit" ini setting by the specified amount
* in kilobytes
*
*
* @param string $additionalKb Number in kilobytes
* @return void
*/
public static function increaseMemoryLimit($additionalKb)
public function increaseMemoryLimit($additionalKb)
{
$limit = ini_get("memory_limit");
if (!is_string($limit) || !strlen($limit)) {
$limit = ini_get('memory_limit');
if (!strlen($limit) || $limit === '-1') {
return;
}
$limit = trim($limit);
$units = strtoupper(substr($limit, -1));
$current = substr($limit, 0, strlen($limit) - 1);
if ($units === "M") {
if ($units === 'M') {
$current = $current * 1024;
$units = "K";
$units = 'K';
}
if ($units === "G") {
if ($units === 'G') {
$current = $current * 1024 * 1024;
$units = "K";
$units = 'K';
}

if ($units === "K") {
ini_set("memory_limit", ceil($current + $additionalKb) . "K");
if ($units === 'K') {
ini_set('memory_limit', ceil($current + $additionalKb) . 'K');
}
}

Expand Down
37 changes: 37 additions & 0 deletions tests/TestCase/Error/ErrorHandlerTest.php
Expand Up @@ -433,4 +433,41 @@ public function testHandlePHP7Error()
$errorHandler->handleException($error);
$this->assertContains('Unexpected variable foo', $errorHandler->response->body(), 'message missing.');
}

/**
* Data provider for memory limit changing.
*
* @return array
*/
public function memoryLimitProvider()
{
return [
// start, adjust, expected
['256M', 4, '262148K'],
['262144K', 4, '262148K'],
['1G', 128, '1048704K'],
];
}

/**
* Test increasing the memory limit.
*
* @dataProvider memoryLimitProvider
* @return void
*/
public function testIncreaseMemoryLimit($start, $adjust, $expected)
{
$initial = ini_get('memory_limit');
$this->skipIf(strlen($initial) === 0, 'Cannot read memory limit, and cannot test increasing it.');

// phpunit.xml often has -1 as memory limit
ini_set('memory_limit', $start);

$errorHandler = new TestErrorHandler();
$this->assertNull($errorHandler->increaseMemoryLimit($adjust));
$new = ini_get('memory_limit');
$this->assertEquals($expected, $new, 'memory limit did not get increased.');

ini_set('memory_limit', $initial);
}
}

0 comments on commit 0c541b7

Please sign in to comment.