From c2bd3c4a79217b124113111321452bdf334cf9ba Mon Sep 17 00:00:00 2001 From: Stephen Hoogendijk Date: Tue, 21 Jul 2015 00:34:08 +0200 Subject: [PATCH] Fixed existing unit tests --- src/InfluxDB/Client.php | 32 ++++++++++++++--------------- src/InfluxDB/Database.php | 8 ++++---- src/InfluxDB/Driver/Guzzle.php | 20 +++--------------- tests/unit/ClientTest.php | 37 +++++++++++++++++----------------- tests/unit/DatabaseTest.php | 16 +++++++-------- 5 files changed, 48 insertions(+), 65 deletions(-) diff --git a/src/InfluxDB/Client.php b/src/InfluxDB/Client.php index 8a49d3ecc..2cb932d1c 100644 --- a/src/InfluxDB/Client.php +++ b/src/InfluxDB/Client.php @@ -114,11 +114,15 @@ class Client $this->baseURI = sprintf('%s://%s:%d', $this->scheme, $this->host, $this->port); // set the default driver to guzzle - $this->driver = new Guzzle([ - 'timeout' => $this->timeout, - 'base_uri' => $this->baseURI, - 'verify' => $this->verifySSL - ]); + $this->driver = new Guzzle( + new \GuzzleHttp\Client( + [ + 'timeout' => $this->timeout, + 'base_uri' => $this->baseURI, + 'verify' => $this->verifySSL + ] + ) + ); } /** @@ -152,7 +156,9 @@ class Client $params['db'] = $database; } - $this->driver->setParameters([ + $driver = $this->getDriver(); + + $driver->setParameters([ 'url' => 'query?' . http_build_query(array_merge(['q' => $query], $params)), 'database' => $database, 'method' => 'get' @@ -160,9 +166,9 @@ class Client try { // send the data - $this->driver->send(); + $driver->send(); - return $this->driver->getResultSet(); + return $driver->getResultSet(); } catch (\Exception $e) { throw new Exception(sprintf('Query has failed, exception: %s', $e->getMessage())); @@ -275,7 +281,7 @@ class Client } /** - * @return DriverInterface + * @return DriverInterface|QueryDriverInterface */ public function getDriver() { @@ -289,12 +295,4 @@ class Client { return $this->host; } - - /** - * @return DriverInterface - */ - public function getDefaultDriver() - { - return $this->defaultDriver; - } } diff --git a/src/InfluxDB/Database.php b/src/InfluxDB/Database.php index 4cc56cf5f..601ca8ec5 100644 --- a/src/InfluxDB/Database.php +++ b/src/InfluxDB/Database.php @@ -127,10 +127,10 @@ class Database try { $driver = $this->client->getDriver(); $driver->setParameters([ - 'url' => sprintf('write?db=%s&precision=%s', $this->name, $precision), - 'database' => $this->name, - 'method' => 'post' - ]); + 'url' => sprintf('write?db=%s&precision=%s', $this->name, $precision), + 'database' => $this->name, + 'method' => 'post' + ]); // send the points to influxDB $driver->send(implode(PHP_EOL, $payload)); diff --git a/src/InfluxDB/Driver/Guzzle.php b/src/InfluxDB/Driver/Guzzle.php index 6ce742ed9..a9b746864 100644 --- a/src/InfluxDB/Driver/Guzzle.php +++ b/src/InfluxDB/Driver/Guzzle.php @@ -20,14 +20,6 @@ class Guzzle implements DriverInterface, QueryDriverInterface */ private $parameters; - /** - * - * Configuration for this driver - * - * @var array - */ - private $config; - /** * @var Client */ @@ -41,19 +33,13 @@ class Guzzle implements DriverInterface, QueryDriverInterface /** * Set the config for this driver * - * @param array $config + * @param Client $client * * @return mixed */ - public function __construct(array $config) + public function __construct(Client $client) { - $this->httpClient = new Client([ - 'timeout' => $config['timeout'], - 'verify' => $config['verify'], - 'base_uri' => $config['base_uri'] - ]); - - $this->config = $config; + $this->httpClient = $client; } /** diff --git a/tests/unit/ClientTest.php b/tests/unit/ClientTest.php index 9240195d5..2b6e8148b 100644 --- a/tests/unit/ClientTest.php +++ b/tests/unit/ClientTest.php @@ -2,22 +2,24 @@ namespace InfluxDB\Test; +use GuzzleHttp\Client as GuzzleClient; +use GuzzleHttp\Handler\MockHandler; +use GuzzleHttp\HandlerStack; +use GuzzleHttp\Psr7\Response; use InfluxDB\Client; -use InfluxDB\ResultSet; +use InfluxDB\Driver\Guzzle; class ClientTest extends \PHPUnit_Framework_TestCase { /** @var Client $client */ protected $client = null; - + public function testBaseURl() { $client = new Client('localhost', 8086); - $this->assertEquals( - $client->getBaseURI(), 'http://localhost:8086' - ); + $this->assertEquals($client->getBaseURI(), 'http://localhost:8086'); } public function testSelectDbShouldReturnDatabaseInstance() @@ -25,25 +27,25 @@ class ClientTest extends \PHPUnit_Framework_TestCase $client = new Client('localhost', 8086); $dbName = 'test-database'; - $db = $client->selectDB($dbName); + $database = $client->selectDB($dbName); - $this->assertInstanceOf('\InfluxDB\Database', $db); + $this->assertInstanceOf('\InfluxDB\Database', $database); - $this->assertEquals($dbName, $db->getName()); + $this->assertEquals($dbName, $database->getName()); } /** */ - public function testQuery() + public function testGuzzleQuery() { $client = new Client('localhost', 8086); $query = "some-bad-query"; $bodyResponse = file_get_contents(dirname(__FILE__) . '/result.example.json'); - $httpMockClient = $this->buildHttpMockClient($bodyResponse); + $httpMockClient = self::buildHttpMockClient($bodyResponse); - $client->setHttpClient($httpMockClient); + $client->setDriver(new Guzzle($httpMockClient)); /** @var \InfluxDB\ResultSet $result */ $result = $client->query(null, $query); @@ -54,15 +56,12 @@ class ClientTest extends \PHPUnit_Framework_TestCase /** * @return \Guzzle\Http\Client */ - protected function buildHttpMockClient($body) + public static 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); + // Create a mock and queue two responses. + $mock = new MockHandler([new Response(200, array(), $body)]); - return $mockedClient; + $handler = HandlerStack::create($mock); + return new GuzzleClient(['handler' => $handler]); } } \ No newline at end of file diff --git a/tests/unit/DatabaseTest.php b/tests/unit/DatabaseTest.php index d6da46be8..25fcd1cf7 100644 --- a/tests/unit/DatabaseTest.php +++ b/tests/unit/DatabaseTest.php @@ -4,6 +4,7 @@ namespace InfluxDB\Test; use InfluxDB\Client; use InfluxDB\Database; +use InfluxDB\Driver\Guzzle; use InfluxDB\Point; use InfluxDB\ResultSet; use PHPUnit_Framework_MockObject_MockObject; @@ -59,6 +60,12 @@ class DatabaseTest extends PHPUnit_Framework_TestCase ->method('listDatabases') ->will($this->returnValue(array('test123', 'test'))); + $httpMockClient = new Guzzle(ClientTest::buildHttpMockClient('')); + + // make sure the client has a valid driver + $this->mockClient->expects($this->any()) + ->method('getDriver') + ->will($this->returnValue($httpMockClient)); $this->db = new Database('influx_test_db', $this->mockClient); @@ -132,13 +139,6 @@ class DatabaseTest extends PHPUnit_Framework_TestCase 0.84 ); - $payloadExpected ="$point1\n$point2"; - - $this->mockClient->expects($this->once()) - ->method('write') - ->with($this->equalTo($this->db->getName()), $this->equalTo($payloadExpected)) - ->will($this->returnValue(true)); - - $this->db->writePoints(array($point1, $point2)); + $this->assertEquals(true, $this->db->writePoints(array($point1, $point2))); } } \ No newline at end of file