Skip to content
This repository has been archived by the owner on Nov 15, 2022. It is now read-only.

Commit

Permalink
Fixed issue with listUsers, added getColumns and added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Hoogendijk authored and Stephen Hoogendijk committed Jan 5, 2016
1 parent 430d3e2 commit a2d6301
Show file tree
Hide file tree
Showing 17 changed files with 160 additions and 16 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,11 @@ $client->admin->revoke(\InfluxDB\Client\Admin::PRIVILEGE_ALL, 'admin_user');

## Changelog

###1.2.2
* Fixed issue with listUsers() method
* Added more unit tests
* Added getColumns method to \InfluxDB\ResultSet

###1.2.0
* Added support for 32 bit systems
* Added setters/getters for Point fields
Expand Down
6 changes: 3 additions & 3 deletions src/InfluxDB/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function __construct(
$username = '',
$password = '',
$ssl = false,
$verifySSL = true,
$verifySSL = false,
$timeout = 0
) {
$this->host = (string) $host;
Expand Down Expand Up @@ -247,9 +247,9 @@ public function listDatabases()
*/
public function listUsers()
{
$result = $this->query(null, 'SHOW USERS')->getPoints();
$result = $this->query(null, 'SHOW USERS')->getColumns();

return $this->pointsToArray($result);
return (array) $result;
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/InfluxDB/Driver/DriverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ interface DriverInterface
*/
public function setParameters(array $parameters);

/**
* @return array
*/
public function getParameters();

/**
* Send the data
*
Expand Down
10 changes: 9 additions & 1 deletion src/InfluxDB/Driver/Guzzle.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ public function setParameters(array $parameters)
$this->parameters = $parameters;
}

/**
* @return array
*/
public function getParameters()
{
return $this->parameters;
}

/**
* Send the data
*
Expand Down Expand Up @@ -106,7 +114,7 @@ public function isSuccess()

if(!in_array($statuscode, ['200', '204']))
{
throw new \Exception('HTTP Code ' . $statuscode . ' ' . $this->response->getBody());
throw new Exception('HTTP Code ' . $statuscode . ' ' . $this->response->getBody());
}

return true;
Expand Down
8 changes: 8 additions & 0 deletions src/InfluxDB/Driver/UDP.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ public function setParameters(array $parameters)
$this->parameters = $parameters;
}

/**
* @return array
*/
public function getParameters()
{
return $this->parameters;
}

/**
* Send the data
*
Expand Down
8 changes: 8 additions & 0 deletions src/InfluxDB/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ function ($object) {
return array_shift($series);
}

/**
* @return mixed
*/
public function getColumns()
{
return $this->getSeries()[0]['columns'];
}

/**
* @param array $serie
* @return array
Expand Down
6 changes: 4 additions & 2 deletions tests/unit/AbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function setUp()
->disableOriginalConstructor()
->getMock();

$this->resultData = file_get_contents(dirname(__FILE__) . '/result.example.json');
$this->resultData = file_get_contents(dirname(__FILE__) . '/json/result.example.json');

$this->mockClient->expects($this->any())
->method('getBaseURI')
Expand Down Expand Up @@ -99,7 +99,9 @@ public function buildHttpMockClient($body)
$mock = new MockHandler([
new Response(200, array(), $body),
new Response(200, array(), $body),
new Response(200, array(), 'fault{')
new Response(400, array(), 'fault{'),
new Response(400, array(), $body),
new Response(400, array(), $body),
]);

$handler = HandlerStack::create($mock);
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/AdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function testChangeUserPassword()

public function testShowUsers()
{
$testJson = file_get_contents(dirname(__FILE__) . '/result-test-users.example.json');
$testJson = file_get_contents(dirname(__FILE__) . '/json/result-test-users.example.json');

$clientMock = $this->getClientMock();
$testResult = new ResultSet($testJson);
Expand Down
74 changes: 69 additions & 5 deletions tests/unit/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,33 @@ public function testSelectDbShouldReturnDatabaseInstance()
$this->assertEquals($dbName, $database->getName());
}

public function testSecureInstance()
{
$client = $this->getClient('test', 'test', true);
$urlParts = parse_url($client->getBaseURI());

$this->assertEquals('https', $urlParts['scheme']);
}

/**
*/
public function testGuzzleQuery()
{
$client = $this->getClient();
$client = $this->getClient('test', 'test');
$query = "some-bad-query";

$bodyResponse = file_get_contents(dirname(__FILE__) . '/result.example.json');
$bodyResponse = file_get_contents(dirname(__FILE__) . '/json/result.example.json');
$httpMockClient = $this->buildHttpMockClient($bodyResponse);

$client->setDriver(new Guzzle($httpMockClient));

/** @var \InfluxDB\ResultSet $result */
$result = $client->query(null, $query);
$result = $client->query('somedb', $query);

$parameters = $client->getDriver()->getParameters();

$this->assertEquals(['test', 'test'], $parameters['auth']);
$this->assertEquals('somedb', $parameters['database']);
$this->assertInstanceOf('\InfluxDB\ResultSet', $result);

$this->assertEquals(
Expand All @@ -78,6 +89,12 @@ public function testGuzzleQuery()
[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()
Expand All @@ -86,9 +103,56 @@ public function testGetLastQuery()
$this->assertEquals($this->getClient()->getLastQuery(), 'SELECT * from test_metric');
}

protected function getClient()
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);
return new Client('localhost', 8086, $username, $password, $ssl);
}

}
4 changes: 2 additions & 2 deletions tests/unit/DatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ public function setUp()
{
parent::setUp();

$this->resultData = file_get_contents(dirname(__FILE__) . '/result.example.json');
$this->resultData = file_get_contents(dirname(__FILE__) . '/json/result.example.json');

$this->mockClient->expects($this->any())
->method('listDatabases')
->will($this->returnValue(array('test123', 'test')));

$this->dataToInsert = file_get_contents(dirname(__FILE__) . '/input.example.json');
$this->dataToInsert = file_get_contents(dirname(__FILE__) . '/json/input.example.json');

}

Expand Down
9 changes: 7 additions & 2 deletions tests/unit/ResultSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ResultSetTest extends \PHPUnit_Framework_TestCase

public function setUp()
{
$resultJsonExample = file_get_contents(dirname(__FILE__) . '/result.example.json');
$resultJsonExample = file_get_contents(dirname(__FILE__) . '/json/result.example.json');
$this->resultSet = new ResultSet($resultJsonExample);
}

Expand Down Expand Up @@ -65,7 +65,7 @@ public function testThrowsInfluxDBExceptionIfAnyErrorInSeries()
*/
public function testGetPointsFromNameWithoudTags()
{
$resultJsonExample = file_get_contents(dirname(__FILE__) . '/result-no-tags.example.json');
$resultJsonExample = file_get_contents(dirname(__FILE__) . '/json/result-no-tags.example.json');
$this->resultSet = new ResultSet($resultJsonExample);

$measurementName = 'cpu_load_short';
Expand Down Expand Up @@ -95,6 +95,11 @@ public function testGetPoints()

}

public function testGetSeries()
{
$this->assertEquals(['time', 'value'], $this->resultSet->getColumns());
}

/**
* We can get points from measurement
*/
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/json/databases.example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"results":[
{
"series":[
{
"name":"databases",
"columns":[
"name"
],
"values":[
[
"test"
],
[
"test1"
],
[
"test2"
]
]
}
]
}
]
}
File renamed without changes.
File renamed without changes.
14 changes: 14 additions & 0 deletions tests/unit/json/users.example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"results":[
{
"series":[
{
"columns":[
"user",
"admin"
]
}
]
}
]
}

1 comment on commit a2d6301

@danibrutal
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.