‪TYPO3CMS  ‪main
FormDataCompilerTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\Test;
21 use PHPUnit\Framework\MockObject\MockObject;
25 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
26 
27 final class ‪FormDataCompilerTest extends UnitTestCase
28 {
31 
32  protected function ‪setUp(): void
33  {
34  parent::setUp();
35  $this->formDataGroupMock = $this->createMock(FormDataGroupInterface::class);
36  $this->subject = new ‪FormDataCompiler();
37  }
38 
39  #[Test]
41  {
42  $input = [
43  'foo' => 'bar',
44  ];
45  $this->expectException(\InvalidArgumentException::class);
46  $this->expectExceptionCode(1440601540);
47  $this->subject->compile($input, $this->formDataGroupMock);
48  }
49 
50  #[Test]
52  {
53  $input = [
54  'command' => 'unknownCommand',
55  ];
56  $this->expectException(\InvalidArgumentException::class);
57  $this->expectExceptionCode(1437653136);
58  $this->subject->compile($input, $this->formDataGroupMock);
59  }
60 
61  #[Test]
63  {
64  $input = [
65  'tableName' => '',
66  ];
67  $this->expectException(\InvalidArgumentException::class);
68  $this->expectExceptionCode(1437654409);
69  $this->subject->compile($input, $this->formDataGroupMock);
70  }
71 
72  #[Test]
74  {
75  $input = [
76  'vanillaUid' => 'foo123',
77  ];
78  $this->expectException(\InvalidArgumentException::class);
79  $this->expectExceptionCode(1437654247);
80  $this->subject->compile($input, $this->formDataGroupMock);
81  }
82 
83  #[Test]
85  {
86  $input = [
87  'command' => 'edit',
88  'vanillaUid' => -100,
89  ];
90  $this->expectException(\InvalidArgumentException::class);
91  $this->expectExceptionCode(1437654332);
92  $this->subject->compile($input, $this->formDataGroupMock);
93  }
94 
95  #[Test]
97  {
98  $this->expectException(\RuntimeException::class);
99  $this->expectExceptionCode(1686867720);
100  $this->expectExceptionMessage('The current ServerRequestInterface must be provided in key "request"');
101 
102  $this->subject->compile(
103  [],
104  $this->formDataGroupMock
105  );
106  }
107 
108  #[Test]
110  {
111  $input = [
112  'request' => new ‪ServerRequest(),
113  'tableName' => 'pages',
114  'vanillaUid' => 123,
115  'command' => 'edit',
116  ];
117  $this->formDataGroupMock->method('compile')->with(self::anything())->willReturnArgument(0);
118  $result = $this->subject->compile($input, $this->formDataGroupMock);
119  self::assertEquals('pages', $result['tableName']);
120  self::assertEquals(123, $result['vanillaUid']);
121  self::assertEquals('edit', $result['command']);
122  }
123 
124  #[Test]
126  {
127  $this->formDataGroupMock->method('compile')->with(self::anything())->willReturnCallback(static function (array $arguments): array {
128  $result = $arguments;
129  $result['databaseRow'] = 'newData';
130  return $result;
131  });
132  $result = $this->subject->compile(
133  [
134  'request' => new ‪ServerRequest(),
135  ],
136  $this->formDataGroupMock
137  );
138  self::assertEquals('newData', $result['databaseRow']);
139  }
140 
141  #[Test]
143  {
144  $this->formDataGroupMock->method('compile')->with(self::anything())->willReturn(null);
145  $this->expectException(\UnexpectedValueException::class);
146  $this->expectExceptionCode(1446664764);
147  $this->subject->compile(
148  [
149  'request' => new ‪ServerRequest(),
150  ],
151  $this->formDataGroupMock
152  );
153  }
154 
155  #[Test]
157  {
158  $this->formDataGroupMock->method('compile')->with(self::anything())->willReturn([
159  'renderData' => [ 'foo' ],
160  ]);
161  $this->expectException(\RuntimeException::class);
162  $this->expectExceptionCode(1485201279);
163  $this->subject->compile(
164  [
165  'request' => new ‪ServerRequest(),
166  ],
167  $this->formDataGroupMock
168  );
169  }
170 
171  #[Test]
173  {
174  $this->formDataGroupMock->method('compile')->with(self::anything())->willReturnCallback(static function (array $arguments): array {
175  $result = $arguments;
176  unset($result['tableName']);
177  return $result;
178  });
179  $this->expectException(\UnexpectedValueException::class);
180  $this->expectExceptionCode(1438079402);
181  $this->subject->compile(
182  [
183  'request' => new ‪ServerRequest(),
184  ],
185  $this->formDataGroupMock
186  );
187  }
188 
189  #[Test]
191  {
192  $this->formDataGroupMock->method('compile')->with(self::anything())->willReturnCallback(static function (array $arguments): array {
193  $result = $arguments;
194  $result['newKey'] = 'newData';
195  return $result;
196  });
197  $this->expectException(\UnexpectedValueException::class);
198  $this->expectExceptionCode(1438079402);
199  $this->subject->compile(
200  [
201  'request' => new ‪ServerRequest(),
202  ],
203  $this->formDataGroupMock
204  );
205  }
206 }
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataCompilerTest\compileReturnsResultArrayWithInputDataSet
‪compileReturnsResultArrayWithInputDataSet()
Definition: FormDataCompilerTest.php:109
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataCompilerTest\compileThrowsExceptionIfFormDataGroupRemovedKeysFromResultArray
‪compileThrowsExceptionIfFormDataGroupRemovedKeysFromResultArray()
Definition: FormDataCompilerTest.php:172
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataCompilerTest\compileThrowsExceptionIfInputContainsKeysNotValidInResult
‪compileThrowsExceptionIfInputContainsKeysNotValidInResult()
Definition: FormDataCompilerTest.php:40
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataCompilerTest\compileThrowsExceptionIfCommandIsEditAndUidIsNegative
‪compileThrowsExceptionIfCommandIsEditAndUidIsNegative()
Definition: FormDataCompilerTest.php:84
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataCompilerTest\$formDataGroupMock
‪FormDataGroupInterface &MockObject $formDataGroupMock
Definition: FormDataCompilerTest.php:30
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataCompilerTest\setUp
‪setUp()
Definition: FormDataCompilerTest.php:32
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataCompilerTest\compileThrowsExceptionIfUidIsNotAnInteger
‪compileThrowsExceptionIfUidIsNotAnInteger()
Definition: FormDataCompilerTest.php:73
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataCompilerTest\compileReturnsResultArrayWithAdditionalDataFormFormDataGroup
‪compileReturnsResultArrayWithAdditionalDataFormFormDataGroup()
Definition: FormDataCompilerTest.php:125
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataCompilerTest\compileThrowsExceptionIfRenderDataIsNotEmpty
‪compileThrowsExceptionIfRenderDataIsNotEmpty()
Definition: FormDataCompilerTest.php:156
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataCompilerTest\compileThrowsExceptionIfFormDataGroupDoesNotReturnArray
‪compileThrowsExceptionIfFormDataGroupDoesNotReturnArray()
Definition: FormDataCompilerTest.php:142
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataCompilerTest\compileThrowsExceptionIfFormDataGroupAddedKeysToResultArray
‪compileThrowsExceptionIfFormDataGroupAddedKeysToResultArray()
Definition: FormDataCompilerTest.php:190
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataCompilerTest\compileThrowsExceptionIfNoTableNameGiven
‪compileThrowsExceptionIfNoTableNameGiven()
Definition: FormDataCompilerTest.php:62
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataCompilerTest\compileThrowsExceptionIfRequestIsNotProvidedInInitialDataArray
‪compileThrowsExceptionIfRequestIsNotProvidedInInitialDataArray()
Definition: FormDataCompilerTest.php:96
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataCompilerTest\compileThrowsExceptionAtUnknownCommand
‪compileThrowsExceptionAtUnknownCommand()
Definition: FormDataCompilerTest.php:51
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Backend\Form\FormDataGroupInterface
Definition: FormDataGroupInterface.php:23
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataCompilerTest\$subject
‪FormDataCompiler $subject
Definition: FormDataCompilerTest.php:29
‪TYPO3\CMS\Backend\Form\FormDataCompiler
Definition: FormDataCompiler.php:26
‪TYPO3\CMS\Backend\Tests\Unit\Form
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataCompilerTest
Definition: FormDataCompilerTest.php:28