Query/tests/core/db_qb_test.php

826 lines
18 KiB
PHP
Raw Normal View History

<?php
/**
* Query
*
* Free Query Builder / Database Abstraction Layer
*
2012-04-20 13:17:39 -04:00
* @package Query
* @author Timothy J. Warren
* @copyright Copyright (c) 2012 - 2014
* @link https://github.com/aviat4ion/Query
2012-04-20 13:17:39 -04:00
* @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
*/
2014-02-14 22:08:19 -05:00
abstract class QBTest extends Query_TestCase {
2013-02-28 12:22:55 -05:00
public function __destruct()
{
if (isset($_GET['show_queries']))
{
echo '<pre>' . print_r($this->db->queries, TRUE) . '</pre>';
}
2013-02-28 12:22:55 -05:00
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
// ! Get tests
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2014-02-11 14:29:41 -05:00
2014-02-14 22:08:19 -05:00
public function testInvalidConnectionName()
2014-02-11 14:29:41 -05:00
{
try
{
$db = Query('foo');
}
catch (InvalidArgumentException $e)
{
2014-02-14 22:08:19 -05:00
$this->assertIsA($e, 'InvalidArgumentException');
2014-02-11 14:29:41 -05:00
}
}
// --------------------------------------------------------------------------
2013-02-28 12:22:55 -05:00
2014-02-14 22:08:19 -05:00
public function testQueryFunctionAlias()
{
$db = Query();
2013-02-28 12:22:55 -05:00
2014-02-14 22:08:19 -05:00
$this->assertTrue($this->db === $db);
}
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testFunctionGet()
{
$query = $this->db->select('id, COUNT(id) as count')
->from('test')
->group_by('id')
->get();
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testGet()
{
$query = $this->db->get('create_test');
2012-04-10 14:06:34 -04:00
$this->assertIsA($query, 'PDOStatement');
}
2012-11-07 08:42:34 -05:00
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testPrefixGet()
2012-11-07 08:42:34 -05:00
{
$query = $this->db->from('test')->get();
$this->assertIsA($query, 'PDOStatement');
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-04-10 14:06:34 -04:00
2014-02-14 22:08:19 -05:00
public function testGetWNumRows()
2012-09-27 13:41:29 -04:00
{
$query = $this->db->get('create_test');
$numrows = count($query->fetchAll(PDO::FETCH_NUM));
$this->assertEqual($this->db->num_rows(), $numrows);
}
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testGetLimit()
{
$query = $this->db->get('create_test', 2);
2012-04-10 14:06:34 -04:00
$this->assertIsA($query, 'PDOStatement');
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-04-10 14:06:34 -04:00
2014-02-14 22:08:19 -05:00
public function testGetLimitSkip()
{
$query = $this->db->get('create_test', 2, 1);
2012-04-10 14:06:34 -04:00
$this->assertIsA($query, 'PDOStatement');
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testGetWhere()
{
$query = $this->db->get_where('create_test', array('id !=' => 1), 2, 1);
$this->assertIsA($query, 'PDOStatement');
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testHaving()
{
$query = $this->db->select('id')
->from('create_test')
->group_by('id')
->having(array('id >' => 1))
->having('id !=', 3)
->get();
$this->assertIsA($query, 'PDOStatement');
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testOrHaving()
{
$query = $this->db->select('id')
->from('create_test')
->group_by('id')
->having(array('id >' => 1))
->or_having('id !=', 3)
->get();
$this->assertIsA($query, 'PDOStatement');
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testGetViews()
{
$this->assertTrue(is_array($this->db->get_views()));
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
// ! Select tests
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testSelectWhereGet()
{
$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-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-04-10 14:06:34 -04:00
2014-02-14 22:08:19 -05:00
public function testSelectWhereGet2()
{
$query = $this->db->select('id, key as k, val')
->where('id !=', 1)
->get('create_test', 2, 1);
$this->assertIsA($query, 'PDOStatement');
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testSelectMax()
{
$query = $this->db->select_max('id', 'di')
2012-11-07 08:42:34 -05:00
->get('test');
$this->assertIsA($query, 'PDOStatement');
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testSelectMin()
{
$query = $this->db->select_min('id', 'di')
->get('create_test');
$this->assertIsA($query, 'PDOStatement');
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testMultiOrderBy()
{
$query = $this->db->from('create_test')
->order_by('id, key')
->get();
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
// ! Grouping tests
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testGroup()
{
$query = $this->db->select('id, key as k, val')
->from('create_test')
->group_start()
->where('id >', 1)
->where('id <', 900)
->group_end()
->limit(2, 1)
->get();
$this->assertIsA($query, 'PDOStatement');
}
2014-02-14 22:08:19 -05:00
public function testOrGroup()
{
$query = $this->db->select('id, key as k, val')
->from('create_test')
->group_start()
->where('id >', 1)
->where('id <', 900)
->group_end()
->or_group_start()
->where('id =', 0)
->group_end()
->limit(2, 1)
->get();
$this->assertIsA($query, 'PDOStatement');
}
2014-02-14 22:08:19 -05:00
public function testOrNotGroup()
{
$query = $this->db->select('id, key as k, val')
->from('create_test')
->group_start()
->where('id >', 1)
->where('id <', 900)
->group_end()
->or_not_group_start()
->where('id =', 0)
->group_end()
->limit(2, 1)
->get();
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
// ! Where In tests
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testWhereIn()
{
$query = $this->db->from('create_test')
->where_in('id', array(0, 6, 56, 563, 341))
->get();
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testOrWhereIn()
{
$query = $this->db->from('create_test')
->where('key', 'false')
->or_where_in('id', array(0, 6, 56, 563, 341))
->get();
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testWhereNotIn()
{
$query = $this->db->from('create_test')
->where('key', 'false')
->where_not_in('id', array(0, 6, 56, 563, 341))
->get();
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testOrWhereNotIn()
{
$query = $this->db->from('create_test')
->where('key', 'false')
->or_where_not_in('id', array(0, 6, 56, 563, 341))
->get();
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testSelectAvg()
{
$query = $this->db->select_avg('id', 'di')
->get('create_test');
$this->assertIsA($query, 'PDOStatement');
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testSelectSum()
{
$query = $this->db->select_sum('id', 'di')
->get('create_test');
$this->assertIsA($query, 'PDOStatement');
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testSelectDistinct()
{
$query = $this->db->select_sum('id', 'di')
->distinct()
->get('create_test');
$this->assertIsA($query, 'PDOStatement');
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testSelectGet()
{
$query = $this->db->select('id, key as k, val')
->get('create_test', 2, 1);
$this->assertIsA($query, 'PDOStatement');
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-04-10 14:06:34 -04:00
2014-02-14 22:08:19 -05:00
public function testSelectFromGet()
{
$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
$this->assertIsA($query, 'PDOStatement');
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-04-10 14:06:34 -04:00
2014-02-14 22:08:19 -05:00
public function testSelectFromLimitGet()
{
$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
$this->assertIsA($query, 'PDOStatement');
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
// ! Query modifier tests
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-04-10 14:06:34 -04:00
2014-02-14 22:08:19 -05:00
public function testOrderBy()
{
$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
$this->assertIsA($query, 'PDOStatement');
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-04-10 14:06:34 -04:00
2014-02-14 22:08:19 -05:00
public function testOrderByRandom()
{
$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
$this->assertIsA($query, 'PDOStatement');
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-04-10 14:06:34 -04:00
2014-02-14 22:08:19 -05:00
public function testGroupBy()
{
$query = $this->db->select('id, key as k, val')
->from('create_test')
->where('id >', 0)
->where('id <', 9000)
->group_by('k')
->group_by(array('id','val'))
->order_by('id', 'DESC')
->order_by('k', 'ASC')
->limit(5,2)
->get();
2012-04-10 14:06:34 -04:00
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
//public function testOr
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-04-10 14:06:34 -04:00
2014-02-14 22:08:19 -05:00
public function testOrWhere()
{
$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
$this->assertIsA($query, 'PDOStatement');
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-04-10 14:06:34 -04:00
2014-02-14 22:08:19 -05:00
public function testLike()
{
$query = $this->db->from('create_test')
->like('key', 'og')
->get();
2012-04-10 14:06:34 -04:00
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testOrLike()
{
$query = $this->db->from('create_test')
->like('key', 'og')
->or_like('key', 'val')
->get();
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testOrNotLike()
{
$query = $this->db->from('create_test')
->like('key', 'og', 'before')
->or_not_like('key', 'val')
->get();
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testNotLike()
{
$query = $this->db->from('create_test')
->like('key', 'og', 'before')
->not_like('key', 'val')
->get();
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testLikeBefore()
{
$query = $this->db->from('create_test')
->like('key', 'og', 'before')
->get();
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testLikeAfter()
{
$query = $this->db->from('create_test')
->like('key', 'og', 'after')
->get();
$this->assertIsA($query, 'PDOStatement');
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-04-10 14:06:34 -04:00
2014-02-14 22:08:19 -05:00
public function testJoin()
{
2012-11-07 08:42:34 -05:00
$query = $this->db->from('create_test ct')
->join('join cj', 'cj.id = ct.id')
->get();
2012-04-10 14:06:34 -04:00
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testLeftJoin()
{
$query = $this->db->from('create_test ct')
->join('join cj', 'cj.id = ct.id', 'left')
->get();
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testInnerJoin()
{
$query = $this->db->from('create_test ct')
->join('join cj', 'cj.id = ct.id', 'inner')
->get();
$this->assertIsA($query, 'PDOStatement');
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
// ! DB update tests
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-04-10 14:06:34 -04:00
2014-02-14 22:08:19 -05:00
public function testInsert()
{
$query = $this->db->set('id', 4)
->set('key', 4)
->set('val', 5)
2012-11-07 08:42:34 -05:00
->insert('test');
2012-04-10 14:06:34 -04:00
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testInsertArray()
{
$query = $this->db->insert('test', array(
'id' => 587,
'key' => 1,
'val' => 2,
));
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testInsertBatch()
{
$data = array(
array(
'id' => 544,
'key' => 3,
'val' => 7,
),
array(
'id' => 89,
'key' => 34,
'val' => 57,
),
array(
'id' => 48,
'key' => 403,
'val' => 97,
),
);
$query = $this->db->insert_batch('test', $data);
$this->assertIsA($query, 'PDOStatement');
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-04-10 14:06:34 -04:00
2014-02-14 22:08:19 -05:00
public function testUpdate()
{
$query = $this->db->where('id', 7)
->update('create_test', array(
'id' => 7,
'key' => 'gogle',
'val' => 'non-word'
));
2012-04-10 14:06:34 -04:00
$this->assertIsA($query, 'PDOStatement');
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-04-10 14:06:34 -04:00
2014-02-14 22:08:19 -05:00
public function testSetArrayUpdate()
2012-10-24 20:19:39 -04:00
{
$array = array(
'id' => 22,
2012-10-24 20:19:39 -04:00
'key' => 'gogle',
'val' => 'non-word'
);
$query = $this->db->set($array)
->where('id', 22)
2012-10-24 20:19:39 -04:00
->update('create_test');
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testWhereSetUpdate()
2012-10-24 20:19:39 -04:00
{
$query = $this->db->where('id', 36)
->set('id', 36)
2012-10-24 20:19:39 -04:00
->set('key', 'gogle')
->set('val', 'non-word')
2014-02-17 20:10:47 -05:00
->update('test');
2012-10-24 20:19:39 -04:00
$this->assertIsA($query, 'PDOStatement');
}
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testDelete()
{
$query = $this->db->delete('create_test', array('id' => 5));
2012-04-10 14:06:34 -04:00
$this->assertIsA($query, 'PDOStatement');
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
// ! Non-data read queries
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testCountAll()
2012-04-10 14:06:34 -04:00
{
2012-11-07 08:42:34 -05:00
$query = $this->db->count_all('test');
$this->assertTrue(is_numeric($query));
2012-04-10 14:06:34 -04:00
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testCountAllResults()
{
2012-11-07 08:42:34 -05:00
$query = $this->db->count_all_results('test');
$this->assertTrue(is_numeric($query));
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testCountAllResults2()
{
$query = $this->db->select('id, key as k, val')
2012-11-07 08:42:34 -05:00
->from('test')
->where(' id ', 1)
->or_where('key >', 0)
->limit(2, 1)
->count_all_results();
$this->assertTrue(is_numeric($query));
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testNumRows()
{
$query = $this->db->get('create_test');
$this->assertTrue(is_numeric($this->db->num_rows()));
}
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
// ! Compiled Query tests
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testGetCompiledSelect()
{
2013-12-06 23:37:43 -05:00
$sql = $this->db->get_compiled_select('create_test');
$qb_res = $this->db->get('create_test');
$sql_res = $this->db->query($sql);
2014-02-14 22:08:19 -05:00
$this->assertEquals($qb_res, $sql_res);
}
2014-02-14 22:08:19 -05:00
public function testGetCompiledUpdate()
{
$sql = $this->db->set(array(
'id' => 4,
'key' => 'foo',
'val' => 'baz'
))->get_compiled_update('create_test');
$this->assertTrue(is_string($sql));
}
2014-02-14 22:08:19 -05:00
public function testGetCompiledInsert()
{
$sql = $this->db->set(array(
'id' => 4,
'key' => 'foo',
'val' => 'baz'
))->get_compiled_insert('create_test');
$this->assertTrue(is_string($sql));
}
2014-02-14 22:08:19 -05:00
public function testGetCompiledDelete()
{
$sql = $this->db->where('id', 4)
->get_compiled_delete('create_test');
$this->assertTrue(is_string($sql));
}
2012-07-26 13:21:24 -04:00
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
// ! Error tests
2012-07-26 13:21:24 -04:00
// --------------------------------------------------------------------------
2012-07-26 13:21:24 -04:00
/**
* Handles invalid drivers
*/
2014-02-14 22:08:19 -05:00
public function testBadDriver()
2012-07-26 13:21:24 -04:00
{
$params = array(
'host' => '127.0.0.1',
'port' => '3306',
'database' => 'test',
'user' => 'root',
'pass' => NULL,
'type' => 'QGYFHGEG'
);
2014-02-14 22:08:19 -05:00
try
{
$this->db = Query($params);
}
catch(BadDBDriverException $e)
{
$this->assertInstanceOf('BadDBDriverException', $e);
}
2012-07-26 13:21:24 -04:00
}
2012-07-27 12:05:29 -04:00
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testBadConnection()
2012-07-27 12:05:29 -04:00
{
$params = array(
'host' => '127.0.0.1',
'port' => '987896',
2012-07-27 12:05:29 -04:00
'database' => 'test',
'user' => NULL,
'pass' => NULL,
'type' => 'mysql',
'name' => 'foobar'
2012-07-27 12:05:29 -04:00
);
2014-02-14 22:08:19 -05:00
try
{
$this->db = @Query($params);
}
catch(BadConnectionException $e)
{
$this->assertInstanceOf('BadConnectionException', $e);
}
2012-07-27 12:05:29 -04:00
}
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testBadMethod()
{
2014-02-14 22:08:19 -05:00
try
{
$res = $this->db->foo();
}
catch(BadMethodCallException $e)
{
$this->assertInstanceOf('BadMethodCallException', $e);
}
}
// --------------------------------------------------------------------------
2014-02-14 22:08:19 -05:00
public function testBadNumRows()
{
$this->db->set(array(
'id' => 999,
'key' => 'ring',
'val' => 'sale'
))->insert('create_test');
$res = $this->db->num_rows();
$this->assertEqual(NULL, $res);
}
}
// End of db_qb_test.php