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

MultipleProxyGenerationTest::getTestedClasses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 20
rs 9.4286
cc 1
eloc 17
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\Functional;
20
21
use PHPUnit_Framework_TestCase;
22
use ProxyManager\Factory\AccessInterceptorScopeLocalizerFactory;
23
use ProxyManager\Factory\AccessInterceptorValueHolderFactory;
24
use ProxyManager\Factory\LazyLoadingGhostFactory;
25
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
26
use ProxyManager\Proxy\AccessInterceptorInterface;
27
use ProxyManager\Proxy\GhostObjectInterface;
28
use ProxyManager\Proxy\ValueHolderInterface;
29
use ProxyManager\Proxy\VirtualProxyInterface;
30
use ProxyManagerTestAsset\BaseClass;
31
use ProxyManagerTestAsset\ClassWithByRefMagicMethods;
32
use ProxyManagerTestAsset\ClassWithCollidingPrivateInheritedProperties;
33
use ProxyManagerTestAsset\ClassWithFinalMagicMethods;
34
use ProxyManagerTestAsset\ClassWithFinalMethods;
35
use ProxyManagerTestAsset\ClassWithMagicMethods;
36
use ProxyManagerTestAsset\ClassWithMethodWithByRefVariadicFunction;
37
use ProxyManagerTestAsset\ClassWithMethodWithVariadicFunction;
38
use ProxyManagerTestAsset\ClassWithMixedProperties;
39
use ProxyManagerTestAsset\ClassWithPrivateProperties;
40
use ProxyManagerTestAsset\ClassWithProtectedProperties;
41
use ProxyManagerTestAsset\ClassWithPublicProperties;
42
use ProxyManagerTestAsset\ClassWithSelfHint;
43
use ProxyManagerTestAsset\EmptyClass;
44
use ProxyManagerTestAsset\HydratedObject;
45
46
/**
47
 * Verifies that proxy factories don't conflict with each other when generating proxies
48
 *
49
 * @author Marco Pivetta <[email protected]>
50
 * @license MIT
51
 *
52
 * @link https://github.com/Ocramius/ProxyManager/issues/10
53
 *
54
 * @group Functional
55
 * @group issue-10
56
 * @coversNothing
57
 */
58
class MultipleProxyGenerationTest extends PHPUnit_Framework_TestCase
59
{
60
    /**
61
     * Verifies that proxies generated from different factories will retain their specific implementation
62
     * and won't conflict
63
     *
64
     * @dataProvider getTestedClasses
65
     *
66
     * @param string $className
67
     */
68
    public function testCanGenerateMultipleDifferentProxiesForSameClass($className)
69
    {
70
        $ghostProxyFactory                      = new LazyLoadingGhostFactory();
71
        $virtualProxyFactory                    = new LazyLoadingValueHolderFactory();
72
        $accessInterceptorFactory               = new AccessInterceptorValueHolderFactory();
73
        $accessInterceptorScopeLocalizerFactory = new AccessInterceptorScopeLocalizerFactory();
74
        $initializer                            = function () {
75
        };
76
77
        $generated = [
78
            $ghostProxyFactory->createProxy($className, $initializer),
79
            $virtualProxyFactory->createProxy($className, $initializer),
80
            $accessInterceptorFactory->createProxy(new $className()),
81
            $accessInterceptorScopeLocalizerFactory->createProxy(new $className()),
82
        ];
83
84
        foreach ($generated as $key => $proxy) {
85
            $this->assertInstanceOf($className, $proxy);
86
87
            foreach ($generated as $comparedKey => $comparedProxy) {
88
                if ($comparedKey === $key) {
89
                    continue;
90
                }
91
92
                $this->assertNotSame(get_class($comparedProxy), get_class($proxy));
93
            }
94
        }
95
96
        $this->assertInstanceOf(GhostObjectInterface::class, $generated[0]);
97
        $this->assertInstanceOf(VirtualProxyInterface::class, $generated[1]);
98
        $this->assertInstanceOf(AccessInterceptorInterface::class, $generated[2]);
99
        $this->assertInstanceOf(ValueHolderInterface::class, $generated[2]);
100
        $this->assertInstanceOf(AccessInterceptorInterface::class, $generated[3]);
101
    }
102
103
    /**
104
     * @return string[][]
105
     */
106
    public function getTestedClasses()
107
    {
108
        return [
109
            [BaseClass::class],
110
            [ClassWithMagicMethods::class],
111
            [ClassWithFinalMethods::class],
112
            [ClassWithFinalMagicMethods::class],
113
            [ClassWithByRefMagicMethods::class],
114
            [ClassWithMixedProperties::class],
115
            [ClassWithPrivateProperties::class],
116
            [ClassWithProtectedProperties::class],
117
            [ClassWithPublicProperties::class],
118
            [EmptyClass::class],
119
            [HydratedObject::class],
120
            [ClassWithSelfHint::class],
121
            [ClassWithCollidingPrivateInheritedProperties::class],
122
            [ClassWithMethodWithVariadicFunction::class],
123
            [ClassWithMethodWithByRefVariadicFunction::class],
124
        ];
125
    }
126
}
127