Completed
Push — master ( 627e2a...ffe287 )
by Meng
02:08
created

SoapClientTest::magicCallClientReturnSoapFault()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 21

Duplication

Lines 32
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 32
loc 32
rs 8.8571
cc 1
eloc 21
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\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();
0 ignored issues
show
Documentation Bug introduced by
The method someSoapMethod does not exist on object<Meng\AsyncSoap\Guzzle\SoapClient>? Since you implemented __call, maybe consider adding a @method annotation.

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:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
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();
0 ignored issues
show
Documentation Bug introduced by
The method someSoapMethod does not exist on object<Meng\AsyncSoap\Guzzle\SoapClient>? Since you implemented __call, maybe consider adding a @method annotation.

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:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
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();
0 ignored issues
show
Documentation Bug introduced by
The method someSoapMethod does not exist on object<Meng\AsyncSoap\Guzzle\SoapClient>? Since you implemented __call, maybe consider adding a @method annotation.

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:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
110
    }
111
112
    /**
113
     * @test
114
     * @expectedException \SoapFault
115
     */
116 View Code Duplication
    public function magicCallClientReturnSoapFault()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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();
0 ignored issues
show
Documentation Bug introduced by
The method someSoapMethod does not exist on object<Meng\AsyncSoap\Guzzle\SoapClient>? Since you implemented __call, maybe consider adding a @method annotation.

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:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
147
    }
148
149
    /**
150
     * @test
151
     */
152 View Code Duplication
    public function magicCallSuccess()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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());
0 ignored issues
show
Documentation Bug introduced by
The method someSoapMethod does not exist on object<Meng\AsyncSoap\Guzzle\SoapClient>? Since you implemented __call, maybe consider adding a @method annotation.

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:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
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();
0 ignored issues
show
Documentation Bug introduced by
The method someSoapMethod does not exist on object<Meng\AsyncSoap\Guzzle\SoapClient>? Since you implemented __call, maybe consider adding a @method annotation.

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:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
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