[FEATURE] Introduce conditional variants for form elements
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Tests / Unit / Domain / Runtime / FormRuntimeTest.php
1 <?php
2 namespace TYPO3\CMS\Form\Tests\Unit\Domain\Runtime;
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
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Extbase\Object\ObjectManager;
19 use TYPO3\CMS\Form\Domain\Exception\RenderingException;
20 use TYPO3\CMS\Form\Domain\Model\FormDefinition;
21 use TYPO3\CMS\Form\Domain\Model\FormElements\Page;
22 use TYPO3\CMS\Form\Domain\Runtime\FormRuntime;
23 use TYPO3\CMS\Form\Domain\Runtime\FormState;
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25
26 /**
27 * Test case
28 */
29 class FormRuntimeTest extends UnitTestCase
30 {
31 /**
32 * @var bool Reset singletons created by subject
33 */
34 protected $resetSingletonInstances = true;
35
36 /**
37 * @test
38 */
39 public function renderThrowsExceptionIfFormDefinitionReturnsNoRendererClassName()
40 {
41 $mockFormRuntime = $this->getAccessibleMock(FormRuntime::class, [
42 'isAfterLastPage', 'processVariants'
43 ], [], '', false);
44
45 $mockPage = $this->getAccessibleMock(Page::class, [
46 'getIndex'
47 ], [], '', false);
48
49 $mockFormState = $this->getAccessibleMock(FormState::class, [
50 'dummy'
51 ], [], '', false);
52
53 $mockFormDefinition = $this->getAccessibleMock(FormDefinition::class, [
54 'getRendererClassName',
55 'getIdentifier'
56 ], [], '', false);
57
58 $mockPage
59 ->expects($this->any())
60 ->method('getIndex')
61 ->willReturn(1);
62
63 $mockFormDefinition
64 ->expects($this->any())
65 ->method('getRendererClassName')
66 ->willReturn('');
67
68 $mockFormDefinition
69 ->expects($this->any())
70 ->method('getIdentifier')
71 ->willReturn('text-1');
72
73 $mockFormRuntime
74 ->expects($this->any())
75 ->method('isAfterLastPage')
76 ->willReturn(false);
77
78 $mockFormRuntime
79 ->expects($this->any())
80 ->method('processVariants')
81 ->willReturn(null);
82
83 $mockFormRuntime->_set('formState', $mockFormState);
84 $mockFormRuntime->_set('currentPage', $mockPage);
85 $mockFormRuntime->_set('formDefinition', $mockFormDefinition);
86
87 $this->expectException(RenderingException::class);
88 $this->expectExceptionCode(1326095912);
89
90 $mockFormRuntime->_call('render');
91 }
92
93 /**
94 * @test
95 */
96 public function renderThrowsExceptionIfRendererClassNameInstanceDoesNotImplementRendererInterface()
97 {
98 $objectManagerProphecy = $this->prophesize(ObjectManager::class);
99 GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManagerProphecy->reveal());
100
101 $mockFormRuntime = $this->getAccessibleMock(FormRuntime::class, [
102 'isAfterLastPage', 'processVariants'
103 ], [], '', false);
104
105 $mockPage = $this->getAccessibleMock(Page::class, [
106 'getIndex'
107 ], [], '', false);
108
109 $mockFormState = $this->getAccessibleMock(FormState::class, [
110 'dummy'
111 ], [], '', false);
112
113 $mockFormDefinition = $this->getAccessibleMock(FormDefinition::class, [
114 'getRendererClassName',
115 'getIdentifier'
116 ], [], '', false);
117
118 $mockPage
119 ->expects($this->any())
120 ->method('getIndex')
121 ->willReturn(1);
122
123 $mockFormDefinition
124 ->expects($this->any())
125 ->method('getRendererClassName')
126 ->willReturn('fooRenderer');
127
128 $mockFormDefinition
129 ->expects($this->any())
130 ->method('getIdentifier')
131 ->willReturn('text-1');
132
133 $mockFormRuntime
134 ->expects($this->any())
135 ->method('isAfterLastPage')
136 ->willReturn(false);
137
138 $mockFormRuntime
139 ->expects($this->any())
140 ->method('processVariants')
141 ->willReturn(null);
142
143 $objectManagerProphecy
144 ->get('fooRenderer')
145 ->willReturn(new \stdClass);
146
147 $mockFormRuntime->_set('formState', $mockFormState);
148 $mockFormRuntime->_set('currentPage', $mockPage);
149 $mockFormRuntime->_set('formDefinition', $mockFormDefinition);
150 $mockFormRuntime->_set('objectManager', $objectManagerProphecy->reveal());
151
152 $this->expectException(RenderingException::class);
153 $this->expectExceptionCode(1326096024);
154
155 $mockFormRuntime->_call('render');
156 }
157 }