Browse Source

Add disableErrorHandlerMiddleware method

Michael Hoffmann 8 years ago
parent
commit
5860b474f0

+ 14 - 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;
@@ -1050,6 +1051,19 @@ abstract class IntegrationTestCase extends TestCase
     }
 
     /**
+     * Disable the error handler middleware.
+     *
+     * By using this function, the exception gets no longer catched by the ErrorHandlerMiddleware
+     * and is instead just rethrown by the TestExceptionRenderer. This results in a more clearer error message.
+     *
+     * @return void
+     */
+    public function disableErrorHandlerMiddleware()
+    {
+        Configure::write('Error.exceptionRenderer', TestExceptionRenderer::class);
+    }
+
+    /**
      * Asserts cookie values which are encrypted by the
      * CookieComponent.
      *

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

@@ -0,0 +1,38 @@
+<?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.
+ */
+class TestExceptionRenderer
+{
+
+    /**
+     * Constructor
+     *
+     * @param \Exception $exception Exception.
+     * @return void
+     * @throws \Exception $exception Rethrows the passed exception.
+     */
+    public function __construct(Exception $exception)
+    {
+        $this->error = $exception;
+
+        throw $exception;
+    }
+}

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

@@ -921,4 +921,18 @@ class IntegrationTestCaseTest extends IntegrationTestCase
         $this->get('/posts/get');
         $this->assertFileResponse('foo');
     }
+
+    /**
+     * undocumented function
+     *
+     * @expectedException \Cake\Routing\Exception\MissingRouteException
+     * @expectedExceptionMessage A route matching "/foo" could not be found.
+     * @return void
+     */
+    public function testDisableErrorHandlerMiddleware()
+    {
+        $this->disableErrorHandlerMiddleware();
+        $this->get('/foo');
+        $this->assertResponseOk();
+    }
 }