-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFormatReflectedFunctionTest.php
More file actions
93 lines (77 loc) · 3.31 KB
/
FormatReflectedFunctionTest.php
File metadata and controls
93 lines (77 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
declare(strict_types=1);
namespace Typhoon\Formatter;
use PHPUnit\Framework\Attributes\CoversFunction;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
#[CoversFunction('Typhoon\Formatter\formatReflectedFunction')]
final class FormatReflectedFunctionTest extends TestCase
{
/**
* @return \Generator<string, array{\ReflectionFunctionAbstract, non-empty-string}>
* @throws \ReflectionException
*/
public static function cases(): \Generator
{
yield 'from object method' => [
new \ReflectionMethod(new self('name'), 'testFormatReflectedFunction'),
self::class . '::testFormatReflectedFunction()',
];
yield 'from class method' => [
new \ReflectionMethod(new self('name'), 'cases'),
self::class . '::cases()',
];
yield 'from named method' => [new \ReflectionFunction('trim'), 'trim()'];
yield 'from closure method' => [
new \ReflectionFunction((new self('name'))->testFormatReflectedFunction(...)),
\sprintf('%s::testFormatReflectedFunction()', self::class),
];
yield 'from eval closure method' => [
(static function (): \ReflectionFunction {
/**
* @var \Closure $closure
*/
$closure = eval('return function() {};');
return new \ReflectionFunction($closure);
})(),
\sprintf('function@%s:%d()', __FILE__, __LINE__ - 4),
];
}
/**
* @param non-empty-string $expectedFormattedFunction
*/
#[DataProvider('cases')]
public function testFormatReflectedFunction(\ReflectionFunctionAbstract $function, string $expectedFormattedFunction): void
{
$formatted = formatReflectedFunction($function);
self::assertSame($expectedFormattedFunction, $formatted);
}
public function testFormatReflectedFunctionWhenFileNameIsUnavailable(): void
{
$mock = $this->createMock(\ReflectionFunction::class);
$mock->method('getName')->willReturn('{closure}');
$mock->method('getFileName')->willReturn(false);
$formatted = formatReflectedFunction($mock);
self::assertSame('function()', $formatted);
}
public function testFormatReflectedFunctionWhenStartLineIsUnavailable(): void
{
$mock = $this->createMock(\ReflectionFunction::class);
$mock->method('getName')->willReturn('{closure}');
$mock->method('getFileName')->willReturn('Path');
$mock->method('getStartLine')->willReturn(false);
$expectedFormattedFunction = \sprintf('function@%s()', $mock->getFileName());
$formatted = formatReflectedFunction($mock);
self::assertSame($expectedFormattedFunction, $formatted);
}
public function testFormatReflectedFunctionWhenAllAvailable(): void
{
$mock = $this->createMock(\ReflectionFunction::class);
$mock->method('getName')->willReturn('{closure}');
$mock->method('getFileName')->willReturn('Path');
$mock->method('getStartLine')->willReturn(5);
$expectedFormattedFunction = \sprintf('function@%s:%d()', $mock->getFileName(), $mock->getStartLine());
$formatted = formatReflectedFunction($mock);
self::assertSame($expectedFormattedFunction, $formatted);
}
}