[!!!][FEATURE] FormEngine element level refactoring
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Tests / Unit / Form / FormDataCompilerTest.php
1 <?php
2 namespace TYPO3\CMS\Backend\Tests\Unit\Form;
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 Prophecy\Argument;
18 use Prophecy\Prophecy\ObjectProphecy;
19 use TYPO3\CMS\Backend\Form\FormDataCompiler;
20 use TYPO3\CMS\Backend\Form\FormDataGroupInterface;
21
22 /**
23 * Test case
24 */
25 class FormDataCompilerTest extends \TYPO3\CMS\Components\TestingFramework\Core\UnitTestCase
26 {
27 /**
28 * @var FormDataCompiler
29 */
30 protected $subject;
31
32 /**
33 * @var FormDataGroupInterface | ObjectProphecy
34 */
35 protected $formDataGroupProphecy;
36
37 protected function setUp()
38 {
39 $this->formDataGroupProphecy = $this->prophesize(FormDataGroupInterface::class);
40 $this->subject = new FormDataCompiler($this->formDataGroupProphecy->reveal());
41 }
42
43 /**
44 * @test
45 */
46 public function compileThrowsExceptionIfInputContainsKeysNotValidInResult()
47 {
48 $input = [
49 'foo' => 'bar',
50 ];
51 $this->expectException(\InvalidArgumentException::class);
52 $this->expectExceptionCode(1440601540);
53 $this->subject->compile($input);
54 }
55
56 /**
57 * @test
58 */
59 public function compileThrowsExceptionAtUnknownCommand()
60 {
61 $input = [
62 'command' => 'unknownCommand',
63 ];
64 $this->expectException(\InvalidArgumentException::class);
65 $this->expectExceptionCode(1437653136);
66 $this->subject->compile($input);
67 }
68
69 /**
70 * @test
71 */
72 public function compileThrowsExceptionIfNoTableNameGiven()
73 {
74 $input = [
75 'tableName' => '',
76 ];
77 $this->expectException(\InvalidArgumentException::class);
78 $this->expectExceptionCode(1437654409);
79 $this->subject->compile($input);
80 }
81
82 /**
83 * @test
84 */
85 public function compileThrowsExceptionIfUidIsNotAnInteger()
86 {
87 $input = [
88 'vanillaUid' => 'foo123',
89 ];
90 $this->expectException(\InvalidArgumentException::class);
91 $this->expectExceptionCode(1437654247);
92 $this->subject->compile($input);
93 }
94
95 /**
96 * @test
97 */
98 public function compileThrowsExceptionIfCommandIsEditAndUidIsNegative()
99 {
100 $input = [
101 'command' => 'edit',
102 'vanillaUid' => -100,
103 ];
104 $this->expectException(\InvalidArgumentException::class);
105 $this->expectExceptionCode(1437654332);
106 $this->subject->compile($input);
107 }
108
109 /**
110 * @test
111 */
112 public function compileReturnsResultArrayWithInputDataSet()
113 {
114 $input = [
115 'tableName' => 'pages',
116 'vanillaUid' => 123,
117 'command' => 'edit',
118 ];
119 $this->formDataGroupProphecy->compile(Argument::cetera())->willReturnArgument(0);
120 $result = $this->subject->compile($input);
121 $this->assertEquals('pages', $result['tableName']);
122 $this->assertEquals(123, $result['vanillaUid']);
123 $this->assertEquals('edit', $result['command']);
124 }
125
126 /**
127 * @test
128 */
129 public function compileReturnsResultArrayWithAdditionalDataFormFormDataGroup()
130 {
131 $this->formDataGroupProphecy->compile(Argument::cetera())->will(function ($arguments) {
132 $result = $arguments[0];
133 $result['databaseRow'] = 'newData';
134 return $result;
135 });
136 $result = $this->subject->compile([]);
137 $this->assertEquals('newData', $result['databaseRow']);
138 }
139
140 /**
141 * @test
142 */
143 public function compileThrowsExceptionIfFormDataGroupDoesNotReturnArray()
144 {
145 $this->formDataGroupProphecy->compile(Argument::cetera())->willReturn(null);
146 $this->expectException(\UnexpectedValueException::class);
147 $this->expectExceptionCode(1446664764);
148 $this->subject->compile([]);
149 }
150
151 /**
152 * @test
153 */
154 public function compileThrowsExceptionIfRenderDataIsNotEmpty()
155 {
156 $this->formDataGroupProphecy->compile(Argument::cetera())->willReturn([
157 'renderData' => [ 'foo' ],
158 ]);
159 $this->expectException(\RuntimeException::class);
160 $this->expectExceptionCode(1485201279);
161 $this->subject->compile([]);
162 }
163
164 /**
165 * @test
166 */
167 public function compileThrowsExceptionIfFormDataGroupRemovedKeysFromResultArray()
168 {
169 $this->formDataGroupProphecy->compile(Argument::cetera())->will(function ($arguments) {
170 $result = $arguments[0];
171 unset($result['tableName']);
172 return $result;
173 });
174 $this->expectException(\UnexpectedValueException::class);
175 $this->expectExceptionCode(1438079402);
176 $this->subject->compile([]);
177 }
178
179 /**
180 * @test
181 */
182 public function compileThrowsExceptionIfFormDataGroupAddedKeysToResultArray()
183 {
184 $this->formDataGroupProphecy->compile(Argument::cetera())->will(function ($arguments) {
185 $result = $arguments[0];
186 $result['newKey'] = 'newData';
187 return $result;
188 });
189 $this->expectException(\UnexpectedValueException::class);
190 $this->expectExceptionCode(1438079402);
191 $this->subject->compile([]);
192 }
193 }