Browse Source

Add coverage for memory limit increases.

Include tests for common memory limits.

Refs #7550
Mark Story 10 years ago
parent
commit
0c541b7141
2 changed files with 50 additions and 13 deletions
  1. 13 13
      src/Error/BaseErrorHandler.php
  2. 37 0
      tests/TestCase/Error/ErrorHandlerTest.php

+ 13 - 13
src/Error/BaseErrorHandler.php

@@ -76,8 +76,8 @@ abstract class BaseErrorHandler
             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)) {
@@ -93,7 +93,7 @@ abstract class BaseErrorHandler
             }
             $this->handleFatalError(
                 $error['type'],
-                $error['message'],
+               $error['message'],
                 $error['file'],
                 $error['line']
             );
@@ -222,30 +222,30 @@ abstract class BaseErrorHandler
     /**
      * 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');
         }
     }
 

+ 37 - 0
tests/TestCase/Error/ErrorHandlerTest.php

@@ -433,4 +433,41 @@ class ErrorHandlerTest extends TestCase
         $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);
+    }
 }