Merge pull request #7 from supertowers/master

PHP5.3 compatibility and InfluxDB deprecated features
This commit is contained in:
César D. Rodas 2014-09-04 13:26:53 -04:00
commit 205bd51cb4
6 changed files with 27 additions and 25 deletions

View File

@ -2,6 +2,7 @@ language: php
script: phpunit script: phpunit
php: php:
- 5.3
- 5.4 - 5.4
- 5.5 - 5.5
@ -9,4 +10,5 @@ before_script:
- curl -s http://getcomposer.org/installer | php - curl -s http://getcomposer.org/installer | php
- wget http://s3.amazonaws.com/influxdb/influxdb_latest_amd64.deb - wget http://s3.amazonaws.com/influxdb/influxdb_latest_amd64.deb
- sudo dpkg -i influxdb_latest_amd64.deb - sudo dpkg -i influxdb_latest_amd64.deb
- travis_retry sudo service influxdb restart
- php composer.phar install - php composer.phar install

View File

@ -64,9 +64,9 @@ class BaseHTTP
$c->children[] = $this; $c->children[] = $this;
} }
protected function getCurl($url, array $args = []) protected function getCurl($url, array $args = array())
{ {
$args = array_merge($args, ['u' => $this->user, 'p' => $this->pass]); $args = array_merge($args, array('u' => $this->user, 'p' => $this->pass));
$url = "http://{$this->host}:{$this->port}/{$this->base}{$url}"; $url = "http://{$this->host}:{$this->port}/{$this->base}{$url}";
$url .= "?" . http_build_query($args); $url .= "?" . http_build_query($args);
$ch = curl_init($url); $ch = curl_init($url);
@ -89,9 +89,9 @@ class BaseHTTP
protected function delete($url) protected function delete($url)
{ {
$ch = $this->getCurl($url); $ch = $this->getCurl($url);
curl_setopt_array($ch, [ curl_setopt_array($ch, array(
CURLOPT_CUSTOMREQUEST => "DELETE", CURLOPT_CUSTOMREQUEST => "DELETE",
]); ));
return $this->execCurl($ch); return $this->execCurl($ch);
} }
@ -119,19 +119,19 @@ class BaseHTTP
throw new \InvalidArgumentException("Expecting s, m or u as time precision"); throw new \InvalidArgumentException("Expecting s, m or u as time precision");
} }
protected function get($url, array $args = []) protected function get($url, array $args = array())
{ {
$ch = $this->getCurl($url, $args); $ch = $this->getCurl($url, $args);
return $this->execCurl($ch, true); return $this->execCurl($ch, true);
} }
protected function post($url, array $body, array $args = []) protected function post($url, array $body, array $args = array())
{ {
$ch = $this->getCurl($url, $args); $ch = $this->getCurl($url, $args);
curl_setopt_array($ch, [ curl_setopt_array($ch, array(
CURLOPT_POST => 1, CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => json_encode($body), CURLOPT_POSTFIELDS => json_encode($body),
]); ));
return $this->execCurl($ch); return $this->execCurl($ch);
} }

View File

@ -59,7 +59,7 @@ class Client extends BaseHTTP
public function createDatabase($name) public function createDatabase($name)
{ {
$this->post('db', ['name' => $name]); $this->post('db', array('name' => $name));
return new DB($this, $name); return new DB($this, $name);
} }
@ -68,7 +68,7 @@ class Client extends BaseHTTP
$self = $this; $self = $this;
return array_map(function($obj) use($self) { return array_map(function($obj) use($self) {
return new DB($self, $obj['name']); return new DB($self, $obj['name']);
}, $this->get('dbs')); }, $this->get('db'));
} }
public function getDatabase($name) public function getDatabase($name)

View File

@ -43,7 +43,7 @@ class Cursor extends ArrayIterator
{ {
public function __construct(array $resultset) public function __construct(array $resultset)
{ {
$rows = []; $rows = array();
foreach ($resultset as $set) { foreach ($resultset as $set) {
foreach ($set['points'] as $row) { foreach ($set['points'] as $row) {
$row = (object)array_combine($set['columns'], $row); $row = (object)array_combine($set['columns'], $row);

View File

@ -62,17 +62,17 @@ class DB extends BaseHTTP
public function insert($name, array $data) public function insert($name, array $data)
{ {
$points = []; $points = array();
$first = current($data); $first = current($data);
if (!is_array($first)) { if (!is_array($first)) {
return $this->insert($name, [$data]); return $this->insert($name, array($data));
} }
$columns = array_keys($first); $columns = array_keys($first);
foreach ($data as $value) { foreach ($data as $value) {
$points[] = array_values($value); $points[] = array_values($value);
} }
$body = compact('name', 'columns', 'points'); $body = compact('name', 'columns', 'points');
return $this->post('series', [$body], ['time_precision' => $this->timePrecision]); return $this->post('series', array($body), array('time_precision' => $this->timePrecision));
} }
public function first($sql) public function first($sql)
@ -82,11 +82,11 @@ class DB extends BaseHTTP
public function query($sql) public function query($sql)
{ {
return new Cursor($this->get('series', ['q' => $sql, 'time_precision' => $this->timePrecision])); return new Cursor($this->get('series', array('q' => $sql, 'time_precision' => $this->timePrecision)));
} }
public function createUser($username, $password) public function createUser($name, $password)
{ {
return $this->post('users', compact('username', 'password')); return $this->post('users', compact('name', 'password'));
} }
} }

View File

@ -2,7 +2,7 @@
use crodas\InfluxPHP\Client; use crodas\InfluxPHP\Client;
use crodas\InfluxPHP\DB; use crodas\InfluxPHP\DB;
class DBTest extends \phpunit_framework_testcase class DBTest extends \PHPUnit_Framework_TestCase
{ {
public function testCreate() public function testCreate()
{ {
@ -69,7 +69,7 @@ class DBTest extends \phpunit_framework_testcase
public function testInvalidTimePrecision() public function testInvalidTimePrecision()
{ {
$client = new Client; $client = new Client;
$client->SetTimePrecision([]); $client->SetTimePrecision(array());
} }
public function testQuery() public function testQuery()
@ -78,9 +78,9 @@ class DBTest extends \phpunit_framework_testcase
$db = $client->createDatabase("test_xxx"); $db = $client->createDatabase("test_xxx");
$db->createUser("root", "root"); $db->createUser("root", "root");
$db->insert("foobar", ['type' => '/foobar', 'karma' => 10]); $db->insert("foobar", array('type' => '/foobar', 'karma' => 10));
$db->insert("foobar", ['type' => '/foobar', 'karma' => 20]); $db->insert("foobar", array('type' => '/foobar', 'karma' => 20));
$db->insert("foobar", ['type' => '/barfoo', 'karma' => 30]); $db->insert("foobar", array('type' => '/barfoo', 'karma' => 30));
$this->assertEquals($db->first("SELECT max(karma) FROM foobar")->max, 30); $this->assertEquals($db->first("SELECT max(karma) FROM foobar")->max, 30);
$this->assertEquals($db->first("SELECT min(karma) FROM foobar")->min, 10); $this->assertEquals($db->first("SELECT min(karma) FROM foobar")->min, 10);
@ -103,17 +103,17 @@ class DBTest extends \phpunit_framework_testcase
$db = $client->test_xxx; $db = $client->test_xxx;
$client->setTimePrecision('u'); $client->setTimePrecision('u');
foreach ($db->query("SELECT mean(karma) FROM foobar GROUP BY type") as $row) { foreach ($db->query("SELECT mean(karma) FROM foobar GROUP BY type, time(1h)") as $row) {
$this->assertTrue($row->time > time()*1000); $this->assertTrue($row->time > time()*1000);
} }
$client->setTimePrecision('m'); $client->setTimePrecision('m');
foreach ($db->query("SELECT mean(karma) FROM foobar GROUP BY type") as $row) { foreach ($db->query("SELECT mean(karma) FROM foobar GROUP BY type, time(1h)") as $row) {
$this->assertTrue($row->time < time()*10000); $this->assertTrue($row->time < time()*10000);
} }
$client->setTimePrecision('s'); $client->setTimePrecision('s');
foreach ($db->query("SELECT mean(karma) FROM foobar GROUP BY type") as $row) { foreach ($db->query("SELECT mean(karma) FROM foobar GROUP BY type, time(1h)") as $row) {
$this->assertTrue($row->time < time()+20); $this->assertTrue($row->time < time()+20);
} }