[TASK] Refactor PostProcessorTest to use Prophecy
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Tests / Unit / PostProcess / PostProcessorTest.php
1 <?php
2 namespace TYPO3\CMS\Form\Tests\Unit\PostProcess;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 use Prophecy\Argument;
17 use TYPO3\CMS\Core\Tests\UnitTestCase;
18 use TYPO3\CMS\Core\TypoScript\TemplateService;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Form\Domain\Factory\TypoScriptFactory;
21 use TYPO3\CMS\Form\Domain\Model\Form;
22 use TYPO3\CMS\Form\Layout;
23 use TYPO3\CMS\Form\PostProcess\PostProcessor;
24 use TYPO3\CMS\Form\Tests\Unit\Fixtures\PostProcessorWithFormPrefixFixture;
25 use TYPO3\CMS\Form\Tests\Unit\Fixtures\PostProcessorWithoutFormPrefixFixture;
26 use TYPO3\CMS\Form\Tests\Unit\Fixtures\PostProcessorWithoutInterfaceFixture;
27
28 /**
29 * Testcase for PostProcessor
30 */
31 class PostProcessorTest extends UnitTestCase {
32
33 /**
34 * @var array A backup of registered singleton instances
35 */
36 protected $singletonInstances = array();
37
38 /**
39 * @var Form
40 */
41 protected $formProphecy;
42
43 /**
44 * @var Layout
45 */
46 protected $typoScriptLayoutProphecy;
47
48 /**
49 * @var TypoScriptFactory
50 */
51 protected $typoScriptFactoryProphecy;
52
53 /**
54 * Set up
55 */
56 protected function setUp() {
57 $this->singletonInstances = GeneralUtility::getSingletonInstances();
58
59 $this->formProphecy = $this->prophesize(Form::class);
60
61 $this->typoScriptFactoryProphecy = $this->prophesize(TypoScriptFactory::class);
62 $this->typoScriptFactoryProphecy->getLayoutFromTypoScript(Argument::any())->willReturn(array());
63 GeneralUtility::setSingletonInstance(TypoScriptFactory::class, $this->typoScriptFactoryProphecy->reveal());
64
65 $this->typoScriptLayoutProphecy = $this->prophesize(Layout::class);
66
67 $templateServiceProphecy = $this->prophesize(TemplateService::class);
68 $templateServiceProphecy->sortedKeyList(Argument::any())->willReturn(array(10, 20));
69 GeneralUtility::addInstance(TemplateService::class, $templateServiceProphecy->reveal());
70 }
71
72 /**
73 * Tear down the tests
74 */
75 protected function tearDown() {
76 GeneralUtility::resetSingletonInstances($this->singletonInstances);
77 parent::tearDown();
78 }
79
80 /**
81 * @test
82 */
83 public function processFindsClassSpecifiedByTypoScriptWithoutFormPrefix() {
84
85 $typoScript = array(
86 10 => $this->getUniqueId('postprocess'),
87 20 => PostProcessorWithoutFormPrefixFixture::class
88 );
89
90 $subject = new PostProcessor($this->formProphecy->reveal(), $typoScript);
91 $this->typoScriptFactoryProphecy->setLayoutHandler($typoScript)->willReturn($this->typoScriptLayoutProphecy->reveal());
92
93 $this->assertEquals('processedWithoutPrefix', $subject->process());
94 }
95
96 /**
97 * @test
98 */
99 public function processFindsClassSpecifiedByTypoScriptWithFormPrefix() {
100 $typoScript = array(
101 10 => $this->getUniqueId('postprocess'),
102 20 => PostProcessorWithFormPrefixFixture::class
103 );
104
105 $subject = new PostProcessor($this->formProphecy->reveal(), $typoScript);
106 $this->typoScriptFactoryProphecy->setLayoutHandler($typoScript)->willReturn($this->typoScriptLayoutProphecy->reveal());
107
108 $this->assertEquals('processedWithPrefix', $subject->process());
109 }
110
111 /**
112 * @test
113 */
114 public function processReturnsEmptyStringIfSpecifiedPostProcessorDoesNotImplementTheInterface() {
115 $typoScript = array(
116 10 => $this->getUniqueId('postprocess'),
117 20 => PostProcessorWithoutInterfaceFixture::class
118 );
119
120 $subject = new PostProcessor($this->formProphecy->reveal(), $typoScript);
121 $this->typoScriptFactoryProphecy->setLayoutHandler($typoScript)->willReturn($this->typoScriptLayoutProphecy->reveal());
122
123 $this->assertEquals('', $subject->process());
124 }
125 }