Completed
Pull Request — default-schema (#32)
by
unknown
05:07 queued 03:11
created

ClientTest::testSetHTTPHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
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 13
rs 9.4286
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
/**
3
 * @author Antoine Hedgcock
4
 */
5
6
namespace CrateTest\PDO\Http;
7
8
use Crate\PDO\Exception\RuntimeException;
9
use Crate\PDO\Exception\UnsupportedException;
10
use Crate\Stdlib\Collection;
11
use GuzzleHttp\Client as GuzzleClient;
12
use GuzzleHttp\ClientInterface as GuzzleClientInterface;
13
use Crate\PDO\Http\Client;
14
use GuzzleHttp\Exception\ClientException;
15
use GuzzleHttp\Message\RequestInterface;
16
use GuzzleHttp\Message\Response;
17
use GuzzleHttp\Stream\Stream;
18
use PHPUnit_Framework_MockObject_MockObject;
19
use PHPUnit_Framework_TestCase;
20
use ReflectionClass;
21
22
/**
23
 * Class ClientTest
24
 *
25
 * @coversDefaultClass \Crate\PDO\Http\Client
26
 * @covers ::<!public>
27
 *
28
 * @group unit
29
 */
30
class ClientTest extends PHPUnit_Framework_TestCase
31
{
32
    const DSN = 'http://localhost:4200';
33
    const SQL = 'SELECT * FROM test_table';
34
35
    /**
36
     * @var Client
37
     */
38
    private $client;
39
40
    /**
41
     * @var GuzzleClient|PHPUnit_Framework_MockObject_MockObject
42
     */
43
    private $internalClient;
44
45
    /**
46
     * @covers ::__construct
47
     */
48
    protected function setUp()
49
    {
50
        $this->client         = new Client(static::DSN, []);
51
        $this->internalClient = $this->getMock(GuzzleClientInterface::class);
52
53
        $reflection = new ReflectionClass(Client::class);
54
55
        $property = $reflection->getProperty('client');
56
        $property->setAccessible(true);
57
        $property->setValue($this->client, $this->internalClient);
58
    }
59
60
    /**
61
     * Create a response to be used
62
     *
63
     * @param int   $statusCode
64
     * @param array $body
65
     *
66
     * @return Response
67
     */
68
    private function createResponse($statusCode, array $body)
69
    {
70
        $body = Stream::factory(json_encode($body));
71
72
        return new Response($statusCode, [], $body);
73
    }
74
75
    /**
76
     * @covers ::execute
77
     */
78
    public function testExecuteWithResponseFailure()
79
    {
80
        $code    = 1337;
81
        $message = 'hello world';
82
83
        $this->setExpectedException(RuntimeException::class, $message, $code);
84
85
        $request = $this->getMock(RequestInterface::class);
86
        $response = $this->createResponse(400, ['error' => ['code' => $code, 'message' => $message]]);
87
88
        $exception = ClientException::create($request, $response);
89
90
        $this->internalClient
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...
91
            ->expects($this->once())
92
            ->method('post')
93
            ->will($this->throwException($exception));
94
95
        $this->client->execute(static::SQL, ['foo' => 'bar']);
96
    }
97
98
    /**
99
     * @covers ::execute
100
     */
101
    public function testExecute()
102
    {
103
        $body = [
104
            'cols'     => ['id', 'name'],
105
            'rows'     => [],
106
            'rowcount' => 0,
107
            'duration' => 0
108
        ];
109
110
        $response = $this->createResponse(200, $body);
111
112
        $this->internalClient
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...
113
            ->expects($this->once())
114
            ->method('post')
115
            ->will($this->returnValue($response));
116
117
        $result = $this->client->execute(static::SQL, ['foo' => 'bar']);
118
119
        $this->assertInstanceOf(Collection::class, $result);
120
    }
121
122
    /**
123
     * @covers ::getServerInfo
124
     */
125
    public function testGetServerInfo()
126
    {
127
        $this->setExpectedException(UnsupportedException::class);
128
        $this->client->getServerInfo();
129
    }
130
131
    /**
132
     * @covers ::getServerVersion
133
     */
134
    public function testGetServerVersion()
135
    {
136
        $this->setExpectedException(UnsupportedException::class);
137
        $this->client->getServerVersion();
138
    }
139
140
    /**
141
     * @covers ::setTimeout
142
     */
143
    public function testSetTimeout()
144
    {
145
        $this->internalClient
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...
146
            ->expects($this->once())
147
            ->method('setDefaultOption')
148
            ->with('timeout', 4);
149
150
        $this->client->setTimeout('4');
151
    }
152
153
    public function testSetHTTPHeader()
154
    {
155
        $pdoClient = new Client(static::DSN, []);
156
        $reflection = new ReflectionClass(Client::class);
157
        $property = $reflection->getProperty('client');
158
        $property->setAccessible(true);
159
160
        $pdoClient->setHttpHeader('default-schema', 'my_schema');
161
        $internalPDOClient = $property->getValue($pdoClient);
162
        $header = $internalPDOClient->getDefaultOption('headers/default-schema');
163
164
        $this->assertEquals('my_schema', $header);
165
    }
166
}
167