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