AbstractTest.php
3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
* @author Stephen "TheCodeAssassin" Hoogendijk
*/
namespace InfluxDB\Test;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use InfluxDB\Client;
use InfluxDB\Database;
use InfluxDB\Driver\Guzzle as GuzzleDriver;
use InfluxDB\ResultSet;
use PHPUnit_Framework_MockObject_MockObject;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Psr7\Response;
/**
* @property mixed resultData
*/
abstract class AbstractTest extends \PHPUnit_Framework_TestCase
{
/** @var Client|PHPUnit_Framework_MockObject_MockObject $client */
protected $mockClient;
/**
* @var string
*/
protected $emptyResult = '{"results":[{}]}';
/**
* @var ResultSet
*/
protected $mockResultSet;
/** @var Database $database */
protected $database = null;
public function setUp()
{
$this->mockClient = $this->getMockBuilder('\InfluxDB\Client')
->disableOriginalConstructor()
->getMock();
$this->resultData = file_get_contents(dirname(__FILE__) . '/json/result.example.json');
$this->mockClient->expects($this->any())
->method('getBaseURI')
->will($this->returnValue($this->equalTo('http://localhost:8086')));
$this->mockClient->expects($this->any())
->method('query')
->will($this->returnCallback(function ($db, $query) {
Client::$lastQuery = $query;
return new ResultSet($this->resultData);
}));
$this->mockClient->expects($this->any())
->method('write')
->will($this->returnValue(true));
$httpMockClient = new GuzzleDriver($this->buildHttpMockClient(''));
// make sure the client has a valid driver
$this->mockClient->expects($this->any())
->method('getDriver')
->will($this->returnValue($httpMockClient));
$this->database = new Database('influx_test_db', $this->mockClient);
}
/**
* @return mixed
*/
public function getMockResultSet()
{
return $this->mockResultSet;
}
/**
* @param mixed $mockResultSet
*/
public function setMockResultSet($mockResultSet)
{
$this->mockResultSet = $mockResultSet;
}
/**
* @return GuzzleClient
*/
public function buildHttpMockClient($body)
{
// Create a mock and queue two responses.
$mock = new MockHandler([
new Response(200, array(), $body),
new Response(200, array(), $body),
new Response(400, array(), 'fault{'),
new Response(400, array(), $body),
new Response(400, array(), $body),
]);
$handler = HandlerStack::create($mock);
return new GuzzleClient(['handler' => $handler]);
}
/**
* @return string
*/
public function getEmptyResult()
{
return $this->emptyResult;
}
/**
* @param bool $emptyResult
*
* @return PHPUnit_Framework_MockObject_MockObject|Client
*/
public function getClientMock($emptyResult = false)
{
$mockClient = $this->getMockBuilder('\InfluxDB\Client')
->disableOriginalConstructor()
->getMock();
if ($emptyResult) {
$mockClient->expects($this->any())
->method('query')
->will($this->returnValue(new ResultSet($this->getEmptyResult())));
}
return $mockClient;
}
}