Completed
Pull Request — master (#348)
by Marco
04:48
created

MethodGeneratorTest::testGenerateSimpleMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 0
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
declare(strict_types=1);
20
21
namespace ProxyManagerTest\Generator;
22
23
use PHPUnit_Framework_TestCase;
24
use ProxyManager\Generator\MethodGenerator;
25
use ProxyManagerTestAsset\BaseClass;
26
use ProxyManagerTestAsset\ClassWithAbstractPublicMethod;
27
use ProxyManagerTestAsset\EmptyClass;
28
use ProxyManagerTestAsset\ReturnTypeHintedClass;
29
use ProxyManagerTestAsset\ScalarTypeHintedClass;
30
use ProxyManagerTestAsset\VoidMethodTypeHintedInterface;
31
use stdClass;
32
use Zend\Code\Generator\ParameterGenerator;
33
use Zend\Code\Reflection\MethodReflection;
34
35
/**
36
 * Tests for {@see \ProxyManager\Generator\MethodGenerator}
37
 *
38
 * @author Marco Pivetta <[email protected]>
39
 * @license MIT
40
 *
41
 * @covers \ProxyManager\Generator\MethodGenerator
42
 * @group Coverage
43
 */
44
class MethodGeneratorTest extends PHPUnit_Framework_TestCase
45
{
46
    public function testGeneratedMethodsAreAllConcrete() : void
47
    {
48
        $methodGenerator = MethodGenerator::fromReflection(new MethodReflection(
49
            ClassWithAbstractPublicMethod::class,
50
            'publicAbstractMethod'
51
        ));
52
53
        self::assertFalse($methodGenerator->isInterface());
54
    }
55
56
    public function testGenerateSimpleMethod() : void
57
    {
58
        $methodGenerator = new MethodGenerator();
59
60
        $methodGenerator->setReturnsReference(true);
61
        $methodGenerator->setName('methodName');
62
        $methodGenerator->setVisibility('protected');
63
        $methodGenerator->setBody('/* body */');
64
        $methodGenerator->setDocBlock('docBlock');
65
        $methodGenerator->setParameter(new ParameterGenerator('foo'));
66
67
        self::assertStringMatchesFormat(
68
            '%a/**%adocBlock%a*/%aprotected function & methodName($foo)%a{%a/* body */%a}',
69
            $methodGenerator->generate()
70
        );
71
    }
72
73
    /**
74
     * Verify that building from reflection works
75
     */
76
    public function testGenerateFromReflection() : void
77
    {
78
        $method = MethodGenerator::fromReflection(new MethodReflection(__CLASS__, __FUNCTION__));
79
80
        self::assertSame(__FUNCTION__, $method->getName());
81
        self::assertSame(MethodGenerator::VISIBILITY_PUBLIC, $method->getVisibility());
82
        self::assertFalse($method->isStatic());
83
        self::assertSame('Verify that building from reflection works', $method->getDocBlock()->getShortDescription());
84
85
        $method = MethodGenerator::fromReflection(new MethodReflection(BaseClass::class, 'protectedMethod'));
86
87
        self::assertSame(MethodGenerator::VISIBILITY_PROTECTED, $method->getVisibility());
88
89
        $method = MethodGenerator::fromReflection(new MethodReflection(BaseClass::class, 'privateMethod'));
90
91
        self::assertSame(MethodGenerator::VISIBILITY_PRIVATE, $method->getVisibility());
92
    }
93
94
    public function testGeneratedParametersFromReflection() : void
95
    {
96
        $method = MethodGenerator::fromReflection(new MethodReflection(
97
            BaseClass::class,
98
            'publicTypeHintedMethod'
99
        ));
100
101
        self::assertSame('publicTypeHintedMethod', $method->getName());
102
103
        $parameters = $method->getParameters();
104
105
        self::assertCount(1, $parameters);
106
107
        $param = $parameters['param'];
108
109
        self::assertSame(stdClass::class, $param->getType());
110
    }
111
112
    /**
113
     * @param string $methodName
114
     * @param string $type
115
     *
116
     * @dataProvider scalarTypeHintedMethods
117
     */
118
    public function testGenerateMethodWithScalarTypeHinting(string $methodName, string $type) : void
119
    {
120
        $method = MethodGenerator::fromReflection(new MethodReflection(
121
            ScalarTypeHintedClass::class,
122
            $methodName
123
        ));
124
125
        self::assertSame($methodName, $method->getName());
126
127
        $parameters = $method->getParameters();
128
129
        self::assertCount(1, $parameters);
130
131
        $param = $parameters['param'];
132
133
        self::assertSame($type, $param->getType());
134
    }
135
136
    public function scalarTypeHintedMethods()
137
    {
138
        return [
139
            ['acceptString', 'string'],
140
            ['acceptInteger', 'int'],
141
            ['acceptBoolean', 'bool'],
142
            ['acceptFloat', 'float'],
143
        ];
144
    }
145
146
    public function testGenerateMethodWithVoidReturnTypeHinting() : void
147
    {
148
        $method = MethodGenerator::fromReflection(new MethodReflection(
149
            VoidMethodTypeHintedInterface::class,
150
            'returnVoid'
151
        ));
152
153
        self::assertSame('returnVoid', $method->getName());
154
        self::assertStringMatchesFormat('%a : void%a', $method->generate());
155
    }
156
157
    /**
158
     * @dataProvider returnTypeHintsProvider
159
     *
160
     * @param string $methodName
161
     * @param string $expectedType
162
     */
163
    public function testReturnTypeHintGeneration(string $methodName, string $expectedType) : void
164
    {
165
        $method = MethodGenerator::fromReflection(new MethodReflection(
166
            ReturnTypeHintedClass::class,
167
            $methodName
168
        ));
169
170
        self::assertSame($methodName, $method->getName());
171
        self::assertStringMatchesFormat('%a : ' . $expectedType . '%a', $method->generate());
172
    }
173
174
    /**
175
     * @return string[][]
176
     */
177
    public function returnTypeHintsProvider() : array
178
    {
179
        return [
180
            ['returnString', 'string'],
181
            ['returnInteger', 'int'],
182
            ['returnBool', 'bool'],
183
            ['returnArray', 'array'],
184
            ['returnCallable', 'callable'],
185
            ['returnSelf', '\\' . ReturnTypeHintedClass::class],
186
            ['returnParent', '\\' . EmptyClass::class],
187
            ['returnVoid', 'void'],
188
            ['returnIterable', 'iterable'],
189
            ['returnSameClass', '\\' . ReturnTypeHintedClass::class],
190
            ['returnOtherClass', '\\' . EmptyClass::class],
191
        ];
192
    }
193
}
194