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

InterceptedMethodTest::testBodyStructure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 9.4286
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
namespace ProxyManagerTest\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator;
20
21
use PHPUnit_Framework_TestCase;
22
use ProxyManager\Generator\MethodGenerator;
23
use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\InterceptedMethod;
24
use ProxyManagerTestAsset\BaseClass;
25
use ProxyManagerTestAsset\ClassWithMethodWithVariadicFunction;
26
use Zend\Code\Generator\PropertyGenerator;
27
use Zend\Code\Reflection\MethodReflection;
28
29
/**
30
 * Tests for {@see \ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\InterceptedMethod}
31
 *
32
 * @author Marco Pivetta <[email protected]>
33
 * @license MIT
34
 *
35
 * @covers \ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\InterceptedMethod
36
 * @group Coverage
37
 */
38
class InterceptedMethodTest extends PHPUnit_Framework_TestCase
39
{
40
    /**
41
     * @var $prefixInterceptors PropertyGenerator|\PHPUnit_Framework_MockObject_MockObject
42
     */
43
    private $prefixInterceptors;
44
45
    /**
46
     * @var $suffixInterceptors PropertyGenerator|\PHPUnit_Framework_MockObject_MockObject
47
     */
48
    private $suffixInterceptors;
49
50
    protected function setUp()
51
    {
52
        parent::setUp();
53
54
        $this->prefixInterceptors = $this->getMock(PropertyGenerator::class);
55
        $this->suffixInterceptors = $this->getMock(PropertyGenerator::class);
56
57
        $this->prefixInterceptors->expects($this->any())->method('getName')->will($this->returnValue('pre'));
58
        $this->suffixInterceptors->expects($this->any())->method('getName')->will($this->returnValue('post'));
59
    }
60
61
    public function testBodyStructure()
62
    {
63
        $method = InterceptedMethod::generateMethod(
64
            new MethodReflection(BaseClass::class, 'publicByReferenceParameterMethod'),
65
            $this->prefixInterceptors,
66
            $this->suffixInterceptors
67
        );
68
69
        $this->assertInstanceOf(MethodGenerator::class, $method);
70
71
        $this->assertSame('publicByReferenceParameterMethod', $method->getName());
72
        $this->assertCount(2, $method->getParameters());
73
        $this->assertStringMatchesFormat(
74
            '%a$returnValue = parent::publicByReferenceParameterMethod($param, $byRefParam);%A',
75
            $method->getBody()
76
        );
77
    }
78
79
    public function testForwardsVariadicParameters()
80
    {
81
        $method = InterceptedMethod::generateMethod(
82
            new MethodReflection(ClassWithMethodWithVariadicFunction::class, 'foo'),
83
            $this->prefixInterceptors,
84
            $this->suffixInterceptors
85
        );
86
87
        $this->assertInstanceOf(MethodGenerator::class, $method);
88
89
        $this->assertSame('foo', $method->getName());
90
        $this->assertCount(2, $method->getParameters());
91
        $this->assertStringMatchesFormat(
92
            '%a$returnValue = parent::foo($bar, ...$baz);%A',
93
            $method->getBody()
94
        );
95
    }
96
}
97