1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ProxyManagerTest\Generator; |
6
|
|
|
|
7
|
|
|
use Laminas\Code\Generator\ParameterGenerator; |
8
|
|
|
use Laminas\Code\Reflection\MethodReflection; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
use ProxyManager\Generator\MethodGenerator; |
11
|
|
|
use ProxyManagerTestAsset\BaseClass; |
12
|
|
|
use ProxyManagerTestAsset\ClassWithAbstractPublicMethod; |
13
|
|
|
use ProxyManagerTestAsset\EmptyClass; |
14
|
|
|
use ProxyManagerTestAsset\ReturnTypeHintedClass; |
15
|
|
|
use ProxyManagerTestAsset\ScalarTypeHintedClass; |
16
|
|
|
use ProxyManagerTestAsset\VoidMethodTypeHintedInterface; |
17
|
|
|
use stdClass; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Tests for {@see \ProxyManager\Generator\MethodGenerator} |
21
|
|
|
* |
22
|
|
|
* @covers \ProxyManager\Generator\MethodGenerator |
23
|
|
|
* @group Coverage |
24
|
|
|
*/ |
25
|
|
|
final class MethodGeneratorTest extends TestCase |
26
|
|
|
{ |
27
|
|
|
public function testGeneratedMethodsAreAllConcrete() : void |
28
|
|
|
{ |
29
|
|
|
$methodGenerator = MethodGenerator::fromReflectionWithoutBodyAndDocBlock(new MethodReflection( |
30
|
|
|
ClassWithAbstractPublicMethod::class, |
31
|
|
|
'publicAbstractMethod' |
32
|
|
|
)); |
33
|
|
|
|
34
|
|
|
self::assertFalse($methodGenerator->isInterface()); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testGenerateSimpleMethod() : void |
38
|
|
|
{ |
39
|
|
|
$methodGenerator = new MethodGenerator(); |
40
|
|
|
|
41
|
|
|
$methodGenerator->setReturnsReference(true); |
42
|
|
|
$methodGenerator->setName('methodName'); |
43
|
|
|
$methodGenerator->setVisibility('protected'); |
44
|
|
|
$methodGenerator->setBody('/* body */'); |
45
|
|
|
$methodGenerator->setDocBlock('docBlock'); |
46
|
|
|
$methodGenerator->setParameter(new ParameterGenerator('foo')); |
47
|
|
|
|
48
|
|
|
self::assertStringMatchesFormat( |
|
|
|
|
49
|
|
|
'%a/**%adocBlock%a*/%aprotected function & methodName($foo)%a{%a/* body */%a}', |
50
|
|
|
$methodGenerator->generate() |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Verify that building from reflection works |
56
|
|
|
*/ |
57
|
|
|
public function testGenerateFromReflection() : void |
58
|
|
|
{ |
59
|
|
|
$method = MethodGenerator::fromReflectionWithoutBodyAndDocBlock(new MethodReflection( |
60
|
|
|
self::class, |
61
|
|
|
__FUNCTION__ |
62
|
|
|
)); |
63
|
|
|
|
64
|
|
|
self::assertSame(__FUNCTION__, $method->getName()); |
|
|
|
|
65
|
|
|
self::assertSame(MethodGenerator::VISIBILITY_PUBLIC, $method->getVisibility()); |
|
|
|
|
66
|
|
|
self::assertFalse($method->isStatic()); |
|
|
|
|
67
|
|
|
self::assertNull($method->getDocBlock(), 'The docblock is ignored'); |
|
|
|
|
68
|
|
|
self::assertSame('', $method->getBody(), 'The body is ignored'); |
|
|
|
|
69
|
|
|
self::assertNull($method->getSourceContent(), 'The source content ignored'); |
|
|
|
|
70
|
|
|
self::assertTrue($method->isSourceDirty(), 'Dirty because the source cannot just be re-used when generating'); |
|
|
|
|
71
|
|
|
|
72
|
|
|
$method = MethodGenerator::fromReflectionWithoutBodyAndDocBlock(new MethodReflection( |
73
|
|
|
BaseClass::class, |
74
|
|
|
'protectedMethod' |
75
|
|
|
)); |
76
|
|
|
|
77
|
|
|
self::assertSame(MethodGenerator::VISIBILITY_PROTECTED, $method->getVisibility()); |
|
|
|
|
78
|
|
|
|
79
|
|
|
$method = MethodGenerator::fromReflectionWithoutBodyAndDocBlock(new MethodReflection( |
80
|
|
|
BaseClass::class, |
81
|
|
|
'privateMethod' |
82
|
|
|
)); |
83
|
|
|
|
84
|
|
|
self::assertSame(MethodGenerator::VISIBILITY_PRIVATE, $method->getVisibility()); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function testGeneratedParametersFromReflection() : void |
88
|
|
|
{ |
89
|
|
|
$method = MethodGenerator::fromReflectionWithoutBodyAndDocBlock(new MethodReflection( |
90
|
|
|
BaseClass::class, |
91
|
|
|
'publicTypeHintedMethod' |
92
|
|
|
)); |
93
|
|
|
|
94
|
|
|
self::assertSame('publicTypeHintedMethod', $method->getName()); |
|
|
|
|
95
|
|
|
|
96
|
|
|
$parameters = $method->getParameters(); |
97
|
|
|
|
98
|
|
|
self::assertCount(1, $parameters); |
|
|
|
|
99
|
|
|
|
100
|
|
|
$param = $parameters['param']; |
101
|
|
|
|
102
|
|
|
self::assertSame(stdClass::class, $param->getType()); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @dataProvider scalarTypeHintedMethods |
107
|
|
|
*/ |
108
|
|
|
public function testGenerateMethodWithScalarTypeHinting(string $methodName, string $type) : void |
109
|
|
|
{ |
110
|
|
|
$method = MethodGenerator::fromReflectionWithoutBodyAndDocBlock(new MethodReflection( |
111
|
|
|
ScalarTypeHintedClass::class, |
112
|
|
|
$methodName |
113
|
|
|
)); |
114
|
|
|
|
115
|
|
|
self::assertSame($methodName, $method->getName()); |
|
|
|
|
116
|
|
|
|
117
|
|
|
$parameters = $method->getParameters(); |
118
|
|
|
|
119
|
|
|
self::assertCount(1, $parameters); |
|
|
|
|
120
|
|
|
|
121
|
|
|
$param = $parameters['param']; |
122
|
|
|
|
123
|
|
|
self::assertSame($type, $param->getType()); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** @return string[][] */ |
127
|
|
|
public function scalarTypeHintedMethods() : array |
128
|
|
|
{ |
129
|
|
|
return [ |
130
|
|
|
['acceptString', 'string'], |
131
|
|
|
['acceptInteger', 'int'], |
132
|
|
|
['acceptBoolean', 'bool'], |
133
|
|
|
['acceptFloat', 'float'], |
134
|
|
|
]; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function testGenerateMethodWithVoidReturnTypeHinting() : void |
138
|
|
|
{ |
139
|
|
|
$method = MethodGenerator::fromReflectionWithoutBodyAndDocBlock(new MethodReflection( |
140
|
|
|
VoidMethodTypeHintedInterface::class, |
141
|
|
|
'returnVoid' |
142
|
|
|
)); |
143
|
|
|
|
144
|
|
|
self::assertSame('returnVoid', $method->getName()); |
|
|
|
|
145
|
|
|
self::assertStringMatchesFormat('%a : void%a', $method->generate()); |
|
|
|
|
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @dataProvider returnTypeHintsProvider |
150
|
|
|
*/ |
151
|
|
|
public function testReturnTypeHintGeneration(string $methodName, string $expectedType) : void |
152
|
|
|
{ |
153
|
|
|
$method = MethodGenerator::fromReflectionWithoutBodyAndDocBlock(new MethodReflection( |
154
|
|
|
ReturnTypeHintedClass::class, |
155
|
|
|
$methodName |
156
|
|
|
)); |
157
|
|
|
|
158
|
|
|
self::assertSame($methodName, $method->getName()); |
|
|
|
|
159
|
|
|
self::assertStringMatchesFormat('%a : ' . $expectedType . '%a', $method->generate()); |
|
|
|
|
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return string[][] |
164
|
|
|
*/ |
165
|
|
|
public static function returnTypeHintsProvider() : array |
166
|
|
|
{ |
167
|
|
|
return [ |
168
|
|
|
['returnString', 'string'], |
169
|
|
|
['returnInteger', 'int'], |
170
|
|
|
['returnBool', 'bool'], |
171
|
|
|
['returnArray', 'array'], |
172
|
|
|
['returnCallable', 'callable'], |
173
|
|
|
['returnSelf', '\\' . ReturnTypeHintedClass::class], |
174
|
|
|
['returnParent', '\\' . EmptyClass::class], |
175
|
|
|
['returnVoid', 'void'], |
176
|
|
|
['returnIterable', 'iterable'], |
177
|
|
|
['returnSameClass', '\\' . ReturnTypeHintedClass::class], |
178
|
|
|
['returnOtherClass', '\\' . EmptyClass::class], |
179
|
|
|
]; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.