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

PublicScopeSimulatorTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 247
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 247
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
B testSimpleGet() 0 44 1
B testSimpleSet() 0 33 1
B testSimpleIsset() 0 33 1
B testSimpleUnset() 0 33 1
A testSetRequiresValueParameterName() 0 12 1
B testDelegatesToValueHolderWhenAvailable() 0 33 1
A testSetRequiresValidOperation() 0 6 1
B testWillReturnDirectlyWithNoReturnParam() 0 43 1
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\ProxyGenerator\Util;
22
23
use InvalidArgumentException;
24
use PHPUnit_Framework_TestCase;
25
use ProxyManager\ProxyGenerator\Util\PublicScopeSimulator;
26
use Zend\Code\Generator\PropertyGenerator;
27
28
/**
29
 * Tests for {@see \ProxyManager\ProxyGenerator\Util\PublicScopeSimulator}
30
 *
31
 * @author Marco Pivetta <[email protected]>
32
 * @license MIT
33
 *
34
 * @covers \ProxyManager\ProxyGenerator\Util\PublicScopeSimulator
35
 * @group Coverage
36
 */
37
class PublicScopeSimulatorTest extends PHPUnit_Framework_TestCase
38
{
39
    public function testSimpleGet() : void
40
    {
41
        $expected = <<<'PHP'
42
$realInstanceReflection = new \ReflectionClass(get_parent_class($this));
43
44
if (! $realInstanceReflection->hasProperty($foo)) {
45
    $targetObject = $this;
46
47
    $backtrace = debug_backtrace(false);
48
    trigger_error(
49
        sprintf(
50
            'Undefined property: %s::$%s in %s on line %s',
51
            get_parent_class($this),
52
            $foo,
53
            $backtrace[0]['file'],
54
            $backtrace[0]['line']
55
        ),
56
        \E_USER_NOTICE
57
    );
58
    return $targetObject->$foo;
59
    return;
60
}
61
62
$targetObject = unserialize(sprintf('O:%d:"%s":0:{}', strlen(get_parent_class($this)), get_parent_class($this)));
63
$accessor = function & () use ($targetObject, $name) {
64
    return $targetObject->$foo;
65
};
66
$backtrace = debug_backtrace(true);
67
$scopeObject = isset($backtrace[1]['object']) ? $backtrace[1]['object'] : new \ProxyManager\Stub\EmptyClassStub();
68
$accessor = $accessor->bindTo($scopeObject, get_class($scopeObject));
69
$bar = & $accessor();
70
PHP;
71
72
        self::assertSame(
73
            $expected,
74
            PublicScopeSimulator::getPublicAccessSimulationCode(
75
                PublicScopeSimulator::OPERATION_GET,
76
                'foo',
77
                null,
78
                null,
79
                'bar'
80
            )
81
        );
82
    }
83
84
    public function testSimpleSet() : void
85
    {
86
        $expected = <<<'PHP'
87
$realInstanceReflection = new \ReflectionClass(get_parent_class($this));
88
89
if (! $realInstanceReflection->hasProperty($foo)) {
90
    $targetObject = $this;
91
92
    return $targetObject->$foo = $baz;
93
    return;
94
}
95
96
$targetObject = unserialize(sprintf('O:%d:"%s":0:{}', strlen(get_parent_class($this)), get_parent_class($this)));
97
$accessor = function & () use ($targetObject, $name, $value) {
98
    return $targetObject->$foo = $baz;
99
};
100
$backtrace = debug_backtrace(true);
101
$scopeObject = isset($backtrace[1]['object']) ? $backtrace[1]['object'] : new \ProxyManager\Stub\EmptyClassStub();
102
$accessor = $accessor->bindTo($scopeObject, get_class($scopeObject));
103
$bar = & $accessor();
104
PHP;
105
106
        self::assertSame(
107
            $expected,
108
            PublicScopeSimulator::getPublicAccessSimulationCode(
109
                PublicScopeSimulator::OPERATION_SET,
110
                'foo',
111
                'baz',
112
                null,
113
                'bar'
114
            )
115
        );
116
    }
117
118
    public function testSimpleIsset() : void
119
    {
120
        $expected = <<<'PHP'
121
$realInstanceReflection = new \ReflectionClass(get_parent_class($this));
122
123
if (! $realInstanceReflection->hasProperty($foo)) {
124
    $targetObject = $this;
125
126
    return isset($targetObject->$foo);
127
    return;
128
}
129
130
$targetObject = unserialize(sprintf('O:%d:"%s":0:{}', strlen(get_parent_class($this)), get_parent_class($this)));
131
$accessor = function () use ($targetObject, $name) {
132
    return isset($targetObject->$foo);
133
};
134
$backtrace = debug_backtrace(true);
135
$scopeObject = isset($backtrace[1]['object']) ? $backtrace[1]['object'] : new \ProxyManager\Stub\EmptyClassStub();
136
$accessor = $accessor->bindTo($scopeObject, get_class($scopeObject));
137
$bar = $accessor();
138
PHP;
139
140
        self::assertSame(
141
            $expected,
142
            PublicScopeSimulator::getPublicAccessSimulationCode(
143
                PublicScopeSimulator::OPERATION_ISSET,
144
                'foo',
145
                null,
146
                null,
147
                'bar'
148
            )
149
        );
150
    }
151
152
    public function testSimpleUnset() : void
153
    {
154
        $expected = <<<'PHP'
155
$realInstanceReflection = new \ReflectionClass(get_parent_class($this));
156
157
if (! $realInstanceReflection->hasProperty($foo)) {
158
    $targetObject = $this;
159
160
    unset($targetObject->$foo);
161
    return;
162
}
163
164
$targetObject = unserialize(sprintf('O:%d:"%s":0:{}', strlen(get_parent_class($this)), get_parent_class($this)));
165
$accessor = function () use ($targetObject, $name) {
166
    unset($targetObject->$foo);
167
};
168
$backtrace = debug_backtrace(true);
169
$scopeObject = isset($backtrace[1]['object']) ? $backtrace[1]['object'] : new \ProxyManager\Stub\EmptyClassStub();
170
$accessor = $accessor->bindTo($scopeObject, get_class($scopeObject));
171
$bar = $accessor();
172
PHP;
173
174
        self::assertSame(
175
            $expected,
176
            PublicScopeSimulator::getPublicAccessSimulationCode(
177
                PublicScopeSimulator::OPERATION_UNSET,
178
                'foo',
179
                null,
180
                null,
181
                'bar'
182
            )
183
        );
184
    }
185
186
    public function testSetRequiresValueParameterName() : void
187
    {
188
        $this->expectException(InvalidArgumentException::class);
189
190
        PublicScopeSimulator::getPublicAccessSimulationCode(
191
            PublicScopeSimulator::OPERATION_SET,
192
            'foo',
193
            null,
194
            null,
195
            'bar'
196
        );
197
    }
198
199
    public function testDelegatesToValueHolderWhenAvailable() : void
200
    {
201
        $expected = <<<'PHP'
202
$realInstanceReflection = new \ReflectionClass(get_parent_class($this));
203
204
if (! $realInstanceReflection->hasProperty($foo)) {
205
    $targetObject = $this->valueHolder;
206
207
    return $targetObject->$foo = $baz;
208
    return;
209
}
210
211
$targetObject = $this->valueHolder;
212
$accessor = function & () use ($targetObject, $name, $value) {
213
    return $targetObject->$foo = $baz;
214
};
215
$backtrace = debug_backtrace(true);
216
$scopeObject = isset($backtrace[1]['object']) ? $backtrace[1]['object'] : new \ProxyManager\Stub\EmptyClassStub();
217
$accessor = $accessor->bindTo($scopeObject, get_class($scopeObject));
218
$bar = & $accessor();
219
PHP;
220
221
        self::assertSame(
222
            $expected,
223
            PublicScopeSimulator::getPublicAccessSimulationCode(
224
                PublicScopeSimulator::OPERATION_SET,
225
                'foo',
226
                'baz',
227
                new PropertyGenerator('valueHolder'),
228
                'bar'
229
            )
230
        );
231
    }
232
233
    public function testSetRequiresValidOperation() : void
234
    {
235
        $this->expectException(InvalidArgumentException::class);
236
237
        PublicScopeSimulator::getPublicAccessSimulationCode('invalid', 'foo');
238
    }
239
240
    public function testWillReturnDirectlyWithNoReturnParam() : void
241
    {
242
        $expected = <<<'PHP'
243
$realInstanceReflection = new \ReflectionClass(get_parent_class($this));
244
245
if (! $realInstanceReflection->hasProperty($foo)) {
246
    $targetObject = $this;
247
248
    $backtrace = debug_backtrace(false);
249
    trigger_error(
250
        sprintf(
251
            'Undefined property: %s::$%s in %s on line %s',
252
            get_parent_class($this),
253
            $foo,
254
            $backtrace[0]['file'],
255
            $backtrace[0]['line']
256
        ),
257
        \E_USER_NOTICE
258
    );
259
    return $targetObject->$foo;
260
    return;
261
}
262
263
$targetObject = unserialize(sprintf('O:%d:"%s":0:{}', strlen(get_parent_class($this)), get_parent_class($this)));
264
$accessor = function & () use ($targetObject, $name) {
265
    return $targetObject->$foo;
266
};
267
$backtrace = debug_backtrace(true);
268
$scopeObject = isset($backtrace[1]['object']) ? $backtrace[1]['object'] : new \ProxyManager\Stub\EmptyClassStub();
269
$accessor = $accessor->bindTo($scopeObject, get_class($scopeObject));
270
$returnValue = & $accessor();
271
272
return $returnValue;
273
PHP;
274
275
        self::assertSame(
276
            $expected,
277
            PublicScopeSimulator::getPublicAccessSimulationCode(
278
                PublicScopeSimulator::OPERATION_GET,
279
                'foo'
280
            )
281
        );
282
    }
283
}
284