Completed
Pull Request — master (#277)
by Marco
04:04 queued 17s
created

testUnsetsPrivatePropertiesAsWell()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
rs 8.8571
cc 1
eloc 16
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\AccessInterceptorValueHolder\MethodGenerator;
20
21
use PHPUnit_Framework_TestCase;
22
use ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator\StaticProxyConstructor;
23
use ProxyManagerTestAsset\ClassWithMixedProperties;
24
use ProxyManagerTestAsset\EmptyClass;
25
use ProxyManagerTestAsset\ProxyGenerator\LazyLoading\MethodGenerator\ClassWithTwoPublicProperties;
26
use ReflectionClass;
27
use Zend\Code\Generator\PropertyGenerator;
28
29
/**
30
 * Tests for {@see \ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator\StaticProxyConstructor}
31
 *
32
 * @author Marco Pivetta <[email protected]>
33
 * @license MIT
34
 *
35
 * @covers \ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator\StaticProxyConstructor
36
 * @group Coverage
37
 */
38
class StaticProxyConstructorTest extends PHPUnit_Framework_TestCase
39
{
40
    public function testBodyStructure()
41
    {
42
        /* @var $valueHolder PropertyGenerator|\PHPUnit_Framework_MockObject_MockObject */
43
        $valueHolder        = $this->getMock(PropertyGenerator::class);
44
        /* @var $prefixInterceptors PropertyGenerator|\PHPUnit_Framework_MockObject_MockObject */
45
        $prefixInterceptors = $this->getMock(PropertyGenerator::class);
46
        /* @var $suffixInterceptors PropertyGenerator|\PHPUnit_Framework_MockObject_MockObject */
47
        $suffixInterceptors = $this->getMock(PropertyGenerator::class);
48
49
        $valueHolder->expects($this->any())->method('getName')->will($this->returnValue('foo'));
50
        $prefixInterceptors->expects($this->any())->method('getName')->will($this->returnValue('pre'));
51
        $suffixInterceptors->expects($this->any())->method('getName')->will($this->returnValue('post'));
52
53
        $constructor = new StaticProxyConstructor(
54
            new ReflectionClass(
55
                ClassWithTwoPublicProperties::class
56
            ),
57
            $valueHolder,
58
            $prefixInterceptors,
59
            $suffixInterceptors
60
        );
61
62
        $this->assertSame('staticProxyConstructor', $constructor->getName());
63
        $this->assertCount(3, $constructor->getParameters());
64
        $this->assertSame(
65
            'static $reflection;
66
67
$reflection = $reflection ?: $reflection = new \ReflectionClass(__CLASS__);
68
$instance = (new \ReflectionClass(get_class()))->newInstanceWithoutConstructor();
69
70
unset($instance->bar, $instance->baz);
71
72
$instance->foo = $wrappedObject;
73
$instance->pre = $prefixInterceptors;
74
$instance->post = $suffixInterceptors;
75
76
return $instance;',
77
            $constructor->getBody()
78
        );
79
    }
80
81
    public function testBodyStructureWithoutPublicProperties()
82
    {
83
        /* @var $valueHolder PropertyGenerator|\PHPUnit_Framework_MockObject_MockObject */
84
        $valueHolder        = $this->getMock(PropertyGenerator::class);
85
        /* @var $prefixInterceptors PropertyGenerator|\PHPUnit_Framework_MockObject_MockObject */
86
        $prefixInterceptors = $this->getMock(PropertyGenerator::class);
87
        /* @var $suffixInterceptors PropertyGenerator|\PHPUnit_Framework_MockObject_MockObject */
88
        $suffixInterceptors = $this->getMock(PropertyGenerator::class);
89
90
        $valueHolder->expects($this->any())->method('getName')->will($this->returnValue('foo'));
91
        $prefixInterceptors->expects($this->any())->method('getName')->will($this->returnValue('pre'));
92
        $suffixInterceptors->expects($this->any())->method('getName')->will($this->returnValue('post'));
93
94
        $constructor = new StaticProxyConstructor(
95
            new ReflectionClass(EmptyClass::class),
96
            $valueHolder,
97
            $prefixInterceptors,
98
            $suffixInterceptors
99
        );
100
101
        $this->assertSame('staticProxyConstructor', $constructor->getName());
102
        $this->assertCount(3, $constructor->getParameters());
103
        $this->assertSame(
104
            'static $reflection;
105
106
$reflection = $reflection ?: $reflection = new \ReflectionClass(__CLASS__);
107
$instance = (new \ReflectionClass(get_class()))->newInstanceWithoutConstructor();
108
109
$instance->foo = $wrappedObject;
110
$instance->pre = $prefixInterceptors;
111
$instance->post = $suffixInterceptors;
112
113
return $instance;',
114
            $constructor->getBody()
115
        );
116
    }
117
118
    /**
119
     * @group 276
120
     */
121
    public function testUnsetsPrivatePropertiesAsWell()
122
    {
123
        /* @var $valueHolder PropertyGenerator|\PHPUnit_Framework_MockObject_MockObject */
124
        $valueHolder        = $this->getMock(PropertyGenerator::class);
125
        /* @var $prefixInterceptors PropertyGenerator|\PHPUnit_Framework_MockObject_MockObject */
126
        $prefixInterceptors = $this->getMock(PropertyGenerator::class);
127
        /* @var $suffixInterceptors PropertyGenerator|\PHPUnit_Framework_MockObject_MockObject */
128
        $suffixInterceptors = $this->getMock(PropertyGenerator::class);
129
130
        $valueHolder->expects($this->any())->method('getName')->will($this->returnValue('foo'));
131
        $prefixInterceptors->expects($this->any())->method('getName')->will($this->returnValue('pre'));
132
        $suffixInterceptors->expects($this->any())->method('getName')->will($this->returnValue('post'));
133
134
        $constructor = new StaticProxyConstructor(
135
            new ReflectionClass(ClassWithMixedProperties::class),
136
            $valueHolder,
137
            $prefixInterceptors,
138
            $suffixInterceptors
139
        );
140
141
        self::assertContains(
142
            'unset($instance->publicProperty0, $instance->publicProperty1, $instance->publicProperty2, '
143
            . '$instance->protectedProperty0, $instance->protectedProperty1, $instance->protectedProperty2);
144
145
\Closure::bind(function (\ProxyManagerTestAsset\ClassWithMixedProperties $instance) {
146
    unset($instance->privateProperty0, $instance->privateProperty1, $instance->privateProperty2);
147
}, $instance, \'ProxyManagerTestAsset\\\\ClassWithMixedProperties\')->__invoke($instance);',
148
            $constructor->getBody()
149
        );
150
    }
151
}
152