[FOLLOWUP][!!!][TASK] Replace ClassInfo with ClassSchema
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Reflection / ReflectionServiceTest.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\Reflection\ReflectionService;
19
20 /**
21 * Test case
22 * @firsttest test for reflection
23 * @anothertest second test for reflection
24 * @anothertest second test for reflection with second value
25 */
26 class ReflectionServiceTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
27 {
28 /**
29 * @param array $foo The foo parameter
30 * @return string
31 */
32 public function fixtureMethodForMethodTagsValues(array $foo)
33 {
34 }
35
36 /**
37 * @param bool $dummy
38 * @param int $foo
39 */
40 public function fixtureMethodForMethodTagsValuesWithShortTypes($dummy, $foo)
41 {
42 }
43
44 /**
45 * @test
46 */
47 public function getClassTagsValues()
48 {
49 $service = GeneralUtility::makeInstance(ReflectionService::class);
50 $classValues = $service->getClassTagsValues(static::class);
51 $this->assertEquals([
52 'firsttest' => ['test for reflection'],
53 'anothertest' => ['second test for reflection', 'second test for reflection with second value']
54 ], $classValues);
55
56 $this->assertEquals(
57 [],
58 $service->getClassTagsValues('NonExistantNamespace\\NonExistantClass')
59 );
60 }
61
62 /**
63 * @test
64 */
65 public function getClassTagValues()
66 {
67 $service = GeneralUtility::makeInstance(ReflectionService::class);
68 $classValues = $service->getClassTagValues(static::class, 'firsttest');
69 $this->assertEquals([
70 'test for reflection',
71 ], $classValues);
72
73 $this->assertEquals(
74 [],
75 $service->getClassTagValues('NonExistantNamespace\\NonExistantClass', 'nonExistantTag')
76 );
77 }
78
79 /**
80 * @test
81 */
82 public function hasMethod()
83 {
84 $service = GeneralUtility::makeInstance(ReflectionService::class);
85 $this->assertTrue($service->hasMethod(static::class, 'fixtureMethodForMethodTagsValues'));
86 $this->assertFalse($service->hasMethod(static::class, 'notExistentMethod'));
87 $this->assertFalse($service->hasMethod('NonExistantNamespace\\NonExistantClass', 'notExistentMethod'));
88 }
89
90 /**
91 * @test
92 */
93 public function getMethodTagsValues()
94 {
95 $service = GeneralUtility::makeInstance(ReflectionService::class);
96 $tagsValues = $service->getMethodTagsValues(static::class, 'fixtureMethodForMethodTagsValues');
97 $this->assertEquals([
98 'param' => ['array $foo The foo parameter'],
99 'return' => ['string']
100 ], $tagsValues);
101
102 $this->assertEquals(
103 [],
104 $service->getMethodTagsValues('NonExistantNamespace\\NonExistantClass', 'notExistentMethod')
105 );
106 }
107
108 /**
109 * @test
110 */
111 public function getMethodParameters()
112 {
113 $service = GeneralUtility::makeInstance(ReflectionService::class);
114 $parameters = $service->getMethodParameters(static::class, 'fixtureMethodForMethodTagsValues');
115 $this->assertSame([
116 'foo' => [
117 'position' => 0,
118 'byReference' => false,
119 'array' => true,
120 'optional' => false,
121 'allowsNull' => false,
122 'class' => null,
123 'type' => 'array',
124 'nullable' => false,
125 'default' => null,
126 'hasDefaultValue' => false,
127 'defaultValue' => null,
128 'dependency' => null,
129 ]
130 ], $parameters);
131
132 $this->assertSame(
133 [],
134 $service->getMethodParameters('NonExistantNamespace\\NonExistantClass', 'notExistentMethod')
135 );
136 }
137
138 /**
139 * @test
140 */
141 public function getMethodParametersWithShortTypeNames()
142 {
143 $service = GeneralUtility::makeInstance(ReflectionService::class);
144 $parameters = $service->getMethodParameters(static::class, 'fixtureMethodForMethodTagsValuesWithShortTypes');
145 $this->assertSame([
146 'dummy' => [
147 'position' => 0,
148 'byReference' => false,
149 'array' => false,
150 'optional' => false,
151 'allowsNull' => true,
152 'class' => null,
153 'type' => 'boolean',
154 'nullable' => true,
155 'default' => null,
156 'hasDefaultValue' => false,
157 'defaultValue' => null,
158 'dependency' => null,
159 ],
160 'foo' => [
161 'position' => 1,
162 'byReference' => false,
163 'array' => false,
164 'optional' => false,
165 'allowsNull' => true,
166 'class' => null,
167 'type' => 'integer',
168 'nullable' => true,
169 'default' => null,
170 'hasDefaultValue' => false,
171 'defaultValue' => null,
172 'dependency' => null,
173 ]
174 ], $parameters);
175 }
176
177 public function testIsClassTaggedWith()
178 {
179 $service = GeneralUtility::makeInstance(ReflectionService::class);
180 $this->assertTrue($service->isClassTaggedWith(
181 Fixture\DummyClassWithTags::class,
182 'foo'
183 ));
184
185 $this->assertFalse($service->isClassTaggedWith(
186 Fixture\DummyClassWithAllTypesOfProperties::class,
187 'bar'
188 ));
189
190 $this->assertFalse($service->isClassTaggedWith(
191 'NonExistantNamespace\\NonExistantClass',
192 'foo'
193 ));
194 }
195
196 public function testIsPropertyTaggedWith()
197 {
198 $service = GeneralUtility::makeInstance(ReflectionService::class);
199 $this->assertTrue($service->isPropertyTaggedWith(
200 Fixture\DummyClassWithAllTypesOfProperties::class,
201 'propertyWithInjectAnnotation',
202 'inject'
203 ));
204
205 $this->assertFalse($service->isPropertyTaggedWith(
206 Fixture\DummyClassWithAllTypesOfProperties::class,
207 'propertyWithInjectAnnotation',
208 'foo'
209 ));
210
211 $this->assertFalse($service->isPropertyTaggedWith(
212 Fixture\DummyClassWithAllTypesOfProperties::class,
213 'nonExistantProperty',
214 'foo'
215 ));
216
217 $this->assertFalse($service->isPropertyTaggedWith(
218 'NonExistantNamespace\\NonExistantClass',
219 'propertyWithInjectAnnotation',
220 'inject'
221 ));
222 }
223
224 public function testgetPropertyTagValues()
225 {
226 $service = GeneralUtility::makeInstance(ReflectionService::class);
227
228 $this->assertSame(
229 [],
230 $service->getPropertyTagValues(
231 Fixture\DummyClassWithAllTypesOfProperties::class,
232 'propertyWithInjectAnnotation',
233 'foo'
234 )
235 );
236
237 $this->assertSame(
238 [],
239 $service->getPropertyTagValues(
240 Fixture\DummyClassWithAllTypesOfProperties::class,
241 'propertyWithInjectAnnotation',
242 'inject'
243 )
244 );
245 }
246
247 public function testGetPropertyTagsValues()
248 {
249 $service = GeneralUtility::makeInstance(ReflectionService::class);
250
251 $this->assertSame(
252 [
253 'inject' => [],
254 'var' => [
255 'DummyClassWithAllTypesOfProperties'
256 ]
257 ],
258 $service->getPropertyTagsValues(
259 Fixture\DummyClassWithAllTypesOfProperties::class,
260 'propertyWithInjectAnnotation'
261 )
262 );
263
264 $this->assertSame(
265 [],
266 $service->getPropertyTagsValues(
267 Fixture\DummyClassWithAllTypesOfProperties::class,
268 'nonExistantProperty'
269 )
270 );
271
272 $this->assertSame(
273 [],
274 $service->getPropertyTagsValues(
275 'NonExistantNamespace\\NonExistantClass',
276 'nonExistantProperty'
277 )
278 );
279 }
280
281 public function testGetClassPropertyNames()
282 {
283 $service = GeneralUtility::makeInstance(ReflectionService::class);
284
285 $this->assertSame(
286 [
287 'publicProperty',
288 'protectedProperty',
289 'privateProperty',
290 'publicStaticProperty',
291 'protectedStaticProperty',
292 'privateStaticProperty',
293 'propertyWithIgnoredTags',
294 'propertyWithInjectAnnotation',
295 'propertyWithTransientAnnotation',
296 'propertyWithCascadeAnnotation',
297 'propertyWithCascadeAnnotationWithoutVarAnnotation',
298 'propertyWithObjectStorageAnnotation'
299 ],
300 $service->getClassPropertyNames(Fixture\DummyClassWithAllTypesOfProperties::class)
301 );
302
303 $this->assertSame(
304 [],
305 $service->getClassPropertyNames('NonExistantNamespace\\NonExistantClass')
306 );
307 }
308 }