11d8b442eb4b2090e18d079ee4de6ac9ba911c12
[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\DummyClassWithInjectDoctrineAnnotation::class);
144 static::assertTrue($classSchema->hasInjectProperties());
145
146 $injectProperties = $classSchema->getInjectProperties();
147 static::assertArrayHasKey('propertyWithFullQualifiedClassName', $injectProperties);
148 static::assertSame(Fixture\DummyClassWithInjectDoctrineAnnotation::class, $injectProperties['propertyWithFullQualifiedClassName']);
149
150 static::assertArrayHasKey('propertyWithRelativeClassName', $injectProperties);
151 static::assertSame('DummyClassWithInjectDoctrineAnnotation', $injectProperties['propertyWithRelativeClassName']);
152
153 static::assertArrayHasKey('propertyWithImportedClassName', $injectProperties);
154 static::assertSame('ClassSchemaTest', $injectProperties['propertyWithImportedClassName']);
155
156 static::assertArrayHasKey('propertyWithImportedAndAliasedClassName', $injectProperties);
157 static::assertSame('AliasedClassSchemaTest', $injectProperties['propertyWithImportedAndAliasedClassName']);
158 }
159
160 public function testClassSchemaDetectsInjectMethods()
161 {
162 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfMethods::class);
163 static::assertTrue($classSchema->hasInjectMethods());
164
165 $methodDefinition = $classSchema->getMethod('injectSettings');
166 static::assertFalse($methodDefinition['injectMethod']);
167
168 $methodDefinition = $classSchema->getMethod('injectMethodWithoutParam');
169 static::assertFalse($methodDefinition['injectMethod']);
170
171 $methodDefinition = $classSchema->getMethod('injectMethodThatIsProtected');
172 static::assertFalse($methodDefinition['injectMethod']);
173
174 $methodDefinition = $classSchema->getMethod('injectFoo');
175 static::assertTrue($methodDefinition['injectMethod']);
176
177 $injectMethods = $classSchema->getInjectMethods();
178 static::assertArrayHasKey('injectFoo', $injectMethods);
179 }
180
181 public function testClassSchemaDetectsStaticMethods()
182 {
183 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfMethods::class);
184
185 $methodDefinition = $classSchema->getMethod('staticMethod');
186 static::assertTrue($methodDefinition['static']);
187 }
188
189 public function testClassSchemaDetectsMandatoryParams()
190 {
191 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfMethods::class);
192
193 $methodDefinition = $classSchema->getMethod('methodWithMandatoryParam');
194 static::assertFalse($methodDefinition['params']['param']['optional']);
195 }
196
197 public function testClassSchemaDetectsNullableParams()
198 {
199 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfMethods::class);
200
201 $methodDefinition = $classSchema->getMethod('methodWithNullableParam');
202 static::assertTrue($methodDefinition['params']['param']['nullable']);
203 }
204
205 public function testClassSchemaDetectsDefaultValueParams()
206 {
207 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfMethods::class);
208
209 $methodDefinition = $classSchema->getMethod('methodWithDefaultValueParam');
210 static::assertSame('foo', $methodDefinition['params']['param']['default']);
211 }
212
213 public function testClassSchemaDetectsParamTypeFromTypeHint()
214 {
215 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfMethods::class);
216
217 $methodDefinition = $classSchema->getMethod('methodWithTypeHintedParam');
218 static::assertSame('string', $methodDefinition['params']['param']['type']);
219 }
220
221 public function testClassSchemaDetectsPropertyVisibility()
222 {
223 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class);
224
225 $propertyDefinition = $classSchema->getProperty('publicProperty');
226 static::assertTrue($propertyDefinition['public']);
227 static::assertFalse($propertyDefinition['protected']);
228 static::assertFalse($propertyDefinition['private']);
229
230 $propertyDefinition = $classSchema->getProperty('protectedProperty');
231 static::assertFalse($propertyDefinition['public']);
232 static::assertTrue($propertyDefinition['protected']);
233 static::assertFalse($propertyDefinition['private']);
234
235 $propertyDefinition = $classSchema->getProperty('privateProperty');
236 static::assertFalse($propertyDefinition['public']);
237 static::assertFalse($propertyDefinition['protected']);
238 static::assertTrue($propertyDefinition['private']);
239 }
240
241 public function testClassSchemaDetectsInjectProperty()
242 {
243 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class);
244
245 $propertyDefinition = $classSchema->getProperty('propertyWithInjectAnnotation');
246 static::assertTrue($propertyDefinition['annotations']['inject']);
247 }
248
249 public function testClassSchemaDetectsTransientProperty()
250 {
251 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class);
252
253 $propertyDefinition = $classSchema->getProperty('propertyWithTransientAnnotation');
254 static::assertTrue($propertyDefinition['annotations']['transient']);
255 }
256
257 public function testClassSchemaDetectsCascadeProperty()
258 {
259 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class);
260
261 $propertyDefinition = $classSchema->getProperty('propertyWithCascadeAnnotation');
262 static::assertSame('remove', $propertyDefinition['annotations']['cascade']);
263 }
264
265 public function testClassSchemaDetectsCascadePropertyOnlyWithVarAnnotation()
266 {
267 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class);
268
269 $propertyDefinition = $classSchema->getProperty('propertyWithCascadeAnnotationWithoutVarAnnotation');
270 static::assertNull($propertyDefinition['annotations']['cascade']);
271 }
272
273 public function testClassSchemaDetectsTypeAndElementType()
274 {
275 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class);
276
277 $propertyDefinition = $classSchema->getProperty('propertyWithObjectStorageAnnotation');
278 static::assertSame(ObjectStorage::class, $propertyDefinition['type']);
279 static::assertSame(Fixture\DummyClassWithAllTypesOfProperties::class, $propertyDefinition['elementType']);
280 }
281
282 public function testClassSchemaDetectsSingletons()
283 {
284 static::assertTrue((new ClassSchema(Fixture\DummySingleton::class))->isSingleton());
285 }
286
287 public function testClassSchemaDetectsModels()
288 {
289 static::assertTrue((new ClassSchema(Fixture\DummyEntity::class))->isModel());
290 static::assertTrue((new ClassSchema(Fixture\DummyValueObject::class))->isModel());
291 }
292
293 public function testClassSchemaDetectsEntities()
294 {
295 static::assertTrue((new ClassSchema(Fixture\DummyEntity::class))->isEntity());
296 }
297
298 public function testClassSchemaDetectsValueObjects()
299 {
300 static::assertTrue((new ClassSchema(Fixture\DummyValueObject::class))->isValueObject());
301 }
302
303 public function testClassSchemaDetectsClassName()
304 {
305 static::assertSame(Fixture\DummyModel::class, (new ClassSchema(Fixture\DummyModel::class))->getClassName());
306 }
307
308 public function testClassSchemaDetectsNonStaticProperties()
309 {
310 static::assertTrue((new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class))->hasProperty('publicProperty'));
311 static::assertTrue((new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class))->hasProperty('protectedProperty'));
312 static::assertTrue((new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class))->hasProperty('privateProperty'));
313 }
314
315 public function testClassSchemaDetectsStaticProperties()
316 {
317 static::assertTrue((new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class))->hasProperty('publicStaticProperty'));
318 static::assertTrue((new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class))->hasProperty('protectedStaticProperty'));
319 static::assertTrue((new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class))->hasProperty('privateStaticProperty'));
320 }
321
322 public function testClassSchemaGetTags()
323 {
324 $tags = (new ClassSchema(Fixture\DummyClassWithTags::class))->getTags();
325 static::assertArrayHasKey('foo', $tags);
326
327 // test ignored tags
328 static::assertArrayNotHasKey('package', $tags);
329 static::assertArrayNotHasKey('subpackage', $tags);
330 static::assertArrayNotHasKey('license', $tags);
331 static::assertArrayNotHasKey('copyright', $tags);
332 static::assertArrayNotHasKey('author', $tags);
333 static::assertArrayNotHasKey('version', $tags);
334 static::assertArrayNotHasKey('const', $tags);
335 }
336 }