[TASK] Make AliasViewHelper Compilable 28/37428/3
authorAnja Leichsenring <aleichsenring@ab-softlab.de>
Sun, 1 Mar 2015 18:45:05 +0000 (19:45 +0100)
committerHelmut Hummel <helmut.hummel@typo3.org>
Sun, 1 Mar 2015 19:56:17 +0000 (20:56 +0100)
Change-Id: Ib3e5093902b5d2b46d4b2c6a3365abf8340da6ed
Resolves: #65438
Releases: master
Reviewed-on: http://review.typo3.org/37428
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Helmut Hummel <helmut.hummel@typo3.org>
Tested-by: Helmut Hummel <helmut.hummel@typo3.org>
typo3/sysext/fluid/Classes/ViewHelpers/AliasViewHelper.php
typo3/sysext/fluid/Tests/Unit/ViewHelpers/AliasViewHelperTest.php

index b5df6f1..fac2501 100644 (file)
@@ -11,6 +11,10 @@ namespace TYPO3\CMS\Fluid\ViewHelpers;
  * The TYPO3 project - inspiring people to share!                         *
  *                                                                        */
 
+use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
+use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
+use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
+
 /**
  * Declares new variables which are aliases of other variables.
  * Takes a "map"-Parameter which is an associative array which defines the shorthand mapping.
@@ -39,21 +43,42 @@ namespace TYPO3\CMS\Fluid\ViewHelpers;
  *
  * @api
  */
-class AliasViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
+class AliasViewHelper extends AbstractViewHelper implements CompilableInterface {
 
        /**
+        * Declare aliases
         *
         * @param array $map array that specifies which variables should be mapped to which alias
         * @return string Rendered string
         * @api
         */
        public function render(array $map) {
+               return self::renderStatic(
+                       array('map' => $map),
+                       $this->buildRenderChildrenClosure(),
+                       $this->renderingContext
+               );
+       }
+
+       /**
+        * Declare aliases
+        *
+        * @param array $arguments
+        * @param \Closure $renderChildrenClosure
+        * @param RenderingContextInterface $renderingContext
+        *
+        * @return string
+        */
+       static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
+               $map = $arguments['map'];
+               $templateVariableContainer = $renderingContext->getTemplateVariableContainer();
+
                foreach ($map as $aliasName => $value) {
-                       $this->templateVariableContainer->add($aliasName, $value);
+                       $templateVariableContainer->add($aliasName, $value);
                }
-               $output = $this->renderChildren();
+               $output = $renderChildrenClosure();
                foreach ($map as $aliasName => $value) {
-                       $this->templateVariableContainer->remove($aliasName);
+                       $templateVariableContainer->remove($aliasName);
                }
                return $output;
        }
index ff413ee..780bd41 100644 (file)
@@ -11,63 +11,52 @@ namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers;
  * The TYPO3 project - inspiring people to share!                         *
  *                                                                        */
 
+use TYPO3\CMS\Fluid\ViewHelpers\AliasViewHelper;
+
 /**
- * Testcase for AliasViewHelper
+ * Test case
  */
-class AliasViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ViewHelperBaseTestcase {
+class AliasViewHelperTest extends ViewHelperBaseTestcase {
 
        /**
-        * @test
+        * @var AliasViewHelper
         */
-       public function renderAddsSingleValueToTemplateVariableContainerAndRemovesItAfterRendering() {
-               $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\AliasViewHelper();
+       protected $subject;
 
-               $mockViewHelperNode = $this->getMock(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\ViewHelperNode::class, array('evaluateChildNodes'), array(), '', FALSE);
-               $mockViewHelperNode->expects($this->once())->method('evaluateChildNodes')->will($this->returnValue('foo'));
+       public function setUp(){
+               parent::setUp();
+               $this->subject = $this->getMock(AliasViewHelper::class, array('renderChildren'));
+               $this->injectDependenciesIntoViewHelper($this->subject);
+       }
 
+       /**
+        * @test
+        */
+       public function renderAddsSingleValueToTemplateVariableContainerAndRemovesItAfterRendering() {
                $this->templateVariableContainer->expects($this->at(0))->method('add')->with('someAlias', 'someValue');
                $this->templateVariableContainer->expects($this->at(1))->method('remove')->with('someAlias');
-
-               $this->injectDependenciesIntoViewHelper($viewHelper);
-               $viewHelper->setViewHelperNode($mockViewHelperNode);
-               $viewHelper->render(array('someAlias' => 'someValue'));
+               $this->subject->render(array('someAlias' => 'someValue'));
        }
 
        /**
         * @test
         */
        public function renderAddsMultipleValuesToTemplateVariableContainerAndRemovesThemAfterRendering() {
-               $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\AliasViewHelper();
-
-               $mockViewHelperNode = $this->getMock(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\ViewHelperNode::class, 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');
-
-               $this->injectDependenciesIntoViewHelper($viewHelper);
-               $viewHelper->setViewHelperNode($mockViewHelperNode);
-               $viewHelper->render(array('someAlias' => 'someValue', 'someOtherAlias' => 'someOtherValue'));
+               $this->subject->render(array('someAlias' => 'someValue', 'someOtherAlias' => 'someOtherValue'));
        }
 
        /**
         * @test
         */
        public function renderDoesNotTouchTemplateVariableContainerAndReturnsChildNodesIfMapIsEmpty() {
-               $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\AliasViewHelper();
-
-               $mockViewHelperNode = $this->getMock(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\ViewHelperNode::class, array('evaluateChildNodes'), array(), '', FALSE);
-               $mockViewHelperNode->expects($this->once())->method('evaluateChildNodes')->will($this->returnValue('foo'));
-
+               $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue('foo'));
                $this->templateVariableContainer->expects($this->never())->method('add');
                $this->templateVariableContainer->expects($this->never())->method('remove');
-
-               $this->injectDependenciesIntoViewHelper($viewHelper);
-               $viewHelper->setViewHelperNode($mockViewHelperNode);
-
-               $this->assertEquals('foo', $viewHelper->render(array()));
+               $this->assertEquals('foo', $this->subject->render(array()));
        }
 
 }