2012-03-19 13:24:36 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Query
|
|
|
|
*
|
|
|
|
* Free Query Builder / Database Abstraction Layer
|
|
|
|
*
|
|
|
|
* @author Timothy J. Warren
|
|
|
|
* @copyright Copyright (c) 2012
|
|
|
|
* @link https://github.com/aviat4ion/Query
|
|
|
|
* @license http://philsturgeon.co.uk/code/dbad-license
|
|
|
|
*/
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:38:49 -04:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Query builder parent test class
|
|
|
|
*/
|
2012-03-19 13:24:36 -04:00
|
|
|
abstract class QBTest extends UnitTestCase {
|
|
|
|
|
2012-04-17 08:29:28 -04:00
|
|
|
// ! Get Tests
|
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
function TestGet()
|
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$query = $this->db->get('create_test');
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
function TestGetLimit()
|
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$query = $this->db->get('create_test', 2);
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
function TestGetLimitSkip()
|
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$query = $this->db->get('create_test', 2, 1);
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-04-17 08:29:28 -04:00
|
|
|
|
|
|
|
function TestGetWhere()
|
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
|
|
|
|
|
|
|
$query = $this->db->get_where('create_test', array('id !=' => 1), 2, 1);
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
|
|
|
|
|
|
|
function TestGetViews()
|
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
|
|
|
|
|
|
|
$this->assertTrue(is_array($this->db->get_views()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ! Select Tests
|
2012-03-19 13:24:36 -04:00
|
|
|
|
|
|
|
function TestSelectWhereGet()
|
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -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-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
function TestSelectWhereGet2()
|
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$query = $this->db->select('id, key as k, val')
|
|
|
|
->where('id !=', 1)
|
|
|
|
->get('create_test', 2, 1);
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-04-13 11:53:11 -04:00
|
|
|
|
2012-04-13 12:49:13 -04:00
|
|
|
function TestSelectMax()
|
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
|
|
|
|
|
|
|
$query = $this->db->select_max('id', 'di')
|
|
|
|
->get('create_test');
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
|
|
|
|
|
|
|
function TestSelectMin()
|
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
|
|
|
|
|
|
|
$query = $this->db->select_min('id', 'di')
|
|
|
|
->get('create_test');
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
|
|
|
|
|
|
|
function TestSelectAvg()
|
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
|
|
|
|
|
|
|
$query = $this->db->select_avg('id', 'di')
|
|
|
|
->get('create_test');
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
|
|
|
|
|
|
|
function TestSelectSum()
|
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
|
|
|
|
|
|
|
$query = $this->db->select_sum('id', 'di')
|
|
|
|
->get('create_test');
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
|
|
|
|
2012-04-13 16:50:05 -04:00
|
|
|
function TestSelectDistinct()
|
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
|
|
|
|
|
|
|
$query = $this->db->select_sum('id', 'di')
|
|
|
|
->distinct()
|
|
|
|
->get('create_test');
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-03-19 13:24:36 -04:00
|
|
|
|
|
|
|
function TestSelectGet()
|
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$query = $this->db->select('id, key as k, val')
|
|
|
|
->get('create_test', 2, 1);
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
function TestSelectFromGet()
|
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$query = $this->db->select('id, key as k, val')
|
|
|
|
->from('create_test ct')
|
|
|
|
->where('id >', 1)
|
|
|
|
->get();
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
function TestSelectFromLimitGet()
|
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$query = $this->db->select('id, key as k, val')
|
|
|
|
->from('create_test ct')
|
|
|
|
->where('id >', 1)
|
|
|
|
->limit(3)
|
|
|
|
->get();
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-04-17 08:29:28 -04:00
|
|
|
|
|
|
|
// ! Query modifier tests
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
function TestOrderBy()
|
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -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-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
function TestOrderByRandom()
|
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -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-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
function TestGroupBy()
|
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$query = $this->db->select('id, key as k, val')
|
|
|
|
->from('create_test')
|
|
|
|
->where('id >', 0)
|
|
|
|
->where('id <', 9000)
|
|
|
|
->group_by('k')
|
2012-04-10 14:06:34 -04:00
|
|
|
->group_by('id')
|
2012-03-19 13:24:36 -04:00
|
|
|
->group_by('val')
|
|
|
|
->order_by('id', 'DESC')
|
|
|
|
->order_by('k', 'ASC')
|
|
|
|
->limit(5,2)
|
|
|
|
->get();
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
function TestOrWhere()
|
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -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-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
function TestLike()
|
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$query = $this->db->from('create_test')
|
|
|
|
->like('key', 'og')
|
|
|
|
->get();
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
function TestJoin()
|
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$query = $this->db->from('create_test')
|
|
|
|
->join('create_join cj', 'cj.id = create_test.id')
|
|
|
|
->get();
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-04-17 08:29:28 -04:00
|
|
|
|
|
|
|
// ! DB update tests
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
function TestInsert()
|
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$query = $this->db->set('id', 4)
|
|
|
|
->set('key', 4)
|
|
|
|
->set('val', 5)
|
|
|
|
->insert('create_test');
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
function TestUpdate()
|
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$query = $this->db->set('id', 4)
|
|
|
|
->set('key', 'gogle')
|
|
|
|
->set('val', 'non-word')
|
|
|
|
->where('id', 4)
|
|
|
|
->update('create_test');
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
function TestDelete()
|
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$query = $this->db->where('id', 4)->delete('create_test');
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-04-17 08:29:28 -04:00
|
|
|
// ! Non-data read queries
|
|
|
|
|
|
|
|
function TestCountAll()
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
|
|
|
if (empty($this->db)) return;
|
2012-04-17 08:29:28 -04:00
|
|
|
$query = $this->db->count_all('create_test');
|
|
|
|
|
|
|
|
$this->assertTrue(is_numeric($query));
|
2012-04-10 14:06:34 -04:00
|
|
|
}
|
2012-04-17 08:29:28 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
}
|
|
|
|
|
2012-04-13 16:50:05 -04:00
|
|
|
// End of db_qb_test.php
|