SuperClosureTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of the DS Framework.
4
 *
5
 * (c) Dan Smith <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Tests\Ds\Router\Serializer;
11
12
use Ds\Router\Interfaces\SerializerInterface;
13
use Ds\Router\Serializer\SuperClosure;
14
use SuperClosure\Serializer;
15
16
/**
17
 * Class SuperClosureTest
18
 * @package Tests\Ds\Router\Serializer
19
 */
20
class SuperClosureTest extends \PHPUnit_Framework_TestCase
21
{
22
    /**
23
     * @var SerializerInterface
24
     */
25
    public $serializer;
26
27
    /**
28
     * @var \PHPUnit_Framework_MockObject_MockObject|\SuperClosure\Serializer
29
     */
30
    public $superClosure;
31
32
    /**
33
     *
34
     */
35
    public function setUp()
36
    {
37
        $this->superClosure = $this->getMockBuilder(Serializer::class)->getMock();
38
        $this->serializer = new SuperClosure($this->superClosure);
39
    }
40
41
    /**
42
     *
43
     */
44
    public function testSerializeCalled()
45
    {
46
        $data = function () {
47
            return true;
48
        };
49
50
        $this->superClosure
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in SuperClosure\Serializer.

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...
51
            ->expects($this->once())
52
            ->method('serialize')
53
            ->with(
54
                $this->equalTo($data)
55
            );
56
        $this->serializer->serialize($data);
57
    }
58
59
    /**
60
     *
61
     */
62
    public function testSerializeNonClosure()
63
    {
64
        $this->setExpectedException(\Exception::class);
65
        $data = $this->superClosure;
66
        $this->serializer->serialize($data);
0 ignored issues
show
Documentation introduced by
$data is of type object<PHPUnit_Framework...uperClosure\Serializer>, but the function expects a object<Closure>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
67
    }
68
69
    /**
70
     *
71
     */
72
    public function testUnserializeCalled()
73
    {
74
        $data = 'some-string';
75
        $this->superClosure
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in SuperClosure\Serializer.

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...
76
            ->expects($this->once())
77
            ->method('unserialize');
78
        $this->serializer->unserialize($data);
79
    }
80
}
81