Query/tests/BaseQueryBuilderTest.php

829 lines
16 KiB
PHP
Raw Permalink 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
*
2020-04-10 20:54:31 -04:00
* PHP version 7.4
2016-10-12 22:12:25 -04:00
*
* @package Query
* @author Timothy J. Warren <tim@timshomepage.net>
2020-03-18 11:31:56 -04:00
* @copyright 2012 - 2020 Timothy J. Warren
2016-10-12 22:12:25 -04:00
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2019-12-11 16:49:42 -05:00
* @link https://git.timshomepage.net/aviat/Query
* @version 3.0.0
*/
namespace Query\Tests;
2018-01-26 08:39:30 -05:00
use BadMethodCallException;
use PDO;
use Query\Exception\BadDBDriverException;
2019-12-10 12:17:40 -05:00
use Query\QueryBuilderInterface;
2012-03-19 13:38:49 -04:00
/**
* Query builder parent test class
*/
abstract class BaseQueryBuilderTest extends TestCase {
/**
2019-12-10 12:17:40 -05:00
* @var QueryBuilderInterface|null
*/
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
}
2019-12-10 12:17:40 -05:00
public static function tearDownAfterClass(): void
{
2018-01-26 08:39:30 -05:00
if (PHP_SAPI !== 'cli' && PHP_SAPI !== 'phpdbg')
{
echo '<pre>' . print_r(self::$db->queries, TRUE) . '</pre>';
}
self::$db = NULL;
}
2016-10-13 21:55:23 -04:00
2014-04-08 17:13:41 -04:00
// ! Driver-specific results
2019-12-10 12:17:40 -05:00
abstract public function testQueryExplain(): void;
2016-10-13 21:55:23 -04:00
2014-02-14 22:08:19 -05:00
// ! Get tests
2019-12-10 12:17:40 -05:00
public function testInvalidConnectionName(): void
2014-02-11 14:29:41 -05:00
{
2016-10-13 21:55:23 -04:00
$this->expectException('InvalidArgumentException');
Query('foo');
2014-02-11 14:29:41 -05:00
}
2014-02-25 13:47:35 -05:00
2019-12-10 12:17:40 -05:00
public function testFunctionGet(): void
{
$query = self::$db->select('id, COUNT(id) as count')
->from('test')
2016-10-13 21:55:23 -04:00
->groupBy('id')
->get();
$this->assertIsA($query, 'PDOStatement');
}
2019-12-10 12:17:40 -05:00
public function testGet(): void
{
$query = self::$db->get('test');
2012-04-10 14:06:34 -04:00
$this->assertIsA($query, 'PDOStatement');
2018-01-22 15:43:56 -05:00
$lastQuery = self::$db->getLastQuery();
$this->assertTrue(\is_string($lastQuery));
}
2019-12-10 12:17:40 -05:00
public function testPrefixGet(): void
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');
}
2019-12-10 12:17:40 -05:00
public function testGetWNumRows(): void
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));
2016-10-13 21:55:23 -04:00
$this->assertEqual(self::$db->numRows(), $numrows);
2012-09-27 13:41:29 -04:00
}
2019-12-10 12:17:40 -05:00
public function testGetLimit(): void
{
$query = self::$db->get('test', 2);
2012-04-10 14:06:34 -04:00
$this->assertIsA($query, 'PDOStatement');
}
2019-12-10 12:17:40 -05:00
public function testGetLimitSkip(): void
{
$query = self::$db->get('test', 2, 1);
2012-04-10 14:06:34 -04:00
$this->assertIsA($query, 'PDOStatement');
}
2019-12-10 12:17:40 -05:00
public function testGetWhere(): void
{
2018-01-26 08:39:30 -05:00
$query = self::$db->getWhere('test', ['id !=' => 1], 2, 1);
$this->assertIsA($query, 'PDOStatement');
}
2019-12-10 12:17:40 -05:00
public function testHaving(): void
{
$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')
2018-01-26 08:39:30 -05:00
->having(['id >' => 1])
->having('id !=', 3)
->get();
$this->assertIsA($query, 'PDOStatement');
}
2019-12-10 12:17:40 -05:00
public function testOrHaving(): void
{
$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')
2018-01-26 08:39:30 -05:00
->having(['id >' => 1])
2016-10-13 21:55:23 -04:00
->orHaving('id !=', 3)
->get();
$this->assertIsA($query, 'PDOStatement');
}
2014-02-14 22:08:19 -05:00
// ! Select tests
2019-12-10 12:17:40 -05:00
public function testSelectWhereGet(): void
{
$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
2019-12-10 12:17:40 -05:00
public function testSelectWhereGetNoAs(): void
{
$query = self::$db->select('id, key, val')
->where('id >', 1)
->where('id <', 900)
->get('test', 2, 1);
$this->assertIsA($query, 'PDOStatement');
}
public function testSelectAvg(): void
{
2016-10-13 21:55:23 -04:00
$query = self::$db->selectAvg('id', 'di')
->get('test');
$this->assertIsA($query, 'PDOStatement');
}
2019-12-10 12:17:40 -05:00
public function testSelectSum(): void
{
2016-10-13 21:55:23 -04:00
$query = self::$db->selectSum('id', 'di')
->get('test');
$this->assertIsA($query, 'PDOStatement');
}
2019-12-10 12:17:40 -05:00
public function testSelectDistinct(): void
{
2016-10-13 21:55:23 -04:00
$query = self::$db->selectSum('id', 'di')
->distinct()
->get('test');
$this->assertIsA($query, 'PDOStatement');
}
2019-12-10 12:17:40 -05:00
public function testSelectGet(): void
{
$query = self::$db->select('id, key as k, val')
->get('test', 2, 1);
$this->assertIsA($query, 'PDOStatement');
}
2019-12-10 12:17:40 -05:00
public function testSelectFromGet(): void
{
$query = self::$db->select('id, key as k, val')
->from('test ct')
->where('id >', 1)
->get();
$this->assertIsA($query, 'PDOStatement');
}
2019-12-10 12:17:40 -05:00
public function testSelectFromLimitGet(): void
{
$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
2019-12-10 12:17:40 -05:00
public function testSelectWhereGet2(): void
{
$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');
}
2019-12-10 12:17:40 -05:00
public function testSelectMax(): void
{
2016-10-13 21:55:23 -04:00
$query = self::$db->selectMax('id', 'di')
2012-11-07 08:42:34 -05:00
->get('test');
$this->assertIsA($query, 'PDOStatement');
}
2019-12-10 12:17:40 -05:00
public function testSelectMin(): void
{
2016-10-13 21:55:23 -04:00
$query = self::$db->selectMin('id', 'di')
2014-02-18 19:29:58 -05:00
->get('test');
$this->assertIsA($query, 'PDOStatement');
}
2019-12-10 12:17:40 -05:00
public function testMultiOrderBy(): void
{
$query = self::$db->from('test')
2016-10-13 21:55:23 -04:00
->orderBy('id, key')
->get();
$this->assertIsA($query, 'PDOStatement');
}
2014-02-14 22:08:19 -05:00
// ! Grouping tests
2019-12-10 12:17:40 -05:00
public function testGroup(): void
{
$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()
->where('id >', 1)
->where('id <', 900)
2016-10-13 21:55:23 -04:00
->groupEnd()
->limit(2, 1)
->get();
$this->assertIsA($query, 'PDOStatement');
}
2014-02-25 13:47:35 -05:00
2019-12-10 12:17:40 -05:00
public function testOrGroup(): void
{
$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()
->where('id >', 1)
->where('id <', 900)
2016-10-13 21:55:23 -04:00
->groupEnd()
->orGroupStart()
->where('id =', 0)
2016-10-13 21:55:23 -04:00
->groupEnd()
->limit(2, 1)
->get();
$this->assertIsA($query, 'PDOStatement');
}
2014-02-25 13:47:35 -05:00
2019-12-10 12:17:40 -05:00
public function testOrNotGroup(): void
{
$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()
->where('id >', 1)
->where('id <', 900)
2016-10-13 21:55:23 -04:00
->groupEnd()
->orNotGroupStart()
->where('id =', 0)
2016-10-13 21:55:23 -04:00
->groupEnd()
->limit(2, 1)
->get();
$this->assertIsA($query, 'PDOStatement');
}
2019-12-10 12:17:40 -05:00
public function testAndNotGroupStart(): void
2016-10-12 20:32:23 -04:00
{
$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');
}
2019-12-10 12:17:40 -05:00
public function testNotGroupStart(): void
2016-10-12 20:32:23 -04:00
{
$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');
}
2019-12-10 12:17:40 -05:00
public function testGroupCamelCase(): void
{
$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
2019-12-10 12:17:40 -05:00
public function testWhereIn(): void
{
$query = self::$db->from('test')
2018-01-26 08:39:30 -05:00
->whereIn('id', [0, 6, 56, 563, 341])
->get();
$this->assertIsA($query, 'PDOStatement');
}
2014-02-25 13:47:35 -05:00
2019-12-10 12:17:40 -05:00
public function testOrWhereIn(): void
{
$query = self::$db->from('test')
->where('key', 'false')
2018-01-26 08:39:30 -05:00
->orWhereIn('id', [0, 6, 56, 563, 341])
->get();
$this->assertIsA($query, 'PDOStatement');
}
2014-02-25 13:47:35 -05:00
2019-12-10 12:17:40 -05:00
public function testWhereNotIn(): void
{
$query = self::$db->from('test')
->where('key', 'false')
2018-01-26 08:39:30 -05:00
->whereNotIn('id', [0, 6, 56, 563, 341])
->get();
$this->assertIsA($query, 'PDOStatement');
}
2014-02-25 13:47:35 -05:00
2019-12-10 12:17:40 -05:00
public function testOrWhereNotIn(): void
{
$query = self::$db->from('test')
->where('key', 'false')
2018-01-26 08:39:30 -05:00
->orWhereNotIn('id', [0, 6, 56, 563, 341])
->get();
$this->assertIsA($query, 'PDOStatement');
}
// ! Query modifier tests
2019-12-10 12:17:40 -05:00
public function testOrderBy(): void
{
$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)
2016-10-13 21:55:23 -04:00
->orderBy('id', 'DESC')
->orderBy('k', 'ASC')
->limit(5,2)
->get();
2012-04-10 14:06:34 -04:00
$this->assertIsA($query, 'PDOStatement');
}
2019-12-10 12:17:40 -05:00
public function testOrderByRandom(): void
{
$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)
2016-10-13 21:55:23 -04:00
->orderBy('id', 'rand')
->limit(5,2)
->get();
2012-04-10 14:06:34 -04:00
$this->assertIsA($query, 'PDOStatement');
}
2019-12-10 12:17:40 -05:00
public function testGroupBy(): void
{
$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)
2016-10-13 21:55:23 -04:00
->groupBy('k')
2018-01-26 08:39:30 -05:00
->groupBy(['id','val'])
2016-10-13 21:55:23 -04:00
->orderBy('id', 'DESC')
->orderBy('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
2019-12-10 12:17:40 -05:00
public function testOrWhere(): void
{
$query = self::$db->select('id, key as k, val')
2014-02-18 19:29:58 -05:00
->from('test')
->where(' id ', 1)
2016-10-13 21:55:23 -04:00
->orWhere('key >', 0)
->limit(2, 1)
->get();
2012-04-10 14:06:34 -04:00
$this->assertIsA($query, 'PDOStatement');
}
2019-12-10 12:17:40 -05:00
public function testLike(): void
{
$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
2019-12-10 12:17:40 -05:00
public function testOrLike(): void
{
$query = self::$db->from('test')
->like('key', 'og')
2016-10-13 21:55:23 -04:00
->orLike('key', 'val')
->get();
$this->assertIsA($query, 'PDOStatement');
}
2014-02-25 13:47:35 -05:00
2019-12-10 12:17:40 -05:00
public function testOrNotLike(): void
{
$query = self::$db->from('test')
->like('key', 'og', 'before')
2016-10-13 21:55:23 -04:00
->orNotLike('key', 'val')
->get();
$this->assertIsA($query, 'PDOStatement');
}
2014-02-25 13:47:35 -05:00
2019-12-10 12:17:40 -05:00
public function testNotLike(): void
{
$query = self::$db->from('test')
->like('key', 'og', 'before')
2016-10-13 21:55:23 -04:00
->notLike('key', 'val')
->get();
$this->assertIsA($query, 'PDOStatement');
}
2014-02-25 13:47:35 -05:00
2019-12-10 12:17:40 -05:00
public function testLikeBefore(): void
{
$query = self::$db->from('test')
->like('key', 'og', 'before')
->get();
$this->assertIsA($query, 'PDOStatement');
}
2014-02-25 13:47:35 -05:00
2019-12-10 12:17:40 -05:00
public function testLikeAfter(): void
{
$query = self::$db->from('test')
->like('key', 'og', 'after')
->get();
$this->assertIsA($query, 'PDOStatement');
}
2019-12-10 12:17:40 -05:00
public function testJoin(): void
{
$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');
}
2019-12-10 12:17:40 -05:00
public function testLeftJoin(): void
{
$query = self::$db->from('test ct')
->join('join cj', 'cj.id = ct.id', 'left')
->get();
$this->assertIsA($query, 'PDOStatement');
}
2019-12-10 12:17:40 -05:00
public function testInnerJoin(): void
{
$query = self::$db->from('test ct')
->join('join cj', 'cj.id = ct.id', 'inner')
->get();
$this->assertIsA($query, 'PDOStatement');
}
2019-12-10 12:17:40 -05:00
public function testJoinWithMultipleWhereValues(): void
{
$query = self::$db->from('test ct')
->join('join cj', 'cj.id=ct.id', 'inner')
2018-01-26 08:39:30 -05:00
->where([
'ct.id < ' => 3,
'ct.key' => 'foo'
2018-01-26 08:39:30 -05:00
])
->get();
$this->assertIsA($query, 'PDOStatement');
}
// ! DB update tests
2019-12-10 12:17:40 -05:00
public function testInsert(): void
{
$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');
2018-01-22 15:43:56 -05:00
$this->assertTrue(self::$db->affectedRows() > 0);
}
2014-02-25 13:47:35 -05:00
2019-12-10 12:17:40 -05:00
public function testInsertArray(): void
{
2018-01-26 08:39:30 -05:00
$query = self::$db->insert('test', [
'id' => 587,
'key' => 1,
'val' => 2,
2018-01-26 08:39:30 -05:00
]);
$this->assertIsA($query, 'PDOStatement');
}
2014-02-25 13:47:35 -05:00
public function testInsertReturning(): void
{
$query = self::$db->set('id', 99)
->set('key', 84)
->set('val', 120)
->returning()
->insert('test');
$row = $query->fetch(PDO::FETCH_ASSOC);
$this->assertIsA($query, 'PDOStatement');
$this->assertEqual([
'id' => 99,
'key' => 84,
'val' => 120,
], $row);
}
2019-12-10 12:17:40 -05:00
public function testInsertBatch(): void
{
2018-01-26 08:39:30 -05:00
$data = [
[
'id' => 544,
'key' => 3,
'val' => 7,
2018-01-26 08:39:30 -05:00
],
[
'id' => 890,
'key' => 34,
'val' => "10 o'clock",
2018-01-26 08:39:30 -05:00
],
[
'id' => 480,
'key' => 403,
'val' => 97,
2018-01-26 08:39:30 -05:00
],
];
2016-10-13 21:55:23 -04:00
$query = self::$db->insertBatch('test', $data);
$this->assertIsA($query, 'PDOStatement');
}
2019-12-10 12:17:40 -05:00
public function testUpdate(): void
{
$query = self::$db->where('id', 7)
2018-01-26 08:39:30 -05:00
->update('test', [
'id' => 7,
'key' => 'gogle',
'val' => 'non-word'
2018-01-26 08:39:30 -05:00
]);
2012-04-10 14:06:34 -04:00
$this->assertIsA($query, 'PDOStatement');
}
2018-01-22 15:43:56 -05:00
public function testUpdateReturning(): void
{
// Make sure the id exists so there isn't an error for that reason
self::$db->insert('test', [
'id' => 7,
'key' => 'kjsgarik yugasdikh',
'val' => 'alkfiasghdfdhs',
]);
$query = self::$db->where('id', 7)
->set([
'id' => 7,
'key' => 'gogle',
'val' => 'non-word'
])
->returning('key')
->update('test');
$this->assertIsA($query, 'PDOStatement');
$row = $query->fetch(PDO::FETCH_ASSOC);
$this->assertEqual([
'key' => 'gogle',
], $row, json_encode($query->errorInfo()));
// $this->assertNotEqual(FALSE, $row, $query->errorInfo());
}
2019-12-10 12:17:40 -05:00
public function testUpdateBatchNull(): void
2018-01-22 15:43:56 -05:00
{
$query = self::$db->updateBatch('test', [], '');
$this->assertNull($query);
}
2019-12-10 12:17:40 -05:00
public function testDriverUpdateBatch(): void
2018-01-26 08:39:30 -05:00
{
$data = [
[
'id' => 480,
'key' => 49,
'val' => '7x7'
],
[
'id' => 890,
'key' => 100,
'val' => '10x10'
]
];
$affectedRows = self::$db->updateBatch('test', $data, 'id');
$this->assertEquals(2, $affectedRows);
}
2019-12-10 12:17:40 -05:00
public function testSetArrayUpdate(): void
2012-10-24 20:19:39 -04:00
{
2018-01-26 08:39:30 -05:00
$array = [
'id' => 22,
2012-10-24 20:19:39 -04:00
'key' => 'gogle',
'val' => 'non-word'
2018-01-26 08:39:30 -05:00
];
2012-10-24 20:19:39 -04:00
$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');
}
2019-12-10 12:17:40 -05:00
public function testWhereSetUpdate(): void
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');
}
2019-12-10 12:17:40 -05:00
public function testDelete(): void
{
2018-01-26 08:39:30 -05:00
$query = self::$db->delete('test', ['id' => 5]);
2012-04-10 14:06:34 -04:00
$this->assertIsA($query, 'PDOStatement');
}
public function testDeleteReturning():void
{
$query = self::$db->returning()->delete('test', ['id' => 99]);
$this->assertIsA($query, 'PDOStatement');
}
2019-12-10 12:17:40 -05:00
public function testDeleteWithMultipleWhereValues(): void
{
2018-01-26 08:39:30 -05:00
$query = self::$db->delete('test', [
'id' => 5,
'key' => 'gogle'
2018-01-26 08:39:30 -05:00
]);
$this->assertIsA($query, 'PDOStatement');
}
2016-10-13 21:55:23 -04:00
// ! Non-data read queries
2019-12-10 12:17:40 -05:00
public function testCountAll(): void
2012-04-10 14:06:34 -04:00
{
2016-10-13 21:55:23 -04:00
$query = self::$db->countAll('test');
$this->assertTrue(is_numeric($query));
2012-04-10 14:06:34 -04:00
}
2019-12-10 12:17:40 -05:00
public function testCountAllResults(): void
{
2016-10-13 21:55:23 -04:00
$query = self::$db->countAllResults('test');
$this->assertTrue(is_numeric($query));
}
2019-12-10 12:17:40 -05:00
public function testCountAllResults2(): void
{
$query = self::$db->select('id, key as k, val')
2012-11-07 08:42:34 -05:00
->from('test')
->where(' id ', 1)
2016-10-13 21:55:23 -04:00
->orWhere('key >', 0)
->limit(2, 1)
2016-10-13 21:55:23 -04:00
->countAllResults();
$this->assertTrue(is_numeric($query));
}
2019-12-10 12:17:40 -05:00
public function testNumRows(): void
{
2016-10-13 21:55:23 -04:00
self::$db->get('test');
$this->assertTrue(is_numeric(self::$db->numRows()));
}
2016-10-13 21:55:23 -04:00
2014-02-14 22:08:19 -05:00
// ! Compiled Query tests
2019-12-10 12:17:40 -05:00
public function testGetCompiledSelect(): void
{
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
2019-12-10 12:17:40 -05:00
$this->assertIsA($qbRes,'PDOStatement', 'Query Builder Result is a PDO Statement');
$this->assertIsA($sqlRes, 'PDOStatement', 'SQL Result is a PDO Statement');
2016-10-13 21:55:23 -04:00
//$this->assertEquals($qbRes, $sqlRes);
}
2014-02-25 13:47:35 -05:00
2019-12-10 12:17:40 -05:00
public function testGetCompiledUpdate(): void
{
2018-01-26 08:39:30 -05:00
$sql = self::$db->set([
'id' => 4,
'key' => 'foo',
'val' => 'baz'
2018-01-26 08:39:30 -05:00
])->getCompiledUpdate('test');
2014-02-25 13:47:35 -05:00
$this->assertTrue(\is_string($sql));
}
2014-02-25 13:47:35 -05:00
2019-12-10 12:17:40 -05:00
public function testGetCompiledInsert(): void
{
2018-01-26 08:39:30 -05:00
$sql = self::$db->set([
'id' => 4,
'key' => 'foo',
'val' => 'baz'
2018-01-26 08:39:30 -05:00
])->getCompiledInsert('test');
2014-02-25 13:47:35 -05:00
$this->assertTrue(\is_string($sql));
}
2014-02-25 13:47:35 -05:00
2019-12-10 12:17:40 -05:00
public function testGetCompiledDelete(): void
{
$sql = self::$db->where('id', 4)
2016-10-13 21:55:23 -04:00
->getCompiledDelete('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
*/
2019-12-10 12:17:40 -05:00
public function testBadDriver(): void
2012-07-26 13:21:24 -04:00
{
2018-01-26 08:39:30 -05:00
$params = [
2012-07-26 13:21:24 -04:00
'host' => '127.0.0.1',
'port' => '3306',
'database' => 'test',
'user' => 'root',
'pass' => NULL,
'type' => 'QGYFHGEG'
2018-01-26 08:39:30 -05:00
];
2014-02-25 13:47:35 -05:00
2018-01-26 08:39:30 -05:00
$this->expectException(BadDBDriverException::class);
2016-10-13 21:55:23 -04:00
self::$db = Query($params);
2012-07-26 13:21:24 -04:00
}
2019-12-10 12:17:40 -05:00
public function testBadMethod(): void
{
2018-01-26 08:39:30 -05:00
$this->expectException(BadMethodCallException::class);
2016-10-13 21:55:23 -04:00
self::$db->foo();
}
2014-02-25 13:47:35 -05:00
2019-12-10 12:17:40 -05:00
public function testBadNumRows(): void
{
2018-01-26 08:39:30 -05:00
self::$db->set([
'id' => 999,
'key' => 'ring',
'val' => 'sale'
2018-01-26 08:39:30 -05:00
])->insert('test');
2014-02-25 13:47:35 -05:00
2016-10-13 21:55:23 -04:00
$res = self::$db->numRows();
$this->assertEqual(NULL, $res);
}
2016-10-13 21:55:23 -04:00
}