From 065fbb4a8c392829eca6716305ed2e38ef416bef Mon Sep 17 00:00:00 2001 From: Stephen Hoogendijk Date: Fri, 3 Jul 2015 15:41:02 +0200 Subject: [PATCH] Merged leaseweb/influxdb-php into influxdb/influxdb-php --- CONTRIBUTE.md | 37 ++++++++++++++++++++ README.md | 17 +++++++--- composer.json | 4 +++ src/InfluxDB/Client.php | 12 ++----- src/InfluxDB/Database.php | 4 +-- src/InfluxDB/Point.php | 8 ++--- tests/unit/DatabaseTest.php | 68 +++++++++++++++++++++++++++++++++++-- 7 files changed, 127 insertions(+), 23 deletions(-) create mode 100644 CONTRIBUTE.md diff --git a/CONTRIBUTE.md b/CONTRIBUTE.md new file mode 100644 index 0000000000..a71780b0da --- /dev/null +++ b/CONTRIBUTE.md @@ -0,0 +1,37 @@ +/* CONTRIBUTE */ + +This is the contribute.md of influxdb-php. Great to have you here. + +Here are a few ways you can help make this project better. + +# Contribute.md + +## Team members + +Stephen "TheCodeAssassin" Hoogendijk +Daniel "danibrutal" Martinez + +## Helping out + +We appreciate any efforts to help us writing this library. You can contribute in any of the following ways: + +* Documentation +* Unit tests +* New features +* Bug fixed +* Reviewing pull requests + +## Guidelines + +In order for your pull requests to get accepted we hold all the code to the following criteria: + +* PSR-1/PSR-2 compliant +* Do not use left hand conditions such as false == $something +* New features need to be documented +* Breaking changes should be well highlighted and explained in the PR + +The following is optional but encouraged: + +* Code should be documented +* Code should be unit tested +* Do not write conditions like false === $something, rather $something === false. diff --git a/README.md b/README.md index de469559c9..9f2e7c460f 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ ## InfluxDB client library for PHP [![Build Status](https://travis-ci.org/LeaseWeb/influxdb-php.svg?branch=master)](https://travis-ci.org/LeaseWeb/influxdb-php) [![Code Climate](https://codeclimate.com/github/LeaseWeb/influxdb-php/badges/gpa.svg)](https://codeclimate.com/github/LeaseWeb/influxdb-php) +[![Test Coverage](https://codeclimate.com/github/LeaseWeb/influxdb-php/badges/coverage.svg)](https://codeclimate.com/github/LeaseWeb/influxdb-php/coverage) ### Overview @@ -39,8 +40,8 @@ To fetch records from InfluxDB you can do a query directly on a database: ```php - // fetch the db - $database = $client->db('influx_test_db'); + // fetch the selectDB + $database = $client->selectDB('influx_test_db'); // executing a query will yield a resultset object $result = $database->query('select * from test_metric LIMIT 5'); @@ -116,8 +117,8 @@ This library makes it easy to provide a retention policy when creating a databas // create the client $client = new \InfluxDB\Client($host, $port, '', ''); - // select the db - $database = $client->db('influx_test_db'); + // select the selectDB + $database = $client->selectDB('influx_test_db'); // create the database with a retention policy $result = $database->create(new RetentionPolicy('test', '5d', 1, true)); @@ -151,6 +152,12 @@ Some functions are too general for a database. So these are available in the cli ## Changelog -####0.1 +####0.1.1 +* Merged repository to influxdb/influxdb-php +* Added unit test for createRetentionPolicy +* -BREAKING CHANGE- changed $client->db to $client->selectDB + + +####0.1.0 ------ * Initial release diff --git a/composer.json b/composer.json index cada240440..1f44e19044 100644 --- a/composer.json +++ b/composer.json @@ -14,6 +14,10 @@ { "name": "Stephen Hoogendijk", "email": "stephen@tca0.nl" + }, + { + "name": "Daniel Martinez", + "email": "danimartcas@hotmail.com" } ], "require": { diff --git a/src/InfluxDB/Client.php b/src/InfluxDB/Client.php index 5158f77c57..2a7e677e35 100644 --- a/src/InfluxDB/Client.php +++ b/src/InfluxDB/Client.php @@ -124,7 +124,7 @@ class Client * * @return Database */ - public function db($name) + public function selectDB($name) { if (empty($name)) { @@ -261,7 +261,7 @@ class Client $timeout ); - return ($dbName ? $client->db($dbName) : $client); + return ($dbName ? $client->selectDB($dbName) : $client); } /** * @return mixed @@ -294,12 +294,4 @@ class Client return $names; } - - /** - * @return \Guzzle\Http\Client - */ - public function getHttpClient() - { - return $this->httpClient; - } } \ No newline at end of file diff --git a/src/InfluxDB/Database.php b/src/InfluxDB/Database.php index 470df190b2..660e02e8a8 100644 --- a/src/InfluxDB/Database.php +++ b/src/InfluxDB/Database.php @@ -44,7 +44,7 @@ class Database $this->client = $client; if (!$name) { - throw new DatabaseException('No database name provided'); + throw new \InvalidArgumentException('No database name provided'); } $this->name = $name; @@ -105,7 +105,7 @@ class Database */ public function createRetentionPolicy(RetentionPolicy $retentionPolicy) { - $this->query($this->getRetentionPolicyQuery('CREATE', $retentionPolicy)); + return $this->query($this->getRetentionPolicyQuery('CREATE', $retentionPolicy)); } /** diff --git a/src/InfluxDB/Point.php b/src/InfluxDB/Point.php index 6567721de5..f6fe43ee26 100644 --- a/src/InfluxDB/Point.php +++ b/src/InfluxDB/Point.php @@ -2,7 +2,7 @@ namespace InfluxDB; -use InfluxDB\Database\Exception; +use InfluxDB\Database\Exception as DatabaseException; /** * Class Point @@ -35,13 +35,13 @@ class Point * @param array $additionalFields Array of optional fields * @param int $timestamp Optional timestamp * - * @throws Exception + * @throws DatabaseException */ public function __construct($measurement, $value, array $tags = array(), array $additionalFields = array(), $timestamp = null) { if (empty($measurement)) { - throw new Exception('Invalid measurement name provided'); + throw new DatabaseException('Invalid measurement name provided'); } $this->measurement = (string) $measurement; @@ -51,7 +51,7 @@ class Point $this->fields += array('value' => (float) $value); if ($timestamp && !$this->isValidTimeStamp($timestamp)) { - throw new Exception(sprintf('%s is not a valid timestamp', $timestamp)); + throw new DatabaseException(sprintf('%s is not a valid timestamp', $timestamp)); } $this->timestamp = $timestamp; diff --git a/tests/unit/DatabaseTest.php b/tests/unit/DatabaseTest.php index ab328dae1d..890c03e451 100644 --- a/tests/unit/DatabaseTest.php +++ b/tests/unit/DatabaseTest.php @@ -6,6 +6,8 @@ namespace InfluxDB\Test; use InfluxDB\Client; use InfluxDB\Database; use InfluxDB\Point; +use InfluxDB\ResultSet; +use PHPUnit_Framework_MockObject_MockObject; class DatabaseTest extends \PHPUnit_Framework_TestCase { @@ -13,24 +15,85 @@ class DatabaseTest extends \PHPUnit_Framework_TestCase /** @var Database $db */ protected $db = null; - /** @var Client $client */ + /** @var Client|PHPUnit_Framework_MockObject_MockObject $client */ protected $mockClient; + /** + * @var string + */ protected $dataToInsert; + /** + * @var string + */ + protected $resultData; + + /** + * @var string + */ + static $emptyResult = '{"results":[{}]}'; + + /** + * @var + */ + protected $mockResultSet; + public function setUp() { $this->mockClient = $this->getMockBuilder('\InfluxDB\Client') ->disableOriginalConstructor() ->getMock(); + $this->resultData = file_get_contents(dirname(__FILE__) . '/result.example.json'); + $this->mockClient->expects($this->any()) ->method('getBaseURI') ->will($this->returnValue($this->equalTo('http://localhost:8086'))); + $this->mockClient->expects($this->any()) + ->method('query') + ->will($this->returnValue(new ResultSet($this->resultData))); + $this->db = new Database('influx_test_db', $this->mockClient); $this->dataToInsert = file_get_contents(dirname(__FILE__) . '/input.example.json'); + + } + + /** + * + */ + public function testQuery() + { + $testResultSet = new ResultSet($this->resultData); + $this->assertEquals($this->db->query('SELECT * FROM test_metric'), $testResultSet); + } + + public function testCreateRetentionPolicy() + { + $retentionPolicy = new Database\RetentionPolicy('test', '1d', 1, true); + + $mockClient = $this->getMockBuilder('\InfluxDB\Client') + ->disableOriginalConstructor() + ->getMock(); + + $mockClient->expects($this->once()) + ->method('query') + ->will($this->returnValue(new ResultSet(self::$emptyResult))); + + + + $database = new Database('test', $mockClient); + + $this->assertEquals($database->createRetentionPolicy($retentionPolicy), new ResultSet(self::$emptyResult)); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testEmptyDatabaseName() + { + new Database(null, $this->mockClient); } @@ -53,7 +116,8 @@ class DatabaseTest extends \PHPUnit_Framework_TestCase $this->mockClient->expects($this->once()) ->method('write') - ->with($this->equalTo($this->db->getName()), $this->equalTo($payloadExpected)); + ->with($this->equalTo($this->db->getName()), $this->equalTo($payloadExpected)) + ->will($this->returnValue(true)); $this->db->writePoints(array($point1, $point2)); }