Skip to content

Commit 8ea246d

Browse files
committed
Add tests for ReflectionTools::getParameterTypes()
1 parent a9fdd32 commit 8ea246d

File tree

3 files changed

+79
-2
lines changed

3 files changed

+79
-2
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Brick\Reflection\Tests\Classes;
6+
7+
use Namespaced\Foo;
8+
use stdClass;
9+
10+
class ParameterTypesPHP72
11+
{
12+
/**
13+
* @param INT|string|Foo|Bar $a
14+
* @param \PDO|null $b
15+
*/
16+
public function x($a, $b, \stdClass $c, ?stdClass $d)
17+
{
18+
}
19+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Brick\Reflection\Tests\Classes;
6+
7+
use stdClass;
8+
use A\B;
9+
10+
class ParameterTypesPHP80 extends ParameterTypesPHP72
11+
{
12+
public function y(?stdClass $a, \stdClass|B|int|string|NULL $b)
13+
{
14+
}
15+
}

tests/ReflectionToolsTest.php

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use Brick\Reflection\ReflectionTools;
88

9+
use Brick\Reflection\Tests\Classes\ParameterTypesPHP72;
10+
use Brick\Reflection\Tests\Classes\ParameterTypesPHP80;
911
use Brick\Reflection\Tests\Classes\PropertyTypesPHP72;
1012
use Brick\Reflection\Tests\Classes\PropertyTypesPHP74;
1113
use Brick\Reflection\Tests\Classes\PropertyTypesPHP80;
@@ -152,14 +154,55 @@ public function providerExportFunction() : array
152154
];
153155
}
154156

157+
/**
158+
* @dataProvider providerGetParameterTypes
159+
*/
160+
public function testGetParameterTypes(string $class, string $method, string $parameter, array $types) : void
161+
{
162+
$tools = new ReflectionTools();
163+
$reflectionMethod = new \ReflectionMethod($class, $method);
164+
165+
foreach ($reflectionMethod->getParameters() as $reflectionParameter) {
166+
if ($reflectionParameter->getName() === $parameter) {
167+
self::assertSame($types, $tools->getParameterTypes($reflectionParameter));
168+
return;
169+
}
170+
}
171+
172+
self::fail(sprintf('Parameter $%s not found in %s::%s()', $parameter, $class, $method));
173+
}
174+
175+
public function providerGetParameterTypes() : array
176+
{
177+
$tests = [
178+
[ParameterTypesPHP72::class, 'x', 'a', ['int', 'string', 'Namespaced\Foo', 'Brick\Reflection\Tests\Classes\Bar']],
179+
[ParameterTypesPHP72::class, 'x', 'b', ['PDO', 'null']],
180+
[ParameterTypesPHP72::class, 'x', 'c', ['stdClass']],
181+
[ParameterTypesPHP72::class, 'x', 'd', ['stdClass', 'null']],
182+
];
183+
184+
if (version_compare(PHP_VERSION, '8.0') >= 0) {
185+
$tests = array_merge($tests, [
186+
[ParameterTypesPHP80::class, 'x', 'a', ['int', 'string', 'Namespaced\Foo', 'Brick\Reflection\Tests\Classes\Bar']],
187+
[ParameterTypesPHP80::class, 'x', 'b', ['PDO', 'null']],
188+
[ParameterTypesPHP80::class, 'x', 'c', ['stdClass']],
189+
[ParameterTypesPHP80::class, 'x', 'd', ['stdClass', 'null']],
190+
[ParameterTypesPHP80::class, 'y', 'a', ['stdClass', 'null']],
191+
[ParameterTypesPHP80::class, 'y', 'b', ['stdClass', 'A\B', 'string', 'int', 'null']],
192+
]);
193+
}
194+
195+
return $tests;
196+
}
197+
155198
/**
156199
* @dataProvider providerGetPropertyTypes
157200
*/
158201
public function testGetPropertyTypes(string $class, string $property, array $types) : void
159202
{
160203
$tools = new ReflectionTools();
161-
$property = new \ReflectionProperty($class, $property);
162-
self::assertSame($types, $tools->getPropertyTypes($property));
204+
$reflectionProperty = new \ReflectionProperty($class, $property);
205+
self::assertSame($types, $tools->getPropertyTypes($reflectionProperty));
163206
}
164207

165208
public function providerGetPropertyTypes() : array

0 commit comments

Comments
 (0)