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