7a2f97c9d03e17e3e2c8a60a490e12a1ed3ff745
[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 use TYPO3\CMS\Extbase\Tests\Unit\Reflection\Fixture\DummyModel;
21
22 /**
23 * Test case
24 */
25 class ClassSchemaTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
26 {
27 /**
28 * @test
29 */
30 public function classSchemaForModelIsSetAggregateRootIfRepositoryClassIsFoundForNamespacedClasses()
31 {
32 /** @var \TYPO3\CMS\Extbase\Reflection\ReflectionService $service */
33 $service = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Reflection\ReflectionService::class);
34 $classSchema = $service->getClassSchema(DummyModel::class);
35 $this->assertTrue($classSchema->isAggregateRoot());
36 }
37
38 public function testClassSchemaHasConstructor()
39 {
40 $classSchema = new ClassSchema(Fixture\DummyClassWithConstructorAndConstructorArguments::class);
41 static::assertTrue($classSchema->hasConstructor());
42 }
43
44 public function testClassSchemaDetectsConstructorArguments()
45 {
46 $classSchema = new ClassSchema(Fixture\DummyClassWithConstructorAndConstructorArguments::class);
47 static::assertTrue($classSchema->hasConstructor());
48
49 $methodDefinition = $classSchema->getMethod('__construct');
50 static::assertArrayHasKey('foo', $methodDefinition['params']);
51 static::assertArrayHasKey('bar', $methodDefinition['params']);
52 }
53
54 public function testClassSchemaDetectsConstructorArgumentsWithDependencies()
55 {
56 $classSchema = new ClassSchema(Fixture\DummyClassWithConstructorAndConstructorArgumentsWithDependencies::class);
57 static::assertTrue($classSchema->hasConstructor());
58
59 $methodDefinition = $classSchema->getMethod('__construct');
60 static::assertArrayHasKey('foo', $methodDefinition['params']);
61 static::assertSame(Fixture\DummyClassWithGettersAndSetters::class, $methodDefinition['params']['foo']['dependency']);
62 }
63
64 public function testClassSchemaDetectsMethodVisibility()
65 {
66 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfMethods::class);
67
68 $methodDefinition = $classSchema->getMethod('publicMethod');
69 static::assertTrue($methodDefinition['public']);
70 static::assertFalse($methodDefinition['protected']);
71 static::assertFalse($methodDefinition['private']);
72
73 $methodDefinition = $classSchema->getMethod('protectedMethod');
74 static::assertFalse($methodDefinition['public']);
75 static::assertTrue($methodDefinition['protected']);
76 static::assertFalse($methodDefinition['private']);
77
78 $methodDefinition = $classSchema->getMethod('privateMethod');
79 static::assertFalse($methodDefinition['public']);
80 static::assertFalse($methodDefinition['protected']);
81 static::assertTrue($methodDefinition['private']);
82 }
83
84 public function testClassSchemaDetectsInjectMethods()
85 {
86 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfMethods::class);
87
88 $methodDefinition = $classSchema->getMethod('injectSettings');
89 static::assertFalse($methodDefinition['injectMethod']);
90
91 $methodDefinition = $classSchema->getMethod('injectMethodWithoutParam');
92 static::assertFalse($methodDefinition['injectMethod']);
93
94 $methodDefinition = $classSchema->getMethod('injectMethodThatIsProtected');
95 static::assertFalse($methodDefinition['injectMethod']);
96
97 $methodDefinition = $classSchema->getMethod('injectFoo');
98 static::assertTrue($methodDefinition['injectMethod']);
99 }
100
101 public function testClassSchemaDetectsStaticMethods()
102 {
103 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfMethods::class);
104
105 $methodDefinition = $classSchema->getMethod('staticMethod');
106 static::assertTrue($methodDefinition['static']);
107 }
108
109 public function testClassSchemaDetectsMandatoryParams()
110 {
111 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfMethods::class);
112
113 $methodDefinition = $classSchema->getMethod('methodWithMandatoryParam');
114 static::assertFalse($methodDefinition['params']['param']['optional']);
115 }
116
117 public function testClassSchemaDetectsNullableParams()
118 {
119 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfMethods::class);
120
121 $methodDefinition = $classSchema->getMethod('methodWithNullableParam');
122 static::assertTrue($methodDefinition['params']['param']['nullable']);
123 }
124
125 public function testClassSchemaDetectsDefaultValueParams()
126 {
127 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfMethods::class);
128
129 $methodDefinition = $classSchema->getMethod('methodWithDefaultValueParam');
130 static::assertSame('foo', $methodDefinition['params']['param']['default']);
131 }
132
133 public function testClassSchemaDetectsParamTypeFromTypeHint()
134 {
135 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfMethods::class);
136
137 $methodDefinition = $classSchema->getMethod('methodWithTypeHintedParam');
138 static::assertSame('string', $methodDefinition['params']['param']['type']);
139 }
140
141 public function testClassSchemaDetectsPropertyVisibility()
142 {
143 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class);
144
145 $propertyDefinition = $classSchema->getProperty('publicProperty');
146 static::assertTrue($propertyDefinition['public']);
147 static::assertFalse($propertyDefinition['protected']);
148 static::assertFalse($propertyDefinition['private']);
149
150 $propertyDefinition = $classSchema->getProperty('protectedProperty');
151 static::assertFalse($propertyDefinition['public']);
152 static::assertTrue($propertyDefinition['protected']);
153 static::assertFalse($propertyDefinition['private']);
154
155 $propertyDefinition = $classSchema->getProperty('privateProperty');
156 static::assertFalse($propertyDefinition['public']);
157 static::assertFalse($propertyDefinition['protected']);
158 static::assertTrue($propertyDefinition['private']);
159 }
160
161 public function testClassSchemaDetectsInjectProperty()
162 {
163 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class);
164
165 $propertyDefinition = $classSchema->getProperty('propertyWithInjectAnnotation');
166 static::assertTrue($propertyDefinition['annotations']['inject']);
167 }
168
169 public function testClassSchemaDetectsTransientProperty()
170 {
171 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class);
172
173 $propertyDefinition = $classSchema->getProperty('propertyWithTransientAnnotation');
174 static::assertTrue($propertyDefinition['annotations']['transient']);
175 }
176
177 public function testClassSchemaDetectsCascadeProperty()
178 {
179 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class);
180
181 $propertyDefinition = $classSchema->getProperty('propertyWithCascadeAnnotation');
182 static::assertSame('remove', $propertyDefinition['annotations']['cascade']);
183 }
184
185 public function testClassSchemaDetectsCascadePropertyOnlyWithVarAnnotation()
186 {
187 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class);
188
189 $propertyDefinition = $classSchema->getProperty('propertyWithCascadeAnnotationWithoutVarAnnotation');
190 static::assertNull($propertyDefinition['annotations']['cascade']);
191 }
192
193 public function testClassSchemaDetectsTypeAndElementType()
194 {
195 $classSchema = new ClassSchema(Fixture\DummyClassWithAllTypesOfProperties::class);
196
197 $propertyDefinition = $classSchema->getProperty('propertyWithObjectStorageAnnotation');
198 static::assertSame(ObjectStorage::class, $propertyDefinition['type']);
199 static::assertSame(Fixture\DummyClassWithAllTypesOfProperties::class, $propertyDefinition['elementType']);
200 }
201 }