Completed
Push — master ( ffe287...8931ec )
by Meng
03:29
created

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