‪TYPO3CMS  10.4
AbstractSectionTest.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 
30 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
31 
35 class ‪AbstractSectionTest extends UnitTestCase
36 {
37  protected ‪$resetSingletonInstances = true;
38 
43  {
44  $this->expectException(IdentifierNotValidException::class);
45  $this->expectExceptionCode(1477082501);
46 
47  // Section inherits from AbstractSection and serves as concrete implementation
48  new ‪Section('', 'foobar');
49  }
50 
55  {
56  $section = new ‪Section('foobar', 'foobar');
57  self::assertInstanceOf(AbstractSection::class, $section);
58  }
59 
64  {
66  $rootForm = $this->getMockBuilder(FormDefinition::class)
67  ->setMethods(['getRenderingOptions', 'getTypeDefinitions'])
68  ->disableOriginalConstructor()
69  ->getMock();
70  $rootForm
71  ->method('getRenderingOptions')
72  ->willReturn(['skipUnknownElements' => false]);
73  $rootForm
74  ->method('getTypeDefinitions')
75  ->willReturn([]);
76 
77  $mockAbstractSection = $this->getAccessibleMockForAbstractClass(
78  AbstractSection::class,
79  [],
80  '',
81  false,
82  false,
83  true,
84  [
85  'getRootForm'
86  ]
87  );
88 
89  $mockAbstractSection
90  ->expects(self::once())
91  ->method('getRootForm')
92  ->willReturn($rootForm);
93 
94  $this->expectException(TypeDefinitionNotFoundException::class);
95  $this->expectExceptionCode(1382364019);
96 
97  $mockAbstractSection->_call('createElement', '', '');
98  }
99 
104  {
106  $rootForm = $this->getMockBuilder(FormDefinition::class)
107  ->setMethods(['getRenderingOptions', 'getTypeDefinitions'])
108  ->disableOriginalConstructor()
109  ->getMock();
110  $rootForm
111  ->method('getRenderingOptions')
112  ->willReturn(['skipUnknownElements' => true]);
113  $rootForm
114  ->method('getTypeDefinitions')
115  ->willReturn([]);
116 
118  $mockAbstractSection = $this->getMockForAbstractClass(
119  AbstractSection::class,
120  [],
121  '',
122  false,
123  false,
124  true,
125  [
126  'getRootForm'
127  ]
128  );
129 
131  $mockAbstractSection
132  ->method('getRootForm')
133  ->willReturn($rootForm);
134 
135  $objectManager = $this->prophesize(ObjectManager::class);
136  $objectManager->get(UnknownFormElement::class, 'foo', 'bar')->willReturn(new ‪UnknownFormElement('foo', 'bar'));
137 
138  GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager->reveal());
139  $result = $mockAbstractSection->createElement('foo', 'bar');
140 
141  self::assertInstanceOf(UnknownFormElement::class, $result);
142  self::assertSame('foo', $result->getIdentifier());
143  self::assertSame('bar', $result->getType());
144  }
145 
150  {
152  $rootForm = $this->getMockBuilder(FormDefinition::class)
153  ->setMethods(['getRenderingOptions', 'getTypeDefinitions'])
154  ->disableOriginalConstructor()
155  ->getMock();
156  $rootForm
157  ->method('getRenderingOptions')
158  ->willReturn(['skipUnknownElements' => true]);
159  $rootForm
160  ->method('getTypeDefinitions')
161  ->willReturn(['foobar' => []]);
162 
164  $mockAbstractSection = $this->getMockForAbstractClass(
165  AbstractSection::class,
166  [],
167  '',
168  false,
169  false,
170  true,
171  [
172  'getRootForm'
173  ]
174  );
175 
176  $mockAbstractSection
177  ->method('getRootForm')
178  ->willReturn($rootForm);
179 
180  $this->expectException(TypeDefinitionNotFoundException::class);
181  $this->expectExceptionCode(1325689855);
182 
183  $mockAbstractSection->createElement('id', 'foobar');
184  }
185 
190  {
191  $this->resetSingletonInstances = true;
193  $mockAbstractSection = $this->getMockForAbstractClass(
194  AbstractSection::class,
195  [],
196  '',
197  false,
198  false,
199  true,
200  [
201  'getRootForm'
202  ]
203  );
204 
206  $rootForm = $this->getMockBuilder(FormDefinition::class)
207  ->setMethods(['getRenderingOptions', 'getTypeDefinitions'])
208  ->disableOriginalConstructor()
209  ->getMock();
210  $rootForm
211  ->method('getRenderingOptions')
212  ->willReturn([]);
213  $rootForm
214  ->method('getTypeDefinitions')
215  ->willReturn(
216  [
217  'foobar' => [
218  'implementationClassName' => self::class
219  ]
220  ]
221  );
222 
223  $mockAbstractSection
224  ->method('getRootForm')
225  ->willReturn($rootForm);
226 
227  $objectManager = $this->prophesize(ObjectManager::class);
228  $objectManager->get(self::class, 'id', 'foobar')->willReturn($this);
229  GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager->reveal());
230 
231  $this->expectException(TypeDefinitionNotValidException::class);
232  $this->expectExceptionCode(1327318156);
233  $mockAbstractSection->createElement('id', 'foobar');
234  }
235 
240  {
241  $implementationMock = $this->getMockForAbstractClass(
242  AbstractFormElement::class,
243  [],
244  '',
245  false,
246  false,
247  true,
248  ['setOptions', 'initializeFormElement']
249  );
250 
251  $typeDefinition = [
252  'foo' => 'bar',
253  'implementationClassName' => get_class($implementationMock),
254  'fizz' => 'buzz'
255  ];
256 
257  $typeDefinitionWithoutImplementationClassName = $typeDefinition;
258  unset($typeDefinitionWithoutImplementationClassName['implementationClassName']);
259 
260  $implementationMock
261  ->expects(self::once())
262  ->method('initializeFormElement');
263 
264  $implementationMock
265  ->expects(self::once())
266  ->method('setOptions')
267  ->with($typeDefinitionWithoutImplementationClassName);
268 
270  $mockAbstractSection = $this->getMockForAbstractClass(
271  AbstractSection::class,
272  [],
273  '',
274  false,
275  false,
276  true,
277  [
278  'getRootForm'
279  ]
280  );
281 
283  $rootForm = $this->getMockBuilder(FormDefinition::class)
284  ->setMethods(['getRenderingOptions', 'getTypeDefinitions'])
285  ->disableOriginalConstructor()
286  ->getMock();
287  $rootForm
288  ->method('getRenderingOptions')
289  ->willReturn([]);
290  $rootForm
291  ->method('getTypeDefinitions')
292  ->willReturn(['foobar' => $typeDefinition]);
293 
294  $mockAbstractSection
295  ->method('getRootForm')
296  ->willReturn($rootForm);
297 
298  $objectManager = $this->prophesize(ObjectManager::class);
299  $objectManager->get(get_class($implementationMock), 'id', 'foobar')->willReturn($implementationMock);
300 
301  GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager->reveal());
302 
303  $mockAbstractSection->createElement('id', 'foobar');
304  }
305 }
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractFormElement
Definition: AbstractFormElement.php:50
‪TYPO3\CMS\Form\Domain\Exception\TypeDefinitionNotFoundException
Definition: TypeDefinitionNotFoundException.php:31
‪TYPO3\CMS\Form\Tests\Unit\Domain\FormElements\AbstractSectionTest\createElementThrowsExceptionIfTypeDefinitionIsNotSet
‪createElementThrowsExceptionIfTypeDefinitionIsNotSet()
Definition: AbstractSectionTest.php:149
‪TYPO3\CMS\Form\Domain\Exception\IdentifierNotValidException
Definition: IdentifierNotValidException.php:31
‪TYPO3\CMS\Form\Domain\Model\FormElements\Section
Definition: Section.php:39
‪TYPO3\CMS\Form\Tests\Unit\Domain\FormElements\AbstractSectionTest\createElementThrowsExceptionIfTypeDefinitionNotFoundAndSkipUnknownElementsIsFalse
‪createElementThrowsExceptionIfTypeDefinitionNotFoundAndSkipUnknownElementsIsFalse()
Definition: AbstractSectionTest.php:63
‪TYPO3\CMS\Form\Tests\Unit\Domain\FormElements\AbstractSectionTest\$resetSingletonInstances
‪$resetSingletonInstances
Definition: AbstractSectionTest.php:37
‪TYPO3\CMS\Form\Tests\Unit\Domain\FormElements
Definition: AbstractFormElementTest.php:18
‪TYPO3\CMS\Form\Tests\Unit\Domain\FormElements\AbstractSectionTest\constructThrowsExceptionWhenIdentifierIsEmpty
‪constructThrowsExceptionWhenIdentifierIsEmpty()
Definition: AbstractSectionTest.php:42
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractSection
Definition: AbstractSection.php:44
‪TYPO3\CMS\Form\Tests\Unit\Domain\FormElements\AbstractSectionTest\createElementExpectedToAddAndInitializeElement
‪createElementExpectedToAddAndInitializeElement()
Definition: AbstractSectionTest.php:239
‪TYPO3\CMS\Form\Tests\Unit\Domain\FormElements\AbstractSectionTest\createElementThrowsExceptionIfTypeDefinitionNotInstanceOfFormElementInterface
‪createElementThrowsExceptionIfTypeDefinitionNotInstanceOfFormElementInterface()
Definition: AbstractSectionTest.php:189
‪TYPO3\CMS\Form\Domain\Model\FormElements\UnknownFormElement
Definition: UnknownFormElement.php:34
‪TYPO3\CMS\Form\Tests\Unit\Domain\FormElements\AbstractSectionTest\createElementReturnsUnknownElementsIfTypeDefinitionIsNotFoundAndSkipUnknownElementsIsTrue
‪createElementReturnsUnknownElementsIfTypeDefinitionIsNotFoundAndSkipUnknownElementsIsTrue()
Definition: AbstractSectionTest.php:103
‪TYPO3\CMS\Form\Tests\Unit\Domain\FormElements\AbstractSectionTest
Definition: AbstractSectionTest.php:36
‪TYPO3\CMS\Form\Domain\Model\FormDefinition
Definition: FormDefinition.php:223
‪TYPO3\CMS\Form\Domain\Exception\TypeDefinitionNotValidException
Definition: TypeDefinitionNotValidException.php:31
‪TYPO3\CMS\Form\Tests\Unit\Domain\FormElements\AbstractSectionTest\constructMustNotThrowExceptionWhenIdentifierIsNonEmptyString
‪constructMustNotThrowExceptionWhenIdentifierIsNonEmptyString()
Definition: AbstractSectionTest.php:54
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Extbase\Object\ObjectManager
Definition: ObjectManager.php:28