Test Failed
Push — master ( 0ee32e...41c938 )
by Dan
07:10
created

SuperClosureTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 61
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testSerializeCalled() 0 14 1
A testSerializeNonClosure() 0 6 1
A testUnserializeCalled() 0 8 1
1
<?php
2
3
namespace Tests\Router\Serializer;
4
5
use Ds\Router\Interfaces\SerializerInterface;
6
use Ds\Router\Serializer\SuperClosure;
7
use SuperClosure\Serializer;
8
9
/**
10
 * Class SuperClosureTest
11
 * @package Tests\Router\Serializer
12
 */
13
class SuperClosureTest extends \PHPUnit_Framework_TestCase
14
{
15
    /**
16
     * @var SerializerInterface
17
     */
18
    public $serializer;
19
20
    /**
21
     * @var \PHPUnit_Framework_MockObject_MockObject|\SuperClosure\Serializer
22
     */
23
    public $superClosure;
24
25
    /**
26
     *
27
     */
28
    public function setUp()
29
    {
30
        $this->superClosure = $this->getMockBuilder(Serializer::class)->getMock();
31
        $this->serializer = new SuperClosure($this->superClosure);
32
    }
33
34
    /**
35
     *
36
     */
37
    public function testSerializeCalled()
38
    {
39
        $data = function () {
40
            return true;
41
        };
42
43
        $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...
44
            ->expects($this->once())
45
            ->method('serialize')
46
            ->with(
47
                $this->equalTo($data)
48
            );
49
        $this->serializer->serialize($data);
50
    }
51
52
    /**
53
     *
54
     */
55
    public function testSerializeNonClosure()
56
    {
57
        $this->setExpectedException(\Exception::class);
58
        $data = $this->superClosure;
59
        $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...
60
    }
61
62
    /**
63
     *
64
     */
65
    public function testUnserializeCalled()
66
    {
67
        $data = 'some-string';
68
        $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...
69
            ->expects($this->once())
70
            ->method('unserialize');
71
        $this->serializer->unserialize($data);
72
    }
73
}
74