Completed
Push — master ( 621c9b...c81ada )
by Meng
8s
created

SoapClientTest::magicCallClientReturnSoapFault()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 16

Duplication

Lines 26
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 26
loc 26
rs 8.8571
cc 1
eloc 16
nc 1
nop 0
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\HttpBinding;
14
use Meng\Soap\HttpBinding\RequestException;
15
16
class SoapClientTest extends \PHPUnit_Framework_TestCase
17
{
18
    private $handlerMock;
19
    private $clientMock;
20
    private $httpBindingMock;
21
    private $deferredHttpBinding;
22
23
    protected function setUp()
24
    {
25
        $this->handlerMock = new MockHandler();
26
        $handler = new HandlerStack($this->handlerMock);
27
        $this->clientMock = new Client(['handler' => $handler]);
28
29
        $this->httpBindingMock = $this->getMockBuilder(HttpBinding::class)
30
            ->disableOriginalConstructor()
31
            ->setMethods(['request', 'response'])
32
            ->getMock();
33
    }
34
35
    /**
36
     * @test
37
     * @expectedException \Exception
38
     */
39
    public function magicCallDeferredHttpBindingRejected()
40
    {
41
        $this->deferredHttpBinding = new RejectedPromise(new \Exception());
42
        $this->httpBindingMock->expects($this->never())->method('request');
43
44
        $client = new SoapClient($this->clientMock, $this->deferredHttpBinding);
45
        $client->someSoapMethod(['some-key' => 'some-value'])->wait();
46
    }
47
48
    /**
49
     * @test
50
     * @expectedException \Meng\Soap\HttpBinding\RequestException
51
     */
52
    public function magicCallHttpBindingFailed()
53
    {
54
        $this->deferredHttpBinding = new FulfilledPromise($this->httpBindingMock);
55
56
        $this->httpBindingMock->method('request')
57
            ->will(
58
                $this->throwException(new RequestException())
59
            )
60
            ->with(
61
                'someSoapMethod', [['some-key' => 'some-value']]
62
            );
63
64
        $this->httpBindingMock->expects($this->never())->method('response');
65
66
        $client = new SoapClient($this->clientMock, $this->deferredHttpBinding);
67
        $client->someSoapMethod(['some-key' => 'some-value'])->wait();
68
    }
69
70
    /**
71
     * @test
72
     */
73
    public function magicCall500Response()
74
    {
75
        $this->deferredHttpBinding = new FulfilledPromise($this->httpBindingMock);
76
77
        $this->httpBindingMock->method('request')
78
            ->willReturn(
79
                new Request('POST', 'www.endpoint.com')
80
            )
81
            ->with(
82
                'someSoapMethod', [['some-key' => 'some-value']]
83
            );
84
85
        $response = new Response('500');
86
        $this->httpBindingMock->method('response')
87
            ->willReturn(
88
                'SoapResult'
89
            )
90
            ->with(
91
                $response, 'someSoapMethod', null
92
            );
93
94
        $this->handlerMock->append(GuzzleRequestException::create(new Request('POST', 'www.endpoint.com'), $response));
95
96
        $client = new SoapClient($this->clientMock, $this->deferredHttpBinding);
97
        $this->assertEquals('SoapResult', $client->someSoapMethod(['some-key' => 'some-value'])->wait());
98
    }
99
100
    /**
101
     * @test
102
     * @expectedException \GuzzleHttp\Exception\RequestException
103
     */
104
    public function magicCallResponseNotReceived()
105
    {
106
        $this->deferredHttpBinding = new FulfilledPromise($this->httpBindingMock);
107
108
        $this->httpBindingMock->method('request')
109
            ->willReturn(
110
                new Request('POST', 'www.endpoint.com')
111
            )
112
            ->with(
113
                'someSoapMethod', [['some-key' => 'some-value']]
114
            );
115
116
        $this->httpBindingMock->expects($this->never())->method('response');
117
118
        $this->handlerMock->append(GuzzleRequestException::create(new Request('POST', 'www.endpoint.com')));
119
120
        $client = new SoapClient($this->clientMock, $this->deferredHttpBinding);
121
        $client->someSoapMethod(['some-key' => 'some-value'])->wait();
122
    }
123
124
    /**
125
     * @test
126
     * @expectedException \Exception
127
     */
128
    public function magicCallUndefinedResponse()
129
    {
130
        $this->deferredHttpBinding = new FulfilledPromise($this->httpBindingMock);
131
132
        $this->httpBindingMock->method('request')
133
            ->willReturn(
134
                new Request('POST', 'www.endpoint.com')
135
            )
136
            ->with(
137
                'someSoapMethod', [['some-key' => 'some-value']]
138
            );
139
140
        $this->httpBindingMock->expects($this->never())->method('response');
141
142
        $this->handlerMock->append(new \Exception());
143
144
        $client = new SoapClient($this->clientMock, $this->deferredHttpBinding);
145
        $client->someSoapMethod(['some-key' => 'some-value'])->wait();
146
147
    }
148
149
    /**
150
     * @test
151
     * @expectedException \SoapFault
152
     */
153
    public function magicCallClientReturnSoapFault()
154
    {
155
        $this->deferredHttpBinding = new FulfilledPromise($this->httpBindingMock);
156
157
        $this->httpBindingMock->method('request')
158
            ->willReturn(
159
                new Request('POST', 'www.endpoint.com')
160
            )
161
            ->with(
162
                'someSoapMethod', [['some-key' => 'some-value']]
163
            );
164
165
        $response = new Response('200', [], 'body');
166
        $this->httpBindingMock->method('response')
167
            ->will(
168
                $this->throwException(new \SoapFault('soap fault', 'soap fault'))
169
            )
170
            ->with(
171
                $response, 'someSoapMethod', null
172
            );
173
174
        $this->handlerMock->append($response);
175
176
        $client = new SoapClient($this->clientMock, $this->deferredHttpBinding);
177
        $client->someSoapMethod(['some-key' => 'some-value'])->wait();
178
    }
179
180
    /**
181
     * @test
182
     */
183
    public function magicCallSuccess()
184
    {
185
        $this->deferredHttpBinding = new FulfilledPromise($this->httpBindingMock);
186
187
        $this->httpBindingMock->method('request')
188
            ->willReturn(
189
                new Request('POST', 'www.endpoint.com')
190
            )
191
            ->with(
192
                'someSoapMethod', [['some-key' => 'some-value']]
193
            );
194
195
        $response = new Response('200', [], 'body');
196
        $this->httpBindingMock->method('response')
197
            ->willReturn(
198
                'SoapResult'
199
            )
200
            ->with(
201
                $response, 'someSoapMethod', null
202
            );
203
204
        $this->handlerMock->append($response);
205
206
        $client = new SoapClient($this->clientMock, $this->deferredHttpBinding);
207
        $this->assertEquals('SoapResult', $client->someSoapMethod(['some-key' => 'some-value'])->wait());
208
    }
209
210
    /**
211
     * @test
212
     */
213
    public function resultsAreEquivalent()
214
    {
215
        $this->deferredHttpBinding = new FulfilledPromise($this->httpBindingMock);
216
217
        $this->httpBindingMock->method('request')
218
            ->willReturn(
219
                new Request('POST', 'www.endpoint.com')
220
            )
221
            ->with(
222
                'someSoapMethod', [['some-key' => 'some-value']]
223
            );
224
225
        $response = new Response('200', [], 'body');
226
        $this->httpBindingMock->method('response')->willReturn(
227
            'SoapResult'
228
        );
229
230
        $this->handlerMock->append($response);
231
        $this->handlerMock->append($response);
232
        $this->handlerMock->append($response);
233
234
        $client = new SoapClient($this->clientMock, $this->deferredHttpBinding);
235
        $magicResult = $client->someSoapMethod(['some-key' => 'some-value'])->wait();
236
        $syncResult = $client->call('someSoapMethod', [['some-key' => 'some-value']]);
237
        $asyncResult = $client->callAsync('someSoapMethod', [['some-key' => 'some-value']])->wait();
238
        $this->assertEquals($magicResult, $asyncResult);
239
        $this->assertEquals($syncResult, $asyncResult);
240
    }
241
}
242