‪TYPO3CMS  9.5
AbstractSectionTest.php
Go to the documentation of this file.
1 <?php
2 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 
27 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
28 
32 class ‪AbstractSectionTest extends UnitTestCase
33 {
38  {
39  $this->expectException(IdentifierNotValidException::class);
40  $this->expectExceptionCode(1477082501);
41 
42  $this->getAccessibleMockForAbstractClass(AbstractSection::class, ['', 'foobar']);
43  }
44 
49  {
50  $mock = $this->getAccessibleMockForAbstractClass(AbstractSection::class, ['foobar', 'foobar']);
51  $this->assertInstanceOf(AbstractSection::class, $mock);
52  }
53 
58  {
60  $rootForm = $this->getMockBuilder(FormDefinition::class)
61  ->setMethods(['getRenderingOptions', 'getTypeDefinitions'])
62  ->disableOriginalConstructor()
63  ->getMock();
64  $rootForm
65  ->expects($this->any())
66  ->method('getRenderingOptions')
67  ->willReturn(['skipUnknownElements' => false]);
68  $rootForm
69  ->expects($this->any())
70  ->method('getTypeDefinitions')
71  ->willReturn([]);
72 
73  $mockAbstractSection = $this->getAccessibleMockForAbstractClass(
74  AbstractSection::class,
75  [],
76  '',
77  false,
78  false,
79  true,
80  [
81  'getRootForm'
82  ]
83  );
84 
85  $mockAbstractSection
86  ->expects($this->once())
87  ->method('getRootForm')
88  ->willReturn($rootForm);
89 
90  $this->expectException(TypeDefinitionNotFoundException::class);
91  $this->expectExceptionCode(1382364019);
92 
93  $mockAbstractSection->_call('createElement', '', '');
94  }
95 
100  {
102  $rootForm = $this->getMockBuilder(FormDefinition::class)
103  ->setMethods(['getRenderingOptions', 'getTypeDefinitions'])
104  ->disableOriginalConstructor()
105  ->getMock();
106  $rootForm
107  ->expects($this->any())
108  ->method('getRenderingOptions')
109  ->willReturn(['skipUnknownElements' => true]);
110  $rootForm
111  ->expects($this->any())
112  ->method('getTypeDefinitions')
113  ->willReturn([]);
114 
116  $mockAbstractSection = $this->getAccessibleMockForAbstractClass(
117  AbstractSection::class,
118  [],
119  '',
120  false,
121  false,
122  true,
123  [
124  'getRootForm'
125  ]
126  );
127 
129  $mockAbstractSection
130  ->expects($this->any())
131  ->method('getRootForm')
132  ->willReturn($rootForm);
133 
134  $objectManager = $this->getMockBuilder(ObjectManager::class)
135  ->setMethods(['get'])
136  ->disableOriginalConstructor()
137  ->getMock();
138 
139  $objectManager
140  ->expects($this->any())
141  ->method('get')
142  ->willReturn(new ‪UnknownFormElement('foo', 'bar'));
143 
144  GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager);
145  $result = $mockAbstractSection->createElement('foo', 'bar');
146  GeneralUtility::removeSingletonInstance(ObjectManager::class, $objectManager);
147 
148  $this->assertInstanceOf(UnknownFormElement::class, $result);
149  $this->assertSame('foo', $result->getIdentifier());
150  $this->assertSame('bar', $result->getType());
151  }
152 
157  {
159  $rootForm = $this->getMockBuilder(FormDefinition::class)
160  ->setMethods(['getRenderingOptions', 'getTypeDefinitions'])
161  ->disableOriginalConstructor()
162  ->getMock();
163  $rootForm
164  ->expects($this->any())
165  ->method('getRenderingOptions')
166  ->willReturn(['skipUnknownElements' => true]);
167  $rootForm
168  ->expects($this->any())
169  ->method('getTypeDefinitions')
170  ->willReturn(['foobar' => []]);
171 
173  $mockAbstractSection = $this->getAccessibleMockForAbstractClass(
174  AbstractSection::class,
175  [],
176  '',
177  false,
178  false,
179  true,
180  [
181  'getRootForm'
182  ]
183  );
184 
185  $mockAbstractSection
186  ->expects($this->any())
187  ->method('getRootForm')
188  ->willReturn($rootForm);
189 
190  $this->expectException(TypeDefinitionNotFoundException::class);
191  $this->expectExceptionCode(1325689855);
192 
193  $mockAbstractSection->createElement('id', 'foobar');
194  }
195 
200  {
201  $this->resetSingletonInstances = true;
203  $mockAbstractSection = $this->getAccessibleMockForAbstractClass(
204  AbstractSection::class,
205  [],
206  '',
207  false,
208  false,
209  true,
210  [
211  'getRootForm'
212  ]
213  );
214 
216  $rootForm = $this->getMockBuilder(FormDefinition::class)
217  ->setMethods(['getRenderingOptions', 'getTypeDefinitions'])
218  ->disableOriginalConstructor()
219  ->getMock();
220  $rootForm
221  ->expects($this->any())
222  ->method('getRenderingOptions')
223  ->willReturn([]);
224  $rootForm
225  ->expects($this->any())
226  ->method('getTypeDefinitions')
227  ->willReturn(
228  [
229  'foobar' => [
230  'implementationClassName' => self::class
231  ]
232  ]
233  );
234 
235  $mockAbstractSection
236  ->expects($this->any())
237  ->method('getRootForm')
238  ->willReturn($rootForm);
239 
240  $objectManager = $this->createMock(ObjectManager::class);
241  $objectManager
242  ->method('get')
243  ->with(self::class)
244  ->willReturn($this);
245 
246  GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager);
247 
248  $this->expectException(TypeDefinitionNotValidException::class);
249  $this->expectExceptionCode(1327318156);
250  $mockAbstractSection->createElement('id', 'foobar');
251 
252  GeneralUtility::removeSingletonInstance(ObjectManager::class, $objectManager);
253  }
254 
259  {
260  $implementationMock = $this->getMockForAbstractClass(
261  AbstractFormElement::class,
262  [],
263  '',
264  false,
265  false,
266  true,
267  ['setOptions', 'initializeFormElement']
268  );
269 
270  $typeDefinition = [
271  'foo' => 'bar',
272  'implementationClassName' => get_class($implementationMock),
273  'fizz' => 'buzz'
274  ];
275 
276  $typeDefinitionWithoutImplementationClassName = $typeDefinition;
277  unset($typeDefinitionWithoutImplementationClassName['implementationClassName']);
278 
279  $implementationMock
280  ->expects($this->once())
281  ->method('initializeFormElement');
282 
283  $implementationMock
284  ->expects($this->once())
285  ->method('setOptions')
286  ->with($typeDefinitionWithoutImplementationClassName);
287 
289  $mockAbstractSection = $this->getAccessibleMockForAbstractClass(
290  AbstractSection::class,
291  [],
292  '',
293  false,
294  false,
295  true,
296  [
297  'getRootForm'
298  ]
299  );
300 
302  $rootForm = $this->getMockBuilder(FormDefinition::class)
303  ->setMethods(['getRenderingOptions', 'getTypeDefinitions'])
304  ->disableOriginalConstructor()
305  ->getMock();
306  $rootForm
307  ->expects($this->any())
308  ->method('getRenderingOptions')
309  ->willReturn([]);
310  $rootForm
311  ->expects($this->any())
312  ->method('getTypeDefinitions')
313  ->willReturn(['foobar' => $typeDefinition]);
314 
315  $mockAbstractSection
316  ->expects($this->any())
317  ->method('getRootForm')
318  ->willReturn($rootForm);
319 
320  $objectManager = $this->createMock(ObjectManager::class);
321  $objectManager
322  ->method('get')
323  ->with(get_class($implementationMock))
324  ->willReturn($implementationMock);
325 
326  GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager);
327 
328  $mockAbstractSection->createElement('id', 'foobar');
329 
330  GeneralUtility::removeSingletonInstance(ObjectManager::class, $objectManager);
331  }
332 }
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractFormElement
Definition: AbstractFormElement.php:46
‪TYPO3\CMS\Form\Domain\Exception\TypeDefinitionNotFoundException
Definition: TypeDefinitionNotFoundException.php:27
‪TYPO3\CMS\Form\Tests\Unit\Domain\FormElements\AbstractSectionTest\createElementThrowsExceptionIfTypeDefinitionIsNotSet
‪createElementThrowsExceptionIfTypeDefinitionIsNotSet()
Definition: AbstractSectionTest.php:156
‪TYPO3\CMS\Form\Domain\Exception\IdentifierNotValidException
Definition: IdentifierNotValidException.php:27
‪TYPO3\CMS\Form\Tests\Unit\Domain\FormElements\AbstractSectionTest\createElementThrowsExceptionIfTypeDefinitionNotFoundAndSkipUnknownElementsIsFalse
‪createElementThrowsExceptionIfTypeDefinitionNotFoundAndSkipUnknownElementsIsFalse()
Definition: AbstractSectionTest.php:57
‪TYPO3\CMS\Form\Tests\Unit\Domain\FormElements
Definition: AbstractFormElementTest.php:3
‪TYPO3\CMS\Form\Tests\Unit\Domain\FormElements\AbstractSectionTest\constructThrowsExceptionWhenIdentifierIsEmpty
‪constructThrowsExceptionWhenIdentifierIsEmpty()
Definition: AbstractSectionTest.php:37
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractSection
Definition: AbstractSection.php:40
‪TYPO3\CMS\Form\Tests\Unit\Domain\FormElements\AbstractSectionTest\createElementExpectedToAddAndInitializeElement
‪createElementExpectedToAddAndInitializeElement()
Definition: AbstractSectionTest.php:258
‪TYPO3\CMS\Form\Tests\Unit\Domain\FormElements\AbstractSectionTest\createElementThrowsExceptionIfTypeDefinitionNotInstanceOfFormElementInterface
‪createElementThrowsExceptionIfTypeDefinitionNotInstanceOfFormElementInterface()
Definition: AbstractSectionTest.php:199
‪TYPO3\CMS\Form\Domain\Model\FormElements\UnknownFormElement
Definition: UnknownFormElement.php:30
‪TYPO3\CMS\Form\Tests\Unit\Domain\FormElements\AbstractSectionTest\createElementReturnsUnknownElementsIfTypeDefinitionIsNotFoundAndSkipUnknownElementsIsTrue
‪createElementReturnsUnknownElementsIfTypeDefinitionIsNotFoundAndSkipUnknownElementsIsTrue()
Definition: AbstractSectionTest.php:99
‪TYPO3\CMS\Form\Tests\Unit\Domain\FormElements\AbstractSectionTest
Definition: AbstractSectionTest.php:33
‪TYPO3\CMS\Form\Domain\Model\FormDefinition
Definition: FormDefinition.php:218
‪TYPO3\CMS\Form\Domain\Exception\TypeDefinitionNotValidException
Definition: TypeDefinitionNotValidException.php:27
‪TYPO3\CMS\Form\Tests\Unit\Domain\FormElements\AbstractSectionTest\constructMustNotThrowExceptionWhenIdentifierIsNonEmptyString
‪constructMustNotThrowExceptionWhenIdentifierIsNonEmptyString()
Definition: AbstractSectionTest.php:48
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Extbase\Object\ObjectManager
Definition: ObjectManager.php:25