TYPO3 API  SVNRelease
AbstractWidgetViewHelperTest.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /*                                                                        *
00004  * This script belongs to the FLOW3 package "Fluid".                      *
00005  *                                                                        *
00006  * It is free software; you can redistribute it and/or modify it under    *
00007  * the terms of the GNU Lesser General Public License as published by the *
00008  * Free Software Foundation, either version 3 of the License, or (at your *
00009  * option) any later version.                                             *
00010  *                                                                        *
00011  * This script is distributed in the hope that it will be useful, but     *
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
00013  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
00014  * General Public License for more details.                               *
00015  *                                                                        *
00016  * You should have received a copy of the GNU Lesser General Public       *
00017  * License along with the script.                                         *
00018  * If not, see http://www.gnu.org/licenses/lgpl.html                      *
00019  *                                                                        *
00020  * The TYPO3 project - inspiring people to share!                         *
00021  *                                                                        */
00022 
00023 /**
00024  * Testcase for AbstractWidgetViewHelper
00025  *
00026  * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
00027  */
00028 class Tx_Fluid_Tests_Unit_Core_Widget_AbstractWidgetViewHelperTest extends Tx_Extbase_Tests_Unit_BaseTestCase {
00029 
00030     /**
00031      * @var Tx_Fluid_Core_Widget_AbstractWidgetViewHelper
00032      */
00033     protected $viewHelper;
00034 
00035     /**
00036      * @var Tx_Fluid_Core_Widget_AjaxWidgetContextHolder
00037      */
00038     protected $ajaxWidgetContextHolder;
00039 
00040     /**
00041      * @var Tx_Fluid_Core_Widget_WidgetContext
00042      */
00043     protected $widgetContext;
00044 
00045     /**
00046      * @var Tx_Extbase_Object_ObjectManagerInterface
00047      */
00048     protected $objectManager;
00049 
00050     /**
00051      * @var Tx_Extbase_MVC_Controller_ControllerContext
00052      */
00053     protected $controllerContext;
00054 
00055     /**
00056      * @var Tx_Extbase_MVC_Web_Request
00057      */
00058     protected $request;
00059 
00060     /**
00061      * @author Sebastian Kurfürst <sebastian@typo3.org>
00062      */
00063     public function setUp() {
00064         $this->viewHelper = $this->getAccessibleMock('Tx_Fluid_Core_Widget_AbstractWidgetViewHelper', array('validateArguments', 'initialize', 'callRenderMethod', 'getWidgetConfiguration', 'getRenderingContext'));
00065 
00066         $this->ajaxWidgetContextHolder = $this->getMock('Tx_Fluid_Core_Widget_AjaxWidgetContextHolder');
00067         $this->viewHelper->injectAjaxWidgetContextHolder($this->ajaxWidgetContextHolder);
00068 
00069         $this->widgetContext = $this->getMock('Tx_Fluid_Core_Widget_WidgetContext');
00070 
00071         $this->objectManager = $this->getMock('Tx_Extbase_Object_ObjectManagerInterface');
00072         $this->objectManager->expects($this->at(0))->method('create')->with('Tx_Fluid_Core_Widget_WidgetContext')->will($this->returnValue($this->widgetContext));
00073         $this->viewHelper->injectObjectManager($this->objectManager);
00074 
00075         $this->request = $this->getMock('Tx_Extbase_MVC_Web_Request');
00076 
00077         $this->controllerContext = $this->getMock('Tx_Extbase_MVC_Controller_ControllerContext', array(), array(), '', FALSE);
00078         $this->controllerContext->expects($this->any())->method('getRequest')->will($this->returnValue($this->request));
00079         $this->viewHelper->_set('controllerContext', $this->controllerContext);
00080 
00081     }
00082 
00083     /**
00084      * @test
00085      * @author Sebastian Kurfürst <sebastian@typo3.org>
00086      */
00087     public function initializeArgumentsAndRenderCallsTheRightSequenceOfMethods() {
00088         $this->callViewHelper();
00089     }
00090 
00091     /**
00092      * @test
00093      * @author Sebastian Kurfürst <sebastian@typo3.org>
00094      */
00095     public function initializeArgumentsAndRenderStoresTheWidgetContextIfInAjaxMode() {
00096         $this->viewHelper->_set('ajaxWidget', TRUE);
00097         $this->ajaxWidgetContextHolder->expects($this->once())->method('store')->with($this->widgetContext);
00098 
00099         $this->callViewHelper();
00100     }
00101 
00102     /**
00103      * Calls the ViewHelper, and emulates a rendering.
00104      *
00105      * @return void
00106      * @author Sebastian Kurfürst <sebastian@typo3.org>
00107      */
00108     public function callViewHelper() {
00109         $viewHelperVariableContainer = $this->getMock('Tx_Fluid_Core_ViewHelper_ViewHelperVariableContainer');
00110         $this->viewHelper->setViewHelperVariableContainer($viewHelperVariableContainer);
00111 
00112         $this->viewHelper->expects($this->once())->method('getWidgetConfiguration')->will($this->returnValue('Some Widget Configuration'));
00113         $this->widgetContext->expects($this->once())->method('setWidgetConfiguration')->with('Some Widget Configuration');
00114 
00115         $this->widgetContext->expects($this->once())->method('setWidgetIdentifier')->with('__widget_0');
00116 
00117         $this->viewHelper->_set('controller', new stdClass());
00118         $this->widgetContext->expects($this->once())->method('setControllerObjectName')->with('stdClass');
00119 
00120         $this->viewHelper->expects($this->once())->method('validateArguments');
00121         $this->viewHelper->expects($this->once())->method('initialize');
00122         $this->viewHelper->expects($this->once())->method('callRenderMethod')->with(array('arg1' => 'val1'))->will($this->returnValue('renderedResult'));
00123         $output = $this->viewHelper->initializeArgumentsAndRender(array('arg1' => 'val1'));
00124         $this->assertEquals('renderedResult', $output);
00125     }
00126 
00127     /**
00128      * @test
00129      * @author Sebastian Kurfürst <sebastian@typo3.org>
00130      */
00131     public function setChildNodesAddsChildNodesToWidgetContext() {
00132         $node1 = $this->getMock('Tx_Fluid_Core_Parser_SyntaxTree_AbstractNode');
00133         $node2 = $this->getMock('Tx_Fluid_Core_Parser_SyntaxTree_TextNode', array(), array(), '', FALSE);
00134         $node3 = $this->getMock('Tx_Fluid_Core_Parser_SyntaxTree_AbstractNode');
00135 
00136         $rootNode = $this->getMock('Tx_Fluid_Core_Parser_SyntaxTree_RootNode');
00137         $rootNode->expects($this->at(0))->method('addChildNode')->with($node1);
00138         $rootNode->expects($this->at(1))->method('addChildNode')->with($node2);
00139         $rootNode->expects($this->at(2))->method('addChildNode')->with($node3);
00140 
00141         $this->objectManager->expects($this->once())->method('create')->with('Tx_Fluid_Core_Parser_SyntaxTree_RootNode')->will($this->returnValue($rootNode));
00142 
00143         $renderingContext = $this->getMock('Tx_Fluid_Core_Rendering_RenderingContextInterface');
00144         $this->viewHelper->expects($this->once())->method('getRenderingContext')->will($this->returnValue($renderingContext));
00145 
00146         $this->widgetContext->expects($this->once())->method('setViewHelperChildNodes')->with($rootNode, $renderingContext);
00147         $this->viewHelper->setChildNodes(array($node1, $node2, $node3));
00148     }
00149 
00150     /**
00151      * @test
00152      * @expectedException Tx_Fluid_Core_Widget_Exception_MissingControllerException
00153      * @author Sebastian Kurfürst <sebastian@typo3.org>
00154      */
00155     public function initiateSubRequestThrowsExceptionIfControllerIsNoWidgetController() {
00156         $controller = $this->getMock('Tx_Fluid_MVC_Controller_ControllerInterface');
00157         $this->viewHelper->_set('controller', $controller);
00158 
00159         $this->viewHelper->_call('initiateSubRequest');
00160     }
00161 
00162     /**
00163      * @test
00164      * @author Sebastian Kurfürst <sebastian@typo3.org>
00165      */
00166     public function initiateSubRequestBuildsRequestProperly() {
00167         $controller = $this->getMock('Tx_Fluid_Core_Widget_AbstractWidgetController', array(), array(), '', FALSE);
00168         $this->viewHelper->_set('controller', $controller);
00169 
00170         // Initial Setup
00171         $widgetRequest = $this->getMock('Tx_Fluid_Core_Widget_WidgetRequest');
00172         $response = $this->getMock('Tx_Extbase_MVC_Web_Response');
00173         $this->objectManager->expects($this->at(0))->method('create')->with('Tx_Fluid_Core_Widget_WidgetRequest')->will($this->returnValue($widgetRequest));
00174         $this->objectManager->expects($this->at(1))->method('create')->with('Tx_Extbase_MVC_Web_Response')->will($this->returnValue($response));
00175 
00176         // Widget Context is set
00177         $widgetRequest->expects($this->once())->method('setWidgetContext')->with($this->widgetContext);
00178 
00179         // The namespaced arguments are passed to the sub-request
00180         // and the action name is exctracted from the namespace.
00181         $this->controllerContext->expects($this->once())->method('getRequest')->will($this->returnValue($this->request));
00182         $this->widgetContext->expects($this->once())->method('getWidgetIdentifier')->will($this->returnValue('widget-1'));
00183         $this->request->expects($this->once())->method('getArguments')->will($this->returnValue(array(
00184             'k1' => 'k2',
00185             'widget-1' => array(
00186                 'arg1' => 'val1',
00187                 'arg2' => 'val2',
00188                 'action' => 'myAction'
00189             )
00190         )));
00191         $widgetRequest->expects($this->once())->method('setArguments')->with(array(
00192             'arg1' => 'val1',
00193             'arg2' => 'val2'
00194         ));
00195         $widgetRequest->expects($this->once())->method('setControllerActionName')->with('myAction');
00196 
00197         // Controller is called
00198         $controller->expects($this->once())->method('processRequest')->with($widgetRequest, $response);
00199         $output = $this->viewHelper->_call('initiateSubRequest');
00200 
00201         // SubResponse is returned
00202         $this->assertSame($response, $output);
00203     }
00204 }
00205 ?>