‪TYPO3CMS  10.4
ClassSchemaTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
33 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
34 
38 class ‪ClassSchemaTest extends UnitTestCase
39 {
44  {
45  $this->resetSingletonInstances = true;
46  $service = GeneralUtility::makeInstance(ReflectionService::class);
47  $classSchema = $service->getClassSchema(DummyModel::class);
48  self::assertTrue($classSchema->isAggregateRoot());
49  }
50 
54  public function ‪classSchemaHasConstructor()
55  {
56  $classSchema = new ‪ClassSchema(DummyClassWithConstructorAndConstructorArguments::class);
57  self::assertTrue($classSchema->hasConstructor());
58  }
59 
63  public function ‪classSchemaGetProperties()
64  {
65  self::assertSame(
66  [
67  'publicProperty',
68  'protectedProperty',
69  'privateProperty',
70  'publicStaticProperty',
71  'protectedStaticProperty',
72  'privateStaticProperty',
73  'publicPropertyWithDefaultValue',
74  'publicStaticPropertyWithDefaultValue',
75  'propertyWithIgnoredTags',
76  'propertyWithInjectAnnotation',
77  'propertyWithTransientAnnotation',
78  'propertyWithCascadeAnnotation',
79  'propertyWithCascadeAnnotationWithoutVarAnnotation',
80  'propertyWithObjectStorageAnnotation',
81  'propertyWithObjectStorageAnnotationWithoutFQCN',
82  'uid',
83  '_localizedUid',
84  '_languageUid',
85  '_versionedUid',
86  'pid',
87  ],
88  array_keys((new ‪ClassSchema(DummyClassWithAllTypesOfProperties::class))->getProperties())
89  );
90  }
91 
95  public function ‪classSchemaHasMethod()
96  {
97  $classSchema = new ‪ClassSchema(DummyClassWithAllTypesOfMethods::class);
98  self::assertTrue($classSchema->hasMethod('publicMethod'));
99  self::assertFalse($classSchema->hasMethod('nonExistentMethod'));
100  }
101 
105  public function ‪classSchemaGetMethods()
106  {
107  self::assertSame(
108  [
109  'publicMethod',
110  'protectedMethod',
111  'privateMethod',
112  'methodWithIgnoredTags',
113  'injectSettings',
114  'injectMethodWithoutParam',
115  'injectMethodThatIsProtected',
116  'injectFoo',
117  'staticMethod',
118  'methodWithMandatoryParam',
119  'methodWithNullableParam',
120  'methodWithDefaultValueParam',
121  'methodWithTypeHintedParam',
122  'methodWithDocBlockTypeHintOnly',
123  ],
124  array_keys((new ‪ClassSchema(DummyClassWithAllTypesOfMethods::class))->getMethods())
125  );
126  }
127 
132  {
133  $classSchema = new ‪ClassSchema(DummyClassWithInjectDoctrineAnnotation::class);
134  self::assertTrue($classSchema->hasInjectProperties());
135 
136  $injectProperties = $classSchema->getInjectProperties();
137  self::assertArrayHasKey('propertyWithFullQualifiedClassName', $injectProperties);
138  self::assertSame(DummyClassWithInjectDoctrineAnnotation::class, $injectProperties['propertyWithFullQualifiedClassName']->getType());
139 
140  self::assertArrayHasKey('propertyWithRelativeClassName', $injectProperties);
141  self::assertSame(DummyClassWithInjectDoctrineAnnotation::class, $injectProperties['propertyWithRelativeClassName']->getType());
142 
143  self::assertArrayHasKey('propertyWithImportedClassName', $injectProperties);
144  self::assertSame(self::class, $injectProperties['propertyWithImportedClassName']->getType());
145 
146  self::assertArrayHasKey('propertyWithImportedAndAliasedClassName', $injectProperties);
147  self::assertSame(self::class, $injectProperties['propertyWithImportedAndAliasedClassName']->getType());
148  }
149 
154  {
155  $classSchema = new ‪ClassSchema(DummyClassWithAllTypesOfProperties::class);
156 
157  $propertyDefinition = $classSchema->getProperty('publicPropertyWithDefaultValue');
158  self::assertSame('foo', $propertyDefinition->getDefaultValue());
159  }
160 
165  {
166  $classSchema = new ‪ClassSchema(DummyClassWithAllTypesOfProperties::class);
167 
168  $propertyDefinition = $classSchema->getProperty('publicStaticPropertyWithDefaultValue');
169  self::assertNull($propertyDefinition->getDefaultValue());
170  }
171 
176  {
177  self::assertTrue((new ‪ClassSchema(DummySingleton::class))->isSingleton());
178  }
179 
183  public function ‪classSchemaDetectsModels()
184  {
185  self::assertTrue((new ‪ClassSchema(DummyEntity::class))->isModel());
186  self::assertTrue((new ‪ClassSchema(DummyValueObject::class))->isModel());
187  }
188 
193  {
194  self::assertTrue((new ‪ClassSchema(DummyEntity::class))->isEntity());
195  }
196 
201  {
202  self::assertTrue((new ‪ClassSchema(DummyValueObject::class))->isValueObject());
203  }
204 
209  {
210  $this->resetSingletonInstances = true;
211  self::assertSame(DummyModel::class, (new ‪ClassSchema(DummyModel::class))->getClassName());
212  }
213 
218  {
219  self::assertTrue((new ‪ClassSchema(DummyClassWithAllTypesOfProperties::class))->hasProperty('publicProperty'));
220  self::assertTrue((new ‪ClassSchema(DummyClassWithAllTypesOfProperties::class))->hasProperty('protectedProperty'));
221  self::assertTrue((new ‪ClassSchema(DummyClassWithAllTypesOfProperties::class))->hasProperty('privateProperty'));
222  }
223 
228  {
229  self::assertTrue((new ‪ClassSchema(DummyClassWithAllTypesOfProperties::class))->hasProperty('publicStaticProperty'));
230  self::assertTrue((new ‪ClassSchema(DummyClassWithAllTypesOfProperties::class))->hasProperty('protectedStaticProperty'));
231  self::assertTrue((new ‪ClassSchema(DummyClassWithAllTypesOfProperties::class))->hasProperty('privateStaticProperty'));
232  }
233 
238  {
239  $this->resetSingletonInstances = true;
240  $this->expectException(InvalidTypeHintException::class);
241  $this->expectExceptionMessage('Missing type information for parameter "$fooParam" in TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyControllerWithValidateAnnotationWithoutParamTypeHint->methodWithValidateAnnotationsAction(): Either use an @param annotation or use a type hint.');
242  $this->expectExceptionCode(1515075192);
243 
244  new ‪ClassSchema(DummyControllerWithValidateAnnotationWithoutParamTypeHint::class);
245  }
246 
251  {
252  $this->resetSingletonInstances = true;
253  $this->expectException(InvalidValidationConfigurationException::class);
254  $this->expectExceptionMessage('Invalid validate annotation in TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyControllerWithValidateAnnotationWithoutParam->methodWithValidateAnnotationsAction(): The following validators have been defined for missing param "$fooParam": NotEmpty, StringLength');
255  $this->expectExceptionCode(1515073585);
256 
257  new ‪ClassSchema(DummyControllerWithValidateAnnotationWithoutParam::class);
258  }
259 
264  {
265  $class = new class() {
266  public function foo(string $foo)
267  {
268  }
269 
270  public function bar(‪ClassSchema $foo)
271  {
272  }
273  };
274 
275  $classSchema = new ‪ClassSchema(get_class($class));
276  self::assertSame('string', $classSchema->getMethod('foo')->getParameter('foo')->getType());
277  self::assertSame(ClassSchema::class, $classSchema->getMethod('bar')->getParameter('foo')->getType());
278  }
279 
284  {
285  $class = new class() {
289  public function foo(string $foo)
290  {
291  }
292  };
293 
294  $classSchema = new ‪ClassSchema(get_class($class));
295  self::assertSame('string', $classSchema->getMethod('foo')->getParameter('foo')->getType());
296  }
297 
302  {
303  $class = new class() {
304  public function __construct(self $copy = null)
305  {
306  }
307  public function injectCopy(self $copy): void
308  {
309  }
310  public function foo($copy): self
311  {
312  }
313  public function bar(self $copy): void
314  {
315  }
316  };
317 
318  $classSchema = new ‪ClassSchema(get_class($class));
319  self::assertSame(get_class($class), $classSchema->getMethod('injectCopy')->getParameter('copy')->getType());
320  self::assertSame(get_class($class), $classSchema->getMethod('bar')->getParameter('copy')->getType());
321  }
322 
327  {
328  $classSchema = new ‪ClassSchema(DummyClassWithAllTypesOfMethods::class);
329  self::assertSame(DummyClassWithAllTypesOfMethods::class, $classSchema->getMethod('methodWithDocBlockTypeHintOnly')->getParameter('param')->getType());
330  }
331 }
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaDetectsInjectProperties
‪classSchemaDetectsInjectProperties()
Definition: ClassSchemaTest.php:131
‪TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationConfigurationException
Definition: InvalidValidationConfigurationException.php:26
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaPrefersMethodParameterTypeDetectionViaReflection
‪classSchemaPrefersMethodParameterTypeDetectionViaReflection()
Definition: ClassSchemaTest.php:283
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyClassWithAllTypesOfProperties
Definition: DummyClassWithAllTypesOfProperties.php:27
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaDetectsValueObjects
‪classSchemaDetectsValueObjects()
Definition: ClassSchemaTest.php:200
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaForModelIsSetAggregateRootIfRepositoryClassIsFoundForNamespacedClasses
‪classSchemaForModelIsSetAggregateRootIfRepositoryClassIsFoundForNamespacedClasses()
Definition: ClassSchemaTest.php:43
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaCanHandleSelfMethodReturnTypes
‪classSchemaCanHandleSelfMethodReturnTypes()
Definition: ClassSchemaTest.php:301
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyControllerWithValidateAnnotationWithoutParam
Definition: DummyControllerWithValidateAnnotationWithoutParam.php:27
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaDetectsEntities
‪classSchemaDetectsEntities()
Definition: ClassSchemaTest.php:192
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaDetectsClassName
‪classSchemaDetectsClassName()
Definition: ClassSchemaTest.php:208
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaDetectsStaticProperties
‪classSchemaDetectsStaticProperties()
Definition: ClassSchemaTest.php:227
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaGetMethods
‪classSchemaGetMethods()
Definition: ClassSchemaTest.php:105
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyClassWithConstructorAndConstructorArguments
Definition: DummyClassWithConstructorAndConstructorArguments.php:22
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaDetectsMethodParameterTypeViaReflection
‪classSchemaDetectsMethodParameterTypeViaReflection()
Definition: ClassSchemaTest.php:263
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest
Definition: ClassSchemaTest.php:39
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaGenerationThrowsExceptionWithValidateDoctrineAnnotationsForParamWithoutTypeHint
‪classSchemaGenerationThrowsExceptionWithValidateDoctrineAnnotationsForParamWithoutTypeHint()
Definition: ClassSchemaTest.php:237
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaDetectsModels
‪classSchemaDetectsModels()
Definition: ClassSchemaTest.php:183
‪TYPO3\CMS\Extbase\Reflection\ReflectionService
Definition: ReflectionService.php:31
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyEntity
Definition: DummyEntity.php:26
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaDetectsMethodParameterTypeDetectionViaDocBlocksIfNoTypeHintIsGiven
‪classSchemaDetectsMethodParameterTypeDetectionViaDocBlocksIfNoTypeHintIsGiven()
Definition: ClassSchemaTest.php:326
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyValueObject
Definition: DummyValueObject.php:26
‪TYPO3\CMS\Extbase\Reflection\ClassSchema
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaDetectsPropertyDefaultValue
‪classSchemaDetectsPropertyDefaultValue()
Definition: ClassSchemaTest.php:153
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyModel
Definition: DummyModel.php:27
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaSkipsDetectionOfDefaultValuesOfStaticClassProperties
‪classSchemaSkipsDetectionOfDefaultValuesOfStaticClassProperties()
Definition: ClassSchemaTest.php:164
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyClassWithInjectDoctrineAnnotation
Definition: DummyClassWithInjectDoctrineAnnotation.php:28
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaHasConstructor
‪classSchemaHasConstructor()
Definition: ClassSchemaTest.php:54
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaDetectsNonStaticProperties
‪classSchemaDetectsNonStaticProperties()
Definition: ClassSchemaTest.php:217
‪TYPO3\CMS\Extbase\Validation\Exception\InvalidTypeHintException
Definition: InvalidTypeHintException.php:26
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaDetectsSingletons
‪classSchemaDetectsSingletons()
Definition: ClassSchemaTest.php:175
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyControllerWithValidateAnnotationWithoutParamTypeHint
Definition: DummyControllerWithValidateAnnotationWithoutParamTypeHint.php:27
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaGenerationThrowsExceptionWithValidateDoctrineAnnotationsForMissingParam
‪classSchemaGenerationThrowsExceptionWithValidateDoctrineAnnotationsForMissingParam()
Definition: ClassSchemaTest.php:250
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaGetProperties
‪classSchemaGetProperties()
Definition: ClassSchemaTest.php:63
‪TYPO3\CMS\Extbase\Reflection\ClassSchema
Definition: ClassSchema.php:55
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaHasMethod
‪classSchemaHasMethod()
Definition: ClassSchemaTest.php:95
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyClassWithAllTypesOfMethods
Definition: DummyClassWithAllTypesOfMethods.php:22
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummySingleton
Definition: DummySingleton.php:26