31bb0ab59b9178afa6d0a1fa6e82f95c2f0d90a7
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / UnitDeprecated / Reflection / ClassSchemaTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\UnitDeprecated\Reflection;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Extbase\Reflection\ClassSchema;
18 use TYPO3\CMS\Extbase\Validation\Exception\InvalidTypeHintException;
19 use TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationConfigurationException;
20 use TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator;
21 use TYPO3\CMS\Extbase\Validation\Validator\StringLengthValidator;
22 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
23
24 /**
25 * Test case
26 */
27 class ClassSchemaTest extends UnitTestCase
28 {
29 /**
30 * @var bool Reset singletons created by subject
31 */
32 protected $resetSingletonInstances = true;
33
34 public function testClassSchemaDetectsInjectProperties()
35 {
36 $classSchema = new ClassSchema(Fixture\DummyClassWithInjectProperty::class);
37 static::assertTrue($classSchema->hasInjectProperties());
38
39 $propertyDefinition = $classSchema->getProperty('propertyWithInjectAnnotation');
40 static::assertTrue($propertyDefinition['annotations']['inject']);
41
42 $injectProperties = $classSchema->getInjectProperties();
43 static::assertArrayHasKey('propertyWithInjectAnnotation', $injectProperties);
44 }
45
46 public function testClassSchemaDetectsLazyProperties()
47 {
48 $classSchema = new ClassSchema(Fixture\DummyClassWithLazyProperty::class);
49 static::assertTrue($classSchema->getProperty('propertyWithLazyAnnotation')['annotations']['lazy']);
50 }
51
52 public function testClassSchemaDetectsCascadeProperty()
53 {
54 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class);
55
56 $propertyDefinition = $classSchema->getProperty('propertyWithCascadeAnnotation');
57 static::assertSame('remove', $propertyDefinition['annotations']['cascade']);
58 }
59
60 public function testClassSchemaDetectsCascadePropertyOnlyWithVarAnnotation()
61 {
62 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class);
63
64 $propertyDefinition = $classSchema->getProperty('propertyWithCascadeAnnotationWithoutVarAnnotation');
65 static::assertNull($propertyDefinition['annotations']['cascade']);
66 }
67
68 public function testClassSchemaDetectsIgnoreValidationAnnotation()
69 {
70 $classSchema = new ClassSchema(Fixture\DummyControllerWithIgnorevalidationAnnotation::class);
71 static::assertTrue(isset($classSchema->getMethod('someAction')['tags']['ignorevalidation']));
72 }
73
74 /**
75 * @test
76 */
77 public function classSchemaDetectsValidateAnnotationsModelProperties(): void
78 {
79 $classSchema = new ClassSchema(Fixture\DummyModelWithValidateAnnotation::class);
80
81 static::assertSame(
82 [
83 [
84 'name' => 'StringLength',
85 'options' => [
86 'minimum' => '1',
87 'maximum' => '10',
88 ],
89 'className' => StringLengthValidator::class
90 ],
91 [
92 'name' => 'NotEmpty',
93 'options' => [],
94 'className' => NotEmptyValidator::class
95 ],
96 [
97 'name' => 'TYPO3.CMS.Extbase:NotEmpty',
98 'options' => [],
99 'className' => NotEmptyValidator::class
100 ],
101 [
102 'name' => 'TYPO3.CMS.Extbase.Tests.UnitDeprecated.Reflection.Fixture:DummyValidator',
103 'options' => [],
104 'className' => Fixture\Validation\Validator\DummyValidator::class
105 ],
106 [
107 'name' => '\TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator',
108 'options' => [],
109 'className' => NotEmptyValidator::class
110 ],
111 [
112 'name' => NotEmptyValidator::class,
113 'options' => [],
114 'className' => NotEmptyValidator::class
115 ]
116 ],
117 $classSchema->getProperty('propertyWithValidateAnnotations')['validators']
118 );
119 }
120
121 /**
122 * @test
123 */
124 public function classSchemaDetectsValidateAnnotationsOfControllerActions(): void
125 {
126 $classSchema = new ClassSchema(Fixture\DummyControllerWithValidateAnnotations::class);
127
128 static::assertSame(
129 [
130 [
131 'name' => 'StringLength',
132 'options' => [
133 'minimum' => '1',
134 'maximum' => '10',
135 ],
136 'className' => StringLengthValidator::class
137 ],
138 [
139 'name' => 'NotEmpty',
140 'options' => [],
141 'className' => NotEmptyValidator::class
142 ],
143 [
144 'name' => 'TYPO3.CMS.Extbase:NotEmpty',
145 'options' => [],
146 'className' => NotEmptyValidator::class
147 ],
148 [
149 'name' => 'TYPO3.CMS.Extbase.Tests.UnitDeprecated.Reflection.Fixture:DummyValidator',
150 'options' => [],
151 'className' => Fixture\Validation\Validator\DummyValidator::class
152 ],
153 [
154 'name' => '\TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator',
155 'options' => [],
156 'className' => NotEmptyValidator::class
157 ],
158 [
159 'name' => NotEmptyValidator::class,
160 'options' => [],
161 'className' => NotEmptyValidator::class
162 ]
163 ],
164 $classSchema->getMethod('methodWithValidateAnnotationsAction')['params']['fooParam']['validators']
165 );
166 }
167
168 /**
169 * @test
170 */
171 public function classSchemaGenerationThrowsExceptionWithValidateAnnotationsForParamWithoutTypeHint(): void
172 {
173 $this->expectException(InvalidTypeHintException::class);
174 $this->expectExceptionMessage('Missing type information for parameter "$fooParam" in TYPO3\CMS\Extbase\Tests\UnitDeprecated\Reflection\Fixture\DummyControllerWithValidateAnnotationWithoutParamTypeHint->methodWithValidateAnnotationsAction(): Either use an @param annotation or use a type hint.');
175 $this->expectExceptionCode(1515075192);
176
177 new ClassSchema(Fixture\DummyControllerWithValidateAnnotationWithoutParamTypeHint::class);
178 }
179
180 /**
181 * @test
182 */
183 public function classSchemaGenerationThrowsExceptionWithValidateAnnotationsForMissingParam(): void
184 {
185 $this->expectException(InvalidValidationConfigurationException::class);
186 $this->expectExceptionMessage('Invalid validate annotation in TYPO3\CMS\Extbase\Tests\UnitDeprecated\Reflection\Fixture\DummyControllerWithValidateAnnotationWithoutParam->methodWithValidateAnnotationsAction(): The following validators have been defined for missing param "$fooParam": NotEmpty, StringLength');
187 $this->expectExceptionCode(1515073585);
188
189 new ClassSchema(Fixture\DummyControllerWithValidateAnnotationWithoutParam::class);
190 }
191 }