Completed
Push — master ( 58620c...f23d9f )
by Vitaliy
02:20
created

shouldNotCallNextCheckedIfPreviouslyCheckWithError()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 19
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the AppleApnPush package
5
 *
6
 * (c) Vitaliy Zhuk <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code
10
 */
11
12
namespace Tests\Apple\ApnPush\Protocol\Http\Visitor;
13
14
use Apple\ApnPush\Model\Message;
15
use Apple\ApnPush\Protocol\Http\Request;
16
use Apple\ApnPush\Protocol\Http\Visitor\HttpProtocolChainVisitor;
17
use Apple\ApnPush\Protocol\Http\Visitor\HttpProtocolVisitorInterface;
18
use PHPUnit\Framework\TestCase;
19
20
class HttpProtocolChainVisitorTest extends TestCase
21
{
22
    /**
23
     * @test
24
     */
25
    public function shouldSuccessCallsWithPriority()
26
    {
27
        $message = self::createMock(Message::class);
28
        $request = self::createMock(Request::class);
29
30
        $visitor1 = $this->createVisitor();
31
        $visitor2 = $this->createVisitor();
32
        $visitor3 = $this->createVisitor();
33
34
        $calls = [];
35
36
        $visitor3->expects(self::exactly(2))
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Apple\ApnPush\Protocol\H...rotocolVisitorInterface.

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...
37
            ->method('visit')
38
            ->with($message, $request)
39
            ->willReturnCallback(function () use (&$calls, $request) {
40
                $calls[] = 3;
41
42
                return $request;
43
            });
44
45
        $visitor1->expects(self::exactly(2))
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Apple\ApnPush\Protocol\H...rotocolVisitorInterface.

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...
46
            ->method('visit')
47
            ->with($message, $request)
48
            ->willReturnCallback(function () use (&$calls, $request) {
49
                $calls[] = 1;
50
51
                return $request;
52
            });
53
54
        $visitor2->expects(self::exactly(2))
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Apple\ApnPush\Protocol\H...rotocolVisitorInterface.

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...
55
            ->method('visit')
56
            ->with($message, $request)
57
            ->willReturnCallback(function () use (&$calls, $request) {
58
                $calls[] = 2;
59
60
                return $request;
61
            });
62
63
        $chain = new HttpProtocolChainVisitor();
64
        $chain->add($visitor3, -1);
0 ignored issues
show
Bug introduced by
It seems like $visitor3 defined by $this->createVisitor() on line 32 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Apple\ApnPush\Protocol\H...ocolChainVisitor::add() does only seem to accept object<Apple\ApnPush\Pro...otocolVisitorInterface>, 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...
65
        $chain->add($visitor1, 0);
0 ignored issues
show
Bug introduced by
It seems like $visitor1 defined by $this->createVisitor() on line 30 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Apple\ApnPush\Protocol\H...ocolChainVisitor::add() does only seem to accept object<Apple\ApnPush\Pro...otocolVisitorInterface>, 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...
66
        $chain->add($visitor2, 1);
0 ignored issues
show
Bug introduced by
It seems like $visitor2 defined by $this->createVisitor() on line 31 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Apple\ApnPush\Protocol\H...ocolChainVisitor::add() does only seem to accept object<Apple\ApnPush\Pro...otocolVisitorInterface>, 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...
67
68
        $chain->visit($message, $request);
69
        // Call to second iteration
70
        $chain->visit($message, $request);
71
72
        self::assertEquals([
73
            2, 1, 3,
74
            2, 1, 3,
75
        ], $calls);
76
    }
77
78
    /**
79
     * @test
80
     *
81
     * @expectedException \InvalidArgumentException
82
     */
83
    public function shouldNotCallNextCheckedIfPreviouslyCheckWithError()
84
    {
85
        $message = self::createMock(Message::class);
86
        $request = self::createMock(Request::class);
87
88
        $visitor1 = $this->createVisitor();
89
        $visitor2 = $this->createVisitor();
90
        $visitor3 = $this->createVisitor();
91
92
        $visitor1->expects(self::once())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Apple\ApnPush\Protocol\H...rotocolVisitorInterface.

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...
93
            ->method('visit')
94
            ->with($message, $request)
95
            ->willThrowException(new \InvalidArgumentException());
96
97
        $visitor2->expects(self::never())
98
            ->method('visit');
99
100
        $visitor3->expects(self::never())
101
            ->method('visit');
102
103
        $chain = new HttpProtocolChainVisitor();
104
        $chain->add($visitor1, 3);
0 ignored issues
show
Bug introduced by
It seems like $visitor1 defined by $this->createVisitor() on line 88 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Apple\ApnPush\Protocol\H...ocolChainVisitor::add() does only seem to accept object<Apple\ApnPush\Pro...otocolVisitorInterface>, 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...
105
        $chain->add($visitor2, 2);
0 ignored issues
show
Bug introduced by
It seems like $visitor2 defined by $this->createVisitor() on line 89 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Apple\ApnPush\Protocol\H...ocolChainVisitor::add() does only seem to accept object<Apple\ApnPush\Pro...otocolVisitorInterface>, 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...
106
        $chain->add($visitor3, 1);
0 ignored issues
show
Bug introduced by
It seems like $visitor3 defined by $this->createVisitor() on line 90 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Apple\ApnPush\Protocol\H...ocolChainVisitor::add() does only seem to accept object<Apple\ApnPush\Pro...otocolVisitorInterface>, 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...
107
108
        $chain->visit($message, $request);
109
    }
110
111
    /**
112
     * Create visitor mock
113
     *
114
     * @return HttpProtocolVisitorInterface|\PHPUnit_Framework_MockObject_MockObject
115
     */
116
    private function createVisitor()
117
    {
118
        $className = sprintf(
119
            'HttpProtocolVisitorInterface_%s',
120
            md5(uniqid(random_int(0, 9999), true))
121
        );
122
123
        $visitor = $this->getMockBuilder(HttpProtocolVisitorInterface::class)
124
            ->setMockClassName($className);
125
126
        return $visitor->getMock();
127
    }
128
}
129