Browse Source

Merge pull request #10777 from cakephp/disbale-error-middleware

Add disableErrorHandlerMiddleware method
Mark Story 8 years ago
parent
commit
f906d20bb9

+ 15 - 0
src/TestSuite/IntegrationTestCase.php

@@ -24,6 +24,7 @@ use Cake\Core\Configure;
 use Cake\Database\Exception as DatabaseException;
 use Cake\Network\Session;
 use Cake\Routing\Router;
+use Cake\TestSuite\Stub\TestExceptionRenderer;
 use Cake\Utility\CookieCryptTrait;
 use Cake\Utility\Hash;
 use Cake\Utility\Security;
@@ -1083,6 +1084,20 @@ abstract class IntegrationTestCase extends TestCase
     }
 
     /**
+     * Disable the error handler middleware.
+     *
+     * By using this function, exceptions are no longer caught by the ErrorHandlerMiddleware
+     * and are instead re-thrown by the TestExceptionRenderer. This can be helpful
+     * when trying to diagnose/debug unexpected failures in test cases.
+     *
+     * @return void
+     */
+    public function disableErrorHandlerMiddleware()
+    {
+        Configure::write('Error.exceptionRenderer', TestExceptionRenderer::class);
+    }
+
+    /**
      * Asserts cookie values which are encrypted by the
      * CookieComponent.
      *

+ 43 - 0
src/TestSuite/Stub/TestExceptionRenderer.php

@@ -0,0 +1,43 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ * @link          https://cakephp.org CakePHP(tm) Project
+ * @since         3.5.0
+ * @license       https://opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\TestSuite\Stub;
+
+use Exception;
+
+/**
+ * Test Exception Renderer.
+ *
+ * Use this class if you want to re-throw exceptions that would otherwise be
+ * caught by the ErrorHandlerMiddleware. This is useful while debugging or
+ * writing integration test cases.
+ *
+ * @see \Cake\TestSuite\IntegrationTestCase::disableErrorHandlerMiddleware()
+ * @internal
+ */
+class TestExceptionRenderer
+{
+
+    /**
+     * Simply rethrow the given exception
+     *
+     * @param \Exception $exception Exception.
+     * @return void
+     * @throws \Exception $exception Rethrows the passed exception.
+     */
+    public function __construct(Exception $exception)
+    {
+        throw $exception;
+    }
+}

+ 13 - 0
tests/TestCase/TestSuite/IntegrationTestCaseTest.php

@@ -968,4 +968,17 @@ class IntegrationTestCaseTest extends IntegrationTestCase
         $this->get('/posts/get');
         $this->assertFileResponse('foo');
     }
+
+    /**
+     * Test disabling the error handler middleware.
+     *
+     * @expectedException \Cake\Routing\Exception\MissingRouteException
+     * @expectedExceptionMessage A route matching "/foo" could not be found.
+     * @return void
+     */
+    public function testDisableErrorHandlerMiddleware()
+    {
+        $this->disableErrorHandlerMiddleware();
+        $this->get('/foo');
+    }
 }