33a8e130f35338de4ca075ef858c0f5f68c47ae9
[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 ]
144 ], $parameters);
145
146 $this->assertSame(
147 [],
148 $service->getMethodParameters(static::class, 'notExistentMethod')
149 );
150
151 $this->assertSame(
152 [],
153 $service->getMethodParameters('NonExistantNamespace\\NonExistantClass', 'notExistentMethod')
154 );
155 }
156
157 /**
158 * @test
159 */
160 public function getMethodParametersWithShortTypeNames()
161 {
162 $service = new ReflectionService();
163 $parameters = $service->getMethodParameters(static::class, 'fixtureMethodForMethodTagsValuesWithShortTypes');
164 $this->assertSame([
165 'dummy' => [
166 'position' => 0,
167 'byReference' => false,
168 'array' => false,
169 'optional' => false,
170 'allowsNull' => true,
171 'class' => null,
172 'type' => 'boolean',
173 'nullable' => true,
174 'default' => null,
175 'hasDefaultValue' => false,
176 'defaultValue' => null,
177 'dependency' => null,
178 ],
179 'foo' => [
180 'position' => 1,
181 'byReference' => false,
182 'array' => false,
183 'optional' => false,
184 'allowsNull' => true,
185 'class' => null,
186 'type' => 'integer',
187 'nullable' => true,
188 'default' => null,
189 'hasDefaultValue' => false,
190 'defaultValue' => null,
191 'dependency' => null,
192 ]
193 ], $parameters);
194 }
195
196 public function testIsClassTaggedWith()
197 {
198 $service = new ReflectionService();
199 $this->assertTrue($service->isClassTaggedWith(
200 Fixture\DummyClassWithTags::class,
201 'see'
202 ));
203
204 $this->assertFalse($service->isClassTaggedWith(
205 Fixture\DummyClassWithAllTypesOfProperties::class,
206 'bar'
207 ));
208
209 $this->assertFalse($service->isClassTaggedWith(
210 'NonExistantNamespace\\NonExistantClass',
211 'foo'
212 ));
213 }
214
215 public function testIsPropertyTaggedWith()
216 {
217 $service = new ReflectionService();
218 $this->assertTrue($service->isPropertyTaggedWith(
219 Fixture\DummyClassWithAllTypesOfProperties::class,
220 'propertyWithInjectAnnotation',
221 'extbase\inject'
222 ));
223
224 $this->assertFalse($service->isPropertyTaggedWith(
225 Fixture\DummyClassWithAllTypesOfProperties::class,
226 'propertyWithInjectAnnotation',
227 'foo'
228 ));
229
230 $this->assertFalse($service->isPropertyTaggedWith(
231 Fixture\DummyClassWithAllTypesOfProperties::class,
232 'nonExistantProperty',
233 'foo'
234 ));
235
236 $this->assertFalse($service->isPropertyTaggedWith(
237 'NonExistantNamespace\\NonExistantClass',
238 'propertyWithInjectAnnotation',
239 'extbase\inject'
240 ));
241 }
242
243 public function testgetPropertyTagValues()
244 {
245 $service = new ReflectionService();
246 $this->assertSame(
247 [],
248 $service->getPropertyTagValues(
249 Fixture\DummyClassWithAllTypesOfProperties::class,
250 'propertyWithInjectAnnotation',
251 'foo'
252 )
253 );
254
255 $this->assertSame(
256 [],
257 $service->getPropertyTagValues(
258 Fixture\DummyClassWithAllTypesOfProperties::class,
259 'propertyWithInjectAnnotation',
260 'inject'
261 )
262 );
263 }
264
265 public function testGetPropertyTagsValues()
266 {
267 $service = new ReflectionService();
268 $this->assertSame(
269 [
270 'extbase\inject' => [],
271 'var' => [
272 'DummyClassWithAllTypesOfProperties'
273 ]
274 ],
275 $service->getPropertyTagsValues(
276 Fixture\DummyClassWithAllTypesOfProperties::class,
277 'propertyWithInjectAnnotation'
278 )
279 );
280
281 $this->assertSame(
282 [],
283 $service->getPropertyTagsValues(
284 Fixture\DummyClassWithAllTypesOfProperties::class,
285 'nonExistantProperty'
286 )
287 );
288
289 $this->assertSame(
290 [],
291 $service->getPropertyTagsValues(
292 'NonExistantNamespace\\NonExistantClass',
293 'nonExistantProperty'
294 )
295 );
296 }
297
298 public function testGetClassPropertyNames()
299 {
300 $service = new ReflectionService();
301 $this->assertSame(
302 [
303 'publicProperty',
304 'protectedProperty',
305 'privateProperty',
306 'publicStaticProperty',
307 'protectedStaticProperty',
308 'privateStaticProperty',
309 'propertyWithIgnoredTags',
310 'propertyWithInjectAnnotation',
311 'propertyWithTransientAnnotation',
312 'propertyWithCascadeAnnotation',
313 'propertyWithCascadeAnnotationWithoutVarAnnotation',
314 'propertyWithObjectStorageAnnotation'
315 ],
316 $service->getClassPropertyNames(Fixture\DummyClassWithAllTypesOfProperties::class)
317 );
318
319 $this->assertSame(
320 [],
321 $service->getClassPropertyNames('NonExistantNamespace\\NonExistantClass')
322 );
323 }
324 }