|
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)) |
|
|
|
|
|
|
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)) |
|
|
|
|
|
|
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)) |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
65
|
|
|
$chain->add($visitor1, 0); |
|
|
|
|
|
|
66
|
|
|
$chain->add($visitor2, 1); |
|
|
|
|
|
|
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()) |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
105
|
|
|
$chain->add($visitor2, 2); |
|
|
|
|
|
|
106
|
|
|
$chain->add($visitor3, 1); |
|
|
|
|
|
|
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
|
|
|
|
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:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: