[FEATURE] Fallback/default case for SwitchViewhelper 39/23739/11
authorJan Kiesewetter <jan@t3easy.de>
Sun, 27 Oct 2013 10:22:44 +0000 (11:22 +0100)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Sun, 8 Dec 2013 13:50:38 +0000 (14:50 +0100)
In order to provide the full functionality known from the
switch/case PHP function, a default case possibility is
introduced. Use it like:
<f:switch expression="{person.gender}">
  <f:case value="female">Mrs.</f:case>
  <f:case value="male">Mr.</f:case>
  <f:case default="TRUE">Mrs. or Mr.</f:case>
</f:switch>

Resolves: #49371
Documentation: #54283
Change-Id: I6b71ec39173ab957aa392bd595a65ceddadc81c9
Releases: 6.2
Reviewed-on: https://review.typo3.org/23739
Reviewed-by: Cedric Ziel
Tested-by: Cedric Ziel
Reviewed-by: Anja Leichsenring
Tested-by: Anja Leichsenring
typo3/sysext/fluid/Classes/ViewHelpers/CaseViewHelper.php
typo3/sysext/fluid/Classes/ViewHelpers/SwitchViewHelper.php
typo3/sysext/fluid/Tests/Unit/ViewHelpers/CaseViewHelperTest.php

index 21a834f..28bc487 100644 (file)
@@ -20,20 +20,26 @@ namespace TYPO3\CMS\Fluid\ViewHelpers;
 class CaseViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
 
        /**
-        * @param mixed $value
-        * @return string the contents of this view helper if $value equals the expression of the surrounding switch view helper, otherwise an empty string
+        * @param mixed $value The switch value. If it matches, the child will be rendered
+        * @param boolean $default If this is set, this child will be rendered, if none else matches
+        *
+        * @return string the contents of this view helper if $value equals the expression of the surrounding switch view helper, or $default is TRUE. otherwise an empty string
         * @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
+        *
         * @api
         */
-       public function render($value) {
+       public function render($value = NULL, $default = FALSE) {
                $viewHelperVariableContainer = $this->renderingContext->getViewHelperVariableContainer();
                if (!$viewHelperVariableContainer->exists('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression')) {
                        throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('The case View helper can only be used within a switch View helper', 1368112037);
                }
+               if (is_null($value) && $default === FALSE) {
+                       throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('The case View helper must have either value or default argument', 1382867521);
+               }
                $switchExpression = $viewHelperVariableContainer->get('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression');
 
                // non-type-safe comparison by intention
-               if ($switchExpression == $value) {
+               if ($default === TRUE || $switchExpression == $value) {
                        $viewHelperVariableContainer->addOrUpdate('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'break', TRUE);
                        return $this->renderChildren();
                }
index 5845355..36b146f 100644 (file)
@@ -21,10 +21,11 @@ namespace TYPO3\CMS\Fluid\ViewHelpers;
  * <f:switch expression="{person.gender}">
  *   <f:case value="male">Mr.</f:case>
  *   <f:case value="female">Mrs.</f:case>
+ *   <f:case default="TRUE">Mrs. or Mr.</f:case>
  * </f:switch>
  * </code>
  * <output>
- * Mr. / Mrs. (depending on the value of {person.gender})
+ * Mr. / Mrs. (depending on the value of {person.gender}) or if no value evaluates to TRUE, default case
  * </output>
  *
  * Note: Using this view helper can be a sign of weak architecture. If you end up using it extensively
index 18427e6..4c2276e 100644 (file)
@@ -87,4 +87,64 @@ class CaseViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ViewHel
                $this->assertSame('', $this->viewHelper->render('someOtherValue'));
        }
 
+       /**
+        * @test
+        */
+       public function renderReturnsChildNodesIfDefaultIsTrue() {
+               $this->viewHelperVariableContainer->expects(
+                       $this->atLeastOnce())
+                       ->method('exists')
+                       ->with('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression')
+                       ->will($this->returnValue(TRUE)
+                       );
+               $this->viewHelperVariableContainer->expects(
+                       $this->atLeastOnce())->method('get')
+                       ->with('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression')
+                       ->will($this->returnValue(NULL)
+                       );
+
+               $renderedChildNodes = 'ChildNodes';
+               $this->viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue($renderedChildNodes));
+
+               $this->assertSame($renderedChildNodes, $this->viewHelper->render(NULL, TRUE));
+       }
+
+       /**
+        * @test
+        * @expectedException \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
+        */
+       public function renderThrowsExceptionIfNeighterValueNorDefaultAreGiven() {
+               $this->viewHelperVariableContainer->expects(
+                       $this->atLeastOnce())
+                       ->method('exists')
+                       ->with('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression')
+                       ->will($this->returnValue(TRUE)
+                       );
+               $this->viewHelperVariableContainer->expects($this->never())->method('get');
+
+               $this->viewHelper->render(NULL, FALSE);
+       }
+
+       /**
+        * @test
+        */
+       public function renderReturnsChildNodesEvenIfValueIsFalseButDefaultIsTrue() {
+               $this->viewHelperVariableContainer->expects(
+                       $this->atLeastOnce())
+                       ->method('exists')
+                       ->with('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression')
+                       ->will($this->returnValue(TRUE)
+                       );
+               $this->viewHelperVariableContainer->expects(
+                       $this->atLeastOnce())->method('get')
+                       ->with('TYPO3\CMS\Fluid\ViewHelpers\SwitchViewHelper', 'switchExpression')
+                       ->will($this->returnValue('someValue')
+                       );
+
+               $renderedChildNodes = 'ChildNodes';
+               $this->viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue($renderedChildNodes));
+
+               $this->assertSame($renderedChildNodes, $this->viewHelper->render('someOtherValue', TRUE));
+       }
+
 }