2016-10-12 22:12:25 -04:00
|
|
|
<?php declare(strict_types=1);
|
2012-08-02 11:59:11 -04:00
|
|
|
/**
|
|
|
|
* Query
|
|
|
|
*
|
2016-10-12 22:12:25 -04:00
|
|
|
* SQL Query Builder / Database Abstraction Layer
|
2012-08-02 11:59:11 -04:00
|
|
|
*
|
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
|
2012-08-02 11:59:11 -04:00
|
|
|
*/
|
2018-01-19 16:48:52 -05:00
|
|
|
namespace Query\Tests;
|
2012-08-02 11:59:11 -04:00
|
|
|
|
2018-01-19 16:48:52 -05:00
|
|
|
use Query\QueryParser;
|
|
|
|
use Query\Drivers\Sqlite\Driver;
|
2012-08-02 11:59:11 -04:00
|
|
|
|
2012-08-09 12:15:36 -04:00
|
|
|
/**
|
|
|
|
* Tests for the Query Parser
|
|
|
|
*/
|
2018-01-19 16:48:52 -05:00
|
|
|
class QueryParserTest extends TestCase {
|
2018-01-22 15:43:56 -05:00
|
|
|
/**
|
|
|
|
* @var QueryParser
|
|
|
|
*/
|
|
|
|
protected $parser;
|
2012-08-02 11:59:11 -04:00
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
public function setUp(): void
|
2012-08-02 11:59:11 -04:00
|
|
|
{
|
2018-01-19 16:48:52 -05:00
|
|
|
$db = new Driver('sqlite::memory:');
|
|
|
|
$this->parser = new QueryParser($db);
|
2012-08-02 11:59:11 -04:00
|
|
|
}
|
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
public function testGeneric(): void
|
2012-08-02 11:59:11 -04:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
$matches = $this->parser->parseJoin('table1.field1=table2.field2');
|
2018-01-26 08:39:30 -05:00
|
|
|
$this->assertEqual($matches['combined'], [
|
2012-08-09 12:15:36 -04:00
|
|
|
'table1.field1', '=', 'table2.field2'
|
2018-01-26 08:39:30 -05:00
|
|
|
]);
|
2012-08-02 11:59:11 -04:00
|
|
|
}
|
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
public function testGeneric2(): void
|
2012-08-02 11:59:11 -04:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
$matches = $this->parser->parseJoin('db1.table1.field1!=db2.table2.field2');
|
2018-01-26 08:39:30 -05:00
|
|
|
$this->assertEqual($matches['combined'], [
|
2012-08-09 12:15:36 -04:00
|
|
|
'db1.table1.field1','!=','db2.table2.field2'
|
2018-01-26 08:39:30 -05:00
|
|
|
]);
|
2012-08-09 12:15:36 -04:00
|
|
|
}
|
2012-08-02 11:59:11 -04:00
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
public function testWUnderscore(): void
|
2012-08-09 12:15:36 -04:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
$matches = $this->parser->parseJoin('table_1.field1 = tab_le2.field_2');
|
2018-01-26 08:39:30 -05:00
|
|
|
$this->assertEqual($matches['combined'], [
|
2012-08-09 12:15:36 -04:00
|
|
|
'table_1.field1', '=', 'tab_le2.field_2'
|
2018-01-26 08:39:30 -05:00
|
|
|
]);
|
2012-08-02 11:59:11 -04:00
|
|
|
}
|
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
public function testFunction(): void
|
2012-08-09 12:15:36 -04:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
$matches = $this->parser->parseJoin('table1.field1 > SUM(3+5)');
|
2018-01-26 08:39:30 -05:00
|
|
|
$this->assertEqual($matches['combined'], [
|
2012-08-09 12:15:36 -04:00
|
|
|
'table1.field1', '>', 'SUM(3+5)'
|
2018-01-26 08:39:30 -05:00
|
|
|
]);
|
2012-08-09 12:15:36 -04:00
|
|
|
}
|
2012-08-02 11:59:11 -04:00
|
|
|
}
|