[TASK] Streamline EXT:form unit test 94/50994/2
authorOliver Hader <oliver@typo3.org>
Sun, 18 Dec 2016 12:21:09 +0000 (13:21 +0100)
committerOliver Hader <oliver.hader@typo3.org>
Mon, 19 Dec 2016 19:33:07 +0000 (20:33 +0100)
EXT:form's FormManagerControllerTest tries to mock
static method calls of BackendUtility. The methods
to be mocked are now wrapped directly instead of
"wrapping" the reference to the class name.

Change-Id: I7c6381fe093ecc28b85e235fcb425f6048c8ff0c
Resolves: #79023
Releases: master
Reviewed-on: https://review.typo3.org/50994
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Susanne Moog <susanne.moog@typo3.org>
Reviewed-by: Oliver Hader <oliver.hader@typo3.org>
Tested-by: Oliver Hader <oliver.hader@typo3.org>
typo3/sysext/form/Classes/Controller/FormManagerController.php
typo3/sysext/form/Tests/Unit/Controller/Fixtures/BackendUtilityFixture.php [deleted file]
typo3/sysext/form/Tests/Unit/Controller/FormManagerControllerTest.php

index 5c6c3c9..e3f77e6 100644 (file)
@@ -274,26 +274,26 @@ class FormManagerController extends AbstractBackendController
 
         $referenceRows = $this->getReferences($persistenceIdentifier);
         foreach ($referenceRows as &$referenceRow) {
-            $record = $this->getBackendUtility()::getRecord($referenceRow['tablename'], $referenceRow['recuid']);
+            $record = $this->getRecord($referenceRow['tablename'], $referenceRow['recuid']);
             if (!$record) {
                 continue;
             }
-            $pageRecord = $this->getBackendUtility()::getRecord('pages', $record['pid']);
+            $pageRecord = $this->getRecord('pages', $record['pid']);
             $urlParameters = [
                 'edit' => [
                     $referenceRow['tablename'] => [
                         $referenceRow['recuid'] => 'edit'
                     ]
                 ],
-                'returnUrl' => $this->getBackendUtility()::getModuleUrl('web_FormFormbuilder')
+                'returnUrl' => $this->getModuleUrl('web_FormFormbuilder')
             ];
 
             $references[] = [
-                'recordPageTitle' => is_array($pageRecord) ? $this->getBackendUtility()::getRecordTitle('pages', $pageRecord) : '',
-                'recordTitle' => $this->getBackendUtility()::getRecordTitle($referenceRow['tablename'], $record, true),
+                'recordPageTitle' => is_array($pageRecord) ? $this->getRecordTitle('pages', $pageRecord) : '',
+                'recordTitle' => $this->getRecordTitle($referenceRow['tablename'], $record, true),
                 'recordIcon' => $iconFactory->getIconForRecord($referenceRow['tablename'], $record, Icon::SIZE_SMALL)->render(),
                 'recordUid' => $referenceRow['recuid'],
-                'recordEditUrl' => $this->getBackendUtility()::getModuleUrl('record_edit', $urlParameters),
+                'recordEditUrl' => $this->getModuleUrl('record_edit', $urlParameters),
             ];
         }
         return $references;
@@ -395,7 +395,7 @@ class FormManagerController extends AbstractBackendController
             $backButton = $buttonBar->makeLinkButton()
                 ->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_common.xlf:back'))
                 ->setIcon($this->view->getModuleTemplate()->getIconFactory()->getIcon('actions-view-go-up', Icon::SIZE_SMALL))
-                ->setHref($this->getBackendUtility()::getModuleUrl($moduleName));
+                ->setHref($this->getModuleUrl($moduleName));
             $buttonBar->addButton($backButton);
         } else {
             $addFormButton = $buttonBar->makeLinkButton()
@@ -421,14 +421,40 @@ class FormManagerController extends AbstractBackendController
     }
 
     /**
-     * Returns the BackendUtility
-     * This wrapper is needed for unit tests.
+     * Wrapper used for unit testing.
      *
+     * @param string $table
+     * @param int $uid
+     * @return array|NULL
+     */
+    protected function getRecord(string $table, int $uid)
+    {
+        return BackendUtility::getRecord($table, $uid);
+    }
+
+    /**
+     * Wrapper used for unit testing.
+     *
+     * @param string $table
+     * @param array $row
+     * @param bool $prep
+     * @return string
+     */
+    protected function getRecordTitle(string $table, array $row, bool $prep = false): string
+    {
+        return BackendUtility::getRecordTitle($table, $row, $prep);
+    }
+
+    /**
+     * Wrapper used for unit testing.
+     *
+     * @param string $moduleName
+     * @param array $urlParameters
      * @return string
      */
-    protected function getBackendUtility(): string
+    protected function getModuleUrl(string $moduleName, array $urlParameters = []): string
     {
-        return BackendUtility::class;
+        return BackendUtility::getModuleUrl($moduleName, $urlParameters);
     }
 
     /**
diff --git a/typo3/sysext/form/Tests/Unit/Controller/Fixtures/BackendUtilityFixture.php b/typo3/sysext/form/Tests/Unit/Controller/Fixtures/BackendUtilityFixture.php
deleted file mode 100644 (file)
index 45177c9..0000000
+++ /dev/null
@@ -1,60 +0,0 @@
-<?php
-declare(strict_types=1);
-namespace TYPO3\CMS\Form\Tests\Unit\Controller\Fixtures;
-
-/*
- * This file is part of the TYPO3 CMS project.
- *
- * It is free software; you can redistribute it and/or modify it under
- * the terms of the GNU General Public License, either version 2
- * of the License, or any later version.
- *
- * For the full copyright and license information, please read the
- * LICENSE.txt file that was distributed with this source code.
- *
- * The TYPO3 project - inspiring people to share!
- */
-
-/**
- * Fixture for BackendUtility methods
- */
-class BackendUtilityFixture
-{
-
-    /**
-     * @param string $table
-     * @param int $uid
-     * @param string $fields
-     * @param string $where
-     * @param bool $useDeleteClause
-     * @return array
-     */
-    public static function getRecord($table, $uid, $fields = '*', $where = '', $useDeleteClause = true)
-    {
-        return [
-            'uid' => 1,
-        ];
-    }
-
-    /**
-     * @param string $table
-     * @param array $row
-     * @param bool $prep
-     * @param bool $forceResult
-     * @return string
-     */
-    public static function getRecordTitle($table, $row, $prep = false, $forceResult = true)
-    {
-        return 'record title';
-    }
-
-    /**
-     * @param string $moduleName
-     * @param array $urlParameters
-     * @return string
-     */
-    public static function getModuleUrl($moduleName, $urlParameters = [])
-    {
-        return '/typo3/index.php?some=param';
-    }
-}
index 83ac020..96d0722 100644 (file)
@@ -25,7 +25,6 @@ use TYPO3\CMS\Extbase\Object\ObjectManager;
 use TYPO3\CMS\Form\Controller\FormManagerController;
 use TYPO3\CMS\Form\Mvc\Persistence\FormPersistenceManager;
 use TYPO3\CMS\Form\Service\TranslationService;
-use TYPO3\CMS\Form\Tests\Unit\Controller\Fixtures\BackendUtilityFixture;
 
 /**
  * Test case
@@ -233,14 +232,26 @@ class FormManagerControllerTest extends UnitTestCase
     public function getProcessedReferencesRowsReturnsProcessedArray()
     {
         $mockController = $this->getAccessibleMock(FormManagerController::class, [
+            'getModuleUrl',
+            'getRecord',
+            'getRecordTitle',
             'getReferences',
-            'getBackendUtility',
         ], [], '', false);
 
         $mockController
             ->expects($this->any())
-            ->method('getBackendUtility')
-            ->willReturn(BackendUtilityFixture::class);
+            ->method('getModuleUrl')
+            ->willReturn('/typo3/index.php?some=param');
+
+        $mockController
+            ->expects($this->any())
+            ->method('getRecord')
+            ->willReturn([ 'uid' => 1, 'pid' => 0 ]);
+
+        $mockController
+            ->expects($this->any())
+            ->method('getRecordTitle')
+            ->willReturn('record title');
 
         $mockController
             ->expects($this->any())