-
Notifications
You must be signed in to change notification settings - Fork 638
/
AbstractSectionTest.php
125 lines (113 loc) · 5.26 KB
/
AbstractSectionTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\Form\Tests\Unit\Domain\FormElements;
use PHPUnit\Framework\Attributes\Test;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Form\Domain\Exception\IdentifierNotValidException;
use TYPO3\CMS\Form\Domain\Exception\TypeDefinitionNotFoundException;
use TYPO3\CMS\Form\Domain\Exception\TypeDefinitionNotValidException;
use TYPO3\CMS\Form\Domain\Model\FormDefinition;
use TYPO3\CMS\Form\Domain\Model\FormElements\AbstractSection;
use TYPO3\CMS\Form\Domain\Model\FormElements\UnknownFormElement;
use TYPO3\CMS\Form\Tests\Unit\Domain\FormElements\Fixtures\AbstractSectionFixture;
use TYPO3\CMS\Form\Tests\Unit\Domain\FormElements\Fixtures\TestingFormElement;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
final class AbstractSectionTest extends UnitTestCase
{
protected bool $resetSingletonInstances = true;
#[Test]
public function constructThrowsExceptionWhenIdentifierIsEmpty(): void
{
$this->expectException(IdentifierNotValidException::class);
$this->expectExceptionCode(1477082501);
new AbstractSectionFixture('', 'foobar');
}
#[Test]
public function constructMustNotThrowExceptionWhenIdentifierIsNonEmptyString(): void
{
$subject = new AbstractSectionFixture('foobar', '');
self::assertInstanceOf(AbstractSection::class, $subject);
}
#[Test]
public function createElementThrowsExceptionIfTypeDefinitionNotFoundAndSkipUnknownElementsIsFalse(): void
{
$rootForm = $this->createMock(FormDefinition::class);
$this->expectException(TypeDefinitionNotFoundException::class);
$this->expectExceptionCode(1382364019);
$subject = new AbstractSectionFixture('identifier', '');
$subject->setParentRenderable($rootForm);
$subject->createElement('', '');
}
#[Test]
public function createElementReturnsUnknownElementsIfTypeDefinitionIsNotFoundAndSkipUnknownElementsIsTrue(): void
{
$rootForm = $this->createMock(FormDefinition::class);
$rootForm->method('getRenderingOptions')->willReturn(['skipUnknownElements' => true]);
GeneralUtility::addInstance(UnknownFormElement::class, new UnknownFormElement('foo', 'bar'));
$subject = new AbstractSectionFixture('testing', '');
$subject->setParentRenderable($rootForm);
$result = $subject->createElement('foo', 'bar');
self::assertInstanceOf(UnknownFormElement::class, $result);
self::assertSame('foo', $result->getIdentifier());
self::assertSame('bar', $result->getType());
}
#[Test]
public function createElementThrowsExceptionIfTypeDefinitionIsNotSet(): void
{
$this->expectException(TypeDefinitionNotFoundException::class);
$this->expectExceptionCode(1325689855);
$rootForm = $this->createMock(FormDefinition::class);
$rootForm->method('getTypeDefinitions')->willReturn(['foobar' => []]);
$subject = new AbstractSectionFixture('testing', '');
$subject->setParentRenderable($rootForm);
$subject->createElement('id', 'foobar');
}
#[Test]
public function createElementThrowsExceptionIfTypeDefinitionNotInstanceOfFormElementInterface(): void
{
$this->expectException(TypeDefinitionNotValidException::class);
$this->expectExceptionCode(1327318156);
$rootForm = $this->createMock(FormDefinition::class);
$rootForm->method('getTypeDefinitions')->willReturn([
'foobar' => [
'implementationClassName' => self::class,
],
]);
$subject = new AbstractSectionFixture('testing', '');
$subject->setParentRenderable($rootForm);
GeneralUtility::addInstance(self::class, $this);
$subject->createElement('id', 'foobar');
}
#[Test]
public function createElementExpectedToAddAndInitializeElement(): void
{
$implementationMock = $this->createMock(TestingFormElement::class);
$typeDefinition = [
'foo' => 'bar',
'implementationClassName' => get_class($implementationMock),
'fizz' => 'buzz',
];
$typeDefinitionWithoutImplementationClassName = $typeDefinition;
unset($typeDefinitionWithoutImplementationClassName['implementationClassName']);
$implementationMock->expects(self::once())->method('initializeFormElement');
$implementationMock->expects(self::once())->method('setOptions')->with($typeDefinitionWithoutImplementationClassName);
$rootForm = $this->createMock(FormDefinition::class);
$rootForm->method('getTypeDefinitions')->willReturn(['foobar' => $typeDefinition]);
$subject = new AbstractSectionFixture('testing', '');
$subject->setParentRenderable($rootForm);
GeneralUtility::addInstance(get_class($implementationMock), $implementationMock);
$subject->createElement('id', 'foobar');
}
}