1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Meng\AsyncSoap\Guzzle; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\Handler\MockHandler; |
7
|
|
|
use GuzzleHttp\HandlerStack; |
8
|
|
|
use GuzzleHttp\Promise\FulfilledPromise; |
9
|
|
|
use GuzzleHttp\Promise\RejectedPromise; |
10
|
|
|
use GuzzleHttp\Psr7\Request; |
11
|
|
|
use GuzzleHttp\Psr7\Response; |
12
|
|
|
use GuzzleHttp\Exception\RequestException as GuzzleRequestException; |
13
|
|
|
use Meng\Soap\HttpBinding\RequestBuilder; |
14
|
|
|
use Meng\Soap\HttpBinding\RequestException; |
15
|
|
|
use Meng\Soap\Interpreter; |
16
|
|
|
use Meng\Soap\SoapRequest; |
17
|
|
|
|
18
|
|
|
class SoapClientTest extends \PHPUnit_Framework_TestCase |
19
|
|
|
{ |
20
|
|
|
private $handlerMock; |
21
|
|
|
private $clientMock; |
22
|
|
|
private $interpreterMock; |
23
|
|
|
private $httpBindingMock; |
24
|
|
|
private $deferredInterpreter; |
25
|
|
|
|
26
|
|
|
protected function setUp() |
27
|
|
|
{ |
28
|
|
|
$this->handlerMock = new MockHandler(); |
29
|
|
|
$handler = new HandlerStack($this->handlerMock); |
30
|
|
|
$this->clientMock = new Client(['handler' => $handler]); |
31
|
|
|
|
32
|
|
|
$this->interpreterMock = $this->getMockBuilder(Interpreter::class) |
33
|
|
|
->disableOriginalConstructor() |
34
|
|
|
->setMethods(['request', 'response']) |
35
|
|
|
->getMock(); |
36
|
|
|
|
37
|
|
|
$this->httpBindingMock = $this->getMockBuilder(RequestBuilder::class) |
38
|
|
|
->disableOriginalConstructor() |
39
|
|
|
->setMethods(['isSOAP11', 'isSOAP12', 'setEndpoint', 'setSoapAction', 'setSoapMessage', 'getSoapHttpRequest']) |
40
|
|
|
->getMock(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @test |
45
|
|
|
* @expectedException \Exception |
46
|
|
|
*/ |
47
|
|
|
public function magicCallInterpreterFailed() |
48
|
|
|
{ |
49
|
|
|
$this->deferredInterpreter = new RejectedPromise(new \Exception()); |
50
|
|
|
$this->interpreterMock->expects($this->never())->method('request'); |
51
|
|
|
|
52
|
|
|
$client = new SoapClient($this->clientMock, $this->deferredInterpreter, $this->httpBindingMock); |
53
|
|
|
$client->someSoapMethod(['some-key' => 'some-value'])->wait(); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @test |
58
|
|
|
* @expectedException \Meng\Soap\HttpBinding\RequestException |
59
|
|
|
*/ |
60
|
|
|
public function magicCallHttpBindingFailed() |
61
|
|
|
{ |
62
|
|
|
$this->deferredInterpreter = new FulfilledPromise($this->interpreterMock); |
63
|
|
|
|
64
|
|
|
$this->interpreterMock->method('request') |
65
|
|
|
->willReturn( |
66
|
|
|
new SoapRequest('www.endpoint.com', 'soapaction', '1', 'message') |
67
|
|
|
) |
68
|
|
|
->with( |
69
|
|
|
'someSoapMethod', [['some-key' => 'some-value']] |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
$this->httpBindingMock->expects($this->exactly(1))->method('isSOAP11'); |
73
|
|
|
$this->httpBindingMock->expects($this->never())->method('isSOAP12'); |
74
|
|
|
$this->httpBindingMock->expects($this->exactly(1))->method('setEndpoint')->with('www.endpoint.com'); |
75
|
|
|
$this->httpBindingMock->expects($this->exactly(1))->method('setSoapAction')->with('soapaction'); |
76
|
|
|
$this->httpBindingMock->expects($this->exactly(1))->method('setSoapMessage')->with('message'); |
77
|
|
|
$this->httpBindingMock->method('getSoapHttpRequest')->will($this->throwException(new RequestException())); |
78
|
|
|
|
79
|
|
|
$client = new SoapClient($this->clientMock, $this->deferredInterpreter, $this->httpBindingMock); |
80
|
|
|
$client->someSoapMethod(['some-key' => 'some-value'])->wait(); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @test |
85
|
|
|
* @expectedException \GuzzleHttp\Exception\RequestException |
86
|
|
|
*/ |
87
|
|
|
public function magicCallClientReturnError() |
88
|
|
|
{ |
89
|
|
|
$this->deferredInterpreter = new FulfilledPromise($this->interpreterMock); |
90
|
|
|
|
91
|
|
|
$this->interpreterMock->method('request') |
92
|
|
|
->willReturn( |
93
|
|
|
new SoapRequest('www.endpoint.com', 'soapaction', '1', 'message') |
94
|
|
|
) |
95
|
|
|
->with( |
96
|
|
|
'someSoapMethod', [['some-key' => 'some-value']] |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
$this->httpBindingMock->expects($this->exactly(1))->method('isSOAP11'); |
100
|
|
|
$this->httpBindingMock->expects($this->never())->method('isSOAP12'); |
101
|
|
|
$this->httpBindingMock->expects($this->exactly(1))->method('setEndpoint')->with('www.endpoint.com'); |
102
|
|
|
$this->httpBindingMock->expects($this->exactly(1))->method('setSoapAction')->with('soapaction'); |
103
|
|
|
$this->httpBindingMock->expects($this->exactly(1))->method('setSoapMessage')->with('message'); |
104
|
|
|
$this->httpBindingMock->method('getSoapHttpRequest')->willReturn(new Request('POST', 'www.endpoint.com')); |
105
|
|
|
|
106
|
|
|
$this->handlerMock->append(GuzzleRequestException::create(new Request('POST', 'www.endpoint.com'), new Response('500'))); |
107
|
|
|
|
108
|
|
|
$client = new SoapClient($this->clientMock, $this->deferredInterpreter, $this->httpBindingMock); |
109
|
|
|
$client->someSoapMethod(['some-key' => 'some-value'])->wait(); |
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @test |
114
|
|
|
* @expectedException \SoapFault |
115
|
|
|
*/ |
116
|
|
View Code Duplication |
public function magicCallClientReturnSoapFault() |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
$this->deferredInterpreter = new FulfilledPromise($this->interpreterMock); |
119
|
|
|
|
120
|
|
|
$this->interpreterMock->method('request') |
121
|
|
|
->willReturn( |
122
|
|
|
new SoapRequest('www.endpoint.com', 'soapaction', '2', 'message') |
123
|
|
|
) |
124
|
|
|
->with( |
125
|
|
|
'someSoapMethod', [['some-key' => 'some-value']] |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
$this->interpreterMock->method('response') |
129
|
|
|
->will( |
130
|
|
|
$this->throwException(new \SoapFault('soap fault', 'soap fault')) |
131
|
|
|
) |
132
|
|
|
->with( |
133
|
|
|
'body', 'someSoapMethod', null |
134
|
|
|
); |
135
|
|
|
|
136
|
|
|
$this->httpBindingMock->expects($this->never())->method('isSOAP11'); |
137
|
|
|
$this->httpBindingMock->expects($this->exactly(1))->method('isSOAP12'); |
138
|
|
|
$this->httpBindingMock->expects($this->exactly(1))->method('setEndpoint')->with('www.endpoint.com'); |
139
|
|
|
$this->httpBindingMock->expects($this->exactly(1))->method('setSoapAction')->with('soapaction'); |
140
|
|
|
$this->httpBindingMock->expects($this->exactly(1))->method('setSoapMessage')->with('message'); |
141
|
|
|
$this->httpBindingMock->method('getSoapHttpRequest')->willReturn(new Request('POST', 'www.endpoint.com')); |
142
|
|
|
|
143
|
|
|
$this->handlerMock->append(new Response('200', [], 'body')); |
144
|
|
|
|
145
|
|
|
$client = new SoapClient($this->clientMock, $this->deferredInterpreter, $this->httpBindingMock); |
146
|
|
|
$client->someSoapMethod(['some-key' => 'some-value'])->wait(); |
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @test |
151
|
|
|
*/ |
152
|
|
View Code Duplication |
public function magicCallSuccess() |
|
|
|
|
153
|
|
|
{ |
154
|
|
|
$this->deferredInterpreter = new FulfilledPromise($this->interpreterMock); |
155
|
|
|
|
156
|
|
|
$this->interpreterMock->method('request') |
157
|
|
|
->willReturn( |
158
|
|
|
new SoapRequest('www.endpoint.com', 'soapaction', '1', 'message') |
159
|
|
|
) |
160
|
|
|
->with( |
161
|
|
|
'someSoapMethod', [['some-key' => 'some-value']] |
162
|
|
|
); |
163
|
|
|
|
164
|
|
|
$this->interpreterMock->method('response') |
165
|
|
|
->willReturn( |
166
|
|
|
'SoapResult' |
167
|
|
|
) |
168
|
|
|
->with( |
169
|
|
|
'body', 'someSoapMethod', null |
170
|
|
|
); |
171
|
|
|
|
172
|
|
|
$this->httpBindingMock->expects($this->exactly(1))->method('isSOAP11'); |
173
|
|
|
$this->httpBindingMock->expects($this->never())->method('isSOAP12'); |
174
|
|
|
$this->httpBindingMock->expects($this->exactly(1))->method('setEndpoint')->with('www.endpoint.com'); |
175
|
|
|
$this->httpBindingMock->expects($this->exactly(1))->method('setSoapAction')->with('soapaction'); |
176
|
|
|
$this->httpBindingMock->expects($this->exactly(1))->method('setSoapMessage')->with('message'); |
177
|
|
|
$this->httpBindingMock->method('getSoapHttpRequest')->willReturn(new Request('POST', 'www.endpoint.com')); |
178
|
|
|
|
179
|
|
|
$this->handlerMock->append(new Response('200', [], 'body')); |
180
|
|
|
|
181
|
|
|
$client = new SoapClient($this->clientMock, $this->deferredInterpreter, $this->httpBindingMock); |
182
|
|
|
$this->assertEquals('SoapResult', $client->someSoapMethod(['some-key' => 'some-value'])->wait()); |
|
|
|
|
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @test |
187
|
|
|
*/ |
188
|
|
|
public function resultsAreEquivalent() |
189
|
|
|
{ |
190
|
|
|
$this->deferredInterpreter = new FulfilledPromise($this->interpreterMock); |
191
|
|
|
|
192
|
|
|
$this->interpreterMock->method('request') |
193
|
|
|
->willReturn( |
194
|
|
|
new SoapRequest('www.endpoint.com', 'soapaction', '1', 'message') |
195
|
|
|
) |
196
|
|
|
->with( |
197
|
|
|
'someSoapMethod', [['some-key' => 'some-value']] |
198
|
|
|
); |
199
|
|
|
|
200
|
|
|
$this->interpreterMock->method('response')->willReturn( |
201
|
|
|
'SoapResult' |
202
|
|
|
); |
203
|
|
|
|
204
|
|
|
$this->httpBindingMock->expects($this->any())->method('isSOAP11'); |
205
|
|
|
$this->httpBindingMock->expects($this->never())->method('isSOAP12'); |
206
|
|
|
$this->httpBindingMock->expects($this->any())->method('setEndpoint')->with('www.endpoint.com'); |
207
|
|
|
$this->httpBindingMock->expects($this->any())->method('setSoapAction')->with('soapaction'); |
208
|
|
|
$this->httpBindingMock->expects($this->any())->method('setSoapMessage')->with('message'); |
209
|
|
|
$this->httpBindingMock->method('getSoapHttpRequest')->willReturn(new Request('POST', 'www.endpoint.com')); |
210
|
|
|
|
211
|
|
|
$this->handlerMock->append(new Response('200', [], 'body')); |
212
|
|
|
$this->handlerMock->append(new Response('200', [], 'body')); |
213
|
|
|
$this->handlerMock->append(new Response('200', [], 'body')); |
214
|
|
|
|
215
|
|
|
$client = new SoapClient($this->clientMock, $this->deferredInterpreter, $this->httpBindingMock); |
216
|
|
|
$magicResult = $client->someSoapMethod(['some-key' => 'some-value'])->wait(); |
|
|
|
|
217
|
|
|
$syncResult = $client->call('someSoapMethod', [['some-key' => 'some-value']]); |
218
|
|
|
$asyncResult = $client->callAsync('someSoapMethod', [['some-key' => 'some-value']])->wait(); |
219
|
|
|
$this->assertEquals($magicResult, $asyncResult); |
220
|
|
|
$this->assertEquals($syncResult, $asyncResult); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: