|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Http\HttplugBundle\Tests\Unit\Collector; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\Request; |
|
6
|
|
|
use GuzzleHttp\Psr7\Response; |
|
7
|
|
|
use Http\Client\Common\Plugin; |
|
8
|
|
|
use Http\Client\Exception\TransferException; |
|
9
|
|
|
use Http\HttplugBundle\Collector\Collector; |
|
10
|
|
|
use Http\HttplugBundle\Collector\Formatter; |
|
11
|
|
|
use Http\HttplugBundle\Collector\ProfilePlugin; |
|
12
|
|
|
use Http\HttplugBundle\Collector\Stack; |
|
13
|
|
|
use Http\Promise\FulfilledPromise; |
|
14
|
|
|
use Http\Promise\Promise; |
|
15
|
|
|
use Http\Promise\RejectedPromise; |
|
16
|
|
|
use PHPUnit\Framework\TestCase; |
|
17
|
|
|
use Psr\Http\Message\RequestInterface; |
|
18
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
19
|
|
|
|
|
20
|
|
|
class ProfilePluginTest extends TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var Plugin |
|
24
|
|
|
*/ |
|
25
|
|
|
private $plugin; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var Collector |
|
29
|
|
|
*/ |
|
30
|
|
|
private $collector; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var RequestInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
private $request; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var ResponseInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
private $response; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var Promise |
|
44
|
|
|
*/ |
|
45
|
|
|
private $fulfilledPromise; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var Stack |
|
49
|
|
|
*/ |
|
50
|
|
|
private $currentStack; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var TransferException |
|
54
|
|
|
*/ |
|
55
|
|
|
private $exception; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var Promise |
|
59
|
|
|
*/ |
|
60
|
|
|
private $rejectedPromise; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var Formatter |
|
64
|
|
|
*/ |
|
65
|
|
|
private $formatter; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @var ProfilePlugin |
|
69
|
|
|
*/ |
|
70
|
|
|
private $subject; |
|
71
|
|
|
|
|
72
|
|
|
public function setUp() |
|
73
|
|
|
{ |
|
74
|
|
|
$this->plugin = $this->getMockBuilder(Plugin::class)->getMock(); |
|
75
|
|
|
$this->collector = $this->getMockBuilder(Collector::class)->disableOriginalConstructor()->getMock(); |
|
76
|
|
|
$this->request = new Request('GET', '/'); |
|
77
|
|
|
$this->response = new Response(); |
|
78
|
|
|
$this->fulfilledPromise = new FulfilledPromise($this->response); |
|
79
|
|
|
$this->currentStack = new Stack('default', 'FormattedRequest'); |
|
80
|
|
|
$this->exception = new TransferException(); |
|
81
|
|
|
$this->rejectedPromise = new RejectedPromise($this->exception); |
|
82
|
|
|
$this->formatter = $this->getMockBuilder(Formatter::class)->disableOriginalConstructor()->getMock(); |
|
83
|
|
|
|
|
84
|
|
|
$this->collector |
|
85
|
|
|
->method('getActiveStack') |
|
86
|
|
|
->willReturn($this->currentStack) |
|
87
|
|
|
; |
|
88
|
|
|
|
|
89
|
|
|
$this->plugin |
|
90
|
|
|
->method('handleRequest') |
|
91
|
|
|
->willReturnCallback(function ($request, $next, $first) { |
|
|
|
|
|
|
92
|
|
|
return $next($request); |
|
93
|
|
|
}) |
|
94
|
|
|
; |
|
95
|
|
|
|
|
96
|
|
|
$this->formatter |
|
97
|
|
|
->method('formatRequest') |
|
98
|
|
|
->with($this->identicalTo($this->request)) |
|
99
|
|
|
->willReturn('FormattedRequest') |
|
100
|
|
|
; |
|
101
|
|
|
|
|
102
|
|
|
$this->formatter |
|
103
|
|
|
->method('formatResponse') |
|
104
|
|
|
->with($this->identicalTo($this->response)) |
|
105
|
|
|
->willReturn('FormattedResponse') |
|
106
|
|
|
; |
|
107
|
|
|
|
|
108
|
|
|
$this->formatter |
|
109
|
|
|
->method('formatException') |
|
110
|
|
|
->with($this->identicalTo($this->exception)) |
|
111
|
|
|
->willReturn('FormattedException') |
|
112
|
|
|
; |
|
113
|
|
|
|
|
114
|
|
|
$this->subject = new ProfilePlugin( |
|
115
|
|
|
$this->plugin, |
|
116
|
|
|
$this->collector, |
|
117
|
|
|
$this->formatter, |
|
118
|
|
|
'http.plugin.mock' |
|
|
|
|
|
|
119
|
|
|
); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function testCallDecoratedPlugin() |
|
123
|
|
|
{ |
|
124
|
|
|
$this->plugin |
|
|
|
|
|
|
125
|
|
|
->expects($this->once()) |
|
126
|
|
|
->method('handleRequest') |
|
127
|
|
|
->with($this->request) |
|
128
|
|
|
; |
|
129
|
|
|
|
|
130
|
|
|
$this->subject->handleRequest($this->request, function () { |
|
131
|
|
|
return $this->fulfilledPromise; |
|
132
|
|
|
}, function () { |
|
133
|
|
|
}); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function testProfileIsInitialized() |
|
137
|
|
|
{ |
|
138
|
|
|
$this->subject->handleRequest($this->request, function () { |
|
139
|
|
|
return $this->fulfilledPromise; |
|
140
|
|
|
}, function () { |
|
141
|
|
|
}); |
|
142
|
|
|
|
|
143
|
|
|
$this->assertCount(1, $this->currentStack->getProfiles()); |
|
144
|
|
|
$profile = $this->currentStack->getProfiles()[0]; |
|
145
|
|
|
$this->assertEquals(get_class($this->plugin), $profile->getPlugin()); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
public function testCollectRequestInformations() |
|
149
|
|
|
{ |
|
150
|
|
|
$this->subject->handleRequest($this->request, function () { |
|
151
|
|
|
return $this->fulfilledPromise; |
|
152
|
|
|
}, function () { |
|
153
|
|
|
}); |
|
154
|
|
|
|
|
155
|
|
|
$profile = $this->currentStack->getProfiles()[0]; |
|
156
|
|
|
$this->assertEquals('FormattedRequest', $profile->getRequest()); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
View Code Duplication |
public function testOnFulfilled() |
|
|
|
|
|
|
160
|
|
|
{ |
|
161
|
|
|
$promise = $this->subject->handleRequest($this->request, function () { |
|
162
|
|
|
return $this->fulfilledPromise; |
|
163
|
|
|
}, function () { |
|
164
|
|
|
}); |
|
165
|
|
|
|
|
166
|
|
|
$this->assertEquals($this->response, $promise->wait()); |
|
167
|
|
|
$profile = $this->currentStack->getProfiles()[0]; |
|
168
|
|
|
$this->assertEquals('FormattedResponse', $profile->getResponse()); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @expectedException \Http\Client\Exception\TransferException |
|
173
|
|
|
*/ |
|
174
|
|
View Code Duplication |
public function testOnRejected() |
|
|
|
|
|
|
175
|
|
|
{ |
|
176
|
|
|
$promise = $this->subject->handleRequest($this->request, function () { |
|
177
|
|
|
return $this->rejectedPromise; |
|
178
|
|
|
}, function () { |
|
179
|
|
|
}); |
|
180
|
|
|
|
|
181
|
|
|
$this->assertEquals($this->exception, $promise->wait()); |
|
182
|
|
|
$profile = $this->currentStack->getProfiles()[0]; |
|
183
|
|
|
$this->assertEquals('FormattedException', $profile->getResponse()); |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.