Fixed existing unit tests

This commit is contained in:
Stephen Hoogendijk
2015-07-21 00:34:08 +02:00
parent 1de116fd03
commit c2bd3c4a79
5 changed files with 48 additions and 65 deletions
+15 -17
View File
@@ -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;
}
}
+4 -4
View File
@@ -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));
+3 -17
View File
@@ -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;
}
/**
+18 -19
View File
@@ -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]);
}
}
+8 -8
View File
@@ -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)));
}
}