Browse Source

Merge pull request #10320 from davidyell/json-exception-testcase

Adding a json exception test case
Mark Story 9 years ago
parent
commit
f5c4af6970
1 changed files with 31 additions and 0 deletions
  1. 31 0
      tests/TestCase/Error/ExceptionRendererTest.php

+ 31 - 0
tests/TestCase/Error/ExceptionRendererTest.php

@@ -416,6 +416,37 @@ class ExceptionRendererTest extends TestCase
     }
 
     /**
+     * testerror400 method when returning as json
+     *
+     * @return void
+     */
+    public function testError400AsJson()
+    {
+        Router::reload();
+
+        $request = new Request('posts/view/1000?sort=title&direction=desc');
+        $request = $request->withHeader('Accept', 'application/json');
+        $request = $request->withHeader('Content-Type', 'application/json');
+        Router::setRequestInfo($request);
+
+        $exception = new NotFoundException('Custom message');
+        $ExceptionRenderer = new ExceptionRenderer($exception);
+        $ExceptionRenderer->controller->response = $this->getMockBuilder('Cake\Network\Response')
+            ->setMethods(['statusCode', '_sendHeader'])
+            ->getMock();
+        $ExceptionRenderer->controller->response->expects($this->once())->method('statusCode')->with(404);
+
+        $result = $ExceptionRenderer->render()->body();
+        $expected = [
+            'message' => 'Custom message',
+            'url' => '/posts/view/1000?sort=title&direction=desc',
+            'code' => 404
+        ];
+
+        $this->assertEquals($expected, json_decode($result, true));
+    }
+
+    /**
      * test that error400 only modifies the messages on Cake Exceptions.
      *
      * @return void