[TASK] Move arguments to initializeArguments() FlashMessagesVH ext:fluid 70/48870/7
authorRoberto Torresani <erreti@gmail.com>
Sun, 10 Jul 2016 11:04:44 +0000 (13:04 +0200)
committerSusanne Moog <typo3@susannemoog.de>
Tue, 19 Jul 2016 05:30:43 +0000 (07:30 +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.

Resolves: #77004
Releases: master
Change-Id: I629d19209276a8162e45db1f923cd2280240df5d
Reviewed-on: https://review.typo3.org/48870
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Bamboo TYPO3com <info@typo3.com>
Reviewed-by: Joerg Boesche <typo3@joergboesche.de>
Reviewed-by: Susanne Moog <typo3@susannemoog.de>
Tested-by: Susanne Moog <typo3@susannemoog.de>
typo3/sysext/fluid/Classes/ViewHelpers/FlashMessagesViewHelper.php
typo3/sysext/fluid/Tests/Unit/ViewHelpers/FlashMessagesViewHelperTest.php

index 0a9b7ed..ec90d3d 100644 (file)
@@ -13,6 +13,7 @@ namespace TYPO3\CMS\Fluid\ViewHelpers;
  *
  * The TYPO3 project - inspiring people to share!
  */
+use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
 
 /**
  * View helper which renders the flash messages (if there are any) as an unsorted list.
@@ -82,7 +83,7 @@ namespace TYPO3\CMS\Fluid\ViewHelpers;
  *
  * @api
  */
-class FlashMessagesViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper
+class FlashMessagesViewHelper extends AbstractTagBasedViewHelper
 {
     /**
      * @var string
@@ -99,7 +100,8 @@ class FlashMessagesViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractT
     {
         parent::initializeArguments();
         $this->registerUniversalTagAttributes();
-        $this->registerArgument('queueIdentifier', 'string', 'Flash-message queue to use', false);
+        $this->registerArgument('queueIdentifier', 'string', 'Flash-message queue to use');
+        $this->registerArgument('as', 'string', 'The name of the current flashMessage variable for rendering inside');
     }
 
     /**
@@ -108,12 +110,12 @@ class FlashMessagesViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractT
      * from being cached.
      *
      * @see \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::no_cache
-     * @param string $as The name of the current flashMessage variable for rendering inside
      * @return string rendered Flash Messages, if there are any.
      * @api
      */
-    public function render($as = null)
+    public function render()
     {
+        $as = $this->arguments['as'];
         $queueIdentifier = isset($this->arguments['queueIdentifier']) ? $this->arguments['queueIdentifier'] : null;
         $flashMessages = $this->renderingContext->getControllerContext()->getFlashMessageQueue($queueIdentifier)->getAllMessagesAndFlush();
         if ($flashMessages === null || count($flashMessages) === 0) {
index a7c73ce..a694ebe 100644 (file)
@@ -14,27 +14,24 @@ namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers;
  * The TYPO3 project - inspiring people to share!
  */
 
-use TYPO3Fluid\Fluid\Core\ViewHelper\TagBuilder;
+use TYPO3\CMS\Core\Messaging\FlashMessage;
+use TYPO3\CMS\Core\Messaging\FlashMessageQueue;
+use TYPO3\CMS\Fluid\ViewHelpers\FlashMessagesViewHelper;
 
 /**
  * Testcase for FlashMessagesViewHelper
  */
-class FlashMessagesViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ViewHelperBaseTestcase
+class FlashMessagesViewHelperTest extends ViewHelperBaseTestcase
 {
     /**
-     * @var \TYPO3\CMS\Fluid\ViewHelpers\FlashMessagesViewHelper| \PHPUnit_Framework_MockObject_MockObject |\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
+     * @var \TYPO3\CMS\Fluid\ViewHelpers\FlashMessagesViewHelper
      */
     protected $viewHelper;
 
     /**
-     * @var TagBuilder
+     * @var FlashMessageQueue
      */
-    protected $mockTagBuilder;
-
-    /**
-     * @var \TYPO3\CMS\Core\Messaging\FlashMessageQueue|\PHPUnit_Framework_MockObject_MockObject $mockFlashMessagingQueue
-     */
-    protected $mockFlashMessagingQueue;
+    protected $flashMessageQueue;
 
     /**
      * Sets up this test case
@@ -44,21 +41,11 @@ class FlashMessagesViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelper
     protected function setUp()
     {
         parent::setUp();
-        /** @var \TYPO3\CMS\Core\Messaging\FlashMessageQueue|\PHPUnit_Framework_MockObject_MockObject $mockFlashMessagingQueue */
-        $mockFlashMessagingQueue = $this->getMockBuilder(\TYPO3\CMS\Core\Messaging\FlashMessageQueue::class)
-            ->setMethods(array('getAllMessagesAndFlush'))
-            ->setConstructorArgs(array('foo'))
-            ->getMock();
-        $mockFlashMessagingQueue->expects($this->once())->method('getAllMessagesAndFlush')->will($this->returnValue(array()));
-        $this->mockFlashMessagingQueue = $mockFlashMessagingQueue;
-
-        $this->controllerContext->expects($this->any())->method('getFlashMessageQueue')->will($this->returnValue($mockFlashMessagingQueue));
-
-        $this->mockTagBuilder = $this->createMock(TagBuilder::class);
-        $this->viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\FlashMessagesViewHelper::class, array('dummy'));
-        $this->viewHelper->_set('tag', $this->mockTagBuilder);
-        $this->viewHelper->setRenderingContext($this->renderingContext);
-        $this->viewHelper->initialize();
+        $this->flashMessageQueue = $this->prophesize(FlashMessageQueue::class);
+        $this->controllerContext->expects($this->any())->method('getFlashMessageQueue')->will($this->returnValue($this->flashMessageQueue->reveal()));
+
+        $this->viewHelper = new FlashMessagesViewHelper();
+        $this->injectDependenciesIntoViewHelper($this->viewHelper);
     }
 
     /**
@@ -66,7 +53,8 @@ class FlashMessagesViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelper
      */
     public function renderReturnsEmptyStringIfNoFlashMessagesAreInQueue()
     {
-        $this->assertEmpty($this->viewHelper->render());
+        $this->flashMessageQueue->getAllMessagesAndFlush()->willReturn();
+        $this->assertEmpty($this->viewHelper->initializeArgumentsAndRender());
     }
 
     /**
@@ -76,10 +64,78 @@ class FlashMessagesViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelper
     {
         $queueIdentifier = 'myQueue';
 
-        $this->controllerContext->expects($this->once())->method('getFlashMessageQueue')->with($queueIdentifier)->will($this->returnValue($this->mockFlashMessagingQueue));
+        $this->flashMessageQueue->getAllMessagesAndFlush()->willReturn();
+        $this->controllerContext->expects($this->once())->method('getFlashMessageQueue')->with($queueIdentifier)->will($this->returnValue($this->flashMessageQueue->reveal()));
+
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'queueIdentifier' => $queueIdentifier
+            ]
+        );
+
+        $this->assertEmpty($this->viewHelper->initializeArgumentsAndRender());
+    }
+
+    /**
+     * @test
+     */
+    public function renderRespectsGivenCssClass()
+    {
+        $flashMessage = new FlashMessage('test message body', 'test message title');
+
+        $this->flashMessageQueue->getAllMessagesAndFlush()->willReturn([$flashMessage]);
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'class' => 'flashy',
+            ]
+        );
+        $expected = '<div class="flashy"><div class="alert alert-success">';
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
+        $this->assertContains($expected, $actualResult);
+    }
+
+    /**
+     * @test
+     */
+    public function renderReturnsDefaultTemplate()
+    {
+        $flashMessage = new FlashMessage('test message body', 'test message title');
+
+        $this->flashMessageQueue->getAllMessagesAndFlush()->willReturn([$flashMessage]);
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            []
+        );
+        $expectedTitle = '<h4 class="alert-title">test message title</h4>';
+        $expectedMessage = '<p class="alert-message">test message body</p>';
+
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
+        $this->assertContains($expectedTitle, $actualResult);
+        $this->assertContains($expectedMessage, $actualResult);
+    }
+
+    /**
+     * @test
+     */
+    public function parameterAsStartsRenderingOnTemplate()
+    {
+        $this->viewHelper->setRenderChildrenClosure(function () {
+            return 'a simple String';
+        });
+
+        $flashMessage = new FlashMessage('test message body', 'test message title');
 
-        $this->viewHelper->setArguments(array('queueIdentifier' => $queueIdentifier));
+        $this->flashMessageQueue->getAllMessagesAndFlush()->willReturn([$flashMessage]);
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'as' => 'flashy',
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
 
-        $this->assertEmpty($this->viewHelper->render());
+        $this->assertEquals('a simple String', $actualResult);
     }
 }