Query/tests/core/base_query_builder_test.php

740 lines
14 KiB
PHP
Raw Normal View History

2016-10-12 22:12:25 -04:00
<?php declare(strict_types=1);
/**
* Query
*
2016-10-12 22:12:25 -04:00
* SQL Query Builder / Database Abstraction Layer
*
2016-10-12 22:12:25 -04:00
* PHP version 7
*
* @package Query
* @author Timothy J. Warren <tim@timshomepage.net>
* @copyright 2012 - 2016 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link https://git.timshomepage.net/aviat4ion/Query
*/
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 {
protected static $db;
2013-02-28 12:22:55 -05:00
public function __destruct()
{
if (isset($_GET['show_queries']))
{
echo '<pre>' . print_r(self::$db->queries, TRUE) . '</pre>';
}
2013-02-28 12:22:55 -05:00
}
public static function tearDownAfterClass()
{
self::$db = NULL;
}
2014-04-08 17:13:41 -04:00
// ! Driver-specific results
abstract public function testQueryExplain();
2014-02-14 22:08:19 -05:00
// ! Get tests
public function testInvalidConnectionName()
2014-02-11 14:29:41 -05:00
{
2014-02-25 13:47:35 -05:00
try
2014-02-11 14:29:41 -05:00
{
$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
}
}
2014-02-25 13:47:35 -05:00
2014-02-14 22:08:19 -05:00
public function testFunctionGet()
{
$query = self::$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 = self::$db->get('test');
2012-04-10 14:06:34 -04:00
$this->assertIsA($query, 'PDOStatement');
}
2014-02-14 22:08:19 -05:00
public function testPrefixGet()
2012-11-07 08:42:34 -05:00
{
$query = self::$db->from('test')->get();
2012-11-07 08:42:34 -05:00
$this->assertIsA($query, 'PDOStatement');
}
2014-02-14 22:08:19 -05:00
public function testGetWNumRows()
2012-09-27 13:41:29 -04:00
{
$query = self::$db->get('test');
2012-09-27 13:41:29 -04:00
$numrows = count($query->fetchAll(PDO::FETCH_NUM));
$this->assertEqual(self::$db->num_rows(), $numrows);
2012-09-27 13:41:29 -04:00
}
2014-02-14 22:08:19 -05:00
public function testGetLimit()
{
$query = self::$db->get('test', 2);
2012-04-10 14:06:34 -04:00
$this->assertIsA($query, 'PDOStatement');
}
2014-02-14 22:08:19 -05:00
public function testGetLimitSkip()
{
$query = self::$db->get('test', 2, 1);
2012-04-10 14:06:34 -04:00
$this->assertIsA($query, 'PDOStatement');
}
2014-02-14 22:08:19 -05:00
public function testGetWhere()
{
$query = self::$db->get_where('test', array('id !=' => 1), 2, 1);
$this->assertIsA($query, 'PDOStatement');
}
2014-02-14 22:08:19 -05:00
public function testHaving()
{
$query = self::$db->select('id')
2014-02-18 19:29:58 -05:00
->from('test')
->group_by('id')
->having(array('id >' => 1))
->having('id !=', 3)
->get();
$this->assertIsA($query, 'PDOStatement');
}
2014-02-14 22:08:19 -05:00
public function testOrHaving()
{
$query = self::$db->select('id')
2014-02-18 19:29:58 -05:00
->from('test')
->group_by('id')
->having(array('id >' => 1))
->or_having('id !=', 3)
->get();
$this->assertIsA($query, 'PDOStatement');
}
2014-02-14 22:08:19 -05:00
// ! Select tests
public function testSelectWhereGet()
{
$query = self::$db->select('id, key as k, val')
->where('id >', 1)
->where('id <', 900)
2014-02-18 19:29:58 -05:00
->get('test', 2, 1);
$this->assertIsA($query, 'PDOStatement');
}
2014-02-25 13:47:35 -05:00
public function testSelectAvg()
{
$query = self::$db->select_avg('id', 'di')
->get('test');
$this->assertIsA($query, 'PDOStatement');
}
public function testSelectSum()
{
$query = self::$db->select_sum('id', 'di')
->get('test');
$this->assertIsA($query, 'PDOStatement');
}
public function testSelectDistinct()
{
$query = self::$db->select_sum('id', 'di')
->distinct()
->get('test');
$this->assertIsA($query, 'PDOStatement');
}
public function testSelectGet()
{
$query = self::$db->select('id, key as k, val')
->get('test', 2, 1);
$this->assertIsA($query, 'PDOStatement');
}
public function testSelectFromGet()
{
$query = self::$db->select('id, key as k, val')
->from('test ct')
->where('id >', 1)
->get();
$this->assertIsA($query, 'PDOStatement');
}
public function testSelectFromLimitGet()
{
$query = self::$db->select('id, key as k, val')
->from('test ct')
->where('id >', 1)
->limit(3)
->get();
$this->assertIsA($query, 'PDOStatement');
}
2014-02-25 13:47:35 -05:00
2014-02-14 22:08:19 -05:00
public function testSelectWhereGet2()
{
$query = self::$db->select('id, key as k, val')
->where('id !=', 1)
2014-02-18 19:29:58 -05:00
->get('test', 2, 1);
$this->assertIsA($query, 'PDOStatement');
}
2014-02-14 22:08:19 -05:00
public function testSelectMax()
{
$query = self::$db->select_max('id', 'di')
2012-11-07 08:42:34 -05:00
->get('test');
$this->assertIsA($query, 'PDOStatement');
}
2014-02-14 22:08:19 -05:00
public function testSelectMin()
{
$query = self::$db->select_min('id', 'di')
2014-02-18 19:29:58 -05:00
->get('test');
$this->assertIsA($query, 'PDOStatement');
}
2014-02-14 22:08:19 -05:00
public function testMultiOrderBy()
{
$query = self::$db->from('test')
->order_by('id, key')
->get();
$this->assertIsA($query, 'PDOStatement');
}
2014-02-14 22:08:19 -05:00
// ! Grouping tests
public function testGroup()
{
$query = self::$db->select('id, key as k, val')
2014-02-18 19:29:58 -05:00
->from('test')
->group_start()
->where('id >', 1)
->where('id <', 900)
->group_end()
->limit(2, 1)
->get();
$this->assertIsA($query, 'PDOStatement');
}
2014-02-25 13:47:35 -05:00
2014-02-14 22:08:19 -05:00
public function testOrGroup()
{
$query = self::$db->select('id, key as k, val')
2014-02-18 19:29:58 -05:00
->from('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-25 13:47:35 -05:00
2014-02-14 22:08:19 -05:00
public function testOrNotGroup()
{
$query = self::$db->select('id, key as k, val')
2014-02-18 19:29:58 -05:00
->from('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');
}
2016-10-12 20:32:23 -04:00
public function testAndNotGroupStart()
{
$query = self::$db->select('id, key as k, val')
->from('test')
->group_start()
->where('id >', 1)
->where('id <', 900)
->group_end()
->not_group_start()
->where('id =', 0)
->group_end()
->limit(2, 1)
->get();
$this->assertIsA($query, 'PDOStatement');
}
public function testNotGroupStart()
{
$query = self::$db->select('id, key as k, val')
->from('test')
->not_group_start()
->where('id =', 0)
->group_end()
->limit(2, 1)
->get();
$this->assertIsA($query, 'PDOStatement');
}
public function testGroupCamelCase()
{
$query = self::$db->select('id, key as k, val')
->from('test')
->groupStart()
->where('id >', 1)
->where('id <', 900)
->groupEnd()
->orNotGroupStart()
->where('id =', 0)
->groupEnd()
->limit(2, 1)
->get();
$this->assertIsA($query, 'PDOStatement');
}
2014-02-14 22:08:19 -05:00
// ! Where In tests
public function testWhereIn()
{
$query = self::$db->from('test')
->where_in('id', array(0, 6, 56, 563, 341))
->get();
$this->assertIsA($query, 'PDOStatement');
}
2014-02-25 13:47:35 -05:00
2014-02-14 22:08:19 -05:00
public function testOrWhereIn()
{
$query = self::$db->from('test')
->where('key', 'false')
->or_where_in('id', array(0, 6, 56, 563, 341))
->get();
$this->assertIsA($query, 'PDOStatement');
}
2014-02-25 13:47:35 -05:00
2014-02-14 22:08:19 -05:00
public function testWhereNotIn()
{
$query = self::$db->from('test')
->where('key', 'false')
->where_not_in('id', array(0, 6, 56, 563, 341))
->get();
$this->assertIsA($query, 'PDOStatement');
}
2014-02-25 13:47:35 -05:00
2014-02-14 22:08:19 -05:00
public function testOrWhereNotIn()
{
$query = self::$db->from('test')
->where('key', 'false')
->or_where_not_in('id', array(0, 6, 56, 563, 341))
->get();
$this->assertIsA($query, 'PDOStatement');
}
// ! Query modifier tests
2014-02-14 22:08:19 -05:00
public function testOrderBy()
{
$query = self::$db->select('id, key as k, val')
2014-02-18 19:29:58 -05:00
->from('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');
}
2014-02-14 22:08:19 -05:00
public function testOrderByRandom()
{
$query = self::$db->select('id, key as k, val')
2014-02-18 19:29:58 -05:00
->from('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');
}
2014-02-14 22:08:19 -05:00
public function testGroupBy()
{
$query = self::$db->select('id, key as k, val')
2014-02-18 19:29:58 -05:00
->from('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
2014-02-25 13:47:35 -05:00
2014-02-14 22:08:19 -05:00
public function testOrWhere()
{
$query = self::$db->select('id, key as k, val')
2014-02-18 19:29:58 -05:00
->from('test')
->where(' id ', 1)
->or_where('key >', 0)
->limit(2, 1)
->get();
2012-04-10 14:06:34 -04:00
$this->assertIsA($query, 'PDOStatement');
}
2014-02-14 22:08:19 -05:00
public function testLike()
{
$query = self::$db->from('test')
->like('key', 'og')
->get();
2012-04-10 14:06:34 -04:00
$this->assertIsA($query, 'PDOStatement');
}
2014-02-25 13:47:35 -05:00
2014-02-14 22:08:19 -05:00
public function testOrLike()
{
$query = self::$db->from('test')
->like('key', 'og')
->or_like('key', 'val')
->get();
$this->assertIsA($query, 'PDOStatement');
}
2014-02-25 13:47:35 -05:00
2014-02-14 22:08:19 -05:00
public function testOrNotLike()
{
$query = self::$db->from('test')
->like('key', 'og', 'before')
->or_not_like('key', 'val')
->get();
$this->assertIsA($query, 'PDOStatement');
}
2014-02-25 13:47:35 -05:00
2014-02-14 22:08:19 -05:00
public function testNotLike()
{
$query = self::$db->from('test')
->like('key', 'og', 'before')
->not_like('key', 'val')
->get();
$this->assertIsA($query, 'PDOStatement');
}
2014-02-25 13:47:35 -05:00
2014-02-14 22:08:19 -05:00
public function testLikeBefore()
{
$query = self::$db->from('test')
->like('key', 'og', 'before')
->get();
$this->assertIsA($query, 'PDOStatement');
}
2014-02-25 13:47:35 -05:00
2014-02-14 22:08:19 -05:00
public function testLikeAfter()
{
$query = self::$db->from('test')
->like('key', 'og', 'after')
->get();
$this->assertIsA($query, 'PDOStatement');
}
2014-02-14 22:08:19 -05:00
public function testJoin()
{
$query = self::$db->from('test ct')
2012-11-07 08:42:34 -05:00
->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 = self::$db->from('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 = self::$db->from('test ct')
->join('join cj', 'cj.id = ct.id', 'inner')
->get();
$this->assertIsA($query, 'PDOStatement');
}
public function testJoinWithMultipleWhereValues()
{
$query = self::$db->from('test ct')
->join('join cj', 'cj.id=ct.id', 'inner')
->where(array(
'ct.id < ' => 3,
'ct.key' => 'foo'
))
->get();
$this->assertIsA($query, 'PDOStatement');
}
// ! DB update tests
2014-02-14 22:08:19 -05:00
public function testInsert()
{
$query = self::$db->set('id', 98)
2014-02-18 19:29:58 -05:00
->set('key', 84)
->set('val', 120)
2012-11-07 08:42:34 -05:00
->insert('test');
2012-04-10 14:06:34 -04:00
$this->assertIsA($query, 'PDOStatement');
}
2014-02-25 13:47:35 -05:00
2014-02-14 22:08:19 -05:00
public function testInsertArray()
{
$query = self::$db->insert('test', array(
'id' => 587,
'key' => 1,
'val' => 2,
));
$this->assertIsA($query, 'PDOStatement');
}
2014-02-25 13:47:35 -05:00
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' => "10 o'clock",
),
array(
'id' => 48,
'key' => 403,
'val' => 97,
),
);
$query = self::$db->insert_batch('test', $data);
$this->assertIsA($query, 'PDOStatement');
}
2014-02-14 22:08:19 -05:00
public function testUpdate()
{
$query = self::$db->where('id', 7)
2014-02-18 19:29:58 -05:00
->update('test', array(
'id' => 7,
'key' => 'gogle',
'val' => 'non-word'
));
2012-04-10 14:06:34 -04:00
$this->assertIsA($query, 'PDOStatement');
}
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 = self::$db->set($array)
->where('id', 22)
2014-02-18 19:29:58 -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 testWhereSetUpdate()
2012-10-24 20:19:39 -04:00
{
$query = self::$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 = self::$db->delete('test', array('id' => 5));
2012-04-10 14:06:34 -04:00
$this->assertIsA($query, 'PDOStatement');
}
public function testDeleteWithMultipleWhereValues()
{
$query = self::$db->delete('test', array(
'id' => 5,
'key' => 'gogle'
));
$this->assertIsA($query, 'PDOStatement');
}
// ! Non-data read queries
2014-02-14 22:08:19 -05:00
public function testCountAll()
2012-04-10 14:06:34 -04:00
{
$query = self::$db->count_all('test');
$this->assertTrue(is_numeric($query));
2012-04-10 14:06:34 -04:00
}
2014-02-14 22:08:19 -05:00
public function testCountAllResults()
{
$query = self::$db->count_all_results('test');
$this->assertTrue(is_numeric($query));
}
2014-02-14 22:08:19 -05:00
public function testCountAllResults2()
{
$query = self::$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));
}
2014-02-14 22:08:19 -05:00
public function testNumRows()
{
$query = self::$db->get('test');
$this->assertTrue(is_numeric(self::$db->num_rows()));
}
2014-02-14 22:08:19 -05:00
// ! Compiled Query tests
public function testGetCompiledSelect()
{
$sql = self::$db->get_compiled_select('test');
$qb_res = self::$db->get('test');
$sql_res = self::$db->query($sql);
2014-02-25 13:47:35 -05:00
$this->assertIsA($qb_res,'PDOStatement', "Query Builder Result is a PDO Statement");
$this->assertIsA($sql_res, 'PDOStatement', "SQL Result is a PDO Statement");
//$this->assertEquals($qb_res, $sql_res);
}
2014-02-25 13:47:35 -05:00
2014-02-14 22:08:19 -05:00
public function testGetCompiledUpdate()
{
$sql = self::$db->set(array(
'id' => 4,
'key' => 'foo',
'val' => 'baz'
2014-02-18 19:29:58 -05:00
))->get_compiled_update('test');
2014-02-25 13:47:35 -05:00
$this->assertTrue(is_string($sql));
}
2014-02-25 13:47:35 -05:00
2014-02-14 22:08:19 -05:00
public function testGetCompiledInsert()
{
$sql = self::$db->set(array(
'id' => 4,
'key' => 'foo',
'val' => 'baz'
2014-02-18 19:29:58 -05:00
))->get_compiled_insert('test');
2014-02-25 13:47:35 -05:00
$this->assertTrue(is_string($sql));
}
2014-02-25 13:47:35 -05:00
2014-02-14 22:08:19 -05:00
public function testGetCompiledDelete()
{
$sql = self::$db->where('id', 4)
2014-02-18 19:29:58 -05:00
->get_compiled_delete('test');
2014-02-25 13:47:35 -05:00
$this->assertTrue(is_string($sql));
}
2014-02-14 22:08:19 -05:00
// ! Error tests
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-25 13:47:35 -05:00
try
2014-02-14 22:08:19 -05:00
{
self::$db = Query($params);
2014-02-14 22:08:19 -05:00
}
2014-04-02 17:08:50 -04:00
catch(\Query\BadDBDriverException $e)
2014-02-14 22:08:19 -05:00
{
2014-04-02 17:08:50 -04:00
$this->assertInstanceOf('\\Query\\BadDBDriverException', $e);
2014-02-14 22:08:19 -05:00
}
2012-07-26 13:21:24 -04:00
}
2014-02-14 22:08:19 -05:00
public function testBadMethod()
{
2014-02-14 22:08:19 -05:00
try
{
self::$db->foo();
2014-02-14 22:08:19 -05:00
}
catch(BadMethodCallException $e)
{
$this->assertInstanceOf('BadMethodCallException', $e);
}
}
2014-02-25 13:47:35 -05:00
2014-02-14 22:08:19 -05:00
public function testBadNumRows()
{
self::$db->set(array(
'id' => 999,
'key' => 'ring',
'val' => 'sale'
2014-02-18 19:29:58 -05:00
))->insert('test');
2014-02-25 13:47:35 -05:00
$res = self::$db->num_rows();
$this->assertEqual(NULL, $res);
}
}
// End of db_qb_test.php