This repository has been archived on 2018-10-12. You can view files and clone it, but cannot push or open issues or pull requests.
OpenSQLManager/tests/parent.php

326 lines
6.0 KiB
PHP
Raw Normal View History

2012-03-19 14:30:52 -04:00
<?php
/**
* OpenSQLManager
*
* Free Database manager for Open Source Databases
*
* @author Timothy J. Warren
* @copyright Copyright (c) 2012
* @link https://github.com/aviat4ion/OpenSQLManager
2012-04-09 09:02:06 -04:00
* @license http://philsturgeon.co.uk/code/dbad-license
2012-03-19 14:30:52 -04:00
*/
// --------------------------------------------------------------------------
/**
* Parent Database Test Class
*/
abstract class DBTest extends UnitTestCase {
abstract function TestConnection();
function tearDown()
{
$this->db = NULL;
2012-03-19 14:30:52 -04:00
}
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
function TestGetTables()
{
if (empty($this->db)) return;
2012-03-19 14:30:52 -04:00
$tables = $this->db->get_tables();
$this->assertTrue(is_array($tables));
}
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
function TestGetSystemTables()
{
if (empty($this->db)) return;
2012-03-19 14:30:52 -04:00
$tables = $this->db->get_system_tables();
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
$this->assertTrue(is_array($tables));
}
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
function TestCreateTransaction()
{
if (empty($this->db)) return;
2012-03-19 14:30:52 -04:00
$res = $this->db->beginTransaction();
$this->assertTrue($res);
}
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
function TestPreparedStatements()
{
if (empty($this->db)) return;
2012-03-19 14:30:52 -04:00
$sql = <<<SQL
2012-04-09 09:02:06 -04:00
INSERT INTO "create_test" ("id", "key", "val")
2012-03-19 14:30:52 -04:00
VALUES (?,?,?)
SQL;
$statement = $this->db->prepare_query($sql, array(1,"boogers", "Gross"));
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
$statement->execute();
}
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
function TestPrepareExecute()
{
if (empty($this->db)) return;
2012-03-19 14:30:52 -04:00
$sql = <<<SQL
2012-04-09 09:02:06 -04:00
INSERT INTO "create_test" ("id", "key", "val")
2012-03-19 14:30:52 -04:00
VALUES (?,?,?)
SQL;
$this->db->prepare_execute($sql, array(
2, "works", 'also?'
));
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
}
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
function TestCommitTransaction()
{
if (empty($this->db)) return;
2012-03-19 14:30:52 -04:00
$res = $this->db->beginTransaction();
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
$sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (10, 12, 14)';
$this->db->query($sql);
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
$res = $this->db->commit();
$this->assertTrue($res);
}
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
function TestRollbackTransaction()
{
if (empty($this->db)) return;
2012-03-19 14:30:52 -04:00
$res = $this->db->beginTransaction();
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
$sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (182, 96, 43)';
$this->db->query($sql);
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
$res = $this->db->rollback();
$this->assertTrue($res);
}
}
// --------------------------------------------------------------------------
/**
* Query builder parent test class
*/
abstract class QBTest extends UnitTestCase {
function TestGet()
{
if (empty($this->db)) return;
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
$query = $this->db->get('create_test');
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
$this->assertIsA($query, 'PDOStatement');
}
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
function TestGetLimit()
{
if (empty($this->db)) return;
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
$query = $this->db->get('create_test', 2);
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
$this->assertIsA($query, 'PDOStatement');
}
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
function TestGetLimitSkip()
{
if (empty($this->db)) return;
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
$query = $this->db->get('create_test', 2, 1);
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
$this->assertIsA($query, 'PDOStatement');
}
function TestSelectWhereGet()
{
if (empty($this->db)) return;
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
$query = $this->db->select('id, key as k, val')
->where('id >', 1)
->where('id <', 900)
->get('create_test', 2, 1);
$this->assertIsA($query, 'PDOStatement');
}
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
function TestSelectWhereGet2()
{
if (empty($this->db)) return;
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
$query = $this->db->select('id, key as k, val')
->where('id !=', 1)
->get('create_test', 2, 1);
$this->assertIsA($query, 'PDOStatement');
}
function TestSelectGet()
{
if (empty($this->db)) return;
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
$query = $this->db->select('id, key as k, val')
->get('create_test', 2, 1);
$this->assertIsA($query, 'PDOStatement');
}
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
function TestSelectFromGet()
{
if (empty($this->db)) return;
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
$query = $this->db->select('id, key as k, val')
->from('create_test ct')
->where('id >', 1)
->get();
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
$this->assertIsA($query, 'PDOStatement');
}
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
function TestSelectFromLimitGet()
{
if (empty($this->db)) return;
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
$query = $this->db->select('id, key as k, val')
->from('create_test ct')
->where('id >', 1)
->limit(3)
->get();
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
$this->assertIsA($query, 'PDOStatement');
}
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
function TestOrderBy()
{
if (empty($this->db)) return;
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
$query = $this->db->select('id, key as k, val')
->from('create_test')
->where('id >', 0)
->where('id <', 9000)
->order_by('id', 'DESC')
->order_by('k', 'ASC')
->limit(5,2)
->get();
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
$this->assertIsA($query, 'PDOStatement');
}
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
function TestOrderByRandom()
{
if (empty($this->db)) return;
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
$query = $this->db->select('id, key as k, val')
->from('create_test')
->where('id >', 0)
->where('id <', 9000)
->order_by('id', 'rand')
->limit(5,2)
->get();
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
$this->assertIsA($query, 'PDOStatement');
}
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
function TestGroupBy()
{
if (empty($this->db)) return;
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
$query = $this->db->select('id, key as k, val')
->from('create_test')
->where('id >', 0)
->where('id <', 9000)
->group_by('k')
->group_by('val')
->order_by('id', 'DESC')
->order_by('k', 'ASC')
->limit(5,2)
->get();
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
$this->assertIsA($query, 'PDOStatement');
}
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
function TestOrWhere()
{
if (empty($this->db)) return;
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
$query = $this->db->select('id, key as k, val')
->from('create_test')
->where(' id ', 1)
->or_where('key >', 0)
->limit(2, 1)
->get();
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
$this->assertIsA($query, 'PDOStatement');
}
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
function TestLike()
{
if (empty($this->db)) return;
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
$query = $this->db->from('create_test')
->like('key', 'og')
->get();
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
$this->assertIsA($query, 'PDOStatement');
}
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
function TestJoin()
{
if (empty($this->db)) return;
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
$query = $this->db->from('create_test')
->join('create_join cj', 'cj.id = create_test.id')
->get();
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
$this->assertIsA($query, 'PDOStatement');
}
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
function TestInsert()
{
if (empty($this->db)) return;
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
$query = $this->db->set('id', 4)
->set('key', 4)
->set('val', 5)
->insert('create_test');
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
$this->assertIsA($query, 'PDOStatement');
}
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
function TestUpdate()
{
if (empty($this->db)) return;
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
$query = $this->db->set('id', 4)
->set('key', 'gogle')
->set('val', 'non-word')
->where('id', 4)
->update('create_test');
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
$this->assertIsA($query, 'PDOStatement');
}
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
function TestDelete()
{
if (empty($this->db)) return;
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
$query = $this->db->where('id', 4)->delete('create_test');
2012-04-09 09:02:06 -04:00
2012-03-19 14:30:52 -04:00
$this->assertIsA($query, 'PDOStatement');
}
function TestGetViews()
2012-04-09 09:02:06 -04:00
{
if (empty($this->db)) return;
$this->assertTrue(is_array($this->db->get_views()));
2012-04-09 09:02:06 -04:00
}
2012-03-19 14:30:52 -04:00
}
// End of parent.php