‪TYPO3CMS  ‪main
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 
20 use PHPUnit\Framework\Attributes\Test;
31 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
32 
33 final class ‪ClassSchemaTest extends UnitTestCase
34 {
35  protected bool ‪$resetSingletonInstances = true;
36 
37  #[Test]
38  public function ‪classSchemaGetProperties(): void
39  {
40  self::assertSame(
41  [
42  'publicProperty',
43  'protectedProperty',
44  'privateProperty',
45  'publicPropertyWithDefaultValue',
46  'stringTypedProperty',
47  'nullableStringTypedProperty',
48  'propertyWithTransientAnnotation',
49  'propertyWithTransientAttribute',
50  'propertyWithCascadeAnnotation',
51  'propertyWithCascadeAnnotationWithoutVarAnnotation',
52  'propertyWithCascadeAttribute',
53  'propertyWithObjectStorageAnnotation',
54  'propertyWithObjectStorageAnnotationWithoutFQCN',
55  'propertyWithLazyLoadingProxy',
56  'propertyWithLazyObjectStorageAnnotationWithoutFQCN',
62  ],
63  array_keys((new ‪ClassSchema(DummyClassWithAllTypesOfProperties::class))->getProperties())
64  );
65  }
66 
67  #[Test]
68  public function ‪classSchemaHasMethod(): void
69  {
70  $classSchema = new ‪ClassSchema(DummyClassWithAllTypesOfMethods::class);
71  self::assertTrue($classSchema->hasMethod('publicMethod'));
72  self::assertFalse($classSchema->hasMethod('nonExistentMethod'));
73  }
74 
75  #[Test]
76  public function ‪classSchemaGetMethods(): void
77  {
78  self::assertSame(
79  [
80  'publicMethod',
81  'protectedMethod',
82  'privateMethod',
83  'methodWithMandatoryParam',
84  'methodWithDefaultValueParam',
85  'methodWithTypeHintedParam',
86  'methodWithDocBlockTypeHintOnly',
87  ],
88  array_keys((new ‪ClassSchema(DummyClassWithAllTypesOfMethods::class))->getMethods())
89  );
90  }
91 
92  #[Test]
94  {
95  self::assertTrue((new ‪ClassSchema(DummyClassWithAllTypesOfProperties::class))->hasProperty('publicProperty'));
96  self::assertTrue((new ‪ClassSchema(DummyClassWithAllTypesOfProperties::class))->hasProperty('protectedProperty'));
97  self::assertTrue((new ‪ClassSchema(DummyClassWithAllTypesOfProperties::class))->hasProperty('privateProperty'));
98  }
99 
100  #[Test]
102  {
103  $this->expectException(InvalidTypeHintException::class);
104  $this->expectExceptionMessage('Missing type information for parameter "$fooParam" in TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyControllerWithValidateAnnotationWithoutParamTypeHint->methodWithValidateAnnotationsAction(): Use a type hint.');
105  $this->expectExceptionCode(1515075192);
106 
107  new ‪ClassSchema(DummyControllerWithValidateAnnotationWithoutParamTypeHint::class);
108  }
109 
110  #[Test]
112  {
113  $this->expectException(InvalidValidationConfigurationException::class);
114  $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');
115  $this->expectExceptionCode(1515073585);
116 
117  new ‪ClassSchema(DummyControllerWithValidateAnnotationWithoutParam::class);
118  }
119  #[Test]
121  {
122  $this->expectException(InvalidTypeHintException::class);
123  $this->expectExceptionMessage('Missing type information for parameter "$fooParam" in TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyControllerWithValidateAttributeWithoutParamTypeHint->methodWithValidateAttributesAction(): Use a type hint.');
124  $this->expectExceptionCode(1515075192);
125 
126  new ‪ClassSchema(DummyControllerWithValidateAttributeWithoutParamTypeHint::class);
127  }
128 
129  #[Test]
131  {
132  $this->expectException(InvalidValidationConfigurationException::class);
133  $this->expectExceptionMessage('Invalid validate annotation in TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyControllerWithValidateAttributeWithoutParam->methodWithValidateAttributesAction(): The following validators have been defined for missing param "$fooParam": NotEmpty, StringLength');
134  $this->expectExceptionCode(1515073585);
135 
136  new ‪ClassSchema(DummyControllerWithValidateAttributeWithoutParam::class);
137  }
138 
139  #[Test]
141  {
142  $class = new class () {
143  public function foo(string $foo): void {}
144 
145  public function bar(‪ClassSchema $foo): void {}
146  };
147 
148  $classSchema = new ‪ClassSchema(get_class($class));
149  self::assertSame('string', $classSchema->getMethod('foo')->getParameter('foo')->getType());
150  self::assertSame(ClassSchema::class, $classSchema->getMethod('bar')->getParameter('foo')->getType());
151  }
152 
153  #[Test]
155  {
156  $class = new class () {
160  public function foo(string $foo): void {}
161  };
162 
163  $classSchema = new ‪ClassSchema(get_class($class));
164  self::assertSame('string', $classSchema->getMethod('foo')->getParameter('foo')->getType());
165  }
166 
167  #[Test]
169  {
170  $class = new class () {
171  public function __construct(self $copy = null) {}
172  public function injectCopy(self $copy): void {}
173  public function foo($copy): self
174  {
175  return $this;
176  }
177  public function bar(self $copy): void {}
178  };
179 
180  $classSchema = new ‪ClassSchema(get_class($class));
181  self::assertSame(get_class($class), $classSchema->getMethod('injectCopy')->getParameter('copy')->getType());
182  self::assertSame(get_class($class), $classSchema->getMethod('bar')->getParameter('copy')->getType());
183  }
184 
185  #[Test]
187  {
188  $classSchema = new ‪ClassSchema(DummyClassWithAllTypesOfMethods::class);
189  self::assertSame(DummyClassWithAllTypesOfMethods::class, $classSchema->getMethod('methodWithDocBlockTypeHintOnly')->getParameter('param')->getType());
190  }
191 }
‪TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationConfigurationException
Definition: InvalidValidationConfigurationException.php:25
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaPrefersMethodParameterTypeDetectionViaReflection
‪classSchemaPrefersMethodParameterTypeDetectionViaReflection()
Definition: ClassSchemaTest.php:154
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyClassWithAllTypesOfProperties
Definition: DummyClassWithAllTypesOfProperties.php:31
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaDetectsDynamicProperties
‪classSchemaDetectsDynamicProperties()
Definition: ClassSchemaTest.php:93
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\PROPERTY_PID
‪const PROPERTY_PID
Definition: AbstractDomainObject.php:33
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaCanHandleSelfMethodReturnTypes
‪classSchemaCanHandleSelfMethodReturnTypes()
Definition: ClassSchemaTest.php:168
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyControllerWithValidateAnnotationWithoutParam
Definition: DummyControllerWithValidateAnnotationWithoutParam.php:27
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyControllerWithValidateAttributeWithoutParamTypeHint
Definition: DummyControllerWithValidateAttributeWithoutParamTypeHint.php:28
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\PROPERTY_LOCALIZED_UID
‪const PROPERTY_LOCALIZED_UID
Definition: AbstractDomainObject.php:34
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaGetMethods
‪classSchemaGetMethods()
Definition: ClassSchemaTest.php:76
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyControllerWithValidateAttributeWithoutParam
Definition: DummyControllerWithValidateAttributeWithoutParam.php:27
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaDetectsMethodParameterTypeViaReflection
‪classSchemaDetectsMethodParameterTypeViaReflection()
Definition: ClassSchemaTest.php:140
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest
Definition: ClassSchemaTest.php:34
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaGenerationThrowsExceptionWithValidateDoctrineAnnotationsForParamWithoutTypeHint
‪classSchemaGenerationThrowsExceptionWithValidateDoctrineAnnotationsForParamWithoutTypeHint()
Definition: ClassSchemaTest.php:101
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaDetectsMethodParameterTypeDetectionViaDocBlocksIfNoTypeHintIsGiven
‪classSchemaDetectsMethodParameterTypeDetectionViaDocBlocksIfNoTypeHintIsGiven()
Definition: ClassSchemaTest.php:186
‪TYPO3\CMS\Extbase\Reflection\ClassSchema
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject
Definition: AbstractDomainObject.php:31
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\PROPERTY_LANGUAGE_UID
‪const PROPERTY_LANGUAGE_UID
Definition: AbstractDomainObject.php:35
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaGenerationThrowsExceptionWithValidateDoctrineAttributesForMissingParam
‪classSchemaGenerationThrowsExceptionWithValidateDoctrineAttributesForMissingParam()
Definition: ClassSchemaTest.php:130
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\PROPERTY_VERSIONED_UID
‪const PROPERTY_VERSIONED_UID
Definition: AbstractDomainObject.php:36
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: ClassSchemaTest.php:35
‪TYPO3\CMS\Extbase\Validation\Exception\InvalidTypeHintException
Definition: InvalidTypeHintException.php:25
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaGenerationThrowsExceptionWithValidateDoctrineAttributesForParamWithoutTypeHint
‪classSchemaGenerationThrowsExceptionWithValidateDoctrineAttributesForParamWithoutTypeHint()
Definition: ClassSchemaTest.php:120
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyControllerWithValidateAnnotationWithoutParamTypeHint
Definition: DummyControllerWithValidateAnnotationWithoutParamTypeHint.php:27
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaGenerationThrowsExceptionWithValidateDoctrineAnnotationsForMissingParam
‪classSchemaGenerationThrowsExceptionWithValidateDoctrineAnnotationsForMissingParam()
Definition: ClassSchemaTest.php:111
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaGetProperties
‪classSchemaGetProperties()
Definition: ClassSchemaTest.php:38
‪TYPO3\CMS\Extbase\Reflection\ClassSchema
Definition: ClassSchema.php:50
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\PROPERTY_UID
‪const PROPERTY_UID
Definition: AbstractDomainObject.php:32
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\ClassSchemaTest\classSchemaHasMethod
‪classSchemaHasMethod()
Definition: ClassSchemaTest.php:68
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyClassWithAllTypesOfMethods
Definition: DummyClassWithAllTypesOfMethods.php:24
‪TYPO3\CMS\Extbase\Tests\Unit\Reflection