TYPO3 API  SVNRelease
AbstractViewHelperTest.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 require_once(dirname(__FILE__) . '/../Fixtures/TestViewHelper.php');
00024 
00025 /**
00026  * Testcase for AbstractViewHelper
00027  *
00028  * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
00029  */
00030 class Tx_Fluid_Tests_Unit_Core_ViewHelper_AbstractViewHelperTest extends Tx_Extbase_Tests_Unit_BaseTestCase {
00031     /**
00032      * @test
00033      * @author Sebastian Kurfürst <sebastian@typo3.org>
00034      */
00035     public function argumentsCanBeRegistered() {
00036         $mockReflectionService = $this->getMock('Tx_Extbase_Reflection_Service', array(), array(), '', FALSE);
00037 
00038         $viewHelper = $this->getAccessibleMock('Tx_Fluid_Core_ViewHelper_AbstractViewHelper', array('render'), array(), '', FALSE);
00039         $viewHelper->injectReflectionService($mockReflectionService);
00040 
00041         $name = "This is a name";
00042         $description = "Example desc";
00043         $type = "string";
00044         $isRequired = TRUE;
00045         $expected = new Tx_Fluid_Core_ViewHelper_ArgumentDefinition($name, $type, $description, $isRequired);
00046 
00047         $viewHelper->_call('registerArgument', $name, $type, $isRequired, $description);
00048         $this->assertEquals($viewHelper->prepareArguments(), array($name => $expected), 'Argument definitions not returned correctly.');
00049     }
00050 
00051     /**
00052      * @test
00053      * @author Sebastian Kurfürst <sebastian@typo3.org>
00054      * @expectedException Tx_Fluid_Core_ViewHelper_Exception
00055      */
00056     public function registeringTheSameArgumentNameAgainThrowsException() {
00057         $viewHelper = $this->getAccessibleMock('Tx_Fluid_Core_ViewHelper_AbstractViewHelper', array('render'), array(), '', FALSE);
00058 
00059         $name = "shortName";
00060         $description = "Example desc";
00061         $type = "string";
00062         $isRequired = TRUE;
00063 
00064         $viewHelper->_call('registerArgument', $name, $type, $isRequired, $description);
00065         $viewHelper->_call('registerArgument', $name, "integer", $isRequired, $description);
00066     }
00067 
00068     /**
00069      * @test
00070      * @author Bastian Waidelich <bastian@typo3.org>
00071      */
00072     public function overrideArgumentOverwritesExistingArgumentDefinition() {
00073         $mockReflectionService = $this->getMock('Tx_Extbase_Reflection_Service', array(), array(), '', FALSE);
00074 
00075         $viewHelper = $this->getAccessibleMock('Tx_Fluid_Core_ViewHelper_AbstractViewHelper', array('render'), array(), '', FALSE);
00076         $viewHelper->injectReflectionService($mockReflectionService);
00077 
00078         $name = 'argumentName';
00079         $description = 'argument description';
00080         $overriddenDescription = 'overwritten argument description';
00081         $type = 'string';
00082         $overriddenType = 'integer';
00083         $isRequired = TRUE;
00084         $expected = new Tx_Fluid_Core_ViewHelper_ArgumentDefinition($name, $overriddenType, $overriddenDescription, $isRequired);
00085 
00086         $viewHelper->_call('registerArgument', $name, $type, $isRequired, $description);
00087         $viewHelper->_call('overrideArgument', $name, $overriddenType, $isRequired, $overriddenDescription);
00088         $this->assertEquals($viewHelper->prepareArguments(), array($name => $expected), 'Argument definitions not returned correctly. The original ArgumentDefinition could not be overridden.');
00089     }
00090 
00091     /**
00092      * @test
00093      * @author Bastian Waidelich <bastian@typo3.org>
00094      * @expectedException Tx_Fluid_Core_ViewHelper_Exception
00095      */
00096     public function overrideArgumentThrowsExceptionWhenTryingToOverwriteAnNonexistingArgument() {
00097         $mockReflectionService = $this->getMock('Tx_Extbase_Reflection_Service', array(), array(), '', FALSE);
00098 
00099         $viewHelper = $this->getAccessibleMock('Tx_Fluid_Core_ViewHelper_AbstractViewHelper', array('render'), array(), '', FALSE);
00100         $viewHelper->injectReflectionService($mockReflectionService);
00101 
00102         $viewHelper->_call('overrideArgument', 'argumentName', 'string', TRUE, 'description');
00103     }
00104 
00105     /**
00106      * @test
00107      * @author Sebastian Kurfürst <sebastian@typo3.org>
00108      */
00109     public function prepareArgumentsCallsInitializeArguments() {
00110         $mockReflectionService = $this->getMock('Tx_Extbase_Reflection_Service', array(), array(), '', FALSE);
00111 
00112         $viewHelper = $this->getAccessibleMock('Tx_Fluid_Core_ViewHelper_AbstractViewHelper', array('render', 'initializeArguments'), array(), '', FALSE);
00113         $viewHelper->injectReflectionService($mockReflectionService);
00114 
00115         $viewHelper->setArguments(new Tx_Fluid_Core_ViewHelper_Arguments(array()));
00116         $viewHelper->expects($this->once())->method('initializeArguments');
00117 
00118         $viewHelper->prepareArguments();
00119     }
00120 
00121     /**
00122      * @test
00123      * @author Sebastian Kurfürst <sebastian@typo3.org>
00124      */
00125     public function prepareArgumentsRegistersAnnotationBasedArgumentsWithDescriptionIfDebugModeIsEnabled() { $this->markTestIncomplete("Works differently in v4.");
00126 
00127         Tx_Fluid_Fluid::$debugMode = TRUE;
00128 
00129         $availableClassNames = array(
00130             'Tx_Fluid_Core_Fixtures_TestViewHelper',
00131         );
00132         $reflectionService = new Tx_Extbase_Reflection_Service();
00133         $reflectionService->setStatusCache($this->getMock('Tx_Fluid_Cache_Frontend_StringFrontend', array(), array(), '', FALSE));
00134         $reflectionService->setDataCache($this->getMock('Tx_Fluid_Cache_Frontend_VariableFrontend', array(), array(), '', FALSE));
00135         // $reflectionService->initialize($availableClassNames);
00136 
00137         $viewHelper = new Tx_Fluid_Core_Fixtures_TestViewHelper();
00138         $viewHelper->injectReflectionService($reflectionService);
00139 
00140         $expected = array(
00141             'param1' => new Tx_Fluid_Core_ViewHelper_ArgumentDefinition('param1', 'integer', 'P1 Stuff', TRUE, null, TRUE),
00142             'param2' => new Tx_Fluid_Core_ViewHelper_ArgumentDefinition('param2', 'array', 'P2 Stuff', TRUE, null, TRUE),
00143             'param3' => new Tx_Fluid_Core_ViewHelper_ArgumentDefinition('param3', 'string', 'P3 Stuff', FALSE, 'default', TRUE),
00144         );
00145 
00146         $this->assertEquals($expected, $viewHelper->prepareArguments(), 'Annotation based arguments were not registered.');
00147 
00148         Tx_Fluid_Fluid::$debugMode = FALSE;
00149     }
00150 
00151     /**
00152      * @test
00153      * @author Sebastian Kurfürst <sebastian@typo3.org>
00154      */
00155     public function prepareArgumentsRegistersAnnotationBasedArgumentsWithoutDescriptionIfDebugModeIsDisabled() { $this->markTestIncomplete("Works differently in v4.");
00156 
00157         Tx_Fluid_Fluid::$debugMode = FALSE;
00158 
00159         $availableClassNames = array(
00160             'Tx_Fluid_Core_Fixtures_TestViewHelper',
00161         );
00162         $reflectionService = new Tx_Extbase_Reflection_Service();
00163         $reflectionService->setStatusCache($this->getMock('Tx_Fluid_Cache_Frontend_StringFrontend', array(), array(), '', FALSE));
00164         $reflectionService->setDataCache($this->getMock('Tx_Fluid_Cache_Frontend_VariableFrontend', array(), array(), '', FALSE));
00165         // $reflectionService->initialize($availableClassNames);
00166 
00167         $viewHelper = new Tx_Fluid_Core_Fixtures_TestViewHelper();
00168         $viewHelper->injectReflectionService($reflectionService);
00169 
00170         $expected = array(
00171             'param1' => new Tx_Fluid_Core_ViewHelper_ArgumentDefinition('param1', 'integer', '', TRUE, null, TRUE),
00172             'param2' => new Tx_Fluid_Core_ViewHelper_ArgumentDefinition('param2', 'array', '', TRUE, null, TRUE),
00173             'param3' => new Tx_Fluid_Core_ViewHelper_ArgumentDefinition('param3', 'string', '', FALSE, 'default', TRUE),
00174         );
00175 
00176         $this->assertEquals($expected, $viewHelper->prepareArguments(), 'Annotation based arguments were not registered.');
00177     }
00178 
00179     /**
00180      * @test
00181      * @author Sebastian Kurfürst <sebastian@typo3.org>
00182      */
00183     public function validateArgumentsCallsPrepareArguments() {
00184         $mockReflectionService = $this->getMock('Tx_Extbase_Reflection_Service', array(), array(), '', FALSE);
00185 
00186         $viewHelper = $this->getAccessibleMock('Tx_Fluid_Core_ViewHelper_AbstractViewHelper', array('render', 'prepareArguments'), array(), '', FALSE);
00187         $viewHelper->injectReflectionService($mockReflectionService);
00188 
00189         $viewHelper->setArguments(new Tx_Fluid_Core_ViewHelper_Arguments(array()));
00190         $viewHelper->expects($this->once())->method('prepareArguments')->will($this->returnValue(array()));
00191 
00192         $viewHelper->validateArguments();
00193     }
00194 
00195     /**
00196      * @test
00197      * @author Robert Lemke <robert@typo3.org>
00198      */
00199     public function validateArgumentsAcceptsAllObjectsImplemtingArrayAccessAsAnArray() {
00200         $viewHelper = $this->getAccessibleMock('Tx_Fluid_Core_ViewHelper_AbstractViewHelper', array('render', 'prepareArguments'), array(), '', FALSE);
00201 
00202         $viewHelper->setArguments(new Tx_Fluid_Core_ViewHelper_Arguments(array('test' => new ArrayObject)));
00203         $viewHelper->expects($this->once())->method('prepareArguments')->will($this->returnValue(array('test' => new Tx_Fluid_Core_ViewHelper_ArgumentDefinition('test', 'array', FALSE, 'documentation'))));
00204         $viewHelper->validateArguments();
00205     }
00206 
00207     /**
00208      * @test
00209      * @author Sebastian Kurfürst <sebastian@typo3.org>
00210      */
00211     public function validateArgumentsCallsTheRightValidators() {
00212         $mockReflectionService = $this->getMock('Tx_Extbase_Reflection_Service', array(), array(), '', FALSE);
00213 
00214         $viewHelper = $this->getAccessibleMock('Tx_Fluid_Core_ViewHelper_AbstractViewHelper', array('render', 'prepareArguments'), array(), '', FALSE);
00215         $viewHelper->injectReflectionService($mockReflectionService);
00216 
00217         $viewHelper->setArguments(new Tx_Fluid_Core_ViewHelper_Arguments(array('test' => 'Value of argument')));
00218 
00219         $viewHelper->expects($this->once())->method('prepareArguments')->will($this->returnValue(array(
00220             'test' => new Tx_Fluid_Core_ViewHelper_ArgumentDefinition("test", "string", FALSE, "documentation")
00221         )));
00222 
00223         $viewHelper->validateArguments();
00224     }
00225 
00226     /**
00227      * @test
00228      * @author Sebastian Kurfürst <sebastian@typo3.org>
00229      * @expectedException InvalidArgumentException
00230      */
00231     public function validateArgumentsCallsTheRightValidatorsAndThrowsExceptionIfValidationIsWrong() {
00232         $mockReflectionService = $this->getMock('Tx_Extbase_Reflection_Service', array(), array(), '', FALSE);
00233 
00234         $viewHelper = $this->getAccessibleMock('Tx_Fluid_Core_ViewHelper_AbstractViewHelper', array('render', 'prepareArguments'), array(), '', FALSE);
00235         $viewHelper->injectReflectionService($mockReflectionService);
00236 
00237         $viewHelper->setArguments(new Tx_Fluid_Core_ViewHelper_Arguments(array('test' => "test")));
00238 
00239         $viewHelper->expects($this->once())->method('prepareArguments')->will($this->returnValue(array(
00240             'test' => new Tx_Fluid_Core_ViewHelper_ArgumentDefinition("test", "stdClass", FALSE, "documentation")
00241         )));
00242 
00243         $viewHelper->validateArguments();
00244     }
00245 
00246 
00247     /**
00248      * @test
00249      * @author Sebastian Kurfürst <sebastian@typo3.org>
00250      */
00251     public function setControllerContextSetsTheControllerContext() {
00252         $controllerContext = $this->getMock('Tx_Extbase_MVC_Controller_ControllerContext', array(), array(), '', FALSE);
00253         $viewHelper = $this->getAccessibleMock('Tx_Fluid_Core_ViewHelper_AbstractViewHelper', array('render', 'prepareArguments'), array(), '', FALSE);
00254 
00255         $viewHelper->setControllerContext($controllerContext);
00256         $this->assertSame($viewHelper->_get('controllerContext'), $controllerContext);
00257     }
00258 
00259     /**
00260      * @test
00261      * @author Sebastian Kurfürst <sebastian@typo3.org>
00262      */
00263     public function setViewHelperVariableContainerSetsTheViewHelperVariableContainer() {
00264         $viewHelperVariableContainer = $this->getMock('Tx_Fluid_Core_ViewHelper_ViewHelperVariableContainer');
00265         $viewHelper = $this->getAccessibleMock('Tx_Fluid_Core_ViewHelper_AbstractViewHelper', array('render', 'prepareArguments'), array(), '', FALSE);
00266 
00267         $viewHelper->setViewHelperVariableContainer($viewHelperVariableContainer);
00268         $this->assertSame($viewHelper->_get('viewHelperVariableContainer'), $viewHelperVariableContainer);
00269     }
00270 
00271     /**
00272      * @test
00273      * @author Sebastian Kurfürst <sebastian@typo3.org>
00274      */
00275     public function initializeArgumentsAndRenderCallsTheCorrectSequenceOfMethods() {
00276         $viewHelper = $this->getAccessibleMock('Tx_Fluid_Core_ViewHelper_AbstractViewHelper', array('validateArguments', 'initialize', 'callRenderMethod'));
00277         $viewHelper->expects($this->at(0))->method('validateArguments');
00278         $viewHelper->expects($this->at(1))->method('initialize');
00279         $viewHelper->expects($this->at(2))->method('callRenderMethod')->with(array('argument1' => 'value1'))->will($this->returnValue('Output'));
00280 
00281         $expectedOutput = 'Output';
00282         $actualOutput = $viewHelper->initializeArgumentsAndRender(array('argument1' => 'value1'));
00283         $this->assertEquals($expectedOutput, $actualOutput);
00284     }
00285 }
00286 ?>