tx_fluid: (fresh backport from Fluid Package r2614 with FluidBackporter r2615):
authorBastian Waidelich <bastian@typo3.org>
Mon, 15 Jun 2009 18:17:24 +0000 (18:17 +0000)
committerBastian Waidelich <bastian@typo3.org>
Mon, 15 Jun 2009 18:17:24 +0000 (18:17 +0000)
  [FEAT] Fluid (ViewHelpers): Added AliasViewHelper, resolves #3592
  [TASK] Fluid (Tests): Fixed and extended ForViewHelperTest
  [TASK] Fluid (Tests): Tweaked ViewHelperBaseTestcase
  [TASK] Fluid (Tests): Fixed some PHP Doc Comments

typo3/sysext/fluid/Classes/ViewHelpers/AliasViewHelper.php [new file with mode: 0644]
typo3/sysext/fluid/Tests/ViewHelpers/AliasViewHelperTest_testcase.php [new file with mode: 0644]
typo3/sysext/fluid/Tests/ViewHelpers/ForViewHelperTest_testcase.php
typo3/sysext/fluid/Tests/ViewHelpers/IfViewHelperTest_testcase.php
typo3/sysext/fluid/Tests/ViewHelpers/ViewHelperBaseTestcase.php

diff --git a/typo3/sysext/fluid/Classes/ViewHelpers/AliasViewHelper.php b/typo3/sysext/fluid/Classes/ViewHelpers/AliasViewHelper.php
new file mode 100644 (file)
index 0000000..4a077f4
--- /dev/null
@@ -0,0 +1,77 @@
+<?php
+
+/*                                                                        *
+ * This script belongs to the FLOW3 package "Fluid".                      *
+ *                                                                        *
+ * It is free software; you can redistribute it and/or modify it under    *
+ * the terms of the GNU Lesser General Public License as published by the *
+ * Free Software Foundation, either version 3 of the License, or (at your *
+ * option) any later version.                                             *
+ *                                                                        *
+ * This script is distributed in the hope that it will be useful, but     *
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
+ * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
+ * General Public License for more details.                               *
+ *                                                                        *
+ * You should have received a copy of the GNU Lesser General Public       *
+ * License along with the script.                                         *
+ * If not, see http://www.gnu.org/licenses/lgpl.html                      *
+ *                                                                        *
+ * The TYPO3 project - inspiring people to share!                         *
+ *                                                                        */
+
+/**
+ * @package Fluid
+ * @subpackage ViewHelpers
+ * @version $Id$
+ */
+
+/**
+ * Alias view helper
+ *
+ * = Examples =
+ *
+ * <code title="Single alias">
+ * <f:alias map="{x: 'foo'}">{x}</f:alias>
+ * </code>
+ *
+ * Output:
+ * foo
+ *
+ * <code title="Multiple mappings">
+ * <f:alias map="{x: foo.bar.baz, y: foo.bar.baz.name}">
+ *   {x.name} or {y}
+ * </f:alias>
+ * </code>
+ *
+ * Output:
+ * [name] or [name]
+ * depending on {foo.bar.baz}
+ *
+ * @package Fluid
+ * @subpackage ViewHelpers
+ * @version $Id$
+ * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
+ * @scope prototype
+ */
+class Tx_Fluid_ViewHelpers_AliasViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {
+
+       /**
+        *
+        * @param array $map 
+        * @return string Rendered string
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public function render(array $map) {
+               foreach ($map as $aliasName => $value) {
+                       $this->templateVariableContainer->add($aliasName, $value);
+               }
+               $output = $this->renderChildren();
+               foreach ($map as $aliasName => $value) {
+                       $this->templateVariableContainer->remove($aliasName);
+               }
+               return $output;
+       }
+}
+
+?>
diff --git a/typo3/sysext/fluid/Tests/ViewHelpers/AliasViewHelperTest_testcase.php b/typo3/sysext/fluid/Tests/ViewHelpers/AliasViewHelperTest_testcase.php
new file mode 100644 (file)
index 0000000..178967d
--- /dev/null
@@ -0,0 +1,95 @@
+<?php
+
+/*                                                                        *
+ * This script is part of the TYPO3 project - inspiring people to share!  *
+ *                                                                        *
+ * TYPO3 is free software; you can redistribute it and/or modify it under *
+ * the terms of the GNU General Public License version 2 as published by  *
+ * the Free Software Foundation.                                          *
+ *                                                                        *
+ * This script is distributed in the hope that it will be useful, but     *
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
+ * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
+ * Public License for more details.                                       *
+ *                                                                        */
+
+/**
+ * @package Fluid
+ * @subpackage Tests
+ * @version $Id$
+ */
+
+require_once(dirname(__FILE__) . '/ViewHelperBaseTestcase.php');
+/**
+ * Testcase for AliasViewHelper
+ *
+ * @package Fluid
+ * @subpackage Tests
+ * @version $Id$
+ * @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
+ */
+
+require_once(t3lib_extMgm::extPath('extbase', 'Tests/Base_testcase.php'));
+class Tx_Fluid_ViewHelpers_AliasViewHelperTest_testcase extends Tx_Fluid_ViewHelpers_ViewHelperBaseTestcase {
+
+       /**
+        * @test
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public function renderAddsSingleValueToTemplateVariableContainerAndRemovesItAfterRendering() {
+               $viewHelper = new Tx_Fluid_ViewHelpers_AliasViewHelper();
+
+               $mockViewHelperNode = $this->getMock('Tx_Fluid_Core_Parser_SyntaxTree_ViewHelperNode', array('evaluateChildNodes'), array(), '', FALSE);
+               $mockViewHelperNode->expects($this->once())->method('evaluateChildNodes')->will($this->returnValue('foo'));
+
+               $this->templateVariableContainer->expects($this->at(0))->method('add')->with('someAlias', 'someValue');
+               $this->templateVariableContainer->expects($this->at(1))->method('remove')->with('someAlias');
+
+               $viewHelper->setTemplateVariableContainer($this->templateVariableContainer);
+               $viewHelper->setViewHelperNode($mockViewHelperNode);
+               $viewHelper->render(array('someAlias' => 'someValue'));
+       }
+
+       /**
+        * @test
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public function renderAddsMultipleValuesToTemplateVariableContainerAndRemovesThemAfterRendering() {
+               $viewHelper = new Tx_Fluid_ViewHelpers_AliasViewHelper();
+
+               $mockViewHelperNode = $this->getMock('Tx_Fluid_Core_Parser_SyntaxTree_ViewHelperNode', array('evaluateChildNodes'), array(), '', FALSE);
+               $mockViewHelperNode->expects($this->once())->method('evaluateChildNodes')->will($this->returnValue('foo'));
+
+               $this->templateVariableContainer->expects($this->at(0))->method('add')->with('someAlias', 'someValue');
+               $this->templateVariableContainer->expects($this->at(1))->method('add')->with('someOtherAlias', 'someOtherValue');
+               $this->templateVariableContainer->expects($this->at(2))->method('remove')->with('someAlias');
+               $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('someOtherAlias');
+
+               $viewHelper->setTemplateVariableContainer($this->templateVariableContainer);
+               $viewHelper->setViewHelperNode($mockViewHelperNode);
+               $viewHelper->render(array('someAlias' => 'someValue', 'someOtherAlias' => 'someOtherValue'));
+       }
+
+       /**
+        * @test
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public function renderDoesNotTouchTemplateVariableContainerAndReturnsChildNodesIfMapIsEmpty() {
+               $viewHelper = new Tx_Fluid_ViewHelpers_AliasViewHelper();
+
+               $mockViewHelperNode = $this->getMock('Tx_Fluid_Core_Parser_SyntaxTree_ViewHelperNode', array('evaluateChildNodes'), array(), '', FALSE);
+               $mockViewHelperNode->expects($this->once())->method('evaluateChildNodes')->will($this->returnValue('foo'));
+
+               $this->templateVariableContainer->expects($this->never())->method('add');
+               $this->templateVariableContainer->expects($this->never())->method('remove');
+
+               $viewHelper->setTemplateVariableContainer($this->templateVariableContainer);
+               $viewHelper->setViewHelperNode($mockViewHelperNode);
+
+               $this->assertEquals('foo', $viewHelper->render(array()));
+       }
+}
+
+
+
+?>
index a2408f0..c0a1a34 100644 (file)
@@ -19,7 +19,7 @@
  * @version $Id$
  */
 /**
- * Testcase for DefaultViewHelper
+ * Testcase for ForViewHelper
  *
  * @package Fluid
  * @subpackage Tests
  */
 
 include_once(dirname(__FILE__) . '/Fixtures/ConstraintSyntaxTreeNode.php');
+require_once(dirname(__FILE__) . '/ViewHelperBaseTestcase.php');
 require_once(t3lib_extMgm::extPath('extbase', 'Tests/Base_testcase.php'));
-class Tx_Fluid_ViewHelpers_ForViewHelperTest_testcase extends Tx_Extbase_Base_testcase {
+class Tx_Fluid_ViewHelpers_ForViewHelperTest_testcase extends Tx_Fluid_ViewHelpers_ViewHelperBaseTestcase {
 
        /**
-        * @author Sebastian Kurfürst <sebastian@typo3.org>
-        */
-       public function setUp() {
-       }
-       
-       /**
         * @test
         * @author Sebastian Kurfürst <sebastian@typo3.org>
         * @author Bastian Waidelich <bastian@typo3.org>
         */
-       public function forExecutesTheLoopCorrectly() {
-               $this->viewHelper = new Tx_Fluid_ViewHelpers_ForViewHelper();
-               
+       public function renderExecutesTheLoopCorrectly() {
+               $viewHelper = new Tx_Fluid_ViewHelpers_ForViewHelper();
+
                $variableContainer = new Tx_Fluid_Core_ViewHelper_TemplateVariableContainer(array());
-               
+
                $viewHelperNode = new Tx_Fluid_ViewHelpers_Fixtures_ConstraintSyntaxTreeNode($variableContainer);               
-               $this->viewHelper->setTemplateVariableContainer($variableContainer);
-               $this->viewHelper->setViewHelperNode($viewHelperNode);
-               $this->viewHelper->render(array(0,1,2,3), 'innerVariable');
-               
+               $viewHelper->setTemplateVariableContainer($variableContainer);
+               $viewHelper->setViewHelperNode($viewHelperNode);
+               $viewHelper->render(array(0,1,2,3), 'innerVariable');
+
                $expectedCallProtocol = array(
                        array('innerVariable' => 0),
                        array('innerVariable' => 1),
@@ -60,6 +55,70 @@ class Tx_Fluid_ViewHelpers_ForViewHelperTest_testcase extends Tx_Extbase_Base_te
                );
                $this->assertEquals($expectedCallProtocol, $viewHelperNode->callProtocol, 'The call protocol differs -> The for loop does not work as it should!');     
        }
+
+       /**
+        * @test
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public function renderReturnsEmptyStringIfObjectIsNull() {
+               $viewHelper = new Tx_Fluid_ViewHelpers_ForViewHelper();
+
+               $this->assertEquals('', $viewHelper->render(NULL, 'foo'));
+       }
+
+       /**
+        * @test
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public function renderReturnsEmptyStringIfObjectIsEmtyArray() {
+               $viewHelper = new Tx_Fluid_ViewHelpers_ForViewHelper();
+
+               $this->assertEquals('', $viewHelper->render(array(), 'foo'));
+       }
+
+       /**
+        * @test
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public function renderAddsCurrentValueToTemplateVariableContainerAndRemovesItAfterRendering() {
+               $viewHelper = new Tx_Fluid_ViewHelpers_ForViewHelper();
+
+               $mockViewHelperNode = $this->getMock('Tx_Fluid_Core_Parser_SyntaxTree_ViewHelperNode', array('evaluateChildNodes'), array(), '', FALSE);
+               $mockViewHelperNode->expects($this->any())->method('evaluateChildNodes')->will($this->returnValue('foo'));
+
+               $this->templateVariableContainer->expects($this->at(0))->method('add')->with('innerVariable', 'bar');
+               $this->templateVariableContainer->expects($this->at(1))->method('remove')->with('innerVariable');
+               $this->templateVariableContainer->expects($this->at(2))->method('add')->with('innerVariable', 'Fluid');
+               $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('innerVariable');
+
+               $viewHelper->setTemplateVariableContainer($this->templateVariableContainer);
+               $viewHelper->setViewHelperNode($mockViewHelperNode);
+               $viewHelper->render(array('foo' => 'bar', 'FLOW3' => 'Fluid'), 'innerVariable');
+       }
+
+       /**
+        * @test
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public function renderAddsCurrentKeyToTemplateVariableContainerAndRemovesItAfterRendering() {
+               $viewHelper = new Tx_Fluid_ViewHelpers_ForViewHelper();
+
+               $mockViewHelperNode = $this->getMock('Tx_Fluid_Core_Parser_SyntaxTree_ViewHelperNode', array('evaluateChildNodes'), array(), '', FALSE);
+               $mockViewHelperNode->expects($this->any())->method('evaluateChildNodes')->will($this->returnValue('foo'));
+
+               $this->templateVariableContainer->expects($this->at(0))->method('add')->with('innerVariable', 'bar');
+               $this->templateVariableContainer->expects($this->at(1))->method('add')->with('someKey', 'foo');
+               $this->templateVariableContainer->expects($this->at(2))->method('remove')->with('innerVariable');
+               $this->templateVariableContainer->expects($this->at(3))->method('remove')->with('someKey');
+               $this->templateVariableContainer->expects($this->at(4))->method('add')->with('innerVariable', 'Fluid');
+               $this->templateVariableContainer->expects($this->at(5))->method('add')->with('someKey', 'FLOW3');
+               $this->templateVariableContainer->expects($this->at(6))->method('remove')->with('innerVariable');
+               $this->templateVariableContainer->expects($this->at(7))->method('remove')->with('someKey');
+
+               $viewHelper->setTemplateVariableContainer($this->templateVariableContainer);
+               $viewHelper->setViewHelperNode($mockViewHelperNode);
+               $viewHelper->render(array('foo' => 'bar', 'FLOW3' => 'Fluid'), 'innerVariable', 'someKey');
+       }
 }
 
 
index 745f933..7f57934 100644 (file)
@@ -19,7 +19,7 @@
  * @version $Id$
  */
 /**
- * Testcase for [insert classname here]
+ * Testcase for IfViewHelper
  *
  * @package
  * @subpackage Tests
index 25b40c7..c40d534 100644 (file)
 /**
  * @package Fluid
  * @subpackage ViewHelpers
- * @version $Id: ViewHelperBaseTestcase.php 2609 2009-06-15 11:21:56Z bwaidelich $
+ * @version $Id: ViewHelperBaseTestcase.php 2614 2009-06-15 18:13:18Z bwaidelich $
  */
 abstract class Tx_Fluid_ViewHelpers_ViewHelperBaseTestcase extends Tx_Extbase_Base_testcase {
+
+       /**
+        * @var Tx_Fluid_Core_ViewHelper_ViewHelperVariableContainer
+        */
+       protected $viewHelperVariableContainer;
+
+       /**
+        * @var Tx_Fluid_Core_ViewHelper_TemplateVariableContainer
+        */
+       protected $templateVariableContainer;
+
+       /**
+        * @var \Tx_Extbase_MVC_Web_Routing_URIBuilder
+        */
+       protected $uriBuilder;
+
+       /**
+        * @var \Tx_Extbase_MVC_Controller_ControllerContext
+        */
+       protected $controllerContext;
+
+       /**
+        * @var Tx_Fluid_Core_ViewHelper_TagBuilder
+        */
+       protected $tagBuilder;
+
+       /**
+        * @return void
+        * @author Sebastian Kurfürst <sebastian@typo3.org>
+        */
        public function setUp() {
                $this->viewHelperVariableContainer = $this->getMock('Tx_Fluid_Core_ViewHelper_ViewHelperVariableContainer');
                $this->templateVariableContainer = $this->getMock('Tx_Fluid_Core_ViewHelper_TemplateVariableContainer');
@@ -35,6 +65,11 @@ abstract class Tx_Fluid_ViewHelpers_ViewHelperBaseTestcase extends Tx_Extbase_Ba
                $this->tagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder');
        }
 
+       /**
+        * @param Tx_Fluid_Core_ViewHelper_AbstractViewHelper $viewHelper
+        * @return void
+        * @author Sebastian Kurfürst <sebastian@typo3.org>
+        */
        protected function injectDependenciesIntoViewHelper(Tx_Fluid_Core_ViewHelper_AbstractViewHelper $viewHelper) {
                $viewHelper->setViewHelperVariableContainer($this->viewHelperVariableContainer);
                $viewHelper->setTemplateVariableContainer($this->templateVariableContainer);