summaryrefslogtreecommitdiff
path: root/libs/picodb/tests
diff options
context:
space:
mode:
Diffstat (limited to 'libs/picodb/tests')
-rw-r--r--libs/picodb/tests/AlternativeSchemaFixture.php15
-rw-r--r--libs/picodb/tests/MysqlDatabaseTest.php101
-rw-r--r--libs/picodb/tests/MysqlDriverTest.php73
-rw-r--r--libs/picodb/tests/MysqlLobTest.php83
-rw-r--r--libs/picodb/tests/MysqlSchemaTest.php49
-rw-r--r--libs/picodb/tests/MysqlTableTest.php356
-rw-r--r--libs/picodb/tests/PostgresDatabaseTest.php100
-rw-r--r--libs/picodb/tests/PostgresDriverTest.php78
-rw-r--r--libs/picodb/tests/PostgresLobTest.php87
-rw-r--r--libs/picodb/tests/PostgresSchemaTest.php40
-rw-r--r--libs/picodb/tests/PostgresTableTest.php355
-rw-r--r--libs/picodb/tests/SchemaFixture.php21
-rw-r--r--libs/picodb/tests/SqliteDatabaseTest.php120
-rw-r--r--libs/picodb/tests/SqliteDriverTest.php70
-rw-r--r--libs/picodb/tests/SqliteLobtest.php84
-rw-r--r--libs/picodb/tests/SqliteSchemaTest.php36
-rw-r--r--libs/picodb/tests/SqliteTableTest.php444
-rw-r--r--libs/picodb/tests/UrlParserTest.php46
18 files changed, 2158 insertions, 0 deletions
diff --git a/libs/picodb/tests/AlternativeSchemaFixture.php b/libs/picodb/tests/AlternativeSchemaFixture.php
new file mode 100644
index 00000000..bebcf137
--- /dev/null
+++ b/libs/picodb/tests/AlternativeSchemaFixture.php
@@ -0,0 +1,15 @@
+<?php
+
+namespace AlternativeSchema;
+
+use PDO;
+
+function version_1(PDO $pdo)
+{
+ $pdo->exec('CREATE TABLE test1 (column1 TEXT)');
+}
+
+function version_2(PDO $pdo)
+{
+ $pdo->exec('CREATE TABLE test2 (column2 TEXT)');
+}
diff --git a/libs/picodb/tests/MysqlDatabaseTest.php b/libs/picodb/tests/MysqlDatabaseTest.php
new file mode 100644
index 00000000..bd819dd9
--- /dev/null
+++ b/libs/picodb/tests/MysqlDatabaseTest.php
@@ -0,0 +1,101 @@
+<?php
+
+require_once __DIR__.'/../../../vendor/autoload.php';
+
+use PicoDb\Database;
+
+class MysqlDatabaseTest extends PHPUnit_Framework_TestCase
+{
+ /**
+ * @var PicoDb\Database
+ */
+ private $db;
+
+ public function setUp()
+ {
+ $this->db = new Database(array('driver' => 'mysql', 'hostname' => 'localhost', 'username' => 'root', 'password' => '', 'database' => 'picodb'));
+ $this->db->getConnection()->exec('CREATE DATABASE IF NOT EXISTS `picodb`');
+ $this->db->getConnection()->exec('DROP TABLE IF EXISTS foobar');
+ $this->db->getConnection()->exec('DROP TABLE IF EXISTS schema_version');
+ }
+
+ public function testEscapeIdentifer()
+ {
+ $this->assertEquals('`a`', $this->db->escapeIdentifier('a'));
+ $this->assertEquals('a.b', $this->db->escapeIdentifier('a.b'));
+ $this->assertEquals('`c`.`a`', $this->db->escapeIdentifier('a', 'c'));
+ $this->assertEquals('a.b', $this->db->escapeIdentifier('a.b', 'c'));
+ $this->assertEquals('SELECT COUNT(*) FROM test', $this->db->escapeIdentifier('SELECT COUNT(*) FROM test'));
+ $this->assertEquals('SELECT COUNT(*) FROM test', $this->db->escapeIdentifier('SELECT COUNT(*) FROM test', 'b'));
+ }
+
+ public function testEscapeIdentiferList()
+ {
+ $this->assertEquals(array('`c`.`a`', '`c`.`b`'), $this->db->escapeIdentifierList(array('a', 'b'), 'c'));
+ $this->assertEquals(array('`a`', 'd.b'), $this->db->escapeIdentifierList(array('a', 'd.b')));
+ }
+
+ public function testThatPreparedStatementWorks()
+ {
+ $this->db->getConnection()->exec('CREATE TABLE foobar (id INT AUTO_INCREMENT NOT NULL, something TEXT, PRIMARY KEY (id)) ENGINE=InnoDB');
+ $this->db->execute('INSERT INTO foobar (something) VALUES (?)', array('a'));
+ $this->assertEquals(1, $this->db->getLastId());
+ $this->assertEquals('a', $this->db->execute('SELECT something FROM foobar WHERE something=?', array('a'))->fetchColumn());
+ }
+
+ /**
+ * @expectedException PicoDb\SQLException
+ */
+ public function testBadSQLQuery()
+ {
+ $this->db->execute('INSERT INTO foobar');
+ }
+
+ public function testDuplicateKey()
+ {
+ $this->db->getConnection()->exec('CREATE TABLE foobar (something CHAR(1) UNIQUE) ENGINE=InnoDB');
+
+ $this->assertNotFalse($this->db->execute('INSERT INTO foobar (something) VALUES (?)', array('a')));
+ $this->assertFalse($this->db->execute('INSERT INTO foobar (something) VALUES (?)', array('a')));
+
+ $this->assertEquals(1, $this->db->execute('SELECT COUNT(*) FROM foobar WHERE something=?', array('a'))->fetchColumn());
+ }
+
+ public function testThatTransactionReturnsAValue()
+ {
+ $this->assertEquals('a', $this->db->transaction(function (Database $db) {
+ $db->getConnection()->exec('CREATE TABLE foobar (something CHAR(1) UNIQUE) ENGINE=InnoDB');
+ $db->execute('INSERT INTO foobar (something) VALUES (?)', array('a'));
+
+ return $db->execute('SELECT something FROM foobar WHERE something=?', array('a'))->fetchColumn();
+ }));
+ }
+
+ public function testThatTransactionReturnsTrue()
+ {
+ $this->assertTrue($this->db->transaction(function (Database $db) {
+ $db->getConnection()->exec('CREATE TABLE foobar (something CHAR(1) UNIQUE) ENGINE=InnoDB');
+ $db->execute('INSERT INTO foobar (something) VALUES (?)', array('a'));
+ }));
+ }
+
+ /**
+ * @expectedException PicoDb\SQLException
+ */
+ public function testThatTransactionThrowExceptionWhenRollbacked()
+ {
+ $this->assertFalse($this->db->transaction(function (Database $db) {
+ $db->getConnection()->exec('CREATE TABL');
+ }));
+ }
+
+ public function testThatTransactionReturnsFalseWhithDuplicateKey()
+ {
+ $this->assertFalse($this->db->transaction(function (Database $db) {
+ $db->getConnection()->exec('CREATE TABLE foobar (something CHAR(1) UNIQUE) ENGINE=InnoDB');
+ $r1 = $db->execute('INSERT INTO foobar (something) VALUES (?)', array('a'));
+ $r2 = $db->execute('INSERT INTO foobar (something) VALUES (?)', array('a'));
+ return $r1 && $r2;
+ }));
+ }
+}
diff --git a/libs/picodb/tests/MysqlDriverTest.php b/libs/picodb/tests/MysqlDriverTest.php
new file mode 100644
index 00000000..2666a993
--- /dev/null
+++ b/libs/picodb/tests/MysqlDriverTest.php
@@ -0,0 +1,73 @@
+<?php
+
+require_once __DIR__.'/../../../vendor/autoload.php';
+
+use PicoDb\Driver\Mysql;
+
+class MysqlDriverTest extends PHPUnit_Framework_TestCase
+{
+ /**
+ * @var PicoDb\Driver\Mysql
+ */
+ private $driver;
+
+ public function setUp()
+ {
+ $this->driver = new Mysql(array('hostname' => 'localhost', 'username' => 'root', 'password' => '', 'database' => 'picodb'));
+ $this->driver->getConnection()->exec('CREATE DATABASE IF NOT EXISTS `picodb`');
+ $this->driver->getConnection()->exec('DROP TABLE IF EXISTS foobar');
+ $this->driver->getConnection()->exec('DROP TABLE IF EXISTS schema_version');
+ }
+
+ /**
+ * @expectedException LogicException
+ */
+ public function testMissingRequiredParameter()
+ {
+ new Mysql(array());
+ }
+
+ public function testDuplicateKeyError()
+ {
+ $this->assertFalse($this->driver->isDuplicateKeyError(1234));
+ $this->assertTrue($this->driver->isDuplicateKeyError(23000));
+ }
+
+ public function testOperator()
+ {
+ $this->assertEquals('LIKE BINARY', $this->driver->getOperator('LIKE'));
+ $this->assertEquals('LIKE', $this->driver->getOperator('ILIKE'));
+ $this->assertEquals('', $this->driver->getOperator('FOO'));
+ }
+
+ public function testSchemaVersion()
+ {
+ $this->assertEquals(0, $this->driver->getSchemaVersion());
+
+ $this->driver->setSchemaVersion(1);
+ $this->assertEquals(1, $this->driver->getSchemaVersion());
+
+ $this->driver->setSchemaVersion(42);
+ $this->assertEquals(42, $this->driver->getSchemaVersion());
+ }
+
+ public function testLastInsertId()
+ {
+ $this->assertEquals(0, $this->driver->getLastId());
+
+ $this->driver->getConnection()->exec('CREATE TABLE foobar (id INT AUTO_INCREMENT NOT NULL, something TEXT, PRIMARY KEY (id)) ENGINE=InnoDB');
+ $this->driver->getConnection()->exec('INSERT INTO foobar (something) VALUES (1)');
+
+ $this->assertEquals(1, $this->driver->getLastId());
+ }
+
+ public function testEscape()
+ {
+ $this->assertEquals('`foobar`', $this->driver->escape('foobar'));
+ }
+
+ public function testDatabaseVersion()
+ {
+ $this->assertStringStartsWith('5.', $this->driver->getDatabaseVersion());
+ }
+}
diff --git a/libs/picodb/tests/MysqlLobTest.php b/libs/picodb/tests/MysqlLobTest.php
new file mode 100644
index 00000000..1291d422
--- /dev/null
+++ b/libs/picodb/tests/MysqlLobTest.php
@@ -0,0 +1,83 @@
+<?php
+
+require_once __DIR__.'/../../../vendor/autoload.php';
+
+class MysqlLobTest extends PHPUnit_Framework_TestCase
+{
+ /**
+ * @var PicoDb\Database
+ */
+ private $db;
+
+ public function setUp()
+ {
+ $this->db = new PicoDb\Database(array('driver' => 'mysql', 'hostname' => 'localhost', 'username' => 'root', 'password' => '', 'database' => 'picodb'));
+ $this->db->getConnection()->exec('DROP TABLE IF EXISTS large_objects');
+ $this->db->getConnection()->exec('CREATE TABLE large_objects (id VARCHAR(20), file_content BLOB)');
+ $this->db->getStatementHandler()->withLogging();
+ }
+
+ public function testInsert()
+ {
+ $result = $this->db->largeObject('large_objects')->insertFromFile('file_content', __FILE__, array('id' => 'test'));
+ $this->assertTrue($result);
+ }
+
+ public function testInsertFromString()
+ {
+ $data = 'test';
+ $result = $this->db->largeObject('large_objects')->insertFromString('file_content', $data, array('id' => 'test'));
+ $this->assertTrue($result);
+ }
+
+ public function testInsertWithOptionalParams()
+ {
+ $result = $this->db->largeObject('large_objects')->insertFromFile('file_content', __FILE__);
+ $this->assertTrue($result);
+ }
+
+ public function testFindOneColumnAsStream()
+ {
+ $result = $this->db->largeObject('large_objects')->insertFromFile('file_content', __FILE__, array('id' => 'test'));
+ $this->assertTrue($result);
+
+ $contents = $this->db->largeObject('large_objects')->eq('id', 'test')->findOneColumnAsStream('file_content');
+ $this->assertSame(md5(file_get_contents(__FILE__)), md5($contents));
+ }
+
+ public function testFindOneColumnAsString()
+ {
+ $result = $this->db->largeObject('large_objects')->insertFromFile('file_content', __FILE__, array('id' => 'test'));
+ $this->assertTrue($result);
+
+ $contents = $this->db->largeObject('large_objects')->eq('id', 'test')->findOneColumnAsString('file_content');
+ $this->assertSame(md5(file_get_contents(__FILE__)), md5($contents));
+ }
+
+ public function testUpdate()
+ {
+ $result = $this->db->largeObject('large_objects')->insertFromFile('file_content', __FILE__, array('id' => 'test1'));
+ $this->assertTrue($result);
+
+ $result = $this->db->largeObject('large_objects')->insertFromFile('file_content', __FILE__, array('id' => 'test2'));
+ $this->assertTrue($result);
+
+ $result = $this->db->largeObject('large_objects')->eq('id', 'test1')->updateFromFile('file_content', __DIR__.'/../LICENSE');
+ $this->assertTrue($result);
+
+ $contents = $this->db->largeObject('large_objects')->eq('id', 'test1')->findOneColumnAsString('file_content');
+ $this->assertSame(md5(file_get_contents(__DIR__.'/../LICENSE')), md5($contents));
+
+ $contents = $this->db->largeObject('large_objects')->eq('id', 'test2')->findOneColumnAsString('file_content');
+ $this->assertSame(md5(file_get_contents(__FILE__)), md5($contents));
+
+ $result = $this->db->largeObject('large_objects')->updateFromFile('file_content', __DIR__.'/../composer.json');
+ $this->assertTrue($result);
+
+ $contents = $this->db->largeObject('large_objects')->eq('id', 'test1')->findOneColumnAsString('file_content');
+ $this->assertSame(md5(file_get_contents(__DIR__.'/../composer.json')), md5($contents));
+
+ $contents = $this->db->largeObject('large_objects')->eq('id', 'test2')->findOneColumnAsString('file_content');
+ $this->assertSame(md5(file_get_contents(__DIR__.'/../composer.json')), md5($contents));
+ }
+}
diff --git a/libs/picodb/tests/MysqlSchemaTest.php b/libs/picodb/tests/MysqlSchemaTest.php
new file mode 100644
index 00000000..4eeee0b9
--- /dev/null
+++ b/libs/picodb/tests/MysqlSchemaTest.php
@@ -0,0 +1,49 @@
+<?php
+
+require_once __DIR__.'/../../../vendor/autoload.php';
+require_once __DIR__.'/SchemaFixture.php';
+require_once __DIR__.'/AlternativeSchemaFixture.php';
+
+class MysqlSchemaTest extends PHPUnit_Framework_TestCase
+{
+ /**
+ * @var PicoDb\Database
+ */
+ private $db;
+
+ public function setUp()
+ {
+ $this->db = new PicoDb\Database(array('driver' => 'mysql', 'hostname' => 'localhost', 'username' => 'root', 'password' => '', 'database' => 'picodb'));
+ $this->db->getConnection()->exec('DROP TABLE IF EXISTS test1');
+ $this->db->getConnection()->exec('DROP TABLE IF EXISTS test2');
+ $this->db->getConnection()->exec('DROP TABLE IF EXISTS schema_version');
+ }
+
+ public function testMigrations()
+ {
+ $this->assertTrue($this->db->schema()->check(2));
+ $this->assertEquals(2, $this->db->getDriver()->getSchemaVersion());
+ $this->assertEquals('\Schema', $this->db->schema()->getNamespace());
+ }
+
+ public function testFailedMigrations()
+ {
+ $this->assertEquals(0, $this->db->getDriver()->getSchemaVersion());
+ $this->assertFalse($this->db->schema()->check(3));
+ $this->assertEquals(2, $this->db->getDriver()->getSchemaVersion());
+
+ $logs = $this->db->getLogMessages();
+ $this->assertNotEmpty($logs);
+ $this->assertEquals('Running migration \Schema\version_1', $logs[0]);
+ $this->assertEquals('Running migration \Schema\version_2', $logs[1]);
+ $this->assertEquals('Running migration \Schema\version_3', $logs[2]);
+ $this->assertStringStartsWith('SQLSTATE[42000]: Syntax error or access violation', $logs[3]);
+ }
+
+ public function testAlternativeSchemaNamespace()
+ {
+ $this->assertEquals('\AlternativeSchema', $this->db->schema('\AlternativeSchema')->getNamespace());
+ $this->assertTrue($this->db->schema('\AlternativeSchema')->check(2));
+ $this->assertEquals(2, $this->db->getDriver()->getSchemaVersion());
+ }
+}
diff --git a/libs/picodb/tests/MysqlTableTest.php b/libs/picodb/tests/MysqlTableTest.php
new file mode 100644
index 00000000..62c1730a
--- /dev/null
+++ b/libs/picodb/tests/MysqlTableTest.php
@@ -0,0 +1,356 @@
+<?php
+
+require_once __DIR__.'/../../../vendor/autoload.php';
+
+use PicoDb\Database;
+use PicoDb\Table;
+
+class MysqlTableTest extends PHPUnit_Framework_TestCase
+{
+ /**
+ * @var PicoDb\Database
+ */
+ private $db;
+
+ public function setUp()
+ {
+ $this->db = new Database(array('driver' => 'mysql', 'hostname' => 'localhost', 'username' => 'root', 'password' => '', 'database' => 'picodb'));
+ $this->db->getConnection()->exec('CREATE DATABASE IF NOT EXISTS `picodb`');
+ $this->db->getConnection()->exec('DROP TABLE IF EXISTS test1');
+ $this->db->getConnection()->exec('DROP TABLE IF EXISTS test2');
+ $this->db->getConnection()->exec('DROP TABLE IF EXISTS foobar');
+ $this->db->getConnection()->exec('DROP TABLE IF EXISTS foobar_persist');
+ $this->db->getConnection()->exec('DROP TABLE IF EXISTS schema_version');
+ }
+
+ public function testSelect()
+ {
+ $this->assertEquals('SELECT 1 FROM `test`', $this->db->table('test')->select(1)->buildSelectQuery());
+ }
+
+ public function testColumns()
+ {
+ $this->assertEquals('SELECT `a`, `b` FROM `test`', $this->db->table('test')->columns('a', 'b')->buildSelectQuery());
+ }
+
+ public function testDistinct()
+ {
+ $this->assertEquals('SELECT DISTINCT `a`, `b` FROM `test`', $this->db->table('test')->distinct('a', 'b')->buildSelectQuery());
+ }
+
+ public function testGroupBy()
+ {
+ $this->assertEquals('SELECT * FROM `test` GROUP BY `a`', $this->db->table('test')->groupBy('a')->buildSelectQuery());
+ }
+
+ public function testOrderBy()
+ {
+ $this->assertEquals('SELECT * FROM `test` ORDER BY `a` ASC', $this->db->table('test')->asc('a')->buildSelectQuery());
+ $this->assertEquals('SELECT * FROM `test` ORDER BY `a` ASC', $this->db->table('test')->orderBy('a', Table::SORT_ASC)->buildSelectQuery());
+
+ $this->assertEquals('SELECT * FROM `test` ORDER BY `a` DESC', $this->db->table('test')->desc('a')->buildSelectQuery());
+ $this->assertEquals('SELECT * FROM `test` ORDER BY `a` DESC', $this->db->table('test')->orderBy('a', Table::SORT_DESC)->buildSelectQuery());
+
+ $this->assertEquals('SELECT * FROM `test` ORDER BY `a` ASC, `b` ASC', $this->db->table('test')->asc('a')->asc('b')->buildSelectQuery());
+ $this->assertEquals('SELECT * FROM `test` ORDER BY `a` DESC, `b` DESC', $this->db->table('test')->desc('a')->desc('b')->buildSelectQuery());
+
+ $this->assertEquals('SELECT * FROM `test` ORDER BY `a` ASC, `b` ASC', $this->db->table('test')->orderBy('a')->orderBy('b')->buildSelectQuery());
+ $this->assertEquals('SELECT * FROM `test` ORDER BY `a` DESC, `b` DESC', $this->db->table('test')->orderBy('a', Table::SORT_DESC)->orderBy('b', Table::SORT_DESC)->buildSelectQuery());
+
+ $this->assertEquals('SELECT * FROM `test` ORDER BY `a` DESC, `b` ASC', $this->db->table('test')->desc('a')->asc('b')->buildSelectQuery());
+ }
+
+ public function testLimit()
+ {
+ $this->assertEquals('SELECT * FROM `test` LIMIT 10', $this->db->table('test')->limit(10)->buildSelectQuery());
+ $this->assertEquals('SELECT * FROM `test`', $this->db->table('test')->limit(null)->buildSelectQuery());
+ }
+
+ public function testOffset()
+ {
+ $this->assertEquals('SELECT * FROM `test` OFFSET 0', $this->db->table('test')->offset(0)->buildSelectQuery());
+ $this->assertEquals('SELECT * FROM `test` OFFSET 10', $this->db->table('test')->offset(10)->buildSelectQuery());
+ $this->assertEquals('SELECT * FROM `test`', $this->db->table('test')->limit(null)->buildSelectQuery());
+ }
+
+ public function testLimitOffset()
+ {
+ $this->assertEquals('SELECT * FROM `test` LIMIT 2 OFFSET 0', $this->db->table('test')->offset(0)->limit(2)->buildSelectQuery());
+ $this->assertEquals('SELECT * FROM `test` LIMIT 5 OFFSET 10', $this->db->table('test')->offset(10)->limit(5)->buildSelectQuery());
+ }
+
+ public function testSubquery()
+ {
+ $this->assertEquals('SELECT (SELECT 1 FROM "foobar" WHERE 1=1) AS `b` FROM `test`', $this->db->table('test')->subquery('SELECT 1 FROM "foobar" WHERE 1=1', 'b')->buildSelectQuery());
+ }
+
+ public function testConditionEqual()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM `test` WHERE `a` = ? AND `b` = ?', $table->eq('a', 2)->eq('b', 'foobar')->buildSelectQuery());
+ $this->assertEquals(array(2, 'foobar'), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionNotEqual()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM `test` WHERE `a` != ?', $table->neq('a', 2)->buildSelectQuery());
+ $this->assertEquals(array(2), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionIn()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM `test` WHERE `a` IN (?, ?)', $table->in('a', array('b', 'c'))->buildSelectQuery());
+ $this->assertEquals(array('b', 'c'), $table->getConditionBuilder()->getValues());
+
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM `test`', $table->in('a', array())->buildSelectQuery());
+ $this->assertEquals(array(), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionInSubquery()
+ {
+ $table = $this->db->table('test');
+ $subquery = $this->db->table('test2')->columns('c')->eq('d', 'e');
+
+ $this->assertEquals(
+ 'SELECT * FROM `test` WHERE `a` IN (SELECT `c` FROM `test2` WHERE `d` = ?)',
+ $table->inSubquery('a', $subquery)->buildSelectQuery()
+ );
+
+ $this->assertEquals(array('e'), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionNotIn()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM `test` WHERE `a` NOT IN (?, ?)', $table->notIn('a', array('b', 'c'))->buildSelectQuery());
+ $this->assertEquals(array('b', 'c'), $table->getConditionBuilder()->getValues());
+
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM `test`', $table->notIn('a', array())->buildSelectQuery());
+ $this->assertEquals(array(), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionNotInSubquery()
+ {
+ $table = $this->db->table('test');
+ $subquery = $this->db->table('test2')->columns('c')->eq('d', 'e');
+
+ $this->assertEquals(
+ 'SELECT * FROM `test` WHERE `a` NOT IN (SELECT `c` FROM `test2` WHERE `d` = ?)',
+ $table->notInSubquery('a', $subquery)->buildSelectQuery()
+ );
+
+ $this->assertEquals(array('e'), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionLike()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM `test` WHERE `a` LIKE BINARY ?', $table->like('a', '%foobar%')->buildSelectQuery());
+ $this->assertEquals(array('%foobar%'), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionILike()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM `test` WHERE `a` LIKE ?', $table->ilike('a', '%foobar%')->buildSelectQuery());
+ $this->assertEquals(array('%foobar%'), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionGreaterThan()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM `test` WHERE `a` > ?', $table->gt('a', 5)->buildSelectQuery());
+ $this->assertEquals(array(5), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionGreaterThanOrEqual()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM `test` WHERE `a` >= ?', $table->gte('a', 5)->buildSelectQuery());
+ $this->assertEquals(array(5), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionLowerThan()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM `test` WHERE `a` < ?', $table->lt('a', 5)->buildSelectQuery());
+ $this->assertEquals(array(5), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionLowerThanOrEqual()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM `test` WHERE `a` <= ?', $table->lte('a', 5)->buildSelectQuery());
+ $this->assertEquals(array(5), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionIsNull()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM `test` WHERE `a` IS NOT NULL', $table->notNull('a')->buildSelectQuery());
+ $this->assertEquals(array(), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testCustomCondition()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM `test` WHERE a=c AND `b` = ?', $table->addCondition('a=c')->eq('b', 4)->buildSelectQuery());
+ $this->assertEquals(array(4), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testOrConditions()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM `test` WHERE `a` IS NOT NULL AND (`b` = ? OR `c` >= ?)', $table->notNull('a')->beginOr()->eq('b', 2)->gte('c', 5)->closeOr()->buildSelectQuery());
+ $this->assertEquals(array(2, 5), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testPersist()
+ {
+ $this->assertNotFalse($this->db->execute('CREATE TABLE foobar_persist (id INT NOT NULL AUTO_INCREMENT, a VARCHAR(10), PRIMARY KEY(id))'));
+ $this->assertSame(1, $this->db->table('foobar_persist')->persist(array('a' => 'b')));
+ }
+
+ public function testInsertUpdate()
+ {
+ $this->assertNotFalse($this->db->execute('CREATE TABLE foobar (a TEXT)'));
+ $this->assertTrue($this->db->table('foobar')->insert(array('a' => 'b')));
+ $this->assertTrue($this->db->table('foobar')->insert(array('a' => 'c')));
+
+ $this->assertEquals(array(array('a' => 'b'), array('a' => 'c')), $this->db->table('foobar')->findAll());
+
+ $this->assertEquals(array('b', 'c'), $this->db->table('foobar')->findAllByColumn('a'));
+
+ $this->assertEquals(array('a' => 'b'), $this->db->table('foobar')->findOne());
+
+ $this->assertEquals('b', $this->db->table('foobar')->findOneColumn('a'));
+
+ $this->assertTrue($this->db->table('foobar')->exists());
+ $this->assertTrue($this->db->table('foobar')->eq('a', 'c')->exists());
+ $this->assertFalse($this->db->table('foobar')->eq('a', 'e')->exists());
+
+ $this->assertEquals(2, $this->db->table('foobar')->count());
+ $this->assertEquals(1, $this->db->table('foobar')->eq('a', 'c')->count());
+ $this->assertEquals(0, $this->db->table('foobar')->eq('a', 'e')->count());
+
+ $this->assertTrue($this->db->table('foobar')->eq('a', 'c')->remove());
+ $this->assertFalse($this->db->table('foobar')->eq('a', 'e')->remove());
+
+ $this->assertTrue($this->db->table('foobar')->eq('a', 'b')->update(array('a' => 'test')));
+ $this->assertTrue($this->db->table('foobar')->eq('a', 'lol')->update(array('a' => 'test')));
+
+ $this->assertNotEmpty($this->db->table('foobar')->eq('a', 'test')->findOne());
+ $this->assertNull($this->db->table('foobar')->eq('a', 'lol')->findOne());
+
+ $this->assertTrue($this->db->table('foobar')->eq('a', 'test')->save(array('a' => 'plop')));
+ $this->assertEquals(1, $this->db->table('foobar')->count());
+ }
+
+ public function testSumColumn()
+ {
+ $this->assertNotFalse($this->db->execute('CREATE TABLE foobar (b FLOAT, c FLOAT)'));
+ $this->assertTrue($this->db->table('foobar')->insert(array('b' => 2, 'c' => 3.3)));
+
+ $this->assertTrue($this->db->table('foobar')->sumColumn('b', 2.5)->sumColumn('c', 3)->update());
+
+ $this->assertEquals(
+ array('b' => 4.5, 'c' => 6.3),
+ $this->db->table('foobar')->findOne()
+ );
+ }
+
+ public function testSum()
+ {
+ $this->assertNotFalse($this->db->execute('CREATE TABLE foobar (a INTEGER)'));
+ $this->assertTrue($this->db->table('foobar')->insert(array('a' => 2)));
+ $this->assertTrue($this->db->table('foobar')->insert(array('a' => 5)));
+ $this->assertEquals(7, $this->db->table('foobar')->sum('a'));
+ }
+
+ public function testIncrement()
+ {
+ $this->assertNotFalse($this->db->execute('CREATE TABLE foobar (a INTEGER DEFAULT 0, b INTEGER DEFAULT 0)'));
+ $this->assertTrue($this->db->table('foobar')->insert(array('a' => 2, 'b' => 5)));
+ $this->assertTrue($this->db->table('foobar')->eq('b', 5)->increment('a', 3));
+ $this->assertEquals(5, $this->db->table('foobar')->findOneColumn('a'));
+ }
+
+ public function testLeftJoin()
+ {
+ $this->assertNotFalse($this->db->execute('CREATE TABLE test1 (a INTEGER NOT NULL, foreign_key INTEGER NOT NULL)'));
+ $this->assertNotFalse($this->db->execute('CREATE TABLE test2 (id INTEGER NOT NULL, b INTEGER NOT NULL)'));
+
+ $this->assertTrue($this->db->table('test2')->insert(array('id' => 42, 'b' => 2)));
+ $this->assertTrue($this->db->table('test1')->insert(array('a' => 18, 'foreign_key' => 42)));
+
+ $this->assertEquals(
+ array('a' => 18, 'b' => 2),
+ $this->db->table('test2')->columns('a', 'b')->eq('a', 18)->left('test1', 't1', 'foreign_key', 'test2', 'id')->findOne()
+ );
+
+ $this->assertEquals(
+ array('a' => 18, 'b' => 2),
+ $this->db->table('test2')->columns('a', 'b')->eq('a', 18)->join('test1', 'foreign_key', 'id')->findOne()
+ );
+
+ $this->assertEquals(
+ array('a' => 18, 'b' => 2),
+ $this->db->table('test1')->columns('a', 'b')->join('test2', 'id', 'foreign_key')->findOne()
+ );
+ }
+
+ public function testHashTable()
+ {
+ $this->assertNotFalse($this->db->execute(
+ 'CREATE TABLE foobar (
+ column1 VARCHAR(20) NOT NULL UNIQUE,
+ column2 VARCHAR(20) default NULL
+ )'
+ ));
+
+ $this->assertTrue($this->db->table('foobar')->insert(array('column1' => 'option1', 'column2' => 'value1')));
+ $this->assertTrue($this->db->table('foobar')->insert(array('column1' => 'option2', 'column2' => 'value2')));
+ $this->assertTrue($this->db->table('foobar')->insert(array('column1' => 'option3', 'column2' => 'value3')));
+
+ $values = array(
+ 'option1' => 'hey',
+ 'option4' => 'ho',
+ );
+
+ $this->assertTrue($this->db->hashtable('foobar')->columnKey('column1')->columnValue('column2')->put($values));
+
+ $this->assertEquals(
+ array('option2' => 'value2', 'option4' => 'ho'),
+ $this->db->hashtable('foobar')->columnKey('column1')->columnValue('column2')->get('option2', 'option4')
+ );
+
+ $this->assertEquals(
+ array('option2' => 'value2', 'option3' => 'value3', 'option1' => 'hey', 'option4' => 'ho'),
+ $this->db->hashtable('foobar')->columnKey('column1')->columnValue('column2')->get()
+ );
+
+ $this->assertEquals(
+ array('option2' => 'value2', 'option3' => 'value3', 'option1' => 'hey', 'option4' => 'ho'),
+ $this->db->hashtable('foobar')->getAll('column1', 'column2')
+ );
+ }
+}
diff --git a/libs/picodb/tests/PostgresDatabaseTest.php b/libs/picodb/tests/PostgresDatabaseTest.php
new file mode 100644
index 00000000..d0d8a644
--- /dev/null
+++ b/libs/picodb/tests/PostgresDatabaseTest.php
@@ -0,0 +1,100 @@
+<?php
+
+require_once __DIR__.'/../../../vendor/autoload.php';
+
+use PicoDb\Database;
+
+class PostgresDatabaseTest extends PHPUnit_Framework_TestCase
+{
+ /**
+ * @var PicoDb\Database
+ */
+ private $db;
+
+ public function setUp()
+ {
+ $this->db = new Database(array('driver' => 'postgres', 'hostname' => 'localhost', 'username' => 'postgres', 'password' => '', 'database' => 'picodb'));
+ $this->db->getConnection()->exec('DROP TABLE IF EXISTS foobar');
+ $this->db->getConnection()->exec('DROP TABLE IF EXISTS schema_version');
+ }
+
+ public function testEscapeIdentifer()
+ {
+ $this->assertEquals('"a"', $this->db->escapeIdentifier('a'));
+ $this->assertEquals('a.b', $this->db->escapeIdentifier('a.b'));
+ $this->assertEquals('"c"."a"', $this->db->escapeIdentifier('a', 'c'));
+ $this->assertEquals('a.b', $this->db->escapeIdentifier('a.b', 'c'));
+ $this->assertEquals('SELECT COUNT(*) FROM test', $this->db->escapeIdentifier('SELECT COUNT(*) FROM test'));
+ $this->assertEquals('SELECT COUNT(*) FROM test', $this->db->escapeIdentifier('SELECT COUNT(*) FROM test', 'b'));
+ }
+
+ public function testEscapeIdentiferList()
+ {
+ $this->assertEquals(array('"c"."a"', '"c"."b"'), $this->db->escapeIdentifierList(array('a', 'b'), 'c'));
+ $this->assertEquals(array('"a"', 'd.b'), $this->db->escapeIdentifierList(array('a', 'd.b')));
+ }
+
+ public function testThatPreparedStatementWorks()
+ {
+ $this->db->getConnection()->exec('CREATE TABLE foobar (id serial PRIMARY KEY, something TEXT)');
+ $this->db->execute('INSERT INTO foobar (something) VALUES (?)', array('a'));
+ $this->assertEquals(1, $this->db->getLastId());
+ $this->assertEquals('a', $this->db->execute('SELECT something FROM foobar WHERE something=?', array('a'))->fetchColumn());
+ }
+
+ /**
+ * @expectedException PicoDb\SQLException
+ */
+ public function testBadSQLQuery()
+ {
+ $this->db->execute('INSERT INTO foobar');
+ }
+
+ public function testDuplicateKey()
+ {
+ $this->db->getConnection()->exec('CREATE TABLE foobar (something TEXT UNIQUE)');
+
+ $this->assertNotFalse($this->db->execute('INSERT INTO foobar (something) VALUES (?)', array('a')));
+ $this->assertFalse($this->db->execute('INSERT INTO foobar (something) VALUES (?)', array('a')));
+
+ $this->assertEquals(1, $this->db->execute('SELECT COUNT(*) FROM foobar WHERE something=?', array('a'))->fetchColumn());
+ }
+
+ public function testThatTransactionReturnsAValue()
+ {
+ $this->assertEquals('a', $this->db->transaction(function (Database $db) {
+ $db->getConnection()->exec('CREATE TABLE foobar (something TEXT UNIQUE)');
+ $db->execute('INSERT INTO foobar (something) VALUES (?)', array('a'));
+
+ return $db->execute('SELECT something FROM foobar WHERE something=?', array('a'))->fetchColumn();
+ }));
+ }
+
+ public function testThatTransactionReturnsTrue()
+ {
+ $this->assertTrue($this->db->transaction(function (Database $db) {
+ $db->getConnection()->exec('CREATE TABLE foobar (something TEXT UNIQUE)');
+ $db->execute('INSERT INTO foobar (something) VALUES (?)', array('a'));
+ }));
+ }
+
+ /**
+ * @expectedException PicoDb\SQLException
+ */
+ public function testThatTransactionThrowExceptionWhenRollbacked()
+ {
+ $this->assertFalse($this->db->transaction(function (Database $db) {
+ $db->getConnection()->exec('CREATE TABL');
+ }));
+ }
+
+ public function testThatTransactionReturnsFalseWhithDuplicateKey()
+ {
+ $this->assertFalse($this->db->transaction(function (Database $db) {
+ $db->getConnection()->exec('CREATE TABLE foobar (something TEXT UNIQUE)');
+ $r1 = $db->execute('INSERT INTO foobar (something) VALUES (?)', array('a'));
+ $r2 = $db->execute('INSERT INTO foobar (something) VALUES (?)', array('a'));
+ return $r1 && $r2;
+ }));
+ }
+}
diff --git a/libs/picodb/tests/PostgresDriverTest.php b/libs/picodb/tests/PostgresDriverTest.php
new file mode 100644
index 00000000..9798042b
--- /dev/null
+++ b/libs/picodb/tests/PostgresDriverTest.php
@@ -0,0 +1,78 @@
+<?php
+
+require_once __DIR__.'/../../../vendor/autoload.php';
+
+use PicoDb\Driver\Postgres;
+
+class PostgresDriverTest extends PHPUnit_Framework_TestCase
+{
+ /**
+ * @var PicoDb\Driver\Postgres
+ */
+ private $driver;
+
+ public function setUp()
+ {
+ $this->driver = new Postgres(array('hostname' => 'localhost', 'username' => 'postgres', 'password' => 'postgres', 'database' => 'picodb'));
+ $this->driver->getConnection()->exec('DROP TABLE IF EXISTS foobar');
+ $this->driver->getConnection()->exec('DROP TABLE IF EXISTS schema_version');
+ }
+
+ public function tearDown()
+ {
+ $this->driver->closeConnection();
+ }
+
+ /**
+ * @expectedException LogicException
+ */
+ public function testMissingRequiredParameter()
+ {
+ new Postgres(array());
+ }
+
+ public function testDuplicateKeyError()
+ {
+ $this->assertFalse($this->driver->isDuplicateKeyError(1234));
+ $this->assertTrue($this->driver->isDuplicateKeyError(23505));
+ $this->assertTrue($this->driver->isDuplicateKeyError(23503));
+ }
+
+ public function testOperator()
+ {
+ $this->assertEquals('LIKE', $this->driver->getOperator('LIKE'));
+ $this->assertEquals('ILIKE', $this->driver->getOperator('ILIKE'));
+ $this->assertEquals('', $this->driver->getOperator('FOO'));
+ }
+
+ public function testSchemaVersion()
+ {
+ $this->assertEquals(0, $this->driver->getSchemaVersion());
+
+ $this->driver->setSchemaVersion(1);
+ $this->assertEquals(1, $this->driver->getSchemaVersion());
+
+ $this->driver->setSchemaVersion(42);
+ $this->assertEquals(42, $this->driver->getSchemaVersion());
+ }
+
+ public function testLastInsertId()
+ {
+ $this->assertEquals(0, $this->driver->getLastId());
+
+ $this->driver->getConnection()->exec('CREATE TABLE foobar (id serial PRIMARY KEY, something TEXT)');
+ $this->driver->getConnection()->exec('INSERT INTO foobar (something) VALUES (1)');
+
+ $this->assertEquals(1, $this->driver->getLastId());
+ }
+
+ public function testEscape()
+ {
+ $this->assertEquals('"foobar"', $this->driver->escape('foobar'));
+ }
+
+ public function testDatabaseVersion()
+ {
+ $this->assertStringStartsWith('9.', $this->driver->getDatabaseVersion());
+ }
+}
diff --git a/libs/picodb/tests/PostgresLobTest.php b/libs/picodb/tests/PostgresLobTest.php
new file mode 100644
index 00000000..39cf8fb0
--- /dev/null
+++ b/libs/picodb/tests/PostgresLobTest.php
@@ -0,0 +1,87 @@
+<?php
+
+require_once __DIR__.'/../../../vendor/autoload.php';
+
+use PicoDb\Database;
+
+class PostgresLobTest extends PHPUnit_Framework_TestCase
+{
+ /**
+ * @var PicoDb\Database
+ */
+ private $db;
+
+ public function setUp()
+ {
+ $this->db = new Database(array('driver' => 'postgres', 'hostname' => 'localhost', 'username' => 'postgres', 'password' => 'postgres', 'database' => 'picodb'));
+ $this->db->getConnection()->exec('DROP TABLE IF EXISTS large_objects');
+ $this->db->getConnection()->exec('CREATE TABLE large_objects (id VARCHAR(20), file_content bytea)');
+ }
+
+ public function testInsert()
+ {
+ $result = $this->db->largeObject('large_objects')->insertFromFile('file_content', __FILE__, array('id' => 'test'));
+ $this->assertTrue($result);
+ }
+
+ public function testInsertFromString()
+ {
+ $data = 'test';
+ $result = $this->db->largeObject('large_objects')->insertFromString('file_content', $data, array('id' => 'test'));
+ $this->assertTrue($result);
+ }
+
+ public function testInsertWithOptionalParams()
+ {
+ $result = $this->db->largeObject('large_objects')->insertFromFile('file_content', __FILE__);
+ $this->assertTrue($result);
+ }
+
+ public function testFindOneColumnAsStream()
+ {
+ $result = $this->db->largeObject('large_objects')->insertFromFile('file_content', __FILE__, array('id' => 'test'));
+ $this->assertTrue($result);
+
+ $fd = $this->db->largeObject('large_objects')->eq('id', 'test')->findOneColumnAsStream('file_content');
+ $contents = fread($fd, filesize(__FILE__));
+ fclose($fd);
+
+ $this->assertSame(md5(file_get_contents(__FILE__)), md5($contents));
+ }
+
+ public function testFindOneColumnAsString()
+ {
+ $result = $this->db->largeObject('large_objects')->insertFromFile('file_content', __FILE__, array('id' => 'test'));
+ $this->assertTrue($result);
+
+ $contents = $this->db->largeObject('large_objects')->eq('id', 'test')->findOneColumnAsString('file_content');
+ $this->assertSame(md5(file_get_contents(__FILE__)), md5($contents));
+ }
+
+ public function testUpdate()
+ {
+ $result = $this->db->largeObject('large_objects')->insertFromFile('file_content', __FILE__, array('id' => 'test1'));
+ $this->assertTrue($result);
+
+ $result = $this->db->largeObject('large_objects')->insertFromFile('file_content', __FILE__, array('id' => 'test2'));
+ $this->assertTrue($result);
+
+ $result = $this->db->largeObject('large_objects')->eq('id', 'test1')->updateFromFile('file_content', __DIR__.'/../LICENSE');
+ $this->assertTrue($result);
+
+ $contents = $this->db->largeObject('large_objects')->eq('id', 'test1')->findOneColumnAsString('file_content');
+ $this->assertSame(md5(file_get_contents(__DIR__.'/../LICENSE')), md5($contents));
+
+ $contents = $this->db->largeObject('large_objects')->eq('id', 'test2')->findOneColumnAsString('file_content');
+ $this->assertSame(md5(file_get_contents(__FILE__)), md5($contents));
+
+ $result = $this->db->largeObject('large_objects')->updateFromFile('file_content', __DIR__.'/../composer.json');
+ $this->assertTrue($result);
+
+ $contents = $this->db->largeObject('large_objects')->eq('id', 'test1')->findOneColumnAsString('file_content');
+ $this->assertSame(md5(file_get_contents(__DIR__.'/../composer.json')), md5($contents));
+
+ $contents = $this->db->largeObject('large_objects')->eq('id', 'test2')->findOneColumnAsString('file_content');
+ $this->assertSame(md5(file_get_contents(__DIR__.'/../composer.json')), md5($contents));
+ }
+}
diff --git a/libs/picodb/tests/PostgresSchemaTest.php b/libs/picodb/tests/PostgresSchemaTest.php
new file mode 100644
index 00000000..5ecf1cc5
--- /dev/null
+++ b/libs/picodb/tests/PostgresSchemaTest.php
@@ -0,0 +1,40 @@
+<?php
+
+require_once __DIR__.'/../../../vendor/autoload.php';
+require_once __DIR__.'/SchemaFixture.php';
+
+class PostgresSchemaTest extends PHPUnit_Framework_TestCase
+{
+ /**
+ * @var PicoDb\Database
+ */
+ private $db;
+
+ public function setUp()
+ {
+ $this->db = new PicoDb\Database(array('driver' => 'postgres', 'hostname' => 'localhost', 'username' => 'postgres', 'password' => 'postgres', 'database' => 'picodb'));
+ $this->db->getConnection()->exec('DROP TABLE IF EXISTS test1');
+ $this->db->getConnection()->exec('DROP TABLE IF EXISTS test2');
+ $this->db->getConnection()->exec('DROP TABLE IF EXISTS schema_version');
+ }
+
+ public function testMigrations()
+ {
+ $this->assertTrue($this->db->schema()->check(2));
+ $this->assertEquals(2, $this->db->getDriver()->getSchemaVersion());
+ }
+
+ public function testFailedMigrations()
+ {
+ $this->assertEquals(0, $this->db->getDriver()->getSchemaVersion());
+ $this->assertFalse($this->db->schema()->check(3));
+ $this->assertEquals(2, $this->db->getDriver()->getSchemaVersion());
+
+ $logs = $this->db->getLogMessages();
+ $this->assertNotEmpty($logs);
+ $this->assertEquals('Running migration \Schema\version_1', $logs[0]);
+ $this->assertEquals('Running migration \Schema\version_2', $logs[1]);
+ $this->assertEquals('Running migration \Schema\version_3', $logs[2]);
+ $this->assertStringStartsWith('SQLSTATE[42601]: Syntax error', $logs[3]);
+ }
+}
diff --git a/libs/picodb/tests/PostgresTableTest.php b/libs/picodb/tests/PostgresTableTest.php
new file mode 100644
index 00000000..dc852adf
--- /dev/null
+++ b/libs/picodb/tests/PostgresTableTest.php
@@ -0,0 +1,355 @@
+<?php
+
+require_once __DIR__.'/../../../vendor/autoload.php';
+
+use PicoDb\Database;
+use PicoDb\Table;
+
+class PostgresTableTest extends PHPUnit_Framework_TestCase
+{
+ /**
+ * @var PicoDb\Database
+ */
+ private $db;
+
+ public function setUp()
+ {
+ $this->db = new Database(array('driver' => 'postgres', 'hostname' => 'localhost', 'username' => 'postgres', 'password' => 'postgres', 'database' => 'picodb'));
+ $this->db->getConnection()->exec('DROP TABLE IF EXISTS test1');
+ $this->db->getConnection()->exec('DROP TABLE IF EXISTS test2');
+ $this->db->getConnection()->exec('DROP TABLE IF EXISTS foobar');
+ $this->db->getConnection()->exec('DROP TABLE IF EXISTS foobar_persist');
+ $this->db->getConnection()->exec('DROP TABLE IF EXISTS schema_version');
+ }
+
+ public function testSelect()
+ {
+ $this->assertEquals('SELECT 1 FROM "test"', $this->db->table('test')->select(1)->buildSelectQuery());
+ }
+
+ public function testColumns()
+ {
+ $this->assertEquals('SELECT "a", "b" FROM "test"', $this->db->table('test')->columns('a', 'b')->buildSelectQuery());
+ }
+
+ public function testDistinct()
+ {
+ $this->assertEquals('SELECT DISTINCT "a", "b" FROM "test"', $this->db->table('test')->distinct('a', 'b')->buildSelectQuery());
+ }
+
+ public function testGroupBy()
+ {
+ $this->assertEquals('SELECT * FROM "test" GROUP BY "a"', $this->db->table('test')->groupBy('a')->buildSelectQuery());
+ }
+
+ public function testOrderBy()
+ {
+ $this->assertEquals('SELECT * FROM "test" ORDER BY "a" ASC', $this->db->table('test')->asc('a')->buildSelectQuery());
+ $this->assertEquals('SELECT * FROM "test" ORDER BY "a" ASC', $this->db->table('test')->orderBy('a', Table::SORT_ASC)->buildSelectQuery());
+
+ $this->assertEquals('SELECT * FROM "test" ORDER BY "a" DESC', $this->db->table('test')->desc('a')->buildSelectQuery());
+ $this->assertEquals('SELECT * FROM "test" ORDER BY "a" DESC', $this->db->table('test')->orderBy('a', Table::SORT_DESC)->buildSelectQuery());
+
+ $this->assertEquals('SELECT * FROM "test" ORDER BY "a" ASC, "b" ASC', $this->db->table('test')->asc('a')->asc('b')->buildSelectQuery());
+ $this->assertEquals('SELECT * FROM "test" ORDER BY "a" DESC, "b" DESC', $this->db->table('test')->desc('a')->desc('b')->buildSelectQuery());
+
+ $this->assertEquals('SELECT * FROM "test" ORDER BY "a" ASC, "b" ASC', $this->db->table('test')->orderBy('a')->orderBy('b')->buildSelectQuery());
+ $this->assertEquals('SELECT * FROM "test" ORDER BY "a" DESC, "b" DESC', $this->db->table('test')->orderBy('a', Table::SORT_DESC)->orderBy('b', Table::SORT_DESC)->buildSelectQuery());
+
+ $this->assertEquals('SELECT * FROM "test" ORDER BY "a" DESC, "b" ASC', $this->db->table('test')->desc('a')->asc('b')->buildSelectQuery());
+ }
+
+ public function testLimit()
+ {
+ $this->assertEquals('SELECT * FROM "test" LIMIT 10', $this->db->table('test')->limit(10)->buildSelectQuery());
+ $this->assertEquals('SELECT * FROM "test"', $this->db->table('test')->limit(null)->buildSelectQuery());
+ }
+
+ public function testOffset()
+ {
+ $this->assertEquals('SELECT * FROM "test" OFFSET 0', $this->db->table('test')->offset(0)->buildSelectQuery());
+ $this->assertEquals('SELECT * FROM "test" OFFSET 10', $this->db->table('test')->offset(10)->buildSelectQuery());
+ $this->assertEquals('SELECT * FROM "test"', $this->db->table('test')->limit(null)->buildSelectQuery());
+ }
+
+ public function testLimitOffset()
+ {
+ $this->assertEquals('SELECT * FROM "test" LIMIT 2 OFFSET 0', $this->db->table('test')->offset(0)->limit(2)->buildSelectQuery());
+ $this->assertEquals('SELECT * FROM "test" LIMIT 5 OFFSET 10', $this->db->table('test')->offset(10)->limit(5)->buildSelectQuery());
+ }
+
+ public function testSubquery()
+ {
+ $this->assertEquals('SELECT (SELECT 1 FROM "foobar" WHERE 1=1) AS "b" FROM "test"', $this->db->table('test')->subquery('SELECT 1 FROM "foobar" WHERE 1=1', 'b')->buildSelectQuery());
+ }
+
+ public function testConditionEqual()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM "test" WHERE "a" = ? AND "b" = ?', $table->eq('a', 2)->eq('b', 'foobar')->buildSelectQuery());
+ $this->assertEquals(array(2, 'foobar'), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionNotEqual()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM "test" WHERE "a" != ?', $table->neq('a', 2)->buildSelectQuery());
+ $this->assertEquals(array(2), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionIn()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM "test" WHERE "a" IN (?, ?)', $table->in('a', array('b', 'c'))->buildSelectQuery());
+ $this->assertEquals(array('b', 'c'), $table->getConditionBuilder()->getValues());
+
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM "test"', $table->in('a', array())->buildSelectQuery());
+ $this->assertEquals(array(), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionInSubquery()
+ {
+ $table = $this->db->table('test');
+ $subquery = $this->db->table('test2')->columns('c')->eq('d', 'e');
+
+ $this->assertEquals(
+ 'SELECT * FROM "test" WHERE "a" IN (SELECT "c" FROM "test2" WHERE "d" = ?)',
+ $table->inSubquery('a', $subquery)->buildSelectQuery()
+ );
+
+ $this->assertEquals(array('e'), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionNotIn()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM "test" WHERE "a" NOT IN (?, ?)', $table->notIn('a', array('b', 'c'))->buildSelectQuery());
+ $this->assertEquals(array('b', 'c'), $table->getConditionBuilder()->getValues());
+
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM "test"', $table->notIn('a', array())->buildSelectQuery());
+ $this->assertEquals(array(), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionNotInSubquery()
+ {
+ $table = $this->db->table('test');
+ $subquery = $this->db->table('test2')->columns('c')->eq('d', 'e');
+
+ $this->assertEquals(
+ 'SELECT * FROM "test" WHERE "a" NOT IN (SELECT "c" FROM "test2" WHERE "d" = ?)',
+ $table->notInSubquery('a', $subquery)->buildSelectQuery()
+ );
+
+ $this->assertEquals(array('e'), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionLike()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM "test" WHERE "a" LIKE ?', $table->like('a', '%foobar%')->buildSelectQuery());
+ $this->assertEquals(array('%foobar%'), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionILike()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM "test" WHERE "a" ILIKE ?', $table->ilike('a', '%foobar%')->buildSelectQuery());
+ $this->assertEquals(array('%foobar%'), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionGreaterThan()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM "test" WHERE "a" > ?', $table->gt('a', 5)->buildSelectQuery());
+ $this->assertEquals(array(5), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionGreaterThanOrEqual()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM "test" WHERE "a" >= ?', $table->gte('a', 5)->buildSelectQuery());
+ $this->assertEquals(array(5), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionLowerThan()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM "test" WHERE "a" < ?', $table->lt('a', 5)->buildSelectQuery());
+ $this->assertEquals(array(5), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionLowerThanOrEqual()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM "test" WHERE "a" <= ?', $table->lte('a', 5)->buildSelectQuery());
+ $this->assertEquals(array(5), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionIsNull()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM "test" WHERE "a" IS NOT NULL', $table->notNull('a')->buildSelectQuery());
+ $this->assertEquals(array(), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testCustomCondition()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM "test" WHERE a=c AND "b" = ?', $table->addCondition('a=c')->eq('b', 4)->buildSelectQuery());
+ $this->assertEquals(array(4), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testOrConditions()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM "test" WHERE "a" IS NOT NULL AND ("b" = ? OR "c" >= ?)', $table->notNull('a')->beginOr()->eq('b', 2)->gte('c', 5)->closeOr()->buildSelectQuery());
+ $this->assertEquals(array(2, 5), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testPersist()
+ {
+ $this->assertNotFalse($this->db->execute('CREATE TABLE foobar_persist (id SERIAL PRIMARY KEY, a VARCHAR(10))'));
+ $this->assertSame(1, $this->db->table('foobar_persist')->persist(array('a' => 'b')));
+ }
+
+ public function testInsertUpdate()
+ {
+ $this->assertNotFalse($this->db->execute('CREATE TABLE foobar (a TEXT)'));
+ $this->assertTrue($this->db->table('foobar')->insert(array('a' => 'b')));
+ $this->assertTrue($this->db->table('foobar')->insert(array('a' => 'c')));
+
+ $this->assertEquals(array(array('a' => 'b'), array('a' => 'c')), $this->db->table('foobar')->findAll());
+
+ $this->assertEquals(array('b', 'c'), $this->db->table('foobar')->findAllByColumn('a'));
+
+ $this->assertEquals(array('a' => 'b'), $this->db->table('foobar')->findOne());
+
+ $this->assertEquals('b', $this->db->table('foobar')->findOneColumn('a'));
+
+ $this->assertTrue($this->db->table('foobar')->exists());
+ $this->assertTrue($this->db->table('foobar')->eq('a', 'c')->exists());
+ $this->assertFalse($this->db->table('foobar')->eq('a', 'e')->exists());
+
+ $this->assertEquals(2, $this->db->table('foobar')->count());
+ $this->assertEquals(1, $this->db->table('foobar')->eq('a', 'c')->count());
+ $this->assertEquals(0, $this->db->table('foobar')->eq('a', 'e')->count());
+
+ $this->assertTrue($this->db->table('foobar')->eq('a', 'c')->remove());
+ $this->assertFalse($this->db->table('foobar')->eq('a', 'e')->remove());
+
+ $this->assertTrue($this->db->table('foobar')->eq('a', 'b')->update(array('a' => 'test')));
+ $this->assertTrue($this->db->table('foobar')->eq('a', 'lol')->update(array('a' => 'test')));
+
+ $this->assertNotEmpty($this->db->table('foobar')->eq('a', 'test')->findOne());
+ $this->assertNull($this->db->table('foobar')->eq('a', 'lol')->findOne());
+
+ $this->assertTrue($this->db->table('foobar')->eq('a', 'test')->save(array('a' => 'plop')));
+ $this->assertEquals(1, $this->db->table('foobar')->count());
+ }
+
+ public function testSumColumn()
+ {
+ $this->assertNotFalse($this->db->execute('CREATE TABLE foobar (b REAL, c REAL)'));
+ $this->assertTrue($this->db->table('foobar')->insert(array('b' => 2, 'c' => 3.3)));
+
+ $this->assertTrue($this->db->table('foobar')->sumColumn('b', 2.5)->sumColumn('c', 3)->update());
+
+ $this->assertEquals(
+ array('b' => 4.5, 'c' => 6.3),
+ $this->db->table('foobar')->findOne()
+ );
+ }
+
+ public function testSum()
+ {
+ $this->assertNotFalse($this->db->execute('CREATE TABLE foobar (a INTEGER)'));
+ $this->assertTrue($this->db->table('foobar')->insert(array('a' => 2)));
+ $this->assertTrue($this->db->table('foobar')->insert(array('a' => 5)));
+ $this->assertEquals(7, $this->db->table('foobar')->sum('a'));
+ }
+
+ public function testIncrement()
+ {
+ $this->assertNotFalse($this->db->execute('CREATE TABLE foobar (a INTEGER DEFAULT 0, b INTEGER DEFAULT 0)'));
+ $this->assertTrue($this->db->table('foobar')->insert(array('a' => 2, 'b' => 5)));
+ $this->assertTrue($this->db->table('foobar')->eq('b', 5)->increment('a', 3));
+ $this->assertEquals(5, $this->db->table('foobar')->findOneColumn('a'));
+ }
+
+ public function testLeftJoin()
+ {
+ $this->assertNotFalse($this->db->execute('CREATE TABLE test1 (a INTEGER NOT NULL, foreign_key INTEGER NOT NULL)'));
+ $this->assertNotFalse($this->db->execute('CREATE TABLE test2 (id INTEGER NOT NULL, b INTEGER NOT NULL)'));
+
+ $this->assertTrue($this->db->table('test2')->insert(array('id' => 42, 'b' => 2)));
+ $this->assertTrue($this->db->table('test1')->insert(array('a' => 18, 'foreign_key' => 42)));
+
+ $this->assertEquals(
+ array('a' => 18, 'b' => 2),
+ $this->db->table('test2')->columns('a', 'b')->eq('a', 18)->left('test1', 't1', 'foreign_key', 'test2', 'id')->findOne()
+ );
+
+ $this->assertEquals(
+ array('a' => 18, 'b' => 2),
+ $this->db->table('test2')->columns('a', 'b')->eq('a', 18)->join('test1', 'foreign_key', 'id')->findOne()
+ );
+
+ $this->assertEquals(
+ array('a' => 18, 'b' => 2),
+ $this->db->table('test1')->columns('a', 'b')->join('test2', 'id', 'foreign_key')->findOne()
+ );
+ }
+
+ public function testHashTable()
+ {
+ $this->assertNotFalse($this->db->execute(
+ 'CREATE TABLE foobar (
+ column1 TEXT NOT NULL UNIQUE,
+ column2 TEXT default NULL
+ )'
+ ));
+
+ $this->assertTrue($this->db->table('foobar')->insert(array('column1' => 'option1', 'column2' => 'value1')));
+ $this->assertTrue($this->db->table('foobar')->insert(array('column1' => 'option2', 'column2' => 'value2')));
+ $this->assertTrue($this->db->table('foobar')->insert(array('column1' => 'option3', 'column2' => 'value3')));
+
+ $values = array(
+ 'option1' => 'hey',
+ 'option4' => 'ho',
+ );
+
+ $this->assertTrue($this->db->hashtable('foobar')->columnKey('column1')->columnValue('column2')->put($values));
+
+ $this->assertEquals(
+ array('option2' => 'value2', 'option4' => 'ho'),
+ $this->db->hashtable('foobar')->columnKey('column1')->columnValue('column2')->get('option2', 'option4')
+ );
+
+ $this->assertEquals(
+ array('option2' => 'value2', 'option3' => 'value3', 'option1' => 'hey', 'option4' => 'ho'),
+ $this->db->hashtable('foobar')->columnKey('column1')->columnValue('column2')->get()
+ );
+
+ $this->assertEquals(
+ array('option2' => 'value2', 'option3' => 'value3', 'option1' => 'hey', 'option4' => 'ho'),
+ $this->db->hashtable('foobar')->getAll('column1', 'column2')
+ );
+ }
+}
diff --git a/libs/picodb/tests/SchemaFixture.php b/libs/picodb/tests/SchemaFixture.php
new file mode 100644
index 00000000..fe4b5031
--- /dev/null
+++ b/libs/picodb/tests/SchemaFixture.php
@@ -0,0 +1,21 @@
+<?php
+
+namespace Schema;
+
+use PDO;
+
+function version_1(PDO $pdo)
+{
+ $pdo->exec('CREATE TABLE test1 (column1 TEXT)');
+}
+
+function version_2(PDO $pdo)
+{
+ $pdo->exec('CREATE TABLE test2 (column2 TEXT)');
+}
+
+function version_3(PDO $pdo)
+{
+ // Simulate an error
+ $pdo->exec('CREATE TABL');
+}
diff --git a/libs/picodb/tests/SqliteDatabaseTest.php b/libs/picodb/tests/SqliteDatabaseTest.php
new file mode 100644
index 00000000..628adb07
--- /dev/null
+++ b/libs/picodb/tests/SqliteDatabaseTest.php
@@ -0,0 +1,120 @@
+<?php
+
+require_once __DIR__.'/../../../vendor/autoload.php';
+
+use PicoDb\Database;
+
+class SqliteDatabaseTest extends PHPUnit_Framework_TestCase
+{
+ /**
+ * @var PicoDb\Database
+ */
+ private $db;
+
+ public function setUp()
+ {
+ $this->db = new Database(array('driver' => 'sqlite', 'filename' => ':memory:'));
+ }
+
+ public function testEscapeIdentifer()
+ {
+ $this->assertEquals('"a"', $this->db->escapeIdentifier('a'));
+ $this->assertEquals('a.b', $this->db->escapeIdentifier('a.b'));
+ $this->assertEquals('"c"."a"', $this->db->escapeIdentifier('a', 'c'));
+ $this->assertEquals('a.b', $this->db->escapeIdentifier('a.b', 'c'));
+ $this->assertEquals('SELECT COUNT(*) FROM test', $this->db->escapeIdentifier('SELECT COUNT(*) FROM test'));
+ $this->assertEquals('SELECT COUNT(*) FROM test', $this->db->escapeIdentifier('SELECT COUNT(*) FROM test', 'b'));
+ }
+
+ public function testEscapeIdentiferList()
+ {
+ $this->assertEquals(array('"c"."a"', '"c"."b"'), $this->db->escapeIdentifierList(array('a', 'b'), 'c'));
+ $this->assertEquals(array('"a"', 'd.b'), $this->db->escapeIdentifierList(array('a', 'd.b')));
+ }
+
+ public function testThatPreparedStatementWorks()
+ {
+ $this->db->getConnection()->exec('CREATE TABLE foobar (id INTEGER PRIMARY KEY, something TEXT)');
+ $this->db->execute('INSERT INTO foobar (something) VALUES (?)', array('a'));
+ $this->assertEquals(1, $this->db->getLastId());
+ $this->assertEquals('a', $this->db->execute('SELECT something FROM foobar WHERE something=?', array('a'))->fetchColumn());
+ }
+
+ /**
+ * @expectedException PicoDb\SQLException
+ */
+ public function testBadSQLQuery()
+ {
+ $this->db->execute('INSERT INTO foobar');
+ }
+
+ public function testDuplicateKey()
+ {
+ $this->db->getConnection()->exec('CREATE TABLE foobar (something TEXT UNIQUE)');
+
+ $this->assertNotFalse($this->db->execute('INSERT INTO foobar (something) VALUES (?)', array('a')));
+ $this->assertFalse($this->db->execute('INSERT INTO foobar (something) VALUES (?)', array('a')));
+
+ $this->assertEquals(1, $this->db->execute('SELECT COUNT(*) FROM foobar WHERE something=?', array('a'))->fetchColumn());
+ }
+
+ public function testThatTransactionReturnsAValue()
+ {
+ $this->assertEquals('a', $this->db->transaction(function (Database $db) {
+ $db->getConnection()->exec('CREATE TABLE foobar (something TEXT UNIQUE)');
+ $db->execute('INSERT INTO foobar (something) VALUES (?)', array('a'));
+
+ return $db->execute('SELECT something FROM foobar WHERE something=?', array('a'))->fetchColumn();
+ }));
+ }
+
+ public function testThatTransactionReturnsTrue()
+ {
+ $this->assertTrue($this->db->transaction(function (Database $db) {
+ $db->getConnection()->exec('CREATE TABLE foobar (something TEXT UNIQUE)');
+ $db->execute('INSERT INTO foobar (something) VALUES (?)', array('a'));
+ }));
+ }
+
+ /**
+ * @expectedException PicoDb\SQLException
+ */
+ public function testThatTransactionThrowExceptionWhenRollbacked()
+ {
+ $this->assertFalse($this->db->transaction(function (Database $db) {
+ $db->getConnection()->exec('CREATE TABL');
+ }));
+ }
+
+ public function testThatTransactionReturnsFalseWhithDuplicateKey()
+ {
+ $this->assertFalse($this->db->transaction(function (Database $db) {
+ $db->getConnection()->exec('CREATE TABLE foobar (something TEXT UNIQUE)');
+ $r1 = $db->execute('INSERT INTO foobar (something) VALUES (?)', array('a'));
+ $r2 = $db->execute('INSERT INTO foobar (something) VALUES (?)', array('a'));
+ return $r1 && $r2;
+ }));
+ }
+
+ public function testGetInstance()
+ {
+ Database::setInstance('main', function () {
+ return new Database(array('driver' => 'sqlite', 'filename' => ':memory:'));
+ });
+
+ $instance1 = Database::getInstance('main');
+ $instance2 = Database::getInstance('main');
+
+ $this->assertInstanceOf('PicoDb\Database', $instance1);
+ $this->assertInstanceOf('PicoDb\Database', $instance2);
+ $this->assertTrue($instance1 === $instance2);
+ }
+
+ /**
+ * @expectedException LogicException
+ */
+ public function testGetMissingInstance()
+ {
+ Database::getInstance('notfound');
+ }
+}
diff --git a/libs/picodb/tests/SqliteDriverTest.php b/libs/picodb/tests/SqliteDriverTest.php
new file mode 100644
index 00000000..9965a39c
--- /dev/null
+++ b/libs/picodb/tests/SqliteDriverTest.php
@@ -0,0 +1,70 @@
+<?php
+
+require_once __DIR__.'/../../../vendor/autoload.php';
+
+use PicoDb\Driver\Sqlite;
+
+class SqliteDriverTest extends PHPUnit_Framework_TestCase
+{
+ /**
+ * @var PicoDb\Driver\Sqlite
+ */
+ private $driver;
+
+ public function setUp()
+ {
+ $this->driver = new Sqlite(array('filename' => ':memory:'));
+ }
+
+ /**
+ * @expectedException LogicException
+ */
+ public function testMissingRequiredParameter()
+ {
+ new Sqlite(array());
+ }
+
+ public function testDuplicateKeyError()
+ {
+ $this->assertFalse($this->driver->isDuplicateKeyError(1234));
+ $this->assertTrue($this->driver->isDuplicateKeyError(23000));
+ }
+
+ public function testOperator()
+ {
+ $this->assertEquals('LIKE', $this->driver->getOperator('LIKE'));
+ $this->assertEquals('LIKE', $this->driver->getOperator('ILIKE'));
+ $this->assertEquals('', $this->driver->getOperator('FOO'));
+ }
+
+ public function testSchemaVersion()
+ {
+ $this->assertEquals(0, $this->driver->getSchemaVersion());
+
+ $this->driver->setSchemaVersion(1);
+ $this->assertEquals(1, $this->driver->getSchemaVersion());
+
+ $this->driver->setSchemaVersion(42);
+ $this->assertEquals(42, $this->driver->getSchemaVersion());
+ }
+
+ public function testLastInsertId()
+ {
+ $this->assertEquals(0, $this->driver->getLastId());
+
+ $this->driver->getConnection()->exec('CREATE TABLE foobar (id INTEGER PRIMARY KEY, something TEXT)');
+ $this->driver->getConnection()->exec('INSERT INTO foobar (something) VALUES (1)');
+
+ $this->assertEquals(1, $this->driver->getLastId());
+ }
+
+ public function testEscape()
+ {
+ $this->assertEquals('"foobar"', $this->driver->escape('foobar'));
+ }
+
+ public function testDatabaseVersion()
+ {
+ $this->assertStringStartsWith('3.', $this->driver->getDatabaseVersion());
+ }
+}
diff --git a/libs/picodb/tests/SqliteLobtest.php b/libs/picodb/tests/SqliteLobtest.php
new file mode 100644
index 00000000..d0889655
--- /dev/null
+++ b/libs/picodb/tests/SqliteLobtest.php
@@ -0,0 +1,84 @@
+<?php
+
+require_once __DIR__.'/../../../vendor/autoload.php';
+
+use PicoDb\Database;
+
+class SqliteLobTest extends PHPUnit_Framework_TestCase
+{
+ /**
+ * @var PicoDb\Database
+ */
+ private $db;
+
+ public function setUp()
+ {
+ $this->db = new Database(array('driver' => 'sqlite', 'filename' => ':memory:'));
+ $this->db->getConnection()->exec('DROP TABLE IF EXISTS large_objects');
+ $this->db->getConnection()->exec('CREATE TABLE large_objects (id VARCHAR(20), file_content BLOB)');
+ }
+
+ public function testInsert()
+ {
+ $result = $this->db->largeObject('large_objects')->insertFromFile('file_content', __FILE__, array('id' => 'test'));
+ $this->assertTrue($result);
+ }
+
+ public function testInsertFromString()
+ {
+ $data = 'test';
+ $result = $this->db->largeObject('large_objects')->insertFromString('file_content', $data, array('id' => 'test'));
+ $this->assertTrue($result);
+ }
+
+ public function testInsertWithOptionalParams()
+ {
+ $result = $this->db->largeObject('large_objects')->insertFromFile('file_content', __FILE__);
+ $this->assertTrue($result);
+ }
+
+ public function testFindOneColumnAsStream()
+ {
+ $result = $this->db->largeObject('large_objects')->insertFromFile('file_content', __FILE__, array('id' => 'test'));
+ $this->assertTrue($result);
+
+ $contents = $this->db->largeObject('large_objects')->eq('id', 'test')->findOneColumnAsStream('file_content');
+ $this->assertSame(md5(file_get_contents(__FILE__)), md5($contents));
+ }
+
+ public function testFindOneColumnAsString()
+ {
+ $result = $this->db->largeObject('large_objects')->insertFromFile('file_content', __FILE__, array('id' => 'test'));
+ $this->assertTrue($result);
+
+ $contents = $this->db->largeObject('large_objects')->eq('id', 'test')->findOneColumnAsString('file_content');
+ $this->assertSame(md5(file_get_contents(__FILE__)), md5($contents));
+ }
+
+ public function testUpdate()
+ {
+ $result = $this->db->largeObject('large_objects')->insertFromFile('file_content', __FILE__, array('id' => 'test1'));
+ $this->assertTrue($result);
+
+ $result = $this->db->largeObject('large_objects')->insertFromFile('file_content', __FILE__, array('id' => 'test2'));
+ $this->assertTrue($result);
+
+ $result = $this->db->largeObject('large_objects')->eq('id', 'test1')->updateFromFile('file_content', __DIR__.'/../LICENSE');
+ $this->assertTrue($result);
+
+ $contents = $this->db->largeObject('large_objects')->eq('id', 'test1')->findOneColumnAsString('file_content');
+ $this->assertSame(md5(file_get_contents(__DIR__.'/../LICENSE')), md5($contents));
+
+ $contents = $this->db->largeObject('large_objects')->eq('id', 'test2')->findOneColumnAsString('file_content');
+ $this->assertSame(md5(file_get_contents(__FILE__)), md5($contents));
+
+ $result = $this->db->largeObject('large_objects')->updateFromFile('file_content', __DIR__.'/../composer.json');
+ $this->assertTrue($result);
+
+ $contents = $this->db->largeObject('large_objects')->eq('id', 'test1')->findOneColumnAsString('file_content');
+ $this->assertSame(md5(file_get_contents(__DIR__.'/../composer.json')), md5($contents));
+
+ $contents = $this->db->largeObject('large_objects')->eq('id', 'test2')->findOneColumnAsString('file_content');
+ $this->assertSame(md5(file_get_contents(__DIR__.'/../composer.json')), md5($contents));
+ }
+}
diff --git a/libs/picodb/tests/SqliteSchemaTest.php b/libs/picodb/tests/SqliteSchemaTest.php
new file mode 100644
index 00000000..7522e10d
--- /dev/null
+++ b/libs/picodb/tests/SqliteSchemaTest.php
@@ -0,0 +1,36 @@
+<?php
+
+require_once __DIR__.'/../../../vendor/autoload.php';
+require_once __DIR__.'/SchemaFixture.php';
+
+class SqliteSchemaTest extends PHPUnit_Framework_TestCase
+{
+ /**
+ * @var PicoDb\Database
+ */
+ private $db;
+
+ public function setUp()
+ {
+ $this->db = new PicoDb\Database(array('driver' => 'sqlite', 'filename' => ':memory:'));
+ }
+
+ public function testMigrations()
+ {
+ $this->assertTrue($this->db->schema()->check(2));
+ $this->assertEquals(2, $this->db->getDriver()->getSchemaVersion());
+ }
+
+ public function testFailedMigrations()
+ {
+ $this->assertFalse($this->db->schema()->check(3));
+ $this->assertEquals(2, $this->db->getDriver()->getSchemaVersion());
+
+ $logs = $this->db->getLogMessages();
+ $this->assertNotEmpty($logs);
+ $this->assertEquals('Running migration \Schema\version_1', $logs[0]);
+ $this->assertEquals('Running migration \Schema\version_2', $logs[1]);
+ $this->assertEquals('Running migration \Schema\version_3', $logs[2]);
+ $this->assertEquals('SQLSTATE[HY000]: General error: 1 near "TABL": syntax error', $logs[3]);
+ }
+}
diff --git a/libs/picodb/tests/SqliteTableTest.php b/libs/picodb/tests/SqliteTableTest.php
new file mode 100644
index 00000000..dae718fa
--- /dev/null
+++ b/libs/picodb/tests/SqliteTableTest.php
@@ -0,0 +1,444 @@
+<?php
+
+require_once __DIR__.'/../../../vendor/autoload.php';
+
+use PicoDb\Database;
+use PicoDb\Table;
+
+class SqliteTableTest extends PHPUnit_Framework_TestCase
+{
+ /**
+ * @var PicoDb\Database
+ */
+ private $db;
+
+ public function setUp()
+ {
+ $this->db = new Database(array('driver' => 'sqlite', 'filename' => ':memory:'));
+ }
+
+ public function testSelect()
+ {
+ $this->assertEquals('SELECT 1 FROM "test"', $this->db->table('test')->select(1)->buildSelectQuery());
+ }
+
+ public function testColumns()
+ {
+ $this->assertEquals('SELECT "a", "b" FROM "test"', $this->db->table('test')->columns('a', 'b')->buildSelectQuery());
+ }
+
+ public function testDistinct()
+ {
+ $this->assertEquals('SELECT DISTINCT "a", "b" FROM "test"', $this->db->table('test')->distinct('a', 'b')->buildSelectQuery());
+ }
+
+ public function testGroupBy()
+ {
+ $this->assertEquals('SELECT * FROM "test" GROUP BY "a"', $this->db->table('test')->groupBy('a')->buildSelectQuery());
+ }
+
+ public function testOrderBy()
+ {
+ $this->assertEquals('SELECT * FROM "test" ORDER BY "a" ASC', $this->db->table('test')->asc('a')->buildSelectQuery());
+ $this->assertEquals('SELECT * FROM "test" ORDER BY "a" ASC', $this->db->table('test')->orderBy('a', Table::SORT_ASC)->buildSelectQuery());
+
+ $this->assertEquals('SELECT * FROM "test" ORDER BY "a" DESC', $this->db->table('test')->desc('a')->buildSelectQuery());
+ $this->assertEquals('SELECT * FROM "test" ORDER BY "a" DESC', $this->db->table('test')->orderBy('a', Table::SORT_DESC)->buildSelectQuery());
+
+ $this->assertEquals('SELECT * FROM "test" ORDER BY "a" ASC, "b" ASC', $this->db->table('test')->asc('a')->asc('b')->buildSelectQuery());
+ $this->assertEquals('SELECT * FROM "test" ORDER BY "a" DESC, "b" DESC', $this->db->table('test')->desc('a')->desc('b')->buildSelectQuery());
+
+ $this->assertEquals('SELECT * FROM "test" ORDER BY "a" ASC, "b" ASC', $this->db->table('test')->orderBy('a')->orderBy('b')->buildSelectQuery());
+ $this->assertEquals('SELECT * FROM "test" ORDER BY "a" DESC, "b" DESC', $this->db->table('test')->orderBy('a', Table::SORT_DESC)->orderBy('b', Table::SORT_DESC)->buildSelectQuery());
+
+ $this->assertEquals('SELECT * FROM "test" ORDER BY "a" DESC, "b" ASC', $this->db->table('test')->desc('a')->asc('b')->buildSelectQuery());
+ }
+
+ public function testLimit()
+ {
+ $this->assertEquals('SELECT * FROM "test" LIMIT 10', $this->db->table('test')->limit(10)->buildSelectQuery());
+ $this->assertEquals('SELECT * FROM "test"', $this->db->table('test')->limit(null)->buildSelectQuery());
+ }
+
+ public function testOffset()
+ {
+ $this->assertEquals('SELECT * FROM "test" OFFSET 0', $this->db->table('test')->offset(0)->buildSelectQuery());
+ $this->assertEquals('SELECT * FROM "test" OFFSET 10', $this->db->table('test')->offset(10)->buildSelectQuery());
+ $this->assertEquals('SELECT * FROM "test"', $this->db->table('test')->limit(null)->buildSelectQuery());
+ }
+
+ public function testLimitOffset()
+ {
+ $this->assertEquals('SELECT * FROM "test" LIMIT 2 OFFSET 0', $this->db->table('test')->offset(0)->limit(2)->buildSelectQuery());
+ $this->assertEquals('SELECT * FROM "test" LIMIT 5 OFFSET 10', $this->db->table('test')->offset(10)->limit(5)->buildSelectQuery());
+ }
+
+ public function testSubquery()
+ {
+ $this->assertEquals('SELECT (SELECT 1 FROM "foobar" WHERE 1=1) AS "b" FROM "test"', $this->db->table('test')->subquery('SELECT 1 FROM "foobar" WHERE 1=1', 'b')->buildSelectQuery());
+ }
+
+ public function testConditionEqual()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM "test" WHERE "a" = ? AND "b" = ?', $table->eq('a', 2)->eq('b', 'foobar')->buildSelectQuery());
+ $this->assertEquals(array(2, 'foobar'), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionNotEqual()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM "test" WHERE "a" != ?', $table->neq('a', 2)->buildSelectQuery());
+ $this->assertEquals(array(2), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionIn()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM "test" WHERE "a" IN (?, ?)', $table->in('a', array('b', 'c'))->buildSelectQuery());
+ $this->assertEquals(array('b', 'c'), $table->getConditionBuilder()->getValues());
+
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM "test"', $table->in('a', array())->buildSelectQuery());
+ $this->assertEquals(array(), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionInSubquery()
+ {
+ $table = $this->db->table('test');
+ $subquery = $this->db->table('test2')->columns('c')->eq('d', 'e');
+
+ $this->assertEquals(
+ 'SELECT * FROM "test" WHERE "a" IN (SELECT "c" FROM "test2" WHERE "d" = ?)',
+ $table->inSubquery('a', $subquery)->buildSelectQuery()
+ );
+
+ $this->assertEquals(array('e'), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionNotIn()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM "test" WHERE "a" NOT IN (?, ?)', $table->notIn('a', array('b', 'c'))->buildSelectQuery());
+ $this->assertEquals(array('b', 'c'), $table->getConditionBuilder()->getValues());
+
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM "test"', $table->notIn('a', array())->buildSelectQuery());
+ $this->assertEquals(array(), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionNotInSubquery()
+ {
+ $table = $this->db->table('test');
+ $subquery = $this->db->table('test2')->columns('c')->eq('d', 'e');
+
+ $this->assertEquals(
+ 'SELECT * FROM "test" WHERE "a" NOT IN (SELECT "c" FROM "test2" WHERE "d" = ?)',
+ $table->notInSubquery('a', $subquery)->buildSelectQuery()
+ );
+
+ $this->assertEquals(array('e'), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionLike()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM "test" WHERE "a" LIKE ?', $table->like('a', '%foobar%')->buildSelectQuery());
+ $this->assertEquals(array('%foobar%'), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionILike()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM "test" WHERE "a" LIKE ?', $table->ilike('a', '%foobar%')->buildSelectQuery());
+ $this->assertEquals(array('%foobar%'), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionGreaterThan()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM "test" WHERE "a" > ?', $table->gt('a', 5)->buildSelectQuery());
+ $this->assertEquals(array(5), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionGreaterThanInSubquery()
+ {
+ $table = $this->db->table('test');
+ $subquery = $this->db->table('test2')->columns('c')->eq('d', 'e');
+
+ $this->assertEquals(
+ 'SELECT * FROM "test" WHERE "a" > (SELECT "c" FROM "test2" WHERE "d" = ?)',
+ $table->gtSubquery('a', $subquery)->buildSelectQuery()
+ );
+
+ $this->assertEquals(array('e'), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionGreaterThanOrEqual()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM "test" WHERE "a" >= ?', $table->gte('a', 5)->buildSelectQuery());
+ $this->assertEquals(array(5), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionGreaterThanEqualInSubquery()
+ {
+ $table = $this->db->table('test');
+ $subquery = $this->db->table('test2')->columns('c')->eq('d', 'e');
+
+ $this->assertEquals(
+ 'SELECT * FROM "test" WHERE "a" >= (SELECT "c" FROM "test2" WHERE "d" = ?)',
+ $table->gteSubquery('a', $subquery)->buildSelectQuery()
+ );
+
+ $this->assertEquals(array('e'), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionLowerThan()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM "test" WHERE "a" < ?', $table->lt('a', 5)->buildSelectQuery());
+ $this->assertEquals(array(5), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionLowerThanInSubquery()
+ {
+ $table = $this->db->table('test');
+ $subquery = $this->db->table('test2')->columns('c')->eq('d', 'e');
+
+ $this->assertEquals(
+ 'SELECT * FROM "test" WHERE "a" < (SELECT "c" FROM "test2" WHERE "d" = ?)',
+ $table->ltSubquery('a', $subquery)->buildSelectQuery()
+ );
+
+ $this->assertEquals(array('e'), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionLowerThanOrEqual()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM "test" WHERE "a" <= ?', $table->lte('a', 5)->buildSelectQuery());
+ $this->assertEquals(array(5), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionLowerThanEqualInSubquery()
+ {
+ $table = $this->db->table('test');
+ $subquery = $this->db->table('test2')->columns('c')->eq('d', 'e');
+
+ $this->assertEquals(
+ 'SELECT * FROM "test" WHERE "a" <= (SELECT "c" FROM "test2" WHERE "d" = ?)',
+ $table->lteSubquery('a', $subquery)->buildSelectQuery()
+ );
+
+ $this->assertEquals(array('e'), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testConditionIsNull()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM "test" WHERE "a" IS NOT NULL', $table->notNull('a')->buildSelectQuery());
+ $this->assertEquals(array(), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testCustomCondition()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM "test" WHERE a=c AND "b" = ?', $table->addCondition('a=c')->eq('b', 4)->buildSelectQuery());
+ $this->assertEquals(array(4), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testOrConditions()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals('SELECT * FROM "test" WHERE "a" IS NOT NULL AND ("b" = ? OR "c" >= ?)', $table->notNull('a')->beginOr()->eq('b', 2)->gte('c', 5)->closeOr()->buildSelectQuery());
+ $this->assertEquals(array(2, 5), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testMultipleOrConditions()
+ {
+ $table = $this->db->table('test');
+
+ $this->assertEquals(
+ 'SELECT * FROM "test" WHERE "a" IS NOT NULL AND ("b" = ? OR ("b" != ? OR "c" = ?) OR "c" >= ?)',
+ $table
+ ->notNull('a')
+ ->beginOr()
+ ->eq('b', 2)
+ ->beginOr()
+ ->neq('b', 6)
+ ->eq('c', 3)
+ ->closeOr()
+ ->gte('c', 5)
+ ->closeOr()
+ ->buildSelectQuery()
+ );
+
+ $this->assertEquals(array(2, 6, 3, 5), $table->getConditionBuilder()->getValues());
+ }
+
+ public function testPersist()
+ {
+ $this->assertNotFalse($this->db->execute('CREATE TABLE foobar_persist (id INTEGER PRIMARY KEY, a TEXT)'));
+ $this->assertSame(1, $this->db->table('foobar_persist')->persist(array('a' => 'b')));
+ }
+
+ public function testInsertUpdate()
+ {
+ $this->assertNotFalse($this->db->execute('CREATE TABLE foobar (a TEXT)'));
+ $this->assertTrue($this->db->table('foobar')->insert(array('a' => 'b')));
+ $this->assertTrue($this->db->table('foobar')->insert(array('a' => 'c')));
+
+ $this->assertEquals(array(array('a' => 'b'), array('a' => 'c')), $this->db->table('foobar')->findAll());
+
+ $this->assertEquals(array('b', 'c'), $this->db->table('foobar')->findAllByColumn('a'));
+
+ $this->assertEquals(array('a' => 'b'), $this->db->table('foobar')->findOne());
+
+ $this->assertEquals('b', $this->db->table('foobar')->findOneColumn('a'));
+
+ $this->assertTrue($this->db->table('foobar')->exists());
+ $this->assertTrue($this->db->table('foobar')->eq('a', 'c')->exists());
+ $this->assertFalse($this->db->table('foobar')->eq('a', 'e')->exists());
+
+ $this->assertEquals(2, $this->db->table('foobar')->count());
+ $this->assertEquals(1, $this->db->table('foobar')->eq('a', 'c')->count());
+ $this->assertEquals(0, $this->db->table('foobar')->eq('a', 'e')->count());
+
+ $this->assertTrue($this->db->table('foobar')->eq('a', 'c')->remove());
+ $this->assertFalse($this->db->table('foobar')->eq('a', 'e')->remove());
+
+ $this->assertTrue($this->db->table('foobar')->eq('a', 'b')->update(array('a' => 'test')));
+ $this->assertTrue($this->db->table('foobar')->eq('a', 'lol')->update(array('a' => 'test')));
+
+ $this->assertNotEmpty($this->db->table('foobar')->eq('a', 'test')->findOne());
+ $this->assertNull($this->db->table('foobar')->eq('a', 'lol')->findOne());
+
+ $this->assertTrue($this->db->table('foobar')->eq('a', 'test')->save(array('a' => 'plop')));
+ $this->assertEquals(1, $this->db->table('foobar')->count());
+ }
+
+ public function testSumColumn()
+ {
+ $this->assertNotFalse($this->db->execute('CREATE TABLE foobar (b REAL, c REAL)'));
+ $this->assertTrue($this->db->table('foobar')->insert(array('b' => 2, 'c' => 3.3)));
+
+ $this->assertTrue($this->db->table('foobar')->sumColumn('b', 2.5)->sumColumn('c', 3)->update());
+
+ $this->assertEquals(
+ array('b' => 4.5, 'c' => 6.3),
+ $this->db->table('foobar')->findOne()
+ );
+ }
+
+ public function testCallback()
+ {
+ $this->assertNotFalse($this->db->execute('CREATE TABLE foobar (a TEXT)'));
+ $this->assertTrue($this->db->table('foobar')->insert(array('a' => 'b')));
+ $this->assertTrue($this->db->table('foobar')->insert(array('a' => 'c')));
+
+ $func = function () {
+ return array('test');
+ };
+
+ $this->assertEquals(array('test'), $this->db->table('foobar')->callback($func)->findAll());
+ $this->assertEquals(array('plop'), $this->db->table('foobar')->callback(array($this, 'myCallback'))->findAll());
+ }
+
+ public function myCallback(array $records)
+ {
+ $this->assertEquals(array(array('a' => 'b'), array('a' => 'c')), $records);
+ return array('plop');
+ }
+
+ public function testSum()
+ {
+ $this->assertNotFalse($this->db->execute('CREATE TABLE foobar (a INTEGER)'));
+ $this->assertTrue($this->db->table('foobar')->insert(array('a' => 2)));
+ $this->assertTrue($this->db->table('foobar')->insert(array('a' => 5)));
+ $this->assertEquals(7, $this->db->table('foobar')->sum('a'));
+ }
+
+ public function testIncrement()
+ {
+ $this->assertNotFalse($this->db->execute('CREATE TABLE foobar (a INTEGER DEFAULT 0, b INTEGER DEFAULT 0)'));
+ $this->assertTrue($this->db->table('foobar')->insert(array('a' => 2, 'b' => 5)));
+ $this->assertTrue($this->db->table('foobar')->eq('b', 5)->increment('a', 3));
+ $this->assertEquals(5, $this->db->table('foobar')->findOneColumn('a'));
+ }
+
+ public function testLeftJoin()
+ {
+ $this->assertNotFalse($this->db->execute('CREATE TABLE test1 (a INTEGER NOT NULL, foreign_key INTEGER NOT NULL)'));
+ $this->assertNotFalse($this->db->execute('CREATE TABLE test2 (id INTEGER NOT NULL, b INTEGER NOT NULL)'));
+
+ $this->assertTrue($this->db->table('test2')->insert(array('id' => 42, 'b' => 2)));
+ $this->assertTrue($this->db->table('test1')->insert(array('a' => 18, 'foreign_key' => 42)));
+
+ $this->assertEquals(
+ array('a' => 18, 'b' => 2),
+ $this->db->table('test2')->columns('a', 'b')->eq('a', 18)->left('test1', 't1', 'foreign_key', 'test2', 'id')->findOne()
+ );
+
+ $this->assertEquals(
+ array('a' => 18, 'b' => 2),
+ $this->db->table('test2')->columns('a', 'b')->eq('a', 18)->join('test1', 'foreign_key', 'id')->findOne()
+ );
+
+ $this->assertEquals(
+ array('a' => 18, 'b' => 2),
+ $this->db->table('test1')->columns('a', 'b')->join('test2', 'id', 'foreign_key')->findOne()
+ );
+ }
+
+ public function testHashTable()
+ {
+ $this->assertNotFalse($this->db->execute(
+ 'CREATE TABLE toto (
+ column1 TEXT NOT NULL UNIQUE,
+ column2 TEXT default NULL
+ )'
+ ));
+
+ $this->assertTrue($this->db->table('toto')->insert(array('column1' => 'option1', 'column2' => 'value1')));
+ $this->assertTrue($this->db->table('toto')->insert(array('column1' => 'option2', 'column2' => 'value2')));
+ $this->assertTrue($this->db->table('toto')->insert(array('column1' => 'option3', 'column2' => 'value3')));
+
+ $values = array(
+ 'option1' => 'hey',
+ 'option4' => 'ho',
+ );
+
+ $this->assertTrue($this->db->hashtable('toto')->columnKey('column1')->columnValue('column2')->put($values));
+
+ $this->assertEquals(
+ array('option2' => 'value2', 'option4' => 'ho'),
+ $this->db->hashtable('toto')->columnKey('column1')->columnValue('column2')->get('option2', 'option4')
+ );
+
+ $this->assertEquals(
+ array('option2' => 'value2', 'option3' => 'value3', 'option1' => 'hey', 'option4' => 'ho'),
+ $this->db->hashtable('toto')->columnKey('column1')->columnValue('column2')->get()
+ );
+
+ $this->assertEquals(
+ array('option2' => 'value2', 'option3' => 'value3', 'option1' => 'hey', 'option4' => 'ho'),
+ $this->db->hashtable('toto')->getAll('column1', 'column2')
+ );
+ }
+}
diff --git a/libs/picodb/tests/UrlParserTest.php b/libs/picodb/tests/UrlParserTest.php
new file mode 100644
index 00000000..ede3d3a5
--- /dev/null
+++ b/libs/picodb/tests/UrlParserTest.php
@@ -0,0 +1,46 @@
+<?php
+
+use PicoDb\UrlParser;
+
+require_once __DIR__.'/../../../vendor/autoload.php';
+
+class UrlParserTest extends PHPUnit_Framework_TestCase
+{
+ public function testParseUrl()
+ {
+ $urlParser = UrlParser::getInstance();
+ $this->assertFalse($urlParser->isEnvironmentVariableDefined());
+
+ $settings = $urlParser->getSettings('postgres://user:pass@hostname:6212/db');
+ $this->assertEquals('postgres', $settings['driver']);
+ $this->assertEquals('user', $settings['username']);
+ $this->assertEquals('pass', $settings['password']);
+ $this->assertEquals('hostname', $settings['hostname']);
+ $this->assertEquals('6212', $settings['port']);
+ $this->assertEquals('db', $settings['database']);
+ }
+
+ public function testParseWrongUrl()
+ {
+ $urlParser = new UrlParser();
+ $settings = $urlParser->getSettings('/');
+ $this->assertEmpty($settings['driver']);
+ $this->assertFalse($urlParser->isEnvironmentVariableDefined());
+ }
+
+ public function testGetUrlFromEnvironment()
+ {
+ putenv('DATABASE_URL=postgres://user:pass@hostname:6212/db');
+
+ $urlParser = new UrlParser();
+ $this->assertTrue($urlParser->isEnvironmentVariableDefined());
+
+ $settings = $urlParser->getSettings();
+ $this->assertEquals('postgres', $settings['driver']);
+ $this->assertEquals('user', $settings['username']);
+ $this->assertEquals('pass', $settings['password']);
+ $this->assertEquals('hostname', $settings['hostname']);
+ $this->assertEquals('6212', $settings['port']);
+ $this->assertEquals('db', $settings['database']);
+ }
+}