[~TASK] Fluid (Tests): committing modified AbstractFormFieldViewHelperTest that I...
authorBastian Waidelich <bastian@typo3.org>
Mon, 26 Jul 2010 15:11:22 +0000 (15:11 +0000)
committerBastian Waidelich <bastian@typo3.org>
Mon, 26 Jul 2010 15:11:22 +0000 (15:11 +0000)
[+TASK] Fluid (ViewHelpers): Small performance improvement in ForViewHelper: Objects will only be converted to arrays if reverse is TRUE. Relates to #8732 (backported from Fluid package r4907)
[+TASK] Fluid (View): Added getter for template parser to AbstractTemplateView. This is useful if you want to use the parser from within your custom ViewHelper (creating a new instance would skip interceptor registration) (backported from Fluid package r4907)

typo3/sysext/fluid/Classes/View/AbstractTemplateView.php
typo3/sysext/fluid/Classes/ViewHelpers/ForViewHelper.php
typo3/sysext/fluid/Tests/Unit/ViewHelpers/Form/AbstractFormFieldViewHelperTest.php

index b58c6bf..a97d7be 100644 (file)
@@ -115,6 +115,17 @@ abstract class Tx_Fluid_View_AbstractTemplateView implements Tx_Fluid_View_Templ
        }
 
        /**
+        * Returns the template parser that is used to parse this views template
+        *
+        * @return Tx_Fluid_Core_Parser_TemplateParser
+        * @author Bastian Waidelich <bastian@typo3.org>
+        * @api
+        */
+       public function getTemplateParser() {
+               return $this->templateParser;
+       }
+
+       /**
         * Assign a value to the variable container.
         *
         * @param string $key The key of a view variable to set
index 69be0c2..febfb7a 100644 (file)
@@ -90,14 +90,15 @@ class Tx_Fluid_ViewHelpers_ForViewHelper extends Tx_Fluid_Core_ViewHelper_Abstra
                if ($each === NULL) {
                        return '';
                }
-               if (is_object($each)) {
-                       if (!$each instanceof Traversable) {
-                               throw new Tx_Fluid_Core_ViewHelper_Exception('ForViewHelper only supports arrays and objects implementing Traversable interface' , 1248728393);
-                       }
-                       $each = iterator_to_array($each);
+               if (is_object($each) && !$each instanceof Traversable) {
+                       throw new Tx_Fluid_Core_ViewHelper_Exception('ForViewHelper only supports arrays and objects implementing Traversable interface' , 1248728393);
                }
 
                if ($reverse === TRUE) {
+                               // array_reverse only supports arrays
+                       if (is_object($each)) {
+                               $each = iterator_to_array($each);
+                       }
                        $each = array_reverse($each);
                }
                $iterationData = array(
index 872fe66..02e07c0 100644 (file)
@@ -67,19 +67,19 @@ class Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelperTest extends Tx_Fluid
         * @author Sebastian Kurfürst <sebastian@typo3.org>
         * @author Bastian Waidelich <bastian@typo3.org>
         */
-       public function getNameBuildsNameFromFieldNamePrefixFormNameAndPropertyIfInObjectAccessorMode() {
+       public function getNameBuildsNameFromFieldNamePrefixFormObjectNameAndPropertyIfInObjectAccessorMode() {
                $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('isObjectAccessorMode'), array(), '', FALSE);
                $this->injectDependenciesIntoViewHelper($formViewHelper);
 
                $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
-               $this->viewHelperVariableContainer->expects($this->at(0))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formName')->will($this->returnValue('myFormName'));
+               $this->viewHelperVariableContainer->expects($this->at(0))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObjectName')->will($this->returnValue('myObjectName'));
                $this->viewHelperVariableContainer->expects($this->at(1))->method('exists')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix')->will($this->returnValue(TRUE));
                $this->viewHelperVariableContainer->expects($this->at(2))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix')->will($this->returnValue('formPrefix'));
 
                        // TODO mock arguments
                $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array('name' => 'fieldName', 'value' => 'fieldValue', 'property' => 'bla'));
                $formViewHelper->_set('arguments', $arguments);
-               $expected = 'formPrefix[myFormName][bla]';
+               $expected = 'formPrefix[myObjectName][bla]';
                $actual = $formViewHelper->_call('getName');
                $this->assertSame($expected, $actual);
        }
@@ -89,19 +89,19 @@ class Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelperTest extends Tx_Fluid
         * @author Sebastian Kurfürst <sebastian@typo3.org>
         * @author Bastian Waidelich <bastian@typo3.org>
         */
-       public function getNameBuildsNameFromFieldNamePrefixFormNameAndHierarchicalPropertyIfInObjectAccessorMode() {
+       public function getNameBuildsNameFromFieldNamePrefixFormObjectNameAndHierarchicalPropertyIfInObjectAccessorMode() {
                $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('isObjectAccessorMode'), array(), '', FALSE);
                $this->injectDependenciesIntoViewHelper($formViewHelper);
 
                $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
-               $this->viewHelperVariableContainer->expects($this->at(0))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formName')->will($this->returnValue('myFormName'));
+               $this->viewHelperVariableContainer->expects($this->at(0))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObjectName')->will($this->returnValue('myObjectName'));
                $this->viewHelperVariableContainer->expects($this->at(1))->method('exists')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix')->will($this->returnValue(TRUE));
                $this->viewHelperVariableContainer->expects($this->at(2))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix')->will($this->returnValue('formPrefix'));
 
                        // TODO mock arguments
                $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array('name' => 'fieldName', 'value' => 'fieldValue', 'property' => 'bla.blubb'));
                $formViewHelper->_set('arguments', $arguments);
-               $expected = 'formPrefix[myFormName][bla][blubb]';
+               $expected = 'formPrefix[myObjectName][bla][blubb]';
                $actual = $formViewHelper->_call('getName');
                $this->assertSame($expected, $actual);
        }
@@ -110,12 +110,12 @@ class Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelperTest extends Tx_Fluid
         * @test
         * @author Bastian Waidelich <bastian@typo3.org>
         */
-       public function getNameBuildsNameFromFieldNamePrefixAndPropertyIfInObjectAccessorModeAndNoFormNameIsSpecified() {
+       public function getNameBuildsNameFromFieldNamePrefixAndPropertyIfInObjectAccessorModeAndNoFormObjectNameIsSpecified() {
                $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('isObjectAccessorMode'), array(), '', FALSE);
                $this->injectDependenciesIntoViewHelper($formViewHelper);
 
                $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
-               $this->viewHelperVariableContainer->expects($this->at(0))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formName')->will($this->returnValue(NULL));
+               $this->viewHelperVariableContainer->expects($this->at(0))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObjectName')->will($this->returnValue(NULL));
                $this->viewHelperVariableContainer->expects($this->at(1))->method('exists')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix')->will($this->returnValue(TRUE));
                $this->viewHelperVariableContainer->expects($this->at(2))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix')->will($this->returnValue('formPrefix'));
 
@@ -221,7 +221,7 @@ class Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelperTest extends Tx_Fluid
                $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('dummy'), array(), '', FALSE);
                $this->injectDependenciesIntoViewHelper($formViewHelper);
 
-               $this->viewHelperVariableContainer->expects($this->once())->method('exists')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formName')->will($this->returnValue(TRUE));
+               $this->viewHelperVariableContainer->expects($this->once())->method('exists')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObjectName')->will($this->returnValue(TRUE));
 
                $formViewHelper->_set('arguments', new Tx_Fluid_Core_ViewHelper_Arguments(array('name' => NULL, 'value' => NULL, 'property' => 'bla')));
                $this->assertTrue($formViewHelper->_call('isObjectAccessorMode'));
@@ -241,7 +241,7 @@ class Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelperTest extends Tx_Fluid
                $mockArguments = $this->getMock('Tx_Fluid_Core_ViewHelper_Arguments', array(), array(), '', FALSE);
                $mockArguments->expects($this->once())->method('offsetGet')->with('property')->will($this->returnValue('bar'));
                $formViewHelper->_set('arguments', $mockArguments);
-               $this->viewHelperVariableContainer->expects($this->any())->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formName')->will($this->returnValue('foo'));
+               $this->viewHelperVariableContainer->expects($this->any())->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObjectName')->will($this->returnValue('foo'));
 
                $mockArgumentError = $this->getMock('Tx_Extbase_MVC_Controller_ArgumentError', array(), array('foo'));
                $mockArgumentError->expects($this->once())->method('getPropertyName')->will($this->returnValue('foo'));
@@ -403,15 +403,15 @@ class Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelperTest extends Tx_Fluid
                        return new ' . $className . ';
                ');
                $property = 'value.something';
-               $formName = 'myForm';
-               $expectedProperty = 'myForm[value]';
+               $objectName = 'myObject';
+               $expectedProperty = 'myObject[value]';
                
                $formFieldViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('renderHiddenIdentityField'), array(), '', FALSE);
                $this->injectDependenciesIntoViewHelper($formFieldViewHelper);
                $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array('property' => $property));
                $formFieldViewHelper->_set('arguments', $arguments);
                $this->viewHelperVariableContainer->expects($this->at(0))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObject')->will($this->returnValue($mockFormObject));
-               $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formName')->will($this->returnValue($formName));
+               $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObjectName')->will($this->returnValue($objectName));
                
                $formFieldViewHelper->expects($this->once())->method('renderHiddenIdentityField')->with($mockFormObject, $expectedProperty);
                
@@ -436,16 +436,16 @@ class Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelperTest extends Tx_Fluid
                        return new ' . $className . ';
                ');
                $property = 'value.value.something';
-               $formName = 'myForm';
-               $expectedProperty1 = 'myForm[value]';
-               $expectedProperty2 = 'myForm[value][value]';
+               $objectName = 'myObject';
+               $expectedProperty1 = 'myObject[value]';
+               $expectedProperty2 = 'myObject[value][value]';
                
                $formFieldViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('renderHiddenIdentityField'), array(), '', FALSE);
                $this->injectDependenciesIntoViewHelper($formFieldViewHelper);
                $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array('property' => $property));
                $formFieldViewHelper->_set('arguments', $arguments);
                $this->viewHelperVariableContainer->expects($this->at(0))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObject')->will($this->returnValue($mockFormObject));
-               $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formName')->will($this->returnValue($formName));
+               $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObjectName')->will($this->returnValue($objectName));
                
                $formFieldViewHelper->expects($this->at(0))->method('renderHiddenIdentityField')->with($mockFormObject, $expectedProperty1);
                $formFieldViewHelper->expects($this->at(1))->method('renderHiddenIdentityField')->with($mockFormObject, $expectedProperty2);