Completed
Pull Request — master (#271)
by Marco
03:11
created

testBodyStructureWithPhp4StyleConstructor()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 34
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 34
rs 8.8571
c 1
b 0
f 0
cc 2
eloc 17
nc 2
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\ValueHolder\MethodGenerator;
20
21
use PHPUnit_Framework_TestCase;
22
use ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator\Constructor;
23
use ProxyManagerTestAsset\ClassWithMixedProperties;
24
use ProxyManagerTestAsset\ClassWithVariadicConstructorArgument;
25
use ProxyManagerTestAsset\EmptyClass;
26
use ProxyManagerTestAsset\ProxyGenerator\LazyLoading\MethodGenerator\ClassWithTwoPublicProperties;
27
use ReflectionClass;
28
use Zend\Code\Generator\PropertyGenerator;
29
30
/**
31
 * Tests for {@see \ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator\Constructor}
32
 *
33
 * @author Marco Pivetta <[email protected]>
34
 * @license MIT
35
 *
36
 * @covers \ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator\Constructor
37
 * @group Coverage
38
 */
39
class ConstructorTest extends PHPUnit_Framework_TestCase
40
{
41
    public function testBodyStructure()
42
    {
43
        /* @var $valueHolder PropertyGenerator|\PHPUnit_Framework_MockObject_MockObject */
44
        $valueHolder = $this->getMock(PropertyGenerator::class);
45
46
        $valueHolder->expects($this->any())->method('getName')->will($this->returnValue('foo'));
47
48
        $constructor = Constructor::generateMethod(
49
            new ReflectionClass(
50
                ClassWithTwoPublicProperties::class
51
            ),
52
            $valueHolder
1 ignored issue
show
Bug introduced by
It seems like $valueHolder can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, ProxyManager\ProxyGenera...uctor::generateMethod() does only seem to accept object<Zend\Code\Generator\PropertyGenerator>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
53
        );
54
55
        $this->assertSame('__construct', $constructor->getName());
56
        $this->assertCount(0, $constructor->getParameters());
57
        $this->assertSame(
58
            'static $reflection;
59
60
if (! $this->foo) {
61
    $reflection = $reflection ?: new \ReflectionClass(\'ProxyManagerTestAsset\\\\ProxyGenerator\\\\LazyLoading\\\\'
62
            . 'MethodGenerator\\\\ClassWithTwoPublicProperties\');
63
    $this->foo = $reflection->newInstanceWithoutConstructor();
64
65
    unset($this->bar);
66
    unset($this->baz);
67
}
68
69
$this->foo->__construct();',
70
            $constructor->getBody()
71
        );
72
    }
73
74
    public function testBodyStructureWithoutPublicProperties()
75
    {
76
        /* @var $valueHolder PropertyGenerator|\PHPUnit_Framework_MockObject_MockObject */
77
        $valueHolder = $this->getMock(PropertyGenerator::class);
78
79
        $valueHolder->expects($this->any())->method('getName')->will($this->returnValue('foo'));
80
81
        $constructor = Constructor::generateMethod(
82
            new ReflectionClass(EmptyClass::class),
83
            $valueHolder
1 ignored issue
show
Bug introduced by
It seems like $valueHolder can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, ProxyManager\ProxyGenera...uctor::generateMethod() does only seem to accept object<Zend\Code\Generator\PropertyGenerator>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
84
        );
85
86
        $this->assertSame('__construct', $constructor->getName());
87
        $this->assertCount(0, $constructor->getParameters());
88
        $this->assertSame(
89
            'static $reflection;
90
91
if (! $this->foo) {
92
    $reflection = $reflection ?: new \ReflectionClass(\'ProxyManagerTestAsset\\\\EmptyClass\');
93
    $this->foo = $reflection->newInstanceWithoutConstructor();
94
}
95
96
$this->foo->__construct();',
97
            $constructor->getBody()
98
        );
99
    }
100
101
    public function testBodyStructureWithStaticProperties()
102
    {
103
        /* @var $valueHolder PropertyGenerator|\PHPUnit_Framework_MockObject_MockObject */
104
        $valueHolder = $this->getMock(PropertyGenerator::class);
105
106
        $valueHolder->expects($this->any())->method('getName')->will($this->returnValue('foo'));
1 ignored issue
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Zend\Code\Generator\PropertyGenerator.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
107
108
        $constructor = Constructor::generateMethod(new ReflectionClass(ClassWithMixedProperties::class), $valueHolder);
109
110
        $this->assertSame('__construct', $constructor->getName());
111
        $this->assertCount(0, $constructor->getParameters());
112
113
        $expectedCode = <<<'PHP'
114
static $reflection;
115
116
if (! $this->foo) {
117
    $reflection = $reflection ?: new \ReflectionClass('ProxyManagerTestAsset\\ClassWithMixedProperties');
118
    $this->foo = $reflection->newInstanceWithoutConstructor();
119
120
    unset($this->publicProperty0);
121
    unset($this->publicProperty1);
122
    unset($this->publicProperty2);
123
}
124
125
$this->foo->__construct();
126
PHP;
127
128
        $this->assertSame($expectedCode, $constructor->getBody());
129
    }
130
131
    public function testBodyStructureWithVariadicArguments()
132
    {
133
        /* @var $valueHolder PropertyGenerator|\PHPUnit_Framework_MockObject_MockObject */
134
        $valueHolder = $this->getMock(PropertyGenerator::class);
135
136
        $valueHolder->expects($this->any())->method('getName')->will($this->returnValue('foo'));
1 ignored issue
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Zend\Code\Generator\PropertyGenerator.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
137
138
        $constructor = Constructor::generateMethod(
139
            new ReflectionClass(ClassWithVariadicConstructorArgument::class),
140
            $valueHolder
1 ignored issue
show
Bug introduced by
It seems like $valueHolder can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, ProxyManager\ProxyGenera...uctor::generateMethod() does only seem to accept object<Zend\Code\Generator\PropertyGenerator>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
141
        );
142
143
        $this->assertSame('__construct', $constructor->getName());
144
        $this->assertCount(2, $constructor->getParameters());
145
146
        $expectedCode = <<<'PHP'
147
static $reflection;
148
149
if (! $this->foo) {
150
    $reflection = $reflection ?: new \ReflectionClass('ProxyManagerTestAsset\\ClassWithVariadicConstructorArgument');
151
    $this->foo = $reflection->newInstanceWithoutConstructor();
152
}
153
154
$this->foo->__construct($foo, ...$bar);
155
PHP;
156
157
        $this->assertSame($expectedCode, $constructor->getBody());
158
    }
159
}
160