[BUGFIX] Fix fatal error in SpacelessViewHelper 94/44794/7
authorWouter Wolters <typo3@wouterwolters.nl>
Thu, 19 Nov 2015 12:08:40 +0000 (13:08 +0100)
committerNicole Cordes <typo3@cordes.co>
Thu, 19 Nov 2015 15:35:21 +0000 (16:35 +0100)
Resolves: #71676
Releases: master
Change-Id: I6e8943809b866ca354d0f264df8069fb630705e1
Reviewed-on: https://review.typo3.org/44794
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Tested-by: Markus Klein <markus.klein@typo3.org>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Nicole Cordes <typo3@cordes.co>
Tested-by: Nicole Cordes <typo3@cordes.co>
typo3/sysext/fluid/Classes/ViewHelpers/SpacelessViewHelper.php
typo3/sysext/fluid/Tests/Unit/ViewHelpers/SpacelessViewHelperTest.php

index f793446..332fb04 100644 (file)
@@ -47,13 +47,14 @@ class SpacelessViewHelper extends AbstractViewHelper implements CompilableInterf
      */
     public function render()
     {
-        return static::renderStatic($this->arguments, $this->renderChildrenClosure, $this->renderingContext);
+        return static::renderStatic($this->arguments, $this->buildRenderChildrenClosure(), $this->renderingContext);
     }
 
     /**
      * @param array $arguments
      * @param \Closure $childClosure
      * @param RenderingContextInterface $renderingContext
+     * @return string
      */
     public static function renderStatic(array $arguments, \Closure $childClosure, RenderingContextInterface $renderingContext)
     {
index a94bd53..59ee773 100644 (file)
@@ -10,50 +10,70 @@ namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers;
  *                                                                        *
  * The TYPO3 project - inspiring people to share!                         *
  *                                                                        */
-use TYPO3\CMS\Fluid\ViewHelpers\SpacelessViewHelper;
+
+use TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\ViewHelperNode;
 use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
+use TYPO3\CMS\Fluid\ViewHelpers\SpacelessViewHelper;
 
 /**
  * Testcase for SpacelessViewHelper
  */
 class SpacelessViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ViewHelperBaseTestcase
 {
+
     /**
-     * @param string $input
-     * @param string $expected
-     * @dataProvider getRenderStaticData
      * @test
      */
-    public function testRender($input, $expected)
+    public function renderWithEmptyChildNodesReturnsNoOutput()
     {
         $instance = new SpacelessViewHelper();
-        $instance->setRenderChildrenClosure(function () use ($input) { return $input; });
-        $instance->setRenderingContext($this->getMock(RenderingContextInterface::class));
-        $instance->setArguments(array());
-        $this->assertEquals($expected, $instance->render());
+        $viewHelperNodeProphecy = $this->prophesize(ViewHelperNode::class);
+        $instance->setViewHelperNode($viewHelperNodeProphecy->reveal());
+        $renderingContextInterfaceProphecy = $this->prophesize(RenderingContextInterface::class);
+        $instance->setRenderingContext($renderingContextInterfaceProphecy->reveal());
+        $instance->setArguments([]);
+        // the render method would not return an empty string in a real usage. The render method just
+        // calls renderStatic, which does the actual work. The tests for this are done in the
+        // renderStatic test directly. The render method test only makes sure the method call
+        // raises no fatal error.
+        $this->assertEquals('', $instance->render());
     }
 
     /**
      * @param string $input
      * @param string $expected
-     * @dataProvider getRenderStaticData
+     *
+     * @dataProvider testRenderStaticDataProvider
      * @test
      */
     public function testRenderStatic($input, $expected)
     {
         $context = $this->getMock(RenderingContextInterface::class);
-        $this->assertEquals($expected, SpacelessViewHelper::renderStatic(array(), function () use ($input) { return $input; }, $context));
+
+        $this->assertEquals($expected,
+            SpacelessViewHelper::renderStatic([], function () use ($input) {
+                return $input;
+            }, $context));
     }
 
     /**
      * @return array
      */
-    public function getRenderStaticData()
+    public function testRenderStaticDataProvider()
     {
-        return array(
-            'extra whitespace between tags' => array('<div>foo</div>  <div>bar</div>', '<div>foo</div><div>bar</div>'),
-            'whitespace preserved in text node' => array(PHP_EOL . '<div>' . PHP_EOL . 'foo</div>', '<div>' . PHP_EOL . 'foo</div>'),
-            'whitespace removed from non-text node' => array(PHP_EOL . '<div>' . PHP_EOL . '<div>foo</div></div>', '<div><div>foo</div></div>')
-        );
+        return [
+            'extra whitespace between tags' => [
+                '<div>foo</div>  <div>bar</div>',
+                '<div>foo</div><div>bar</div>',
+            ],
+            'whitespace preserved in text node' => [
+                PHP_EOL . '<div>' . PHP_EOL . 'foo</div>',
+                '<div>' . PHP_EOL . 'foo</div>',
+            ],
+            'whitespace removed from non-text node' => [
+                PHP_EOL . '<div>' . PHP_EOL . '<div>foo</div></div>',
+                '<div><div>foo</div></div>',
+            ],
+        ];
     }
 }