1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\HttplugBundle\Tests\Unit\Collector; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\Request; |
6
|
|
|
use GuzzleHttp\Psr7\Response; |
7
|
|
|
use GuzzleHttp\Psr7\Uri; |
8
|
|
|
use Http\Client\HttpAsyncClient; |
9
|
|
|
use Http\Client\HttpClient; |
10
|
|
|
use Http\HttplugBundle\Collector\Collector; |
11
|
|
|
use Http\HttplugBundle\Collector\Formatter; |
12
|
|
|
use Http\HttplugBundle\Collector\ProfileClient; |
13
|
|
|
use Http\HttplugBundle\Collector\Stack; |
14
|
|
|
use Http\Promise\FulfilledPromise; |
15
|
|
|
use Http\Promise\Promise; |
16
|
|
|
use Http\Promise\RejectedPromise; |
17
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
18
|
|
|
use PHPUnit\Framework\TestCase; |
19
|
|
|
use Psr\Http\Message\RequestInterface; |
20
|
|
|
use Psr\Http\Message\ResponseInterface; |
21
|
|
|
use Psr\Http\Message\UriInterface; |
22
|
|
|
use Symfony\Component\Stopwatch\Stopwatch; |
23
|
|
|
use Symfony\Component\Stopwatch\StopwatchEvent; |
24
|
|
|
|
25
|
|
|
class ProfileClientTest extends TestCase |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var Collector |
29
|
|
|
*/ |
30
|
|
|
private $collector; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Stack |
34
|
|
|
*/ |
35
|
|
|
private $activeStack; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var HttpClient|MockObject |
39
|
|
|
*/ |
40
|
|
|
private $client; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var RequestInterface |
44
|
|
|
*/ |
45
|
|
|
private $request; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var Formatter|MockObject |
49
|
|
|
*/ |
50
|
|
|
private $formatter; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var Stopwatch |
54
|
|
|
*/ |
55
|
|
|
private $stopwatch; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var StopwatchEvent |
59
|
|
|
*/ |
60
|
|
|
private $stopwatchEvent; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var ProfileClient |
64
|
|
|
*/ |
65
|
|
|
private $subject; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var ResponseInterface |
69
|
|
|
*/ |
70
|
|
|
private $response; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var Promise |
74
|
|
|
*/ |
75
|
|
|
private $fulfilledPromise; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @var \Exception |
79
|
|
|
*/ |
80
|
|
|
private $exception; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @var RejectedPromise |
84
|
|
|
*/ |
85
|
|
|
private $rejectedPromise; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @var UriInterface |
89
|
|
|
*/ |
90
|
|
|
private $uri; |
91
|
|
|
|
92
|
|
|
public function setUp(): void |
93
|
|
|
{ |
94
|
|
|
$this->collector = $this->getMockBuilder(Collector::class)->disableOriginalConstructor()->getMock(); |
95
|
|
|
$this->activeStack = new Stack('default', 'FormattedRequest'); |
96
|
|
|
$this->client = $this->getMockBuilder(ClientInterface::class)->getMock(); |
97
|
|
|
$this->uri = new Uri('https://example.com/target'); |
98
|
|
|
$this->request = new Request('GET', $this->uri); |
99
|
|
|
$this->formatter = $this->getMockBuilder(Formatter::class)->disableOriginalConstructor()->getMock(); |
100
|
|
|
$this->stopwatch = $this->getMockBuilder(Stopwatch::class)->disableOriginalConstructor()->getMock(); |
101
|
|
|
$this->stopwatchEvent = $this->getMockBuilder(StopwatchEvent::class)->disableOriginalConstructor()->getMock(); |
102
|
|
|
$this->subject = new ProfileClient($this->client, $this->collector, $this->formatter, $this->stopwatch); |
103
|
|
|
$this->response = new Response(); |
104
|
|
|
$this->exception = new \Exception(); |
105
|
|
|
$this->fulfilledPromise = new FulfilledPromise($this->response); |
106
|
|
|
$this->rejectedPromise = new RejectedPromise($this->exception); |
107
|
|
|
|
108
|
|
|
$this->collector->method('getActiveStack')->willReturn($this->activeStack); |
109
|
|
|
$this->formatter |
110
|
|
|
->method('formatResponse') |
111
|
|
|
->with($this->response) |
112
|
|
|
->willReturn('FormattedResponse') |
113
|
|
|
; |
114
|
|
|
|
115
|
|
|
$this->stopwatch |
116
|
|
|
->method('start') |
117
|
|
|
->willReturn($this->stopwatchEvent) |
118
|
|
|
; |
119
|
|
|
|
120
|
|
|
$this->stopwatchEvent |
121
|
|
|
->method('getDuration') |
122
|
|
|
->willReturn(42) |
123
|
|
|
; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function testSendRequest(): void |
127
|
|
|
{ |
128
|
|
|
$this->client |
|
|
|
|
129
|
|
|
->expects($this->once()) |
130
|
|
|
->method('sendRequest') |
131
|
|
|
->with($this->identicalTo($this->request)) |
132
|
|
|
->willReturn($this->response) |
133
|
|
|
; |
134
|
|
|
|
135
|
|
|
$response = $this->subject->sendRequest($this->request); |
136
|
|
|
|
137
|
|
|
$this->assertEquals($this->response, $response); |
138
|
|
|
$this->assertEquals('GET', $this->activeStack->getRequestMethod()); |
139
|
|
|
$this->assertEquals('/target', $this->activeStack->getRequestTarget()); |
140
|
|
|
$this->assertEquals('example.com', $this->activeStack->getRequestHost()); |
141
|
|
|
$this->assertEquals('https', $this->activeStack->getRequestScheme()); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @expectedException \Error |
146
|
|
|
* @expectedException "You set string to int prop" |
147
|
|
|
*/ |
148
|
|
|
public function testSendRequestTypeError() |
149
|
|
|
{ |
150
|
|
|
$this->client |
|
|
|
|
151
|
|
|
->expects($this->once()) |
152
|
|
|
->method('sendRequest') |
153
|
|
|
->willReturnCallback(function () { |
154
|
|
|
throw new \Error('You set string to int prop'); |
|
|
|
|
155
|
|
|
}); |
156
|
|
|
$this->formatter |
|
|
|
|
157
|
|
|
->expects($this->once()) |
158
|
|
|
->method('formatException') |
159
|
|
|
->with($this->isInstanceOf(\Error::class)); |
160
|
|
|
|
161
|
|
|
$this->subject->sendRequest($this->request); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function testSendAsyncRequest(): void |
165
|
|
|
{ |
166
|
|
|
$this->client |
|
|
|
|
167
|
|
|
->expects($this->once()) |
168
|
|
|
->method('sendAsyncRequest') |
169
|
|
|
->with($this->identicalTo($this->request)) |
170
|
|
|
->willReturn($this->fulfilledPromise) |
171
|
|
|
; |
172
|
|
|
|
173
|
|
|
$this->collector |
|
|
|
|
174
|
|
|
->expects($this->once()) |
175
|
|
|
->method('deactivateStack') |
176
|
|
|
; |
177
|
|
|
|
178
|
|
|
$promise = $this->subject->sendAsyncRequest($this->request); |
179
|
|
|
|
180
|
|
|
$this->assertEquals($this->fulfilledPromise, $promise); |
181
|
|
|
$this->assertEquals('GET', $this->activeStack->getRequestMethod()); |
182
|
|
|
$this->assertEquals('/target', $this->activeStack->getRequestTarget()); |
183
|
|
|
$this->assertEquals('example.com', $this->activeStack->getRequestHost()); |
184
|
|
|
$this->assertEquals('https', $this->activeStack->getRequestScheme()); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
View Code Duplication |
public function testOnFulfilled(): void |
|
|
|
|
188
|
|
|
{ |
189
|
|
|
$this->collector |
|
|
|
|
190
|
|
|
->expects($this->once()) |
191
|
|
|
->method('activateStack') |
192
|
|
|
->with($this->activeStack) |
193
|
|
|
; |
194
|
|
|
|
195
|
|
|
$this->stopwatchEvent |
|
|
|
|
196
|
|
|
->expects($this->once()) |
197
|
|
|
->method('stop') |
198
|
|
|
; |
199
|
|
|
|
200
|
|
|
$this->client |
|
|
|
|
201
|
|
|
->method('sendAsyncRequest') |
202
|
|
|
->willReturn($this->fulfilledPromise) |
203
|
|
|
; |
204
|
|
|
|
205
|
|
|
$this->subject->sendAsyncRequest($this->request); |
206
|
|
|
|
207
|
|
|
$this->assertEquals(42, $this->activeStack->getDuration()); |
208
|
|
|
$this->assertEquals(200, $this->activeStack->getResponseCode()); |
209
|
|
|
$this->assertEquals('FormattedResponse', $this->activeStack->getClientResponse()); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
View Code Duplication |
public function testOnRejected(): void |
|
|
|
|
213
|
|
|
{ |
214
|
|
|
$this->collector |
|
|
|
|
215
|
|
|
->expects($this->once()) |
216
|
|
|
->method('activateStack') |
217
|
|
|
->with($this->activeStack) |
218
|
|
|
; |
219
|
|
|
|
220
|
|
|
$this->stopwatchEvent |
|
|
|
|
221
|
|
|
->expects($this->once()) |
222
|
|
|
->method('stop') |
223
|
|
|
; |
224
|
|
|
|
225
|
|
|
$this->client |
|
|
|
|
226
|
|
|
->method('sendAsyncRequest') |
227
|
|
|
->willReturn($this->rejectedPromise) |
228
|
|
|
; |
229
|
|
|
|
230
|
|
|
$this->formatter |
|
|
|
|
231
|
|
|
->method('formatException') |
232
|
|
|
->with($this->exception) |
233
|
|
|
->willReturn('FormattedException') |
234
|
|
|
; |
235
|
|
|
|
236
|
|
|
$this->subject->sendAsyncRequest($this->request); |
237
|
|
|
|
238
|
|
|
$this->assertEquals(42, $this->activeStack->getDuration()); |
239
|
|
|
$this->assertEquals('FormattedException', $this->activeStack->getClientException()); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
interface ClientInterface extends HttpClient, HttpAsyncClient |
244
|
|
|
{ |
245
|
|
|
} |
246
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.