Query/tests/QueryParserTest.php

70 lines
1.6 KiB
PHP
Raw Permalink Normal View History

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
*
2022-09-29 11:33:08 -04:00
* PHP version 8.1
2016-10-12 22:12:25 -04:00
*
* @package Query
2023-01-20 11:30:51 -05:00
* @author Timothy J. Warren <tim@timshome.page>
* @copyright 2012 - 2023 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
2023-03-17 16:34:21 -04:00
* @version 4.1.0
2012-08-02 11:59:11 -04:00
*/
2023-03-17 15:30:36 -04:00
namespace Query\Tests;
2012-08-02 11:59:11 -04:00
use Query\Drivers\Sqlite\Driver;
2023-03-17 15:30:36 -04:00
use Query\QueryParser;
2012-08-02 11:59:11 -04:00
2012-08-09 12:15:36 -04:00
/**
* Tests for the Query Parser
*/
class QueryParserTest extends BaseTestCase
2023-03-17 15:30:36 -04:00
{
2018-01-22 15:43:56 -05:00
/**
* @var QueryParser
*/
protected $parser;
2012-08-02 11:59:11 -04:00
2023-03-17 15:30:36 -04:00
protected function setUp(): void
2012-08-02 11:59:11 -04: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');
$this->assertEquals($matches['combined'], [
2023-03-17 15:30: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');
$this->assertEquals($matches['combined'], [
2023-03-17 15:30: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');
$this->assertEquals($matches['combined'], [
2023-03-17 15:30: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)');
$this->assertEquals($matches['combined'], [
2023-03-17 15:30:36 -04:00
'table1.field1', '>', 'SUM(3+5)',
2018-01-26 08:39:30 -05:00
]);
2012-08-09 12:15:36 -04:00
}
2023-03-17 15:30:36 -04:00
}