Completed
Push — master ( ce6809...9d322b )
by Meng
02:26
created

SoapClientTest::magicCallHttpBindingFailed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 15
rs 9.4285
cc 1
eloc 9
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();
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...
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
        $client = new SoapClient($this->clientMock, $this->deferredHttpBinding);
65
        $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...
66
    }
67
68
    /**
69
     * @test
70
     */
71 View Code Duplication
    public function magicCallClientReturnError()
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...
72
    {
73
        $this->deferredHttpBinding = new FulfilledPromise($this->httpBindingMock);
74
75
        $this->httpBindingMock->method('request')
76
            ->willReturn(
77
                new Request('POST', 'www.endpoint.com')
78
            )
79
            ->with(
80
                'someSoapMethod', [['some-key' => 'some-value']]
81
            );
82
83
        $response = new Response('500');
84
        $this->httpBindingMock->method('response')
85
            ->willReturn(
86
                'SoapResult'
87
            )
88
            ->with(
89
                $response, 'someSoapMethod', null
90
            );
91
92
        $this->handlerMock->append(GuzzleRequestException::create(new Request('POST', 'www.endpoint.com'), $response));
93
94
        $client = new SoapClient($this->clientMock, $this->deferredHttpBinding);
95
        $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...
96
    }
97
98
    /**
99
     * @test
100
     * @expectedException \SoapFault
101
     */
102 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...
103
    {
104
        $this->deferredHttpBinding = new FulfilledPromise($this->httpBindingMock);
105
106
        $this->httpBindingMock->method('request')
107
            ->willReturn(
108
                new Request('POST', 'www.endpoint.com')
109
            )
110
            ->with(
111
                'someSoapMethod', [['some-key' => 'some-value']]
112
            );
113
114
        $response = new Response('200', [], 'body');
115
        $this->httpBindingMock->method('response')
116
            ->will(
117
                $this->throwException(new \SoapFault('soap fault', 'soap fault'))
118
            )
119
            ->with(
120
                $response, 'someSoapMethod', null
121
            );
122
123
        $this->handlerMock->append($response);
124
125
        $client = new SoapClient($this->clientMock, $this->deferredHttpBinding);
126
        $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...
127
    }
128
129
    /**
130
     * @test
131
     */
132 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...
133
    {
134
        $this->deferredHttpBinding = new FulfilledPromise($this->httpBindingMock);
135
136
        $this->httpBindingMock->method('request')
137
            ->willReturn(
138
                new Request('POST', 'www.endpoint.com')
139
            )
140
            ->with(
141
                'someSoapMethod', [['some-key' => 'some-value']]
142
            );
143
144
        $response = new Response('200', [], 'body');
145
        $this->httpBindingMock->method('response')
146
            ->willReturn(
147
                'SoapResult'
148
            )
149
            ->with(
150
                $response, 'someSoapMethod', null
151
            );
152
153
        $this->handlerMock->append($response);
154
155
        $client = new SoapClient($this->clientMock, $this->deferredHttpBinding);
156
        $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...
157
    }
158
159
    /**
160
     * @test
161
     */
162
    public function resultsAreEquivalent()
163
    {
164
        $this->deferredHttpBinding = new FulfilledPromise($this->httpBindingMock);
165
166
        $this->httpBindingMock->method('request')
167
            ->willReturn(
168
                new Request('POST', 'www.endpoint.com')
169
            )
170
            ->with(
171
                'someSoapMethod', [['some-key' => 'some-value']]
172
            );
173
174
        $response = new Response('200', [], 'body');
175
        $this->httpBindingMock->method('response')->willReturn(
176
            'SoapResult'
177
        );
178
179
        $this->handlerMock->append($response);
180
        $this->handlerMock->append($response);
181
        $this->handlerMock->append($response);
182
183
        $client = new SoapClient($this->clientMock, $this->deferredHttpBinding);
184
        $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...
185
        $syncResult = $client->call('someSoapMethod', [['some-key' => 'some-value']]);
186
        $asyncResult = $client->callAsync('someSoapMethod', [['some-key' => 'some-value']])->wait();
187
        $this->assertEquals($magicResult, $asyncResult);
188
        $this->assertEquals($syncResult, $asyncResult);
189
    }
190
}
191