[TASK] Move arguments to initializeArguments() in TranslateVH in ext:fluid 04/49004/4
authorRoberto Torresani <erreti@gmail.com>
Sat, 16 Jul 2016 19:45:19 +0000 (21:45 +0200)
committerSusanne Moog <typo3@susannemoog.de>
Tue, 19 Jul 2016 07:47:19 +0000 (09:47 +0200)
Move the argument registrations away from the render() method
to initializeArguments(), to prevent any errors with PHP7 and
subclassed ViewHelpers if/when render() method signatures change.
Change the UnitTest TranslateViewHelperTest with setArguments()

Resolves: #77127
Releases: master
Change-Id: I8621e5410c60873d1295da0ca009c0599434114c
Reviewed-on: https://review.typo3.org/49004
Reviewed-by: Joerg Boesche <typo3@joergboesche.de>
Tested-by: Joerg Boesche <typo3@joergboesche.de>
Tested-by: Bamboo TYPO3com <info@typo3.com>
Reviewed-by: Susanne Moog <typo3@susannemoog.de>
Tested-by: Susanne Moog <typo3@susannemoog.de>
typo3/sysext/fluid/Classes/ViewHelpers/TranslateViewHelper.php
typo3/sysext/fluid/Tests/Unit/ViewHelpers/TranslateViewHelperTest.php

index eac000f..1b404ee 100644 (file)
@@ -80,18 +80,36 @@ class TranslateViewHelper extends AbstractViewHelper
     protected $escapeChildren = false;
 
     /**
+     * Initialize arguments.
+     *
+     * @api
+     * @throws \TYPO3Fluid\Fluid\Core\ViewHelper\Exception
+     */
+    public function initializeArguments()
+    {
+        parent::initializeArguments();
+        $this->registerArgument('key', 'string', 'Translation Key');
+        $this->registerArgument('id', 'string', 'Translation Key compatible to TYPO3 Flow');
+        $this->registerArgument('default', 'string', 'If the given locallang key could not be found, this value is used. If this argument is not set, child nodes will be used to render the default');
+        $this->registerArgument('htmlEscape', 'bool', 'TRUE if the result should be htmlescaped. This won\'t have an effect for the default value');
+        $this->registerArgument('arguments', 'array', 'Arguments to be replaced in the resulting string');
+        $this->registerArgument('extensionName', 'string', 'UpperCamelCased extension key (for example BlogExample)');
+    }
+
+    /**
      * Render translation
      *
-     * @param string $key Translation Key
-     * @param string $id Translation Key compatible to TYPO3 Flow
-     * @param string $default If the given locallang key could not be found, this value is used. If this argument is not set, child nodes will be used to render the default
-     * @param bool $htmlEscape TRUE if the result should be htmlescaped. This won't have an effect for the default value
-     * @param array $arguments Arguments to be replaced in the resulting string
-     * @param string $extensionName UpperCamelCased extension key (for example BlogExample)
      * @return string The translated key or tag body if key doesn't exist
      */
-    public function render($key = null, $id = null, $default = null, $htmlEscape = null, array $arguments = null, $extensionName = null)
+    public function render()
     {
+        $key = $this->arguments['key'];
+        $id = $this->arguments['id'];
+        $default = $this->arguments['default'];
+        $htmlEscape = $this->arguments['htmlEscape'];
+        $arguments = $this->arguments['arguments'];
+        $extensionName = $this->arguments['extensionName'];
+
         return static::renderStatic(
             array(
                 'key' => $key,
index abf9c60..d8fc0ca 100644 (file)
@@ -13,9 +13,6 @@ namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers;
  *
  * The TYPO3 project - inspiring people to share!
  */
-use TYPO3\CMS\Core\Utility\GeneralUtility;
-use TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Fixtures\TranslateViewHelperFixtureForEmptyString;
-use TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Fixtures\TranslateViewHelperFixtureForTranslatedString;
 use TYPO3\CMS\Fluid\ViewHelpers\TranslateViewHelper;
 
 /**
@@ -26,20 +23,28 @@ class TranslateViewHelperTest extends ViewHelperBaseTestcase
     /**
      * @var TranslateViewHelper
      */
-    protected $subject;
+    protected $viewHelper;
+
+    protected function setUp()
+    {
+        parent::setUp();
+        $this->viewHelper = new TranslateViewHelper();
+        $this->injectDependenciesIntoViewHelper($this->viewHelper);
+    }
 
     /**
      * @test
      */
     public function renderThrowsExceptionIfNoKeyOrIdParameterIsGiven()
     {
-        $this->subject = GeneralUtility::makeInstance(TranslateViewHelper::class);
-        $this->injectDependenciesIntoViewHelper($this->subject);
-
         $this->expectException(\TYPO3\CMS\Fluid\Core\ViewHelper\Exception\InvalidVariableException::class);
         $this->expectExceptionCode(1351584844);
 
-        $this->subject->render();
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            []
+        );
+        $this->viewHelper->initializeArgumentsAndRender();
     }
 
     /**
@@ -47,9 +52,19 @@ class TranslateViewHelperTest extends ViewHelperBaseTestcase
      */
     public function renderReturnsStringForGivenKey()
     {
-        $this->subject = GeneralUtility::makeInstance(TranslateViewHelperFixtureForTranslatedString::class);
-        $this->injectDependenciesIntoViewHelper($this->subject);
-        $this->assertEquals('<p>hello world</p>', $this->subject->render('foo'));
+        $this->viewHelper->setRenderChildrenClosure(
+            function () {
+                return '<p>hello world</p>';
+            }
+        );
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'key' => 'foo'
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
+        $this->assertEquals('<p>hello world</p>', $actualResult);
     }
 
     /**
@@ -57,9 +72,20 @@ class TranslateViewHelperTest extends ViewHelperBaseTestcase
      */
     public function renderReturnsStringForGivenId()
     {
-        $this->subject = GeneralUtility::makeInstance(TranslateViewHelperFixtureForTranslatedString::class);
-        $this->injectDependenciesIntoViewHelper($this->subject);
-        $this->assertEquals('<p>hello world</p>', $this->subject->render(null, 'bar'));
+        $this->viewHelper->setRenderChildrenClosure(
+            function () {
+                return '<p>hello world</p>';
+            }
+        );
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'key' => null,
+                'id' => 'bar'
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
+        $this->assertEquals('<p>hello world</p>', $actualResult);
     }
 
     /**
@@ -67,8 +93,20 @@ class TranslateViewHelperTest extends ViewHelperBaseTestcase
      */
     public function renderReturnsDefaultIfNoTranslationIsFound()
     {
-        $this->subject = GeneralUtility::makeInstance(TranslateViewHelperFixtureForEmptyString::class);
-        $this->injectDependenciesIntoViewHelper($this->subject);
-        $this->assertEquals('default', $this->subject->render(null, 'bar', 'default'));
+        $this->viewHelper->setRenderChildrenClosure(
+            function () {
+                return 'default';
+            }
+        );
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'key' => null,
+                'id' => 'bar',
+                'default' => 'default'
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
+        $this->assertEquals('default', $actualResult);
     }
 }