[FOLLOWUP][!!!][TASK] Replace ClassInfo with ClassSchema
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Reflection / ClassSchemaTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\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\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
19 use TYPO3\CMS\Extbase\Reflection\ClassSchema;
20
21 /**
22 * Test case
23 */
24 class ClassSchemaTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
25 {
26 /**
27 * @test
28 */
29 public function classSchemaForModelIsSetAggregateRootIfRepositoryClassIsFoundForNamespacedClasses()
30 {
31 /** @var \TYPO3\CMS\Extbase\Reflection\ReflectionService $service */
32 $service = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Reflection\ReflectionService::class);
33 $classSchema = $service->getClassSchema(Fixture\DummyModel::class);
34 $this->assertTrue($classSchema->isAggregateRoot());
35 }
36
37 public function testClassSchemaHasConstructor()
38 {
39 $classSchema = new ClassSchema(Fixture\DummyClassWithConstructorAndConstructorArguments::class);
40 static::assertTrue($classSchema->hasConstructor());
41 }
42
43 public function testClassSchemaDetectsConstructorArguments()
44 {
45 $classSchema = new ClassSchema(Fixture\DummyClassWithConstructorAndConstructorArguments::class);
46 static::assertTrue($classSchema->hasConstructor());
47
48 $constructorArguments = $classSchema->getConstructorArguments();
49 static::assertArrayHasKey('foo', $constructorArguments);
50 static::assertArrayHasKey('bar', $constructorArguments);
51
52 $classSchema = new ClassSchema(Fixture\DummyClassWithConstructorAndWithoutConstructorArguments::class);
53 static::assertTrue($classSchema->hasConstructor());
54 static::assertSame([], $classSchema->getConstructorArguments());
55
56 $classSchema = new ClassSchema(Fixture\DummyClassWithoutConstructor::class);
57 static::assertFalse($classSchema->hasConstructor());
58 static::assertSame([], $classSchema->getConstructorArguments());
59 }
60
61 public function testClassSchemaDetectsConstructorArgumentsWithDependencies()
62 {
63 $classSchema = new ClassSchema(Fixture\DummyClassWithConstructorAndConstructorArgumentsWithDependencies::class);
64 static::assertTrue($classSchema->hasConstructor());
65
66 $methodDefinition = $classSchema->getMethod('__construct');
67 static::assertArrayHasKey('foo', $methodDefinition['params']);
68 static::assertSame(Fixture\DummyClassWithGettersAndSetters::class, $methodDefinition['params']['foo']['dependency']);
69 }
70
71 public function testClassSchemaGetProperties()
72 {
73 static::assertSame(
74 [
75 'publicProperty',
76 'protectedProperty',
77 'privateProperty',
78 'publicStaticProperty',
79 'protectedStaticProperty',
80 'privateStaticProperty',
81 'propertyWithIgnoredTags',
82 'propertyWithInjectAnnotation',
83 'propertyWithTransientAnnotation',
84 'propertyWithCascadeAnnotation',
85 'propertyWithCascadeAnnotationWithoutVarAnnotation',
86 'propertyWithObjectStorageAnnotation'
87 ],
88 array_keys((new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class))->getProperties())
89 );
90 }
91
92 public function testClassSchemaHasMethod()
93 {
94 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfMethods::class);
95 static::assertTrue($classSchema->hasMethod('publicMethod'));
96 static::assertFalse($classSchema->hasMethod('nonExistentMethod'));
97 }
98
99 public function testClassSchemaGetMethods()
100 {
101 static::assertSame(
102 [
103 'publicMethod',
104 'protectedMethod',
105 'privateMethod',
106 'methodWithIgnoredTags',
107 'injectSettings',
108 'injectMethodWithoutParam',
109 'injectMethodThatIsProtected',
110 'injectFoo',
111 'staticMethod',
112 'methodWithMandatoryParam',
113 'methodWithNullableParam',
114 'methodWithDefaultValueParam',
115 'methodWithTypeHintedParam'
116 ],
117 array_keys((new ClassSchema(Fixture\DummyClassWithAllTypesOfMethods::class))->getMethods())
118 );
119 }
120
121 public function testClassSchemaDetectsMethodVisibility()
122 {
123 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfMethods::class);
124
125 $methodDefinition = $classSchema->getMethod('publicMethod');
126 static::assertTrue($methodDefinition['public']);
127 static::assertFalse($methodDefinition['protected']);
128 static::assertFalse($methodDefinition['private']);
129
130 $methodDefinition = $classSchema->getMethod('protectedMethod');
131 static::assertFalse($methodDefinition['public']);
132 static::assertTrue($methodDefinition['protected']);
133 static::assertFalse($methodDefinition['private']);
134
135 $methodDefinition = $classSchema->getMethod('privateMethod');
136 static::assertFalse($methodDefinition['public']);
137 static::assertFalse($methodDefinition['protected']);
138 static::assertTrue($methodDefinition['private']);
139 }
140
141 public function testClassSchemaDetectsInjectProperties()
142 {
143 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class);
144 static::assertTrue($classSchema->hasInjectProperties());
145
146 $propertyDefinition = $classSchema->getProperty('propertyWithInjectAnnotation');
147 static::assertTrue($propertyDefinition['annotations']['inject']);
148
149 $injectProperties = $classSchema->getInjectProperties();
150 static::assertArrayHasKey('propertyWithInjectAnnotation', $injectProperties);
151 }
152
153 public function testClassSchemaDetectsInjectMethods()
154 {
155 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfMethods::class);
156 static::assertTrue($classSchema->hasInjectMethods());
157
158 $methodDefinition = $classSchema->getMethod('injectSettings');
159 static::assertFalse($methodDefinition['injectMethod']);
160
161 $methodDefinition = $classSchema->getMethod('injectMethodWithoutParam');
162 static::assertFalse($methodDefinition['injectMethod']);
163
164 $methodDefinition = $classSchema->getMethod('injectMethodThatIsProtected');
165 static::assertFalse($methodDefinition['injectMethod']);
166
167 $methodDefinition = $classSchema->getMethod('injectFoo');
168 static::assertTrue($methodDefinition['injectMethod']);
169
170 $injectMethods = $classSchema->getInjectMethods();
171 static::assertArrayHasKey('injectFoo', $injectMethods);
172 }
173
174 public function testClassSchemaDetectsStaticMethods()
175 {
176 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfMethods::class);
177
178 $methodDefinition = $classSchema->getMethod('staticMethod');
179 static::assertTrue($methodDefinition['static']);
180 }
181
182 public function testClassSchemaDetectsMandatoryParams()
183 {
184 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfMethods::class);
185
186 $methodDefinition = $classSchema->getMethod('methodWithMandatoryParam');
187 static::assertFalse($methodDefinition['params']['param']['optional']);
188 }
189
190 public function testClassSchemaDetectsNullableParams()
191 {
192 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfMethods::class);
193
194 $methodDefinition = $classSchema->getMethod('methodWithNullableParam');
195 static::assertTrue($methodDefinition['params']['param']['nullable']);
196 }
197
198 public function testClassSchemaDetectsDefaultValueParams()
199 {
200 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfMethods::class);
201
202 $methodDefinition = $classSchema->getMethod('methodWithDefaultValueParam');
203 static::assertSame('foo', $methodDefinition['params']['param']['default']);
204 }
205
206 public function testClassSchemaDetectsParamTypeFromTypeHint()
207 {
208 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfMethods::class);
209
210 $methodDefinition = $classSchema->getMethod('methodWithTypeHintedParam');
211 static::assertSame('string', $methodDefinition['params']['param']['type']);
212 }
213
214 public function testClassSchemaDetectsPropertyVisibility()
215 {
216 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class);
217
218 $propertyDefinition = $classSchema->getProperty('publicProperty');
219 static::assertTrue($propertyDefinition['public']);
220 static::assertFalse($propertyDefinition['protected']);
221 static::assertFalse($propertyDefinition['private']);
222
223 $propertyDefinition = $classSchema->getProperty('protectedProperty');
224 static::assertFalse($propertyDefinition['public']);
225 static::assertTrue($propertyDefinition['protected']);
226 static::assertFalse($propertyDefinition['private']);
227
228 $propertyDefinition = $classSchema->getProperty('privateProperty');
229 static::assertFalse($propertyDefinition['public']);
230 static::assertFalse($propertyDefinition['protected']);
231 static::assertTrue($propertyDefinition['private']);
232 }
233
234 public function testClassSchemaDetectsInjectProperty()
235 {
236 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class);
237
238 $propertyDefinition = $classSchema->getProperty('propertyWithInjectAnnotation');
239 static::assertTrue($propertyDefinition['annotations']['inject']);
240 }
241
242 public function testClassSchemaDetectsTransientProperty()
243 {
244 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class);
245
246 $propertyDefinition = $classSchema->getProperty('propertyWithTransientAnnotation');
247 static::assertTrue($propertyDefinition['annotations']['transient']);
248 }
249
250 public function testClassSchemaDetectsCascadeProperty()
251 {
252 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class);
253
254 $propertyDefinition = $classSchema->getProperty('propertyWithCascadeAnnotation');
255 static::assertSame('remove', $propertyDefinition['annotations']['cascade']);
256 }
257
258 public function testClassSchemaDetectsCascadePropertyOnlyWithVarAnnotation()
259 {
260 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class);
261
262 $propertyDefinition = $classSchema->getProperty('propertyWithCascadeAnnotationWithoutVarAnnotation');
263 static::assertNull($propertyDefinition['annotations']['cascade']);
264 }
265
266 public function testClassSchemaDetectsTypeAndElementType()
267 {
268 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class);
269
270 $propertyDefinition = $classSchema->getProperty('propertyWithObjectStorageAnnotation');
271 static::assertSame(ObjectStorage::class, $propertyDefinition['type']);
272 static::assertSame(Fixture\DummyClassWithAllTypesOfProperties::class, $propertyDefinition['elementType']);
273 }
274
275 public function testClassSchemaDetectsSingletons()
276 {
277 static::assertTrue((new ClassSchema(Fixture\DummySingleton::class))->isSingleton());
278 }
279
280 public function testClassSchemaDetectsModels()
281 {
282 static::assertTrue((new ClassSchema(Fixture\DummyEntity::class))->isModel());
283 static::assertTrue((new ClassSchema(Fixture\DummyValueObject::class))->isModel());
284 }
285
286 public function testClassSchemaDetectsEntities()
287 {
288 static::assertTrue((new ClassSchema(Fixture\DummyEntity::class))->isEntity());
289 }
290
291 public function testClassSchemaDetectsValueObjects()
292 {
293 static::assertTrue((new ClassSchema(Fixture\DummyValueObject::class))->isValueObject());
294 }
295
296 public function testClassSchemaDetectsClassName()
297 {
298 static::assertSame(Fixture\DummyModel::class, (new ClassSchema(Fixture\DummyModel::class))->getClassName());
299 }
300
301 public function testClassSchemaDetectsNonStaticProperties()
302 {
303 static::assertTrue((new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class))->hasProperty('publicProperty'));
304 static::assertTrue((new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class))->hasProperty('protectedProperty'));
305 static::assertTrue((new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class))->hasProperty('privateProperty'));
306 }
307
308 public function testClassSchemaDetectsStaticProperties()
309 {
310 static::assertTrue((new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class))->hasProperty('publicStaticProperty'));
311 static::assertTrue((new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class))->hasProperty('protectedStaticProperty'));
312 static::assertTrue((new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class))->hasProperty('privateStaticProperty'));
313 }
314
315 public function testClassSchemaGetTags()
316 {
317 $tags = (new ClassSchema(Fixture\DummyClassWithTags::class))->getTags();
318 static::assertArrayHasKey('foo', $tags);
319
320 // test ignored tags
321 static::assertArrayNotHasKey('package', $tags);
322 static::assertArrayNotHasKey('subpackage', $tags);
323 static::assertArrayNotHasKey('license', $tags);
324 static::assertArrayNotHasKey('copyright', $tags);
325 static::assertArrayNotHasKey('author', $tags);
326 static::assertArrayNotHasKey('version', $tags);
327 static::assertArrayNotHasKey('const', $tags);
328 }
329 }