Completed
Pull Request — master (#266)
by Marco
15:13
created

testIsVariadicParamTurnTrueWhenPassedAVariadicByRefMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 10
rs 9.4286
cc 1
eloc 6
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
namespace ProxyManagerTest\Generator;
20
21
use Phar;
22
use PHPUnit_Framework_TestCase;
23
use ProxyManager\Generator\ParameterGenerator;
24
use ProxyManagerTestAsset\BaseClass;
25
use ProxyManagerTestAsset\CallableTypeHintClass;
26
use ProxyManagerTestAsset\ClassWithMethodWithByRefVariadicFunction;
27
use ProxyManagerTestAsset\ClassWithMethodWithDefaultParameters;
28
use ProxyManagerTestAsset\ClassWithMethodWithVariadicFunction;
29
use stdClass;
30
use Zend\Code\Generator\ValueGenerator;
31
use Zend\Code\Reflection\ParameterReflection;
32
33
/**
34
 * Tests for {@see \ProxyManager\Generator\ParameterGenerator}
35
 *
36
 * @author Marco Pivetta <[email protected]>
37
 * @license MIT
38
 *
39
 * @covers \ProxyManager\Generator\ParameterGenerator
40
 * @group Coverage
41
 */
42
class ParameterGeneratorTest extends PHPUnit_Framework_TestCase
43
{
44
    public function testGeneratesProperTypeHint()
45
    {
46
        $generator = new ParameterGenerator('foo');
47
48
        $generator->setType('array');
49
        $this->assertSame('array $foo', $generator->generate());
50
51
        $generator->setType('stdClass');
52
        $this->assertSame('\\stdClass $foo', $generator->generate());
53
54
        $generator->setType('\\fooClass');
55
        $this->assertSame('\\fooClass $foo', $generator->generate());
56
    }
57
58
    public function testGeneratesMethodWithCallableType()
59
    {
60
        $generator = new ParameterGenerator();
61
62
        $generator->setType('callable');
63
        $generator->setName('foo');
64
65
        $this->assertSame('callable $foo', $generator->generate());
66
    }
67
68
    public function testVisitMethodWithCallable()
69
    {
70
        $parameter = new ParameterReflection(
71
            [CallableTypeHintClass::class, 'callableTypeHintMethod'],
72
            'parameter'
73
        );
74
75
        $generator = ParameterGenerator::fromReflection($parameter);
76
77
        $this->assertSame('callable', $generator->getType());
78
    }
79
80
    public function testReadsParameterDefaults()
81
    {
82
        $parameter = ParameterGenerator::fromReflection(new ParameterReflection(
83
            [
84
                ClassWithMethodWithDefaultParameters::class,
85
                'publicMethodWithDefaults'
86
            ],
87
            'parameter'
88
        ));
89
90
        /* @var $defaultValue ValueGenerator */
91
        $defaultValue = $parameter->getDefaultValue();
92
93
        $this->assertInstanceOf(ValueGenerator::class, $defaultValue);
94
        $this->assertSame(['foo'], $defaultValue->getValue());
95
96
        $this->assertStringMatchesFormat('array%a$parameter%a=%aarray(\'foo\')', $parameter->generate());
97
    }
98
99
    public function testReadsParameterTypeHint()
100
    {
101
        $parameter = ParameterGenerator::fromReflection(new ParameterReflection(
102
            [BaseClass::class, 'publicTypeHintedMethod'],
103
            'param'
104
        ));
105
106
        $this->assertSame(stdClass::class, $parameter->getType());
107
    }
108
109
    public function testVariadicParamIsSettedByDefaultAsFalse()
110
    {
111
        $parameter = new ParameterGenerator();
112
        $this->assertFalse($parameter->isVariadic());
113
    }
114
115
    public function testVariadicParamKeepAsFalseIfANotVariadicMethodIsPassed()
116
    {
117
        $parameter = ParameterGenerator::fromReflection(new ParameterReflection(
118
            [BaseClass::class, 'publicTypeHintedMethod'],
119
            'param'
120
        ));
121
122
        $this->assertFalse($parameter->isVariadic());
123
    }
124
125
    public function testIsVariadicParamTurnTrueWhenPassAVariadicMethod()
126
    {
127
        $parameter = ParameterGenerator::fromReflection(new ParameterReflection(
128
            [ClassWithMethodWithVariadicFunction::class, 'buz'],
129
            'fooz'
130
        ));
131
132
        $this->assertTrue($parameter->isVariadic());
133
    }
134
135
    public function testIsVariadicParamTurnTrueWhenPassedAVariadicByRefMethod()
136
    {
137
        $parameter = ParameterGenerator::fromReflection(new ParameterReflection(
138
            [ClassWithMethodWithByRefVariadicFunction::class, 'tuz'],
139
            'fooz'
140
        ));
141
142
        $this->assertTrue($parameter->isVariadic());
143
        $this->assertTrue($parameter->getPassedByReference());
144
    }
145
146
    public function testGeneratesParameterPassedByReference()
147
    {
148
        $parameter = new ParameterGenerator('foo');
149
150
        $parameter->setPassedByReference(true);
151
152
        $this->assertStringMatchesFormat('&%A$foo', $parameter->generate());
153
    }
154
155
    public function testGeneratesDefaultParameterForInternalPhpClasses()
156
    {
157
        $parameter = ParameterGenerator::fromReflection(new ParameterReflection([Phar::class, 'compress'], 1));
158
159
        $this->assertSame('null', strtolower((string) $parameter->getDefaultValue()));
160
    }
161
162
    public function testGeneratedParametersAreProperlyEscaped()
163
    {
164
        $parameter = new ParameterGenerator();
165
166
        $parameter->setName('foo');
167
        $parameter->setDefaultValue('\'bar\\baz');
168
169
        $this->assertThat(
170
            $parameter->generate(),
171
            $this->logicalOr(
172
                $this->equalTo('$foo = \'\\\'bar\\baz\''),
173
                $this->equalTo('$foo = \'\\\'bar\\\\baz\'')
174
            )
175
        );
176
    }
177
}
178