diff --git a/src/InfluxDB/Client.php b/src/InfluxDB/Client.php index 2a7e677e35..0aa51dff4e 100644 --- a/src/InfluxDB/Client.php +++ b/src/InfluxDB/Client.php @@ -117,6 +117,18 @@ class Client } + /** + * For testing + * @param \Guzzle\Http\Client $client + * @return $this + */ + public function setHttpClient(\Guzzle\Http\Client $client) + { + $this->httpClient = $client; + + return $this; + } + /** * Use the given database * diff --git a/tests/unit/ClientTest.php b/tests/unit/ClientTest.php index 609bb632d2..9240195d51 100644 --- a/tests/unit/ClientTest.php +++ b/tests/unit/ClientTest.php @@ -3,6 +3,7 @@ namespace InfluxDB\Test; use InfluxDB\Client; +use InfluxDB\ResultSet; class ClientTest extends \PHPUnit_Framework_TestCase { @@ -30,4 +31,38 @@ class ClientTest extends \PHPUnit_Framework_TestCase $this->assertEquals($dbName, $db->getName()); } + + + /** + */ + public function testQuery() + { + $client = new Client('localhost', 8086); + $query = "some-bad-query"; + + $bodyResponse = file_get_contents(dirname(__FILE__) . '/result.example.json'); + $httpMockClient = $this->buildHttpMockClient($bodyResponse); + + $client->setHttpClient($httpMockClient); + + /** @var \InfluxDB\ResultSet $result */ + $result = $client->query(null, $query); + + $this->assertInstanceOf('\InfluxDB\ResultSet', $result); + } + + /** + * @return \Guzzle\Http\Client + */ + protected function buildHttpMockClient($body) + { + $plugin = new \Guzzle\Plugin\Mock\MockPlugin(); + $response= new \Guzzle\Http\Message\Response(200); + $response->setBody($body); + $plugin->addResponse($response); + $mockedClient = new \Guzzle\Http\Client(); + $mockedClient->addSubscriber($plugin); + + return $mockedClient; + } } \ No newline at end of file