Completed
Pull Request — develop (#48)
by Hugo
04:15 queued 01:59
created

ClientTest::handleErrorShouldBypassEventWhenHostIsNotFromPagSeguro()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
namespace PHPSC\PagSeguro\Client;
3
4
use GuzzleHttp\ClientInterface;
5
use GuzzleHttp\Client as HttpClient;
6
use Psr\Http\Message\RequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
9
class ClientTest extends \PHPUnit_Framework_TestCase
10
{
11
    /**
12
     * @var HttpClient|\PHPUnit_Framework_MockObject_MockObject
13
     */
14
    protected $httpClient;
15
16
    /**
17
     * @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject
18
     */
19
    protected $request;
20
    private $response;
21
22
    protected function setUp()
23
    {
24
        $this->httpClient = $this->createMock(ClientInterface::class);
25
        $this->request    = $this->createMock(RequestInterface::class);
26
        $this->response   = $this->createMock(ResponseInterface::class);
27
28
        $xml = <<<'XML'
29
<?xml version="1.0" encoding="UTF-8"?>
30
<checkout>
31
    <code>8CF4BE7DCECEF0F004A6DFA0A8243412</code>
32
    <date>2010-12-02T10:11:28.000-02:00</date>
33
</checkout>
34
XML;
35
36
        $this->response->expects($this->once())
37
                       ->method('getBody')
38
                       ->willReturn($xml);
39
    }
40
41
    /**
42
     * @test
43
     */
44
    public function postShouldSendTheBodyAsXml()
45
    {
46
        $client = new Client($this->httpClient);
47
        $xml = simplexml_load_string('<?xml version="1.0" encoding="UTF-8"?><checkout/>');
48
49
        $this->httpClient->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in GuzzleHttp\Client.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
50
                         ->method('request')
51
                         ->with(
52
                             'POST',
53
                             '/test',
54
                             [
55
                                'headers' => ['Content-Type' => 'application/xml; charset=UTF-8'],
56
                                'body'    => $xml->asXML(),
57
                                'verify'  => false
58
                             ]
59
                         )->willReturn($this->response);
60
61
        $this->assertInstanceOf('SimpleXMLElement', $client->post('/test', $xml));
62
    }
63
64
    /**
65
     * @test
66
     */
67
    public function getShouldConfigureHeaders()
68
    {
69
        $client = new Client($this->httpClient);
70
71
        $this->httpClient->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in GuzzleHttp\Client.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
72
                         ->method('request')
73
                         ->with('GET', '/test?name=Test', ['verify' => false])
74
                         ->willReturn($this->response);
75
76
        $this->assertInstanceOf('SimpleXMLElement', $client->get('/test?name=Test'));
77
    }
78
79
80
}
81