Browse Source

Merge pull request #11315 from saeideng/error-helper

[3.next] implement withErrorReporting helper
Mark Story 8 years ago
parent
commit
2fa8d16669
2 changed files with 50 additions and 0 deletions
  1. 18 0
      src/TestSuite/TestCase.php
  2. 32 0
      tests/TestCase/TestSuite/TestCaseTest.php

+ 18 - 0
src/TestSuite/TestCase.php

@@ -88,6 +88,24 @@ abstract class TestCase extends BaseTestCase
     }
 
     /**
+     * Helper method for tests that needs to use error_reporting()
+     *
+     * @param int $errorLevel value of error_reporting() that needs to use
+     * @param callable $callable callable function that will receive asserts
+     * @return void
+     */
+    public function withErrorReporting($errorLevel, $callable)
+    {
+        try {
+            $default = error_reporting();
+            error_reporting($errorLevel);
+            $callable();
+        } finally {
+            error_reporting($default);
+        }
+    }
+
+    /**
      * Helper method for check deprecation methods
      *
      * @param callable $callable callable function that will receive asserts

+ 32 - 0
tests/TestCase/TestSuite/TestCaseTest.php

@@ -176,6 +176,38 @@ class TestCaseTest extends TestCase
     }
 
     /**
+     * test withErrorReporting
+     *
+     * @return void
+     */
+    public function testWithErrorReporting()
+    {
+        $errorLevel = error_reporting();
+        $this->withErrorReporting(E_USER_WARNING, function () {
+              $this->assertSame(E_USER_WARNING, error_reporting());
+        });
+        $this->assertSame($errorLevel, error_reporting());
+    }
+
+    /**
+     * test withErrorReporting with exceptions
+     *
+     * @expectedException \PHPUnit\Framework\AssertionFailedError
+     * @return void
+     */
+    public function testWithErrorReportingWithException()
+    {
+        $errorLevel = error_reporting();
+        try {
+            $this->withErrorReporting(E_USER_WARNING, function () {
+                $this->assertSame(1, 2);
+            });
+        } finally {
+            $this->assertSame($errorLevel, error_reporting());
+        }
+    }
+
+    /**
      * testDeprecated
      *
      * @return void