[TASK] Refactor PostProcessorTest to use Prophecy 29/42929/2
authorAnja Leichsenring <aleichsenring@ab-softlab.de>
Sun, 30 Aug 2015 12:28:07 +0000 (14:28 +0200)
committerOliver Hader <oliver.hader@typo3.org>
Sun, 30 Aug 2015 13:34:30 +0000 (15:34 +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, 6.2
Reviewed-on: http://review.typo3.org/42929
Reviewed-by: Oliver Hader <oliver.hader@typo3.org>
Tested-by: Oliver Hader <oliver.hader@typo3.org>
typo3/sysext/form/Tests/Unit/PostProcess/PostProcessorTest.php

index e781dae..5920a65 100644 (file)
@@ -13,26 +13,65 @@ 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\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;
+
 /**
  * Testcase for PostProcessor
  */
-class PostProcessorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
+class PostProcessorTest extends UnitTestCase {
+
+       /**
+        * @var array A backup of registered singleton instances
+        */
+       protected $singletonInstances = array();
+
+       /**
+        * @var Form
+        */
+       protected $formProphecy;
+
+       /**
+        * @var Layout
+        */
+       protected $typoScriptLayoutProphecy;
 
        /**
-        * @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Form\PostProcess\PostProcessor
+        * @var TypoScriptFactory
         */
-       public $fixture;
+       protected $typoScriptFactoryProphecy;
 
        /**
         * Set up
         */
-       public function setUp() {
-               $form = $this->getMock('TYPO3\\CMS\\Form\\Domain\\Model\\Form', array(), array(), '', FALSE);
-               $this->fixture = $this->getMock(
-                       'TYPO3\CMS\Form\PostProcess\PostProcessor',
-                       array('sortTypoScriptKeyList'),
-                       array($form, array())
-               );
+       protected function setUp() {
+               $this->singletonInstances = GeneralUtility::getSingletonInstances();
+
+               $this->formProphecy = $this->prophesize('TYPO3\\CMS\\Form\\Domain\\Model\\Form');
+
+               $this->typoScriptFactoryProphecy = $this->prophesize('TYPO3\\CMS\\Form\\Domain\\Factory\\TypoScriptFactory');
+               $this->typoScriptFactoryProphecy->getLayoutFromTypoScript(Argument::any())->willReturn(array());
+               GeneralUtility::setSingletonInstance('TYPO3\\CMS\\Form\\Domain\\Factory\\TypoScriptFactory', $this->typoScriptFactoryProphecy->reveal());
+
+               $this->typoScriptLayoutProphecy = $this->prophesize('TYPO3\\CMS\\Form\\Layout');
+
+               $templateServiceProphecy = $this->prophesize('TYPO3\\CMS\\Core\\TypoScript\\TemplateService');
+               $templateServiceProphecy->sortedKeyList(Argument::any())->willReturn(array(10, 20));
+               GeneralUtility::addInstance('TYPO3\\CMS\\Core\\TypoScript\\TemplateService', $templateServiceProphecy->reveal());
+       }
+
+       /**
+        * Tear down the tests
+        */
+       protected function tearDown() {
+               GeneralUtility::resetSingletonInstances($this->singletonInstances);
+               parent::tearDown();
        }
 
        /**
@@ -43,10 +82,11 @@ class PostProcessorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                        10 => $this->getUniqueId('postprocess'),
                        20 => 'TYPO3\\CMS\\Form\\Tests\\Unit\\Fixtures\\PostProcessorWithoutFormPrefixFixture'
                );
-               $this->fixture->typoScript = $typoScript;
-               $this->fixture->expects($this->once())->method('sortTypoScriptKeyList')->will($this->returnValue(array(10, 20)));
-               $returnValue = $this->fixture->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());
        }
 
        /**
@@ -57,10 +97,11 @@ class PostProcessorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                        10 => $this->getUniqueId('postprocess'),
                        20 => 'TYPO3\\CMS\\Form\\Tests\\Unit\\Fixtures\\PostProcessorWithFormPrefixFixture'
                );
-               $this->fixture->typoScript = $typoScript;
-               $this->fixture->expects($this->once())->method('sortTypoScriptKeyList')->will($this->returnValue(array(10, 20)));
-               $returnValue = $this->fixture->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());
        }
 
        /**
@@ -71,10 +112,10 @@ class PostProcessorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                        10 => $this->getUniqueId('postprocess'),
                        20 => 'TYPO3\\CMS\\Form\\Tests\\Unit\\Fixtures\\PostProcessorWithoutInterfaceFixture'
                );
-               $this->fixture->typoScript = $typoScript;
-               $this->fixture->expects($this->once())->method('sortTypoScriptKeyList')->will($this->returnValue(array(10, 20)));
-               $returnValue = $this->fixture->process();
-               $this->assertEquals('', $returnValue);
-       }
 
+               $subject = new PostProcessor($this->formProphecy->reveal(), $typoScript);
+               $this->typoScriptFactoryProphecy->setLayoutHandler($typoScript)->willReturn($this->typoScriptLayoutProphecy->reveal());
+
+               $this->assertEquals('', $subject->process());
+       }
 }