From 7ea1a590354617d13f31d644e09fbddf7e667271 Mon Sep 17 00:00:00 2001 From: Ceeram Date: Fri, 10 Aug 2012 10:29:55 +0200 Subject: [PATCH] adding test for viewClass mapping --- .../Component/RequestHandlerComponentTest.php | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Test/Case/Controller/Component/RequestHandlerComponentTest.php b/lib/Cake/Test/Case/Controller/Component/RequestHandlerComponentTest.php index 0840c64d3f8..19729844824 100644 --- a/lib/Cake/Test/Case/Controller/Component/RequestHandlerComponentTest.php +++ b/lib/Cake/Test/Case/Controller/Component/RequestHandlerComponentTest.php @@ -21,6 +21,7 @@ App::uses('CakeRequest', 'Network'); App::uses('CakeResponse', 'Network'); App::uses('Router', 'Routing'); +App::uses('JsonView', 'View'); /** * RequestHandlerTestController class @@ -70,6 +71,15 @@ public function ajax2_layout() { } +class CustomJsonView extends JsonView { + +/** + * Test method for viewClassMap and overriding _serialize() + */ + protected function _serialize($serialize) { + return json_encode(array('return' => 'ok')); + } +} /** * RequestHandlerComponentTest class @@ -137,12 +147,14 @@ public function tearDown() { */ public function testConstructorSettings() { $settings = array( - 'ajaxLayout' => 'test_ajax' + 'ajaxLayout' => 'test_ajax', + 'viewClassMap' => array('json' => 'MyPlugin.MyJson') ); $Collection = new ComponentCollection(); $Collection->init($this->Controller); $RequestHandler = new RequestHandlerComponent($Collection, $settings); $this->assertEquals('test_ajax', $RequestHandler->ajaxLayout); + $this->assertEquals(array('json' => 'MyPlugin.MyJson'), $RequestHandler->settings['viewClassMap']); } /** @@ -264,6 +276,33 @@ public function testInitializeContentTypeAndExtensionMismatch() { call_user_func_array(array('Router', 'parseExtensions'), $extensions); } +/** + * testViewClassMap method + * + * @return void + */ + public function testViewClassMap() { + $settings = array('viewClassMap' => array('json' => 'CustomJson')); + $this->RequestHandler->initialize($this->Controller, $settings); + $result = $this->RequestHandler->viewClassMap(); + $expected = array( + 'json' => 'CustomJson', + 'xml' => 'Xml' + ); + $this->assertEquals($expected, $result); + + $result = $this->RequestHandler->viewClassMap('xls', 'Excel.Excel'); + $expected = array( + 'json' => 'CustomJson', + 'xml' => 'Xml', + 'xls' => 'Excel.Excel' + ); + $this->assertEquals($expected, $result); + + $this->RequestHandler->renderAs($this->Controller, 'json'); + $this->assertEquals('CustomJson', $this->Controller->viewClass); + } + /** * testDisabling method *