|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TonicHealthCheck\Test\Incident\Siren\NotificationType; |
|
4
|
|
|
|
|
5
|
|
|
use Http\Client\Common\HttpMethodsClient; |
|
6
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
|
7
|
|
|
use Http\Mock\Client as MockClient; |
|
8
|
|
|
use PHPUnit_Framework_MockObject_MockObject; |
|
9
|
|
|
use PHPUnit_Framework_TestCase; |
|
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
11
|
|
|
use Psr\Http\Message\StreamInterface; |
|
12
|
|
|
use TonicHealthCheck\Incident\IncidentInterface; |
|
13
|
|
|
use TonicHealthCheck\Incident\Siren\NotificationType\RequestNotificationType; |
|
14
|
|
|
use TonicHealthCheck\Test\Incident\IncidentCreateTrait; |
|
15
|
|
|
use TonicHealthCheck\Test\Incident\Subject\SubjectCreateTrait; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class RequestNotificationTypeTest. |
|
19
|
|
|
*/ |
|
20
|
|
|
class RequestNotificationTypeTest extends PHPUnit_Framework_TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
use SubjectCreateTrait; |
|
23
|
|
|
use IncidentCreateTrait; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var PHPUnit_Framework_MockObject_MockObject|MockClient |
|
27
|
|
|
*/ |
|
28
|
|
|
private $mockClient; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var HttpMethodsClient |
|
32
|
|
|
*/ |
|
33
|
|
|
private $httpClient; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var RequestNotificationType; |
|
37
|
|
|
*/ |
|
38
|
|
|
private $requestNType; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
private $resourceUrl = '/incident'; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* set up base env Request type test. |
|
47
|
|
|
*/ |
|
48
|
|
|
public function setUp() |
|
49
|
|
|
{ |
|
50
|
|
|
$this->setMockClient(new MockClient()); |
|
51
|
|
|
|
|
52
|
|
|
$this->setHttpClient( |
|
53
|
|
|
$this |
|
54
|
|
|
->getMockBuilder(HttpMethodsClient::class) |
|
55
|
|
|
->setConstructorArgs([ |
|
56
|
|
|
$this->getMockClient(), |
|
57
|
|
|
MessageFactoryDiscovery::find(), |
|
58
|
|
|
]) |
|
59
|
|
|
->enableProxyingToOriginalMethods() |
|
60
|
|
|
->getMock() |
|
61
|
|
|
); |
|
62
|
|
|
|
|
63
|
|
|
$this->setRequestNType(new RequestNotificationType( |
|
64
|
|
|
$this->getHttpClient(), |
|
|
|
|
|
|
65
|
|
|
$this->getResourceUrl() |
|
66
|
|
|
)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Test request notify create. |
|
71
|
|
|
*/ |
|
72
|
|
|
public function testNotifyCreate() |
|
73
|
|
|
{ |
|
74
|
|
|
$this->assertEquals($this->getResourceUrl(), $this->getRequestNType()->getResourceUrl()); |
|
75
|
|
|
|
|
76
|
|
|
$incident = $this->createIncidentMock(); |
|
77
|
|
|
|
|
78
|
|
|
$incident |
|
|
|
|
|
|
79
|
|
|
->expects($this->any()) |
|
80
|
|
|
->method('getStatus') |
|
81
|
|
|
->willReturn(IncidentInterface::STATUS_OK + 1); |
|
82
|
|
|
|
|
83
|
|
|
$subject = $this->createSubject('target', '* * * * *'); |
|
84
|
|
|
|
|
85
|
|
|
$streamMock = $this->getMockBuilder(StreamInterface::class)->getMock(); |
|
86
|
|
|
|
|
87
|
|
|
$streamMock |
|
88
|
|
|
->expects($this->once()) |
|
89
|
|
|
->method('getContents') |
|
90
|
|
|
->willReturn('{"data":{"id":22}}'); |
|
91
|
|
|
|
|
92
|
|
|
$response = $this->getMockBuilder(ResponseInterface::class)->getMock(); |
|
93
|
|
|
|
|
94
|
|
|
$response->expects($this->once())->method('getBody')->willReturn($streamMock); |
|
95
|
|
|
|
|
96
|
|
|
$this->getMockClient()->addResponse($response); |
|
97
|
|
|
|
|
98
|
|
|
$this->getHttpClient() |
|
|
|
|
|
|
99
|
|
|
->expects($this->once()) |
|
100
|
|
|
->method('post') |
|
101
|
|
|
->with( |
|
102
|
|
|
$this->identicalTo($subject->getTarget().$this->getResourceUrl()), |
|
103
|
|
|
$this->identicalTo(['Content-type' => 'application/json']), |
|
104
|
|
|
$this->identicalTo( |
|
105
|
|
|
sprintf( |
|
106
|
|
|
'{"name":"%s","message":"%s","status":1,"visible":1}', |
|
107
|
|
|
$incident->getIdent(), |
|
|
|
|
|
|
108
|
|
|
$incident->getMessage() |
|
|
|
|
|
|
109
|
|
|
) |
|
110
|
|
|
) |
|
111
|
|
|
); |
|
112
|
|
|
|
|
113
|
|
|
$this->getRequestNType()->notify($subject, $incident); |
|
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Test request notify update. |
|
118
|
|
|
*/ |
|
119
|
|
|
public function testNotifyUpdate() |
|
120
|
|
|
{ |
|
121
|
|
|
$incident = $this->createIncidentMock(); |
|
122
|
|
|
|
|
123
|
|
|
$incident->expects($this->any())->method('getStatus')->willReturn(IncidentInterface::STATUS_OK); |
|
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
$subject = $this->createSubject('target', '* * * * *'); |
|
126
|
|
|
|
|
127
|
|
|
$this->getHttpClient() |
|
|
|
|
|
|
128
|
|
|
->expects($this->once()) |
|
129
|
|
|
->method('put') |
|
130
|
|
|
->with( |
|
131
|
|
|
$this->identicalTo($subject->getTarget().$this->getResourceUrl().'/'), |
|
132
|
|
|
$this->identicalTo(['Content-type' => 'application/json']), |
|
133
|
|
|
$this->identicalTo('{"status":4}') |
|
134
|
|
|
); |
|
135
|
|
|
|
|
136
|
|
|
$this->getRequestNType()->notify($subject, $incident); |
|
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @return MockClient |
|
141
|
|
|
*/ |
|
142
|
|
|
protected function getMockClient() |
|
143
|
|
|
{ |
|
144
|
|
|
return $this->mockClient; |
|
|
|
|
|
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param MockClient $mockClient |
|
149
|
|
|
*/ |
|
150
|
|
|
protected function setMockClient(MockClient $mockClient) |
|
151
|
|
|
{ |
|
152
|
|
|
$this->mockClient = $mockClient; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @return PHPUnit_Framework_MockObject_MockObject|HttpMethodsClient |
|
157
|
|
|
*/ |
|
158
|
|
|
protected function getHttpClient() |
|
159
|
|
|
{ |
|
160
|
|
|
return $this->httpClient; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @param HttpMethodsClient $httpClient |
|
165
|
|
|
*/ |
|
166
|
|
|
protected function setHttpClient(HttpMethodsClient $httpClient) |
|
167
|
|
|
{ |
|
168
|
|
|
$this->httpClient = $httpClient; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @return string |
|
173
|
|
|
*/ |
|
174
|
|
|
protected function getResourceUrl() |
|
175
|
|
|
{ |
|
176
|
|
|
return $this->resourceUrl; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @return RequestNotificationType |
|
181
|
|
|
*/ |
|
182
|
|
|
protected function getRequestNType() |
|
183
|
|
|
{ |
|
184
|
|
|
return $this->requestNType; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* @param RequestNotificationType $requestNType |
|
189
|
|
|
*/ |
|
190
|
|
|
protected function setRequestNType(RequestNotificationType $requestNType) |
|
191
|
|
|
{ |
|
192
|
|
|
$this->requestNType = $requestNType; |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.