Files

158 lines
4.3 KiB
PHP
Raw Permalink Normal View History

2015-06-18 14:59:00 +02:00
<?php
2015-06-23 09:19:24 +02:00
namespace InfluxDB\Test;
2015-06-18 14:59:00 +02:00
2015-06-23 09:19:24 +02:00
use InfluxDB\Client;
2015-07-21 00:34:08 +02:00
use InfluxDB\Driver\Guzzle;
use InfluxDB\Point;
2015-06-18 14:59:00 +02:00
class ClientTest extends AbstractTest
2015-06-18 14:59:00 +02:00
{
/**
*
*/
public function setUp()
{
parent::setUp();
}
2015-06-18 14:59:00 +02:00
/** @var Client $client */
protected $client = null;
2015-07-21 00:34:08 +02:00
public function testGetters()
{
$client = $this->getClient();
$this->assertEquals('http://localhost:8086', $client->getBaseURI());
$this->assertInstanceOf('InfluxDB\Driver\Guzzle', $client->getDriver());
$this->assertEquals('localhost', $client->getHost());
$this->assertEquals('0', $client->getTimeout());
}
2015-06-18 14:59:00 +02:00
public function testBaseURl()
{
$client = $this->getClient();
2015-07-03 17:20:35 +02:00
2015-07-21 00:34:08 +02:00
$this->assertEquals($client->getBaseURI(), 'http://localhost:8086');
2015-06-18 14:59:00 +02:00
}
2015-07-03 17:29:25 +02:00
public function testSelectDbShouldReturnDatabaseInstance()
{
$client = $this->getClient();
2015-07-03 17:29:25 +02:00
$dbName = 'test-database';
2015-07-21 00:34:08 +02:00
$database = $client->selectDB($dbName);
2015-07-03 17:29:25 +02:00
2015-07-21 00:34:08 +02:00
$this->assertInstanceOf('\InfluxDB\Database', $database);
2015-07-03 17:29:25 +02:00
2015-07-21 00:34:08 +02:00
$this->assertEquals($dbName, $database->getName());
2015-07-03 17:29:25 +02:00
}
2015-07-06 15:58:29 +02:00
public function testSecureInstance()
{
$client = $this->getClient('test', 'test', true);
$urlParts = parse_url($client->getBaseURI());
$this->assertEquals('https', $urlParts['scheme']);
}
2015-07-06 15:58:29 +02:00
/**
*/
2015-07-21 00:34:08 +02:00
public function testGuzzleQuery()
2015-07-06 15:58:29 +02:00
{
$client = $this->getClient('test', 'test');
2015-07-06 15:58:29 +02:00
$query = "some-bad-query";
$bodyResponse = file_get_contents(dirname(__FILE__) . '/json/result.example.json');
$httpMockClient = $this->buildHttpMockClient($bodyResponse);
2015-07-06 15:58:29 +02:00
2015-07-21 00:34:08 +02:00
$client->setDriver(new Guzzle($httpMockClient));
2015-07-06 15:58:29 +02:00
/** @var \InfluxDB\ResultSet $result */
$result = $client->query('somedb', $query);
$parameters = $client->getDriver()->getParameters();
2015-07-06 15:58:29 +02:00
$this->assertEquals(['test', 'test'], $parameters['auth']);
$this->assertEquals('somedb', $parameters['database']);
2015-07-06 15:58:29 +02:00
$this->assertInstanceOf('\InfluxDB\ResultSet', $result);
$this->assertEquals(
true,
$client->write(
[
'url' => 'http://localhost',
'database' => 'influx_test_db',
'method' => 'post'
],
[new Point('test', 1.0)]
)
);
$this->setExpectedException('\InvalidArgumentException');
$client->query('test', 'bad-query');
$this->setExpectedException('\InfluxDB\Driver\Exception');
$client->query('test', 'bad-query');
}
public function testGetLastQuery()
{
$this->mockClient->query('test', 'SELECT * from test_metric');
$this->assertEquals($this->getClient()->getLastQuery(), 'SELECT * from test_metric');
}
public function testListDatabases()
{
$this->doTestResponse('databases.example.json', ['test', 'test1', 'test2'], 'listDatabases');
}
public function testListUsers()
{
$this->doTestResponse('users.example.json', ['user', 'admin'], 'listUsers');
}
public function testFactoryMethod()
{
$client = $this->getClient('test', 'test', true);
$staticClient = \InfluxDB\Client::fromDSN('https+influxdb://test:test@localhost:8086/');
$this->assertEquals($client, $staticClient);
$db = $client->selectDB('testdb');
$staticDB = \InfluxDB\Client::fromDSN('https+influxdb://test:test@localhost:8086/testdb');
$this->assertEquals($db, $staticDB);
}
/**
* @param string $responseFile
* @param array $result
* @param string $method
*/
protected function doTestResponse($responseFile, array $result, $method)
{
$client = $this->getClient();
$bodyResponse = file_get_contents(dirname(__FILE__) . '/json/'. $responseFile);
$httpMockClient = $this->buildHttpMockClient($bodyResponse);
$client->setDriver(new Guzzle($httpMockClient));
$this->assertEquals($result, $client->$method());
}
/**
* @param string $username
* @param string $password
* @param bool|false $ssl
*
* @return Client
*/
protected function getClient($username = '', $password = '', $ssl = false)
{
return new Client('localhost', 8086, $username, $password, $ssl);
2015-07-06 15:58:29 +02:00
}
2015-06-18 14:59:00 +02:00
}