[TASK] Refactor PostProcessorTest to use Prophecy 21/42921/3
authorAnja Leichsenring <aleichsenring@ab-softlab.de>
Sat, 29 Aug 2015 14:15:05 +0000 (16:15 +0200)
committerOliver Hader <oliver.hader@typo3.org>
Sat, 29 Aug 2015 15:41:17 +0000 (17:41 +0200)
In order to get rid of the mocking of the system under test, the
tests of ext:form PostProcessorTest are refactored to use
Prophecy. This step precedes the addition of other tests for
the class.

Change-Id: I1a2e9c5305c070f291b549965360abcfb8a695d0
Resolves: #69407
Releases: master
Reviewed-on: http://review.typo3.org/42921
Reviewed-by: Andreas Fernandez <typo3@scripting-base.de>
Tested-by: Andreas Fernandez <typo3@scripting-base.de>
Reviewed-by: Oliver Hader <oliver.hader@typo3.org>
Tested-by: Oliver Hader <oliver.hader@typo3.org>
typo3/sysext/form/Classes/PostProcess/PostProcessor.php
typo3/sysext/form/Tests/Unit/PostProcess/PostProcessorTest.php

index 34fe579..090f43a 100644 (file)
@@ -55,7 +55,7 @@ class PostProcessor {
        public function process() {
                $html = '';
                if (is_array($this->typoScript)) {
-                       $keys = $this->sortTypoScriptKeyList();
+                       $keys = \TYPO3\CMS\Core\TypoScript\TemplateService::sortedKeyList($this->typoScript);
                        $layoutHandler = $this->typoscriptFactory->setLayoutHandler($this->typoScript);
 
                        foreach ($keys as $key) {
@@ -90,14 +90,4 @@ class PostProcessor {
                return $html;
        }
 
-       /**
-        * Wrapper method for \TYPO3\CMS\Core\TypoScript\TemplateService::sortedKeyList
-        * (makes unit testing possible)
-        *
-        * @return array
-        */
-       public function sortTypoScriptKeyList() {
-               return \TYPO3\CMS\Core\TypoScript\TemplateService::sortedKeyList($this->typoScript);
-       }
-
 }
index 4990007..34e7d11 100644 (file)
@@ -13,41 +13,84 @@ namespace TYPO3\CMS\Form\Tests\Unit\PostProcess;
  *
  * The TYPO3 project - inspiring people to share!
  */
+use Prophecy\Argument;
+use TYPO3\CMS\Core\Tests\UnitTestCase;
+use TYPO3\CMS\Core\TypoScript\TemplateService;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Form\Domain\Factory\TypoScriptFactory;
+use TYPO3\CMS\Form\Domain\Model\Form;
+use TYPO3\CMS\Form\Layout;
+use TYPO3\CMS\Form\PostProcess\PostProcessor;
+use TYPO3\CMS\Form\Tests\Unit\Fixtures\PostProcessorWithFormPrefixFixture;
+use TYPO3\CMS\Form\Tests\Unit\Fixtures\PostProcessorWithoutFormPrefixFixture;
+use TYPO3\CMS\Form\Tests\Unit\Fixtures\PostProcessorWithoutInterfaceFixture;
 
 /**
  * Testcase for PostProcessor
  */
-class PostProcessorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
+class PostProcessorTest extends UnitTestCase {
 
        /**
-        * @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Form\PostProcess\PostProcessor
+        * @var array A backup of registered singleton instances
         */
-       public $subject;
+       protected $singletonInstances = array();
+
+       /**
+        * @var Form
+        */
+       protected $formProphecy;
+
+       /**
+        * @var Layout
+        */
+       protected $typoScriptLayoutProphecy;
+
+       /**
+        * @var TypoScriptFactory
+        */
+       protected $typoScriptFactoryProphecy;
 
        /**
         * Set up
         */
        protected function setUp() {
-               $form = $this->getMock(\TYPO3\CMS\Form\Domain\Model\Form::class, array(), array(), '', FALSE);
-               $this->subject = $this->getMock(
-                       \TYPO3\CMS\Form\PostProcess\PostProcessor::class,
-                       array('sortTypoScriptKeyList'),
-                       array($form, array())
-               );
+               $this->singletonInstances = GeneralUtility::getSingletonInstances();
+
+               $this->formProphecy = $this->prophesize(Form::class);
+
+               $this->typoScriptFactoryProphecy = $this->prophesize(TypoScriptFactory::class);
+               $this->typoScriptFactoryProphecy->getLayoutFromTypoScript(Argument::any())->willReturn(array());
+               GeneralUtility::setSingletonInstance(TypoScriptFactory::class, $this->typoScriptFactoryProphecy->reveal());
+
+               $this->typoScriptLayoutProphecy = $this->prophesize(Layout::class);
+
+               $templateServiceProphecy = $this->prophesize(TemplateService::class);
+               $templateServiceProphecy->sortedKeyList(Argument::any())->willReturn(array(10, 20));
+               GeneralUtility::addInstance(TemplateService::class, $templateServiceProphecy->reveal());
+       }
+
+       /**
+        * Tear down the tests
+        */
+       protected function tearDown() {
+               GeneralUtility::resetSingletonInstances($this->singletonInstances);
+               parent::tearDown();
        }
 
        /**
         * @test
         */
        public function processFindsClassSpecifiedByTypoScriptWithoutFormPrefix() {
+
                $typoScript = array(
                        10 => $this->getUniqueId('postprocess'),
-                       20 => \TYPO3\CMS\Form\Tests\Unit\Fixtures\PostProcessorWithoutFormPrefixFixture::class
+                       20 => PostProcessorWithoutFormPrefixFixture::class
                );
-               $this->subject->typoScript = $typoScript;
-               $this->subject->expects($this->once())->method('sortTypoScriptKeyList')->will($this->returnValue(array(10, 20)));
-               $returnValue = $this->subject->process();
-               $this->assertEquals('processedWithoutPrefix', $returnValue);
+
+               $subject = new PostProcessor($this->formProphecy->reveal(), $typoScript);
+               $this->typoScriptFactoryProphecy->setLayoutHandler($typoScript)->willReturn($this->typoScriptLayoutProphecy->reveal());
+
+               $this->assertEquals('processedWithoutPrefix', $subject->process());
        }
 
        /**
@@ -56,12 +99,13 @@ class PostProcessorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
        public function processFindsClassSpecifiedByTypoScriptWithFormPrefix() {
                $typoScript = array(
                        10 => $this->getUniqueId('postprocess'),
-                       20 => \TYPO3\CMS\Form\Tests\Unit\Fixtures\PostProcessorWithFormPrefixFixture::class
+                       20 => PostProcessorWithFormPrefixFixture::class
                );
-               $this->subject->typoScript = $typoScript;
-               $this->subject->expects($this->once())->method('sortTypoScriptKeyList')->will($this->returnValue(array(10, 20)));
-               $returnValue = $this->subject->process();
-               $this->assertEquals('processedWithPrefix', $returnValue);
+
+               $subject = new PostProcessor($this->formProphecy->reveal(), $typoScript);
+               $this->typoScriptFactoryProphecy->setLayoutHandler($typoScript)->willReturn($this->typoScriptLayoutProphecy->reveal());
+
+               $this->assertEquals('processedWithPrefix', $subject->process());
        }
 
        /**
@@ -70,12 +114,12 @@ class PostProcessorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
        public function processReturnsEmptyStringIfSpecifiedPostProcessorDoesNotImplementTheInterface() {
                $typoScript = array(
                        10 => $this->getUniqueId('postprocess'),
-                       20 => \TYPO3\CMS\Form\Tests\Unit\Fixtures\PostProcessorWithoutInterfaceFixture::class
+                       20 => PostProcessorWithoutInterfaceFixture::class
                );
-               $this->subject->typoScript = $typoScript;
-               $this->subject->expects($this->once())->method('sortTypoScriptKeyList')->will($this->returnValue(array(10, 20)));
-               $returnValue = $this->subject->process();
-               $this->assertEquals('', $returnValue);
-       }
 
+               $subject = new PostProcessor($this->formProphecy->reveal(), $typoScript);
+               $this->typoScriptFactoryProphecy->setLayoutHandler($typoScript)->willReturn($this->typoScriptLayoutProphecy->reveal());
+
+               $this->assertEquals('', $subject->process());
+       }
 }