‪TYPO3CMS  11.5
ClassSchemaTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
34 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
35 
39 class ‪ClassSchemaTest extends UnitTestCase
40 {
45  {
46  $this->resetSingletonInstances = true;
47  $service = new ‪ReflectionService(new ‪NullFrontend('extbase'), 'ClassSchemata');
48  $classSchema = $service->getClassSchema(DummyModel::class);
49  self::assertTrue($classSchema->isAggregateRoot());
50  }
51 
55  public function ‪classSchemaHasConstructor(): void
56  {
57  $classSchema = new ‪ClassSchema(DummyClassWithConstructorAndConstructorArguments::class);
58  self::assertTrue($classSchema->hasConstructor());
59  }
60 
64  public function ‪classSchemaGetProperties(): void
65  {
66  self::assertSame(
67  [
68  'publicProperty',
69  'protectedProperty',
70  'privateProperty',
71  'publicStaticProperty',
72  'protectedStaticProperty',
73  'privateStaticProperty',
74  'publicPropertyWithDefaultValue',
75  'publicStaticPropertyWithDefaultValue',
76  'stringTypedProperty',
77  'nullableStringTypedProperty',
78  'propertyWithIgnoredTags',
79  'propertyWithInjectAnnotation',
80  'propertyWithTransientAnnotation',
81  'propertyWithCascadeAnnotation',
82  'propertyWithCascadeAnnotationWithoutVarAnnotation',
83  'propertyWithObjectStorageAnnotation',
84  'propertyWithObjectStorageAnnotationWithoutFQCN',
85  'uid',
86  '_localizedUid',
87  '_languageUid',
88  '_versionedUid',
89  'pid',
90  ],
91  array_keys((new ‪ClassSchema(DummyClassWithAllTypesOfProperties::class))->getProperties())
92  );
93  }
94 
98  public function ‪classSchemaHasMethod(): void
99  {
100  $classSchema = new ‪ClassSchema(DummyClassWithAllTypesOfMethods::class);
101  self::assertTrue($classSchema->hasMethod('publicMethod'));
102  self::assertFalse($classSchema->hasMethod('nonExistentMethod'));
103  }
104 
108  public function ‪classSchemaGetMethods(): void
109  {
110  self::assertSame(
111  [
112  'publicMethod',
113  'protectedMethod',
114  'privateMethod',
115  'methodWithIgnoredTags',
116  'injectSettings',
117  'injectMethodWithoutParam',
118  'injectMethodThatIsProtected',
119  'injectFoo',
120  'staticMethod',
121  'methodWithMandatoryParam',
122  'methodWithNullableParam',
123  'methodWithDefaultValueParam',
124  'methodWithTypeHintedParam',
125  'methodWithDocBlockTypeHintOnly',
126  ],
127  array_keys((new ‪ClassSchema(DummyClassWithAllTypesOfMethods::class))->getMethods())
128  );
129  }
130 
135  {
136  $classSchema = new ‪ClassSchema(DummyClassWithAllTypesOfProperties::class);
137 
138  $propertyDefinition = $classSchema->getProperty('publicPropertyWithDefaultValue');
139  self::assertSame('foo', $propertyDefinition->getDefaultValue());
140  }
141 
146  {
147  $classSchema = new ‪ClassSchema(DummyClassWithAllTypesOfProperties::class);
148 
149  $propertyDefinition = $classSchema->getProperty('publicStaticPropertyWithDefaultValue');
150  self::assertNull($propertyDefinition->getDefaultValue());
151  }
152 
156  public function ‪classSchemaDetectsSingletons(): void
157  {
158  self::assertTrue((new ‪ClassSchema(DummySingleton::class))->isSingleton());
159  }
160 
164  public function ‪classSchemaDetectsModels(): void
165  {
166  self::assertTrue((new ‪ClassSchema(DummyEntity::class))->isModel());
167  self::assertTrue((new ‪ClassSchema(DummyValueObject::class))->isModel());
168  }
169 
173  public function ‪classSchemaDetectsEntities(): void
174  {
175  self::assertTrue((new ‪ClassSchema(DummyEntity::class))->isEntity());
176  }
177 
181  public function ‪classSchemaDetectsValueObjects(): void
182  {
183  self::assertTrue((new ‪ClassSchema(DummyValueObject::class))->isValueObject());
184  }
185 
189  public function ‪classSchemaDetectsClassName(): void
190  {
191  $this->resetSingletonInstances = true;
192  self::assertSame(DummyModel::class, (new ‪ClassSchema(DummyModel::class))->getClassName());
193  }
194 
199  {
200  self::assertTrue((new ‪ClassSchema(DummyClassWithAllTypesOfProperties::class))->hasProperty('publicProperty'));
201  self::assertTrue((new ‪ClassSchema(DummyClassWithAllTypesOfProperties::class))->hasProperty('protectedProperty'));
202  self::assertTrue((new ‪ClassSchema(DummyClassWithAllTypesOfProperties::class))->hasProperty('privateProperty'));
203  }
204 
209  {
210  self::assertTrue((new ‪ClassSchema(DummyClassWithAllTypesOfProperties::class))->hasProperty('publicStaticProperty'));
211  self::assertTrue((new ‪ClassSchema(DummyClassWithAllTypesOfProperties::class))->hasProperty('protectedStaticProperty'));
212  self::assertTrue((new ‪ClassSchema(DummyClassWithAllTypesOfProperties::class))->hasProperty('privateStaticProperty'));
213  }
214 
219  {
220  $this->resetSingletonInstances = true;
221  $this->expectException(InvalidTypeHintException::class);
222  $this->expectExceptionMessage('Missing type information for parameter "$fooParam" in TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyControllerWithValidateAnnotationWithoutParamTypeHint->methodWithValidateAnnotationsAction(): Use a type hint.');
223  $this->expectExceptionCode(1515075192);
224 
225  new ‪ClassSchema(DummyControllerWithValidateAnnotationWithoutParamTypeHint::class);
226  }
227 
232  {
233  $this->resetSingletonInstances = true;
234  $this->expectException(InvalidValidationConfigurationException::class);
235  $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');
236  $this->expectExceptionCode(1515073585);
237 
238  new ‪ClassSchema(DummyControllerWithValidateAnnotationWithoutParam::class);
239  }
240 
245  {
246  $class = new class () {
247  public function foo(string $foo): void {}
248 
249  public function bar(‪ClassSchema $foo): void {}
250  };
251 
252  $classSchema = new ‪ClassSchema(get_class($class));
253  self::assertSame('string', $classSchema->getMethod('foo')->getParameter('foo')->getType());
254  self::assertSame(ClassSchema::class, $classSchema->getMethod('bar')->getParameter('foo')->getType());
255  }
256 
261  {
262  $class = new class () {
266  public function foo(string $foo): void {}
267  };
268 
269  $classSchema = new ‪ClassSchema(get_class($class));
270  self::assertSame('string', $classSchema->getMethod('foo')->getParameter('foo')->getType());
271  }
272 
277  {
278  $class = new class () {
279  public function __construct(self $copy = null) {}
280  public function injectCopy(self $copy): void {}
281  public function foo($copy): self
282  {
283  return $this;
284  }
285  public function bar(self $copy): void {}
286  };
287 
288  $classSchema = new ‪ClassSchema(get_class($class));
289  self::assertSame(get_class($class), $classSchema->getMethod('injectCopy')->getParameter('copy')->getType());
290  self::assertSame(get_class($class), $classSchema->getMethod('bar')->getParameter('copy')->getType());
291  }
292 
297  {
298  $classSchema = new ‪ClassSchema(DummyClassWithAllTypesOfMethods::class);
299  self::assertSame(DummyClassWithAllTypesOfMethods::class, $classSchema->getMethod('methodWithDocBlockTypeHintOnly')->getParameter('param')->getType());
300  }
301 }
‪TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationConfigurationException
Definition: InvalidValidationConfigurationException.php:25
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaPrefersMethodParameterTypeDetectionViaReflection
‪classSchemaPrefersMethodParameterTypeDetectionViaReflection()
Definition: ClassSchemaTest.php:260
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyClassWithAllTypesOfProperties
Definition: DummyClassWithAllTypesOfProperties.php:29
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaDetectsValueObjects
‪classSchemaDetectsValueObjects()
Definition: ClassSchemaTest.php:181
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaForModelIsSetAggregateRootIfRepositoryClassIsFoundForNamespacedClasses
‪classSchemaForModelIsSetAggregateRootIfRepositoryClassIsFoundForNamespacedClasses()
Definition: ClassSchemaTest.php:44
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaCanHandleSelfMethodReturnTypes
‪classSchemaCanHandleSelfMethodReturnTypes()
Definition: ClassSchemaTest.php:276
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyControllerWithValidateAnnotationWithoutParam
Definition: DummyControllerWithValidateAnnotationWithoutParam.php:27
‪TYPO3\CMS\Core\Cache\Frontend\NullFrontend
Definition: NullFrontend.php:29
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaDetectsEntities
‪classSchemaDetectsEntities()
Definition: ClassSchemaTest.php:173
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaDetectsClassName
‪classSchemaDetectsClassName()
Definition: ClassSchemaTest.php:189
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaDetectsStaticProperties
‪classSchemaDetectsStaticProperties()
Definition: ClassSchemaTest.php:208
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaGetMethods
‪classSchemaGetMethods()
Definition: ClassSchemaTest.php:108
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyClassWithConstructorAndConstructorArguments
Definition: DummyClassWithConstructorAndConstructorArguments.php:24
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaDetectsMethodParameterTypeViaReflection
‪classSchemaDetectsMethodParameterTypeViaReflection()
Definition: ClassSchemaTest.php:244
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest
Definition: ClassSchemaTest.php:40
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaGenerationThrowsExceptionWithValidateDoctrineAnnotationsForParamWithoutTypeHint
‪classSchemaGenerationThrowsExceptionWithValidateDoctrineAnnotationsForParamWithoutTypeHint()
Definition: ClassSchemaTest.php:218
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaDetectsModels
‪classSchemaDetectsModels()
Definition: ClassSchemaTest.php:164
‪TYPO3\CMS\Extbase\Reflection\ReflectionService
Definition: ReflectionService.php:28
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyEntity
Definition: DummyEntity.php:25
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaDetectsMethodParameterTypeDetectionViaDocBlocksIfNoTypeHintIsGiven
‪classSchemaDetectsMethodParameterTypeDetectionViaDocBlocksIfNoTypeHintIsGiven()
Definition: ClassSchemaTest.php:296
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyValueObject
Definition: DummyValueObject.php:25
‪TYPO3\CMS\Extbase\Reflection\ClassSchema
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaDetectsPropertyDefaultValue
‪classSchemaDetectsPropertyDefaultValue()
Definition: ClassSchemaTest.php:134
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyModel
Definition: DummyModel.php:27
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaSkipsDetectionOfDefaultValuesOfStaticClassProperties
‪classSchemaSkipsDetectionOfDefaultValuesOfStaticClassProperties()
Definition: ClassSchemaTest.php:145
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaHasConstructor
‪classSchemaHasConstructor()
Definition: ClassSchemaTest.php:55
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaDetectsNonStaticProperties
‪classSchemaDetectsNonStaticProperties()
Definition: ClassSchemaTest.php:198
‪TYPO3\CMS\Extbase\Validation\Exception\InvalidTypeHintException
Definition: InvalidTypeHintException.php:25
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaDetectsSingletons
‪classSchemaDetectsSingletons()
Definition: ClassSchemaTest.php:156
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyControllerWithValidateAnnotationWithoutParamTypeHint
Definition: DummyControllerWithValidateAnnotationWithoutParamTypeHint.php:27
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaGenerationThrowsExceptionWithValidateDoctrineAnnotationsForMissingParam
‪classSchemaGenerationThrowsExceptionWithValidateDoctrineAnnotationsForMissingParam()
Definition: ClassSchemaTest.php:231
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaGetProperties
‪classSchemaGetProperties()
Definition: ClassSchemaTest.php:64
‪TYPO3\CMS\Extbase\Reflection\ClassSchema
Definition: ClassSchema.php:54
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaHasMethod
‪classSchemaHasMethod()
Definition: ClassSchemaTest.php:98
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyClassWithAllTypesOfMethods
Definition: DummyClassWithAllTypesOfMethods.php:24
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummySingleton
Definition: DummySingleton.php:25