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

ConstructorTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 1
cbo 3
dl 0
loc 156
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B testBodyStructure() 0 32 1
B testBodyStructureWithoutPublicProperties() 0 26 1
B testBodyStructureWithPhp4StyleConstructor() 0 34 2
B testBodyStructureWithStaticProperties() 0 29 1
B testBodyStructureWithVariadicArguments() 0 28 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
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'));
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...
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'));
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...
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 testBodyStructureWithPhp4StyleConstructor()
102
    {
103
        if (PHP_VERSION_ID >= 70000) {
104
            $this->markTestSkipped('Can\'t run this test on PHP7 or high version!');
105
        }
106
107
        $className = uniqid('ClassWithPhp4Constructor');
108
109
        eval('class ' . $className . '{ public function ' . $className . '($first, $second, $third) {}}');
110
111
        /* @var $valueHolder PropertyGenerator|\PHPUnit_Framework_MockObject_MockObject */
112
        $valueHolder = $this->getMock(PropertyGenerator::class);
113
114
        $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...
115
116
        $constructor = Constructor::generateMethod(
117
            new ReflectionClass($className),
118
            $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...
119
        );
120
121
        $this->assertSame($className, $constructor->getName());
122
        $this->assertCount(3, $constructor->getParameters());
123
        $this->assertSame(
124
            'static $reflection;
125
126
if (! $this->foo) {
127
    $reflection = $reflection ?: new \ReflectionClass(\'' . $className . '\');
128
    $this->foo = $reflection->newInstanceWithoutConstructor();
129
}
130
131
$this->foo->' . $className . '($first, $second, $third);',
132
            $constructor->getBody()
133
        );
134
    }
135
136
    public function testBodyStructureWithStaticProperties()
137
    {
138
        /* @var $valueHolder PropertyGenerator|\PHPUnit_Framework_MockObject_MockObject */
139
        $valueHolder = $this->getMock(PropertyGenerator::class);
140
141
        $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...
142
143
        $constructor = Constructor::generateMethod(new ReflectionClass(ClassWithMixedProperties::class), $valueHolder);
144
145
        $this->assertSame('__construct', $constructor->getName());
146
        $this->assertCount(0, $constructor->getParameters());
147
148
        $expectedCode = <<<'PHP'
149
static $reflection;
150
151
if (! $this->foo) {
152
    $reflection = $reflection ?: new \ReflectionClass('ProxyManagerTestAsset\\ClassWithMixedProperties');
153
    $this->foo = $reflection->newInstanceWithoutConstructor();
154
155
    unset($this->publicProperty0);
156
    unset($this->publicProperty1);
157
    unset($this->publicProperty2);
158
}
159
160
$this->foo->__construct();
161
PHP;
162
163
        $this->assertSame($expectedCode, $constructor->getBody());
164
    }
165
166
    public function testBodyStructureWithVariadicArguments()
167
    {
168
        /* @var $valueHolder PropertyGenerator|\PHPUnit_Framework_MockObject_MockObject */
169
        $valueHolder = $this->getMock(PropertyGenerator::class);
170
171
        $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...
172
173
        $constructor = Constructor::generateMethod(
174
            new ReflectionClass(ClassWithVariadicConstructorArgument::class),
175
            $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...
176
        );
177
178
        $this->assertSame('__construct', $constructor->getName());
179
        $this->assertCount(2, $constructor->getParameters());
180
181
        $expectedCode = <<<'PHP'
182
static $reflection;
183
184
if (! $this->foo) {
185
    $reflection = $reflection ?: new \ReflectionClass('ProxyManagerTestAsset\\ClassWithVariadicConstructorArgument');
186
    $this->foo = $reflection->newInstanceWithoutConstructor();
187
}
188
189
$this->foo->__construct($foo, ...$bar);
190
PHP;
191
192
        $this->assertSame($expectedCode, $constructor->getBody());
193
    }
194
}
195