2016-10-12 22:12:25 -04:00
|
|
|
<?php declare(strict_types=1);
|
2012-03-19 13:24:36 -04:00
|
|
|
/**
|
|
|
|
* Query
|
|
|
|
*
|
2016-10-12 22:12:25 -04:00
|
|
|
* SQL Query Builder / Database Abstraction Layer
|
2012-03-19 13:24:36 -04:00
|
|
|
*
|
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-03-19 13:24:36 -04:00
|
|
|
*/
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2018-01-19 16:48:52 -05:00
|
|
|
namespace Query\Tests;
|
|
|
|
|
|
|
|
use PDO;
|
|
|
|
|
2012-03-19 13:38:49 -04:00
|
|
|
/**
|
|
|
|
* Query builder parent test class
|
|
|
|
*/
|
2018-01-19 16:48:52 -05:00
|
|
|
abstract class BaseQueryBuilderTest extends TestCase {
|
2012-03-19 13:24:36 -04:00
|
|
|
|
2015-07-30 16:40:30 -04:00
|
|
|
protected static $db;
|
|
|
|
|
2013-02-28 12:22:55 -05:00
|
|
|
public function __destruct()
|
|
|
|
{
|
2013-12-06 23:00:32 -05:00
|
|
|
if (isset($_GET['show_queries']))
|
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
echo '<pre>' . print_r(self::$db->queries, TRUE) . '</pre>';
|
2013-12-06 23:00:32 -05:00
|
|
|
}
|
2013-02-28 12:22:55 -05:00
|
|
|
}
|
|
|
|
|
2015-07-30 16:40:30 -04:00
|
|
|
public static function tearDownAfterClass()
|
|
|
|
{
|
|
|
|
self::$db = NULL;
|
|
|
|
}
|
2016-10-13 21:55:23 -04:00
|
|
|
|
2014-04-08 17:13:41 -04:00
|
|
|
// ! Driver-specific results
|
|
|
|
abstract public function testQueryExplain();
|
2016-10-13 21:55:23 -04:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
// ! Get tests
|
|
|
|
public function testInvalidConnectionName()
|
2014-02-11 14:29:41 -05:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
$this->expectException('InvalidArgumentException');
|
|
|
|
|
2018-01-19 16:48:52 -05:00
|
|
|
Query('foo');
|
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()
|
2013-04-25 08:15:56 -04:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->select('id, COUNT(id) as count')
|
2013-04-25 08:15:56 -04:00
|
|
|
->from('test')
|
2016-10-13 21:55:23 -04:00
|
|
|
->groupBy('id')
|
2013-04-25 08:15:56 -04:00
|
|
|
->get();
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testGet()
|
2012-03-19 13:24:36 -04:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->get('test');
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testPrefixGet()
|
2012-11-07 08:42:34 -05:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->from('test')->get();
|
2012-11-07 08:42:34 -05:00
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-11-08 14:28:49 -05:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testGetWNumRows()
|
2012-09-27 13:41:29 -04:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->get('test');
|
2012-09-27 13:41:29 -04:00
|
|
|
$numrows = count($query->fetchAll(PDO::FETCH_NUM));
|
|
|
|
|
2016-10-13 21:55:23 -04:00
|
|
|
$this->assertEqual(self::$db->numRows(), $numrows);
|
2012-09-27 13:41:29 -04:00
|
|
|
}
|
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testGetLimit()
|
2012-03-19 13:24:36 -04:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->get('test', 2);
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testGetLimitSkip()
|
2012-03-19 13:24:36 -04:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->get('test', 2, 1);
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testGetWhere()
|
2012-04-17 08:29:28 -04:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
$query = self::$db->getWhere('test', array('id !=' => 1), 2, 1);
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-17 08:29:28 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testHaving()
|
2012-04-17 14:22:51 -04:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->select('id')
|
2014-02-18 19:29:58 -05:00
|
|
|
->from('test')
|
2016-10-13 21:55:23 -04:00
|
|
|
->groupBy('id')
|
2012-04-17 14:22:51 -04:00
|
|
|
->having(array('id >' => 1))
|
|
|
|
->having('id !=', 3)
|
|
|
|
->get();
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-17 14:22:51 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testOrHaving()
|
2012-04-17 14:22:51 -04:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->select('id')
|
2014-02-18 19:29:58 -05:00
|
|
|
->from('test')
|
2016-10-13 21:55:23 -04:00
|
|
|
->groupBy('id')
|
2012-04-17 14:22:51 -04:00
|
|
|
->having(array('id >' => 1))
|
2016-10-13 21:55:23 -04:00
|
|
|
->orHaving('id !=', 3)
|
2012-04-17 14:22:51 -04:00
|
|
|
->get();
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-17 14:22:51 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2014-02-14 22:08:19 -05:00
|
|
|
// ! Select tests
|
|
|
|
public function testSelectWhereGet()
|
2012-03-19 13:24:36 -04:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->select('id, key as k, val')
|
2012-03-19 13:24:36 -04:00
|
|
|
->where('id >', 1)
|
|
|
|
->where('id <', 900)
|
2014-02-18 19:29:58 -05:00
|
|
|
->get('test', 2, 1);
|
2012-03-19 13:24:36 -04:00
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2014-11-07 12:14:46 -05:00
|
|
|
public function testSelectAvg()
|
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
$query = self::$db->selectAvg('id', 'di')
|
2014-11-07 12:14:46 -05:00
|
|
|
->get('test');
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSelectSum()
|
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
$query = self::$db->selectSum('id', 'di')
|
2014-11-07 12:14:46 -05:00
|
|
|
->get('test');
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSelectDistinct()
|
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
$query = self::$db->selectSum('id', 'di')
|
2014-11-07 12:14:46 -05:00
|
|
|
->distinct()
|
|
|
|
->get('test');
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSelectGet()
|
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->select('id, key as k, val')
|
2014-11-07 12:14:46 -05:00
|
|
|
->get('test', 2, 1);
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSelectFromGet()
|
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->select('id, key as k, val')
|
2014-11-07 12:14:46 -05:00
|
|
|
->from('test ct')
|
|
|
|
->where('id >', 1)
|
|
|
|
->get();
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSelectFromLimitGet()
|
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->select('id, key as k, val')
|
2014-11-07 12:14:46 -05:00
|
|
|
->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()
|
2012-03-19 13:24:36 -04:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->select('id, key as k, val')
|
2012-03-19 13:24:36 -04:00
|
|
|
->where('id !=', 1)
|
2014-02-18 19:29:58 -05:00
|
|
|
->get('test', 2, 1);
|
2012-03-19 13:24:36 -04:00
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testSelectMax()
|
2012-04-13 12:49:13 -04:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
$query = self::$db->selectMax('id', 'di')
|
2012-11-07 08:42:34 -05:00
|
|
|
->get('test');
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-13 12:49:13 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testSelectMin()
|
2012-04-13 12:49:13 -04:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
$query = self::$db->selectMin('id', 'di')
|
2014-02-18 19:29:58 -05:00
|
|
|
->get('test');
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-13 12:49:13 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testMultiOrderBy()
|
2012-08-22 21:17:27 -04:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->from('test')
|
2016-10-13 21:55:23 -04:00
|
|
|
->orderBy('id, key')
|
2012-08-22 21:17:27 -04:00
|
|
|
->get();
|
2012-08-24 12:17:08 -04:00
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2014-02-14 22:08:19 -05:00
|
|
|
// ! Grouping tests
|
|
|
|
public function testGroup()
|
2013-12-06 23:00:32 -05:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->select('id, key as k, val')
|
2014-02-18 19:29:58 -05:00
|
|
|
->from('test')
|
2016-10-13 21:55:23 -04:00
|
|
|
->groupStart()
|
2013-12-06 23:00:32 -05:00
|
|
|
->where('id >', 1)
|
|
|
|
->where('id <', 900)
|
2016-10-13 21:55:23 -04:00
|
|
|
->groupEnd()
|
2013-12-06 23:00:32 -05:00
|
|
|
->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()
|
2013-12-06 23:00:32 -05:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->select('id, key as k, val')
|
2014-02-18 19:29:58 -05:00
|
|
|
->from('test')
|
2016-10-13 21:55:23 -04:00
|
|
|
->groupStart()
|
2013-12-06 23:00:32 -05:00
|
|
|
->where('id >', 1)
|
|
|
|
->where('id <', 900)
|
2016-10-13 21:55:23 -04:00
|
|
|
->groupEnd()
|
|
|
|
->orGroupStart()
|
2013-12-06 23:00:32 -05:00
|
|
|
->where('id =', 0)
|
2016-10-13 21:55:23 -04:00
|
|
|
->groupEnd()
|
2013-12-06 23:00:32 -05:00
|
|
|
->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()
|
2013-12-06 23:00:32 -05:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->select('id, key as k, val')
|
2014-02-18 19:29:58 -05:00
|
|
|
->from('test')
|
2016-10-13 21:55:23 -04:00
|
|
|
->groupStart()
|
2013-12-06 23:00:32 -05:00
|
|
|
->where('id >', 1)
|
|
|
|
->where('id <', 900)
|
2016-10-13 21:55:23 -04:00
|
|
|
->groupEnd()
|
|
|
|
->orNotGroupStart()
|
2013-12-06 23:00:32 -05:00
|
|
|
->where('id =', 0)
|
2016-10-13 21:55:23 -04:00
|
|
|
->groupEnd()
|
2013-12-06 23:00:32 -05:00
|
|
|
->limit(2, 1)
|
|
|
|
->get();
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-24 12:17:08 -04:00
|
|
|
|
2016-10-12 20:32:23 -04:00
|
|
|
public function testAndNotGroupStart()
|
|
|
|
{
|
|
|
|
$query = self::$db->select('id, key as k, val')
|
|
|
|
->from('test')
|
2016-10-13 21:55:23 -04:00
|
|
|
->groupStart()
|
2016-10-12 20:32:23 -04:00
|
|
|
->where('id >', 1)
|
|
|
|
->where('id <', 900)
|
2016-10-13 21:55:23 -04:00
|
|
|
->groupEnd()
|
|
|
|
->notGroupStart()
|
2016-10-12 20:32:23 -04:00
|
|
|
->where('id =', 0)
|
2016-10-13 21:55:23 -04:00
|
|
|
->groupEnd()
|
2016-10-12 20:32:23 -04:00
|
|
|
->limit(2, 1)
|
|
|
|
->get();
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNotGroupStart()
|
|
|
|
{
|
|
|
|
$query = self::$db->select('id, key as k, val')
|
|
|
|
->from('test')
|
2016-10-13 21:55:23 -04:00
|
|
|
->notGroupStart()
|
2016-10-12 20:32:23 -04:00
|
|
|
->where('id =', 0)
|
2016-10-13 21:55:23 -04:00
|
|
|
->groupEnd()
|
2016-10-12 20:32:23 -04:00
|
|
|
->limit(2, 1)
|
|
|
|
->get();
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
|
|
|
|
2014-04-24 14:50:53 -04:00
|
|
|
public function testGroupCamelCase()
|
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->select('id, key as k, val')
|
2014-04-24 14:50:53 -04:00
|
|
|
->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()
|
2012-08-24 12:17:08 -04:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->from('test')
|
2016-10-13 21:55:23 -04:00
|
|
|
->whereIn('id', array(0, 6, 56, 563, 341))
|
2012-08-24 12:17:08 -04:00
|
|
|
->get();
|
2012-08-22 21:17:27 -04:00
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testOrWhereIn()
|
2013-12-06 23:00:32 -05:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->from('test')
|
2013-12-06 23:00:32 -05:00
|
|
|
->where('key', 'false')
|
2016-10-13 21:55:23 -04:00
|
|
|
->orWhereIn('id', array(0, 6, 56, 563, 341))
|
2013-12-06 23:00:32 -05:00
|
|
|
->get();
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testWhereNotIn()
|
2013-12-06 23:00:32 -05:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->from('test')
|
2013-12-06 23:00:32 -05:00
|
|
|
->where('key', 'false')
|
2016-10-13 21:55:23 -04:00
|
|
|
->whereNotIn('id', array(0, 6, 56, 563, 341))
|
2013-12-06 23:00:32 -05:00
|
|
|
->get();
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testOrWhereNotIn()
|
2013-12-06 23:00:32 -05:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->from('test')
|
2013-12-06 23:00:32 -05:00
|
|
|
->where('key', 'false')
|
2016-10-13 21:55:23 -04:00
|
|
|
->orWhereNotIn('id', array(0, 6, 56, 563, 341))
|
2013-12-06 23:00:32 -05:00
|
|
|
->get();
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-04-17 08:29:28 -04:00
|
|
|
// ! Query modifier tests
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testOrderBy()
|
2012-03-19 13:24:36 -04:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->select('id, key as k, val')
|
2014-02-18 19:29:58 -05:00
|
|
|
->from('test')
|
2012-03-19 13:24:36 -04:00
|
|
|
->where('id >', 0)
|
|
|
|
->where('id <', 9000)
|
2016-10-13 21:55:23 -04:00
|
|
|
->orderBy('id', 'DESC')
|
|
|
|
->orderBy('k', 'ASC')
|
2012-03-19 13:24:36 -04:00
|
|
|
->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-08-22 21:17:27 -04:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testOrderByRandom()
|
2012-03-19 13:24:36 -04:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->select('id, key as k, val')
|
2014-02-18 19:29:58 -05:00
|
|
|
->from('test')
|
2012-03-19 13:24:36 -04:00
|
|
|
->where('id >', 0)
|
|
|
|
->where('id <', 9000)
|
2016-10-13 21:55:23 -04:00
|
|
|
->orderBy('id', 'rand')
|
2012-03-19 13:24:36 -04:00
|
|
|
->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-08-22 21:17:27 -04:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testGroupBy()
|
2012-03-19 13:24:36 -04:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->select('id, key as k, val')
|
2014-02-18 19:29:58 -05:00
|
|
|
->from('test')
|
2012-03-19 13:24:36 -04:00
|
|
|
->where('id >', 0)
|
|
|
|
->where('id <', 9000)
|
2016-10-13 21:55:23 -04:00
|
|
|
->groupBy('k')
|
|
|
|
->groupBy(array('id','val'))
|
|
|
|
->orderBy('id', 'DESC')
|
|
|
|
->orderBy('k', 'ASC')
|
2012-03-19 13:24:36 -04:00
|
|
|
->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-08-22 21:17:27 -04:00
|
|
|
|
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()
|
2012-03-19 13:24:36 -04:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->select('id, key as k, val')
|
2014-02-18 19:29:58 -05:00
|
|
|
->from('test')
|
2012-03-19 13:24:36 -04:00
|
|
|
->where(' id ', 1)
|
2016-10-13 21:55:23 -04:00
|
|
|
->orWhere('key >', 0)
|
2012-03-19 13:24:36 -04:00
|
|
|
->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-08-22 21:17:27 -04:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testLike()
|
2012-03-19 13:24:36 -04:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->from('test')
|
2012-03-19 13:24:36 -04:00
|
|
|
->like('key', 'og')
|
|
|
|
->get();
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -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()
|
2013-12-06 23:00:32 -05:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->from('test')
|
2013-12-06 23:00:32 -05:00
|
|
|
->like('key', 'og')
|
2016-10-13 21:55:23 -04:00
|
|
|
->orLike('key', 'val')
|
2013-12-06 23:00:32 -05:00
|
|
|
->get();
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testOrNotLike()
|
2013-12-06 23:00:32 -05:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->from('test')
|
2013-12-06 23:00:32 -05:00
|
|
|
->like('key', 'og', 'before')
|
2016-10-13 21:55:23 -04:00
|
|
|
->orNotLike('key', 'val')
|
2013-12-06 23:00:32 -05:00
|
|
|
->get();
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testNotLike()
|
2013-12-06 23:00:32 -05:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->from('test')
|
2013-12-06 23:00:32 -05:00
|
|
|
->like('key', 'og', 'before')
|
2016-10-13 21:55:23 -04:00
|
|
|
->notLike('key', 'val')
|
2013-12-06 23:00:32 -05:00
|
|
|
->get();
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testLikeBefore()
|
2013-12-06 23:00:32 -05:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->from('test')
|
2013-12-06 23:00:32 -05:00
|
|
|
->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()
|
2013-12-06 23:00:32 -05:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->from('test')
|
2013-12-06 23:00:32 -05:00
|
|
|
->like('key', 'og', 'after')
|
|
|
|
->get();
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testJoin()
|
2012-03-19 13:24:36 -04:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->from('test ct')
|
2012-11-07 08:42:34 -05:00
|
|
|
->join('join cj', 'cj.id = ct.id')
|
2012-03-19 13:24:36 -04:00
|
|
|
->get();
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testLeftJoin()
|
2013-05-01 15:59:23 -04:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->from('test ct')
|
2013-05-01 15:59:23 -04:00
|
|
|
->join('join cj', 'cj.id = ct.id', 'left')
|
|
|
|
->get();
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testInnerJoin()
|
2013-05-01 15:59:23 -04:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->from('test ct')
|
2013-05-01 15:59:23 -04:00
|
|
|
->join('join cj', 'cj.id = ct.id', 'inner')
|
|
|
|
->get();
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
|
|
|
|
2014-11-07 12:14:46 -05:00
|
|
|
public function testJoinWithMultipleWhereValues()
|
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->from('test ct')
|
2014-11-07 12:14:46 -05:00
|
|
|
->join('join cj', 'cj.id=ct.id', 'inner')
|
|
|
|
->where(array(
|
|
|
|
'ct.id < ' => 3,
|
|
|
|
'ct.key' => 'foo'
|
|
|
|
))
|
|
|
|
->get();
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2018-01-19 16:48:52 -05:00
|
|
|
|
2012-04-17 08:29:28 -04:00
|
|
|
// ! DB update tests
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testInsert()
|
2012-03-19 13:24:36 -04:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$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
|
|
|
|
2012-03-19 13:24:36 -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()
|
2013-12-06 23:00:32 -05:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->insert('test', array(
|
2018-01-19 16:48:52 -05:00
|
|
|
'id' => 587,
|
|
|
|
'key' => 1,
|
|
|
|
'val' => 2,
|
|
|
|
));
|
2013-12-06 23:00:32 -05:00
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testInsertBatch()
|
2013-05-03 13:07:34 -04:00
|
|
|
{
|
|
|
|
$data = array(
|
|
|
|
array(
|
|
|
|
'id' => 544,
|
|
|
|
'key' => 3,
|
|
|
|
'val' => 7,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id' => 89,
|
|
|
|
'key' => 34,
|
2014-04-03 13:28:30 -04:00
|
|
|
'val' => "10 o'clock",
|
2013-05-03 13:07:34 -04:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id' => 48,
|
|
|
|
'key' => 403,
|
|
|
|
'val' => 97,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2016-10-13 21:55:23 -04:00
|
|
|
$query = self::$db->insertBatch('test', $data);
|
2013-05-03 13:07:34 -04:00
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testUpdate()
|
2012-03-19 13:24:36 -04:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->where('id', 7)
|
2014-02-18 19:29:58 -05:00
|
|
|
->update('test', array(
|
2014-02-18 14:50:20 -05:00
|
|
|
'id' => 7,
|
2013-12-06 23:00:32 -05:00
|
|
|
'key' => 'gogle',
|
|
|
|
'val' => 'non-word'
|
|
|
|
));
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testSetArrayUpdate()
|
2012-10-24 20:19:39 -04:00
|
|
|
{
|
|
|
|
$array = array(
|
2014-02-18 14:50:20 -05:00
|
|
|
'id' => 22,
|
2012-10-24 20:19:39 -04:00
|
|
|
'key' => 'gogle',
|
|
|
|
'val' => 'non-word'
|
|
|
|
);
|
|
|
|
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->set($array)
|
2014-02-18 14:50:20 -05:00
|
|
|
->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
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->where('id', 36)
|
2014-02-18 14:50:20 -05:00
|
|
|
->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()
|
2012-03-19 13:24:36 -04:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->delete('test', array('id' => 5));
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-19 13:24:36 -04:00
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2014-11-07 12:14:46 -05:00
|
|
|
public function testDeleteWithMultipleWhereValues()
|
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->delete('test', array(
|
2014-11-07 12:14:46 -05:00
|
|
|
'id' => 5,
|
|
|
|
'key' => 'gogle'
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2016-10-13 21:55:23 -04:00
|
|
|
|
2012-04-17 08:29:28 -04:00
|
|
|
// ! Non-data read queries
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testCountAll()
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
$query = self::$db->countAll('test');
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-17 08:29:28 -04:00
|
|
|
$this->assertTrue(is_numeric($query));
|
2012-04-10 14:06:34 -04:00
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testCountAllResults()
|
2012-04-17 11:37:39 -04:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
$query = self::$db->countAllResults('test');
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-17 11:37:39 -04:00
|
|
|
$this->assertTrue(is_numeric($query));
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testCountAllResults2()
|
2012-04-17 11:37:39 -04:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$query = self::$db->select('id, key as k, val')
|
2012-11-07 08:42:34 -05:00
|
|
|
->from('test')
|
2012-04-17 11:37:39 -04:00
|
|
|
->where(' id ', 1)
|
2016-10-13 21:55:23 -04:00
|
|
|
->orWhere('key >', 0)
|
2012-04-17 11:37:39 -04:00
|
|
|
->limit(2, 1)
|
2016-10-13 21:55:23 -04:00
|
|
|
->countAllResults();
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2012-04-17 11:37:39 -04:00
|
|
|
$this->assertTrue(is_numeric($query));
|
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testNumRows()
|
2012-04-20 16:33:40 -04:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
self::$db->get('test');
|
|
|
|
$this->assertTrue(is_numeric(self::$db->numRows()));
|
2012-04-20 16:33:40 -04:00
|
|
|
}
|
2016-10-13 21:55:23 -04:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
// ! Compiled Query tests
|
|
|
|
public function testGetCompiledSelect()
|
2013-12-06 23:00:32 -05:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
$sql = self::$db->getCompiledSelect('test');
|
|
|
|
$qbRes = self::$db->get('test');
|
|
|
|
$sqlRes = self::$db->query($sql);
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2016-10-13 21:55:23 -04:00
|
|
|
$this->assertIsA($qbRes,'PDOStatement', "Query Builder Result is a PDO Statement");
|
|
|
|
$this->assertIsA($sqlRes, 'PDOStatement', "SQL Result is a PDO Statement");
|
|
|
|
//$this->assertEquals($qbRes, $sqlRes);
|
2013-12-06 23:00:32 -05:00
|
|
|
}
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testGetCompiledUpdate()
|
2013-12-09 19:46:34 -05:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$sql = self::$db->set(array(
|
2013-12-09 19:46:34 -05:00
|
|
|
'id' => 4,
|
|
|
|
'key' => 'foo',
|
|
|
|
'val' => 'baz'
|
2016-10-13 21:55:23 -04:00
|
|
|
))->getCompiledUpdate('test');
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2018-01-19 16:48:52 -05:00
|
|
|
$this->assertTrue(\is_string($sql));
|
2013-12-09 19:46:34 -05:00
|
|
|
}
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testGetCompiledInsert()
|
2013-12-09 19:46:34 -05:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$sql = self::$db->set(array(
|
2013-12-09 19:46:34 -05:00
|
|
|
'id' => 4,
|
|
|
|
'key' => 'foo',
|
|
|
|
'val' => 'baz'
|
2016-10-13 21:55:23 -04:00
|
|
|
))->getCompiledInsert('test');
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2018-01-19 16:48:52 -05:00
|
|
|
$this->assertTrue(\is_string($sql));
|
2013-12-09 19:46:34 -05:00
|
|
|
}
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testGetCompiledDelete()
|
2013-12-09 19:46:34 -05:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
$sql = self::$db->where('id', 4)
|
2016-10-13 21:55:23 -04:00
|
|
|
->getCompiledDelete('test');
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2018-01-19 16:48:52 -05:00
|
|
|
$this->assertTrue(\is_string($sql));
|
2013-12-09 19:46:34 -05:00
|
|
|
}
|
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
|
|
|
|
2016-10-13 21:55:23 -04:00
|
|
|
$this->expectException('Query\BadDBDriverException');
|
|
|
|
|
|
|
|
self::$db = Query($params);
|
2012-07-26 13:21:24 -04:00
|
|
|
}
|
2012-08-22 21:17:27 -04:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testBadMethod()
|
2013-12-06 23:00:32 -05:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
$this->expectException('BadMethodCallException');
|
|
|
|
|
|
|
|
self::$db->foo();
|
2013-12-06 23:00:32 -05:00
|
|
|
}
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2014-02-14 22:08:19 -05:00
|
|
|
public function testBadNumRows()
|
2013-12-09 19:46:34 -05:00
|
|
|
{
|
2015-07-30 16:40:30 -04:00
|
|
|
self::$db->set(array(
|
2013-12-09 19:46:34 -05:00
|
|
|
'id' => 999,
|
|
|
|
'key' => 'ring',
|
|
|
|
'val' => 'sale'
|
2014-02-18 19:29:58 -05:00
|
|
|
))->insert('test');
|
2014-02-25 13:47:35 -05:00
|
|
|
|
2016-10-13 21:55:23 -04:00
|
|
|
$res = self::$db->numRows();
|
2013-12-09 19:46:34 -05:00
|
|
|
$this->assertEqual(NULL, $res);
|
|
|
|
}
|
2016-10-13 21:55:23 -04:00
|
|
|
}
|