|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
14
|
|
|
* |
|
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
16
|
|
|
* and is licensed under the MIT license. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace ProxyManagerTest\Generator; |
|
20
|
|
|
|
|
21
|
|
|
use PHPUnit_Framework_TestCase; |
|
22
|
|
|
use ProxyManager\Generator\MethodGenerator; |
|
23
|
|
|
use Zend\Code\Generator\ParameterGenerator; |
|
24
|
|
|
use ProxyManagerTestAsset\BaseClass; |
|
25
|
|
|
use ProxyManagerTestAsset\ScalarTypeHintedClass; |
|
26
|
|
|
use stdClass; |
|
27
|
|
|
use Zend\Code\Reflection\MethodReflection; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Tests for {@see \ProxyManager\Generator\MethodGenerator} |
|
31
|
|
|
* |
|
32
|
|
|
* @author Marco Pivetta <[email protected]> |
|
33
|
|
|
* @license MIT |
|
34
|
|
|
* |
|
35
|
|
|
* @covers \ProxyManager\Generator\MethodGenerator |
|
36
|
|
|
* @group Coverage |
|
37
|
|
|
*/ |
|
38
|
|
|
class MethodGeneratorTest extends PHPUnit_Framework_TestCase |
|
39
|
|
|
{ |
|
40
|
|
|
public function testGenerateSimpleMethod() |
|
41
|
|
|
{ |
|
42
|
|
|
$methodGenerator = new MethodGenerator(); |
|
43
|
|
|
|
|
44
|
|
|
$methodGenerator->setReturnsReference(true); |
|
45
|
|
|
$methodGenerator->setName('methodName'); |
|
46
|
|
|
$methodGenerator->setVisibility('protected'); |
|
47
|
|
|
$methodGenerator->setBody('/* body */'); |
|
48
|
|
|
$methodGenerator->setDocBlock('docBlock'); |
|
49
|
|
|
$methodGenerator->setParameter(new ParameterGenerator('foo')); |
|
50
|
|
|
|
|
51
|
|
|
$this->assertStringMatchesFormat( |
|
52
|
|
|
'%a/**%adocBlock%a*/%aprotected function & methodName($foo)%a{%a/* body */%a}', |
|
53
|
|
|
$methodGenerator->generate() |
|
54
|
|
|
); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Verify that building from reflection works |
|
59
|
|
|
*/ |
|
60
|
|
|
public function testGenerateFromReflection() |
|
61
|
|
|
{ |
|
62
|
|
|
$method = MethodGenerator::fromReflection(new MethodReflection(__CLASS__, __FUNCTION__)); |
|
63
|
|
|
|
|
64
|
|
|
$this->assertSame(__FUNCTION__, $method->getName()); |
|
65
|
|
|
$this->assertSame(MethodGenerator::VISIBILITY_PUBLIC, $method->getVisibility()); |
|
66
|
|
|
$this->assertFalse($method->isStatic()); |
|
67
|
|
|
$this->assertSame('Verify that building from reflection works', $method->getDocBlock()->getShortDescription()); |
|
68
|
|
|
|
|
69
|
|
|
$method = MethodGenerator::fromReflection(new MethodReflection(BaseClass::class, 'protectedMethod')); |
|
70
|
|
|
|
|
71
|
|
|
$this->assertSame(MethodGenerator::VISIBILITY_PROTECTED, $method->getVisibility()); |
|
72
|
|
|
|
|
73
|
|
|
$method = MethodGenerator::fromReflection(new MethodReflection(BaseClass::class, 'privateMethod')); |
|
74
|
|
|
|
|
75
|
|
|
$this->assertSame(MethodGenerator::VISIBILITY_PRIVATE, $method->getVisibility()); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function testGeneratedParametersFromReflection() |
|
79
|
|
|
{ |
|
80
|
|
|
$method = MethodGenerator::fromReflection(new MethodReflection( |
|
81
|
|
|
BaseClass::class, |
|
82
|
|
|
'publicTypeHintedMethod' |
|
83
|
|
|
)); |
|
84
|
|
|
|
|
85
|
|
|
$this->assertSame('publicTypeHintedMethod', $method->getName()); |
|
86
|
|
|
|
|
87
|
|
|
$parameters = $method->getParameters(); |
|
88
|
|
|
|
|
89
|
|
|
$this->assertCount(1, $parameters); |
|
90
|
|
|
|
|
91
|
|
|
$param = $parameters['param']; |
|
92
|
|
|
|
|
93
|
|
|
$this->assertSame(stdClass::class, $param->getType()); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param string $methodName |
|
98
|
|
|
* @param $type |
|
99
|
|
|
* |
|
100
|
|
|
* @dataProvider scalarTypeHintedMethods |
|
101
|
|
|
*/ |
|
102
|
|
|
public function testGenerateMethodWithScalarTypeHinting($methodName, $type) |
|
103
|
|
|
{ |
|
104
|
|
|
$method = MethodGenerator::fromReflection(new MethodReflection( |
|
105
|
|
|
ScalarTypeHintedClass::class, |
|
106
|
|
|
$methodName |
|
107
|
|
|
)); |
|
108
|
|
|
|
|
109
|
|
|
$this->assertSame($methodName, $method->getName()); |
|
110
|
|
|
|
|
111
|
|
|
$parameters = $method->getParameters(); |
|
112
|
|
|
|
|
113
|
|
|
$this->assertCount(1, $parameters); |
|
114
|
|
|
|
|
115
|
|
|
$param = $parameters['param']; |
|
116
|
|
|
|
|
117
|
|
|
$this->assertSame($type, $param->getType()); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function scalarTypeHintedMethods() |
|
121
|
|
|
{ |
|
122
|
|
|
return [ |
|
123
|
|
|
['acceptString', 'string'], |
|
124
|
|
|
['acceptInteger', 'int'], |
|
125
|
|
|
['acceptBoolean', 'bool'], |
|
126
|
|
|
['acceptFloat', 'float'], |
|
127
|
|
|
]; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
|