Core/tests/core_queryTest.php

504 lines
19 KiB
PHP
Raw Normal View History

<?php
/**
* FuzeWorks
*
* The FuzeWorks MVC PHP FrameWork
*
* Copyright (C) 2015 TechFuze
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author TechFuze
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
* @copyright Copyright (c) 1996 - 2015, Free Software Foundation, Inc. (http://www.fsf.org/)
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 License
* @link http://fuzeworks.techfuze.net
* @since Version 0.0.1
* @version Version 0.0.1
*/
2015-09-06 13:42:57 +00:00
use \FuzeWorks\Modules;
use \Module\DatabaseUtils\Query;
2015-09-06 13:42:57 +00:00
class QueryTests extends \CoreTestAbstract {
/**
* @var Query
*/
public $query;
2015-09-06 13:42:57 +00:00
public $core;
/**
* @before
*/
2015-09-06 13:42:57 +00:00
protected function setUp(){
Modules::get('core/databaseutils');
$this->query = new Query();
}
public function testConstructor(){
$query = new Query('table');
$this->assertEquals('table', $query->getTable());
}
/*
* Select
*/
public function testSelectSimple()
{
$this->query->select()->from('table');
2015-09-06 13:42:57 +00:00
$this->assertEquals('SELECT * FROM table', $this->query->getSql());
}
public function testSelectSimpleDefaultTable(){
2015-05-14 11:30:40 +00:00
$this->query->setTable("table")->select()->from();
2015-09-06 13:42:57 +00:00
$this->assertEquals('SELECT * FROM table', $this->query->getSql());
}
2015-05-14 11:30:40 +00:00
public function testSelectSimpleComboTable()
{
$this->query->select()->from('table', 'table2');
2015-09-06 13:42:57 +00:00
$this->assertEquals('SELECT * FROM table, table2', $this->query->getSql());
2015-05-14 11:30:40 +00:00
}
public function testSelectSimpleAlias()
{
$this->query->select()->from('table t');
2015-09-06 13:42:57 +00:00
$this->assertEquals('SELECT * FROM table t', $this->query->getSql());
2015-05-14 11:30:40 +00:00
}
public function testSelectSimpleComboAlias()
{
$this->query->select()->from('table t', 'table2 t2');
2015-09-06 13:42:57 +00:00
$this->assertEquals('SELECT * FROM table t, table2 t2', $this->query->getSql());
2015-05-14 11:30:40 +00:00
}
public function testSelectSimpleOneField(){
$this->query->select('field1')->from('table');
2015-09-06 13:42:57 +00:00
$this->assertEquals('SELECT field1 FROM table', $this->query->getSql());
}
public function testSelectSimpleTwoFields(){
$this->query->select('field1', 'field2')->from('table');
2015-09-06 13:42:57 +00:00
$this->assertEquals('SELECT field1, field2 FROM table', $this->query->getSql());
}
/*
* Where
*/
public function testSelectWhere(){
$this->query->select()->from('table')->where("field", "value");
2015-09-06 13:42:57 +00:00
$this->assertEquals('SELECT * FROM table WHERE field = ?', $this->query->getSql());
$this->assertEquals(array('value'), $this->query->getBinds());
}
public function testSelectWhereLike(){
$this->query->select()->from('table')->where("field", "like", '%value%');
2015-09-06 13:42:57 +00:00
$this->assertEquals('SELECT * FROM table WHERE field LIKE ?', $this->query->getSql());
$this->assertEquals(array('%value%'), $this->query->getBinds());
}
public function testSelectWhereBetween(){
$this->query->select()->from('table')->where("field", "between", array(2, 4));
2015-09-06 13:42:57 +00:00
$this->assertEquals('SELECT * FROM table WHERE field BETWEEN ? AND ?', $this->query->getSql());
$this->assertEquals(array(2, 4), $this->query->getBinds());
}
public function testSelectWhereIn(){
$this->query->select()->from('table')->where("field", "in", array(2, 3, 4));
2015-09-06 13:42:57 +00:00
$this->assertEquals('SELECT * FROM table WHERE field IN (?,?,?)', $this->query->getSql());
$this->assertEquals(array(2, 3, 4), $this->query->getBinds());
}
2015-09-06 13:42:57 +00:00
/**
* @todo Make this work!
* @return [type] [description]
*/
/*public function testSelectWhereInSubQuery(){
$subQuery = new Query();
$subQuery->select()->from('table2')->where('field', 'value');
$this->query->select()->from('table')->where("field", "in", $subQuery);
$this->assertEquals('SELECT * FROM table WHERE field IN (SELECT * FROM table2 WHERE field = ?)', $this->query->getSql());
$this->assertEquals(array('value'), $this->query->getBinds());
}*/
public function testSelectWhereNot(){
$this->query->select()->from('table')->where("field", "<>", "value");
2015-09-06 13:42:57 +00:00
$this->assertEquals('SELECT * FROM table WHERE field <> ?', $this->query->getSql());
$this->assertEquals(array('value'), $this->query->getBinds());
}
2015-09-06 13:42:57 +00:00
public function testSelectWhereGreater(){
$this->query->select()->from('table')->where("field", ">", "value");
2015-09-06 13:42:57 +00:00
$this->assertEquals('SELECT * FROM table WHERE field > ?', $this->query->getSql());
$this->assertEquals(array('value'), $this->query->getBinds());
}
2015-09-06 13:42:57 +00:00
public function testSelectWhereGreaterEqual(){
$this->query->select()->from('table')->where("field", ">=", "value");
2015-09-06 13:42:57 +00:00
$this->assertEquals('SELECT * FROM table WHERE field >= ?', $this->query->getSql());
$this->assertEquals(array('value'), $this->query->getBinds());
}
2015-09-06 13:42:57 +00:00
public function testSelectWhereSmaller(){
$this->query->select()->from('table')->where("field", "<", "value");
2015-09-06 13:42:57 +00:00
$this->assertEquals('SELECT * FROM table WHERE field < ?', $this->query->getSql());
$this->assertEquals(array('value'), $this->query->getBinds());
}
2015-09-06 13:42:57 +00:00
public function testSelectWhereSmallerEqual(){
$this->query->select()->from('table')->where("field", "<=", "value");
2015-09-06 13:42:57 +00:00
$this->assertEquals('SELECT * FROM table WHERE field <= ?', $this->query->getSql());
$this->assertEquals(array('value'), $this->query->getBinds());
}
2015-09-06 13:42:57 +00:00
public function testSelectWhereAnd(){
2015-09-06 13:42:57 +00:00
$this->query->select()->from('table')->where("field1", "value1")->and()->where("field2", "value2");
$this->assertEquals('SELECT * FROM table WHERE field1 = ? AND field2 = ?', $this->query->getSql());
$this->assertEquals(array('value1', 'value2'), $this->query->getBinds());
}
public function testSelectWhereAndOpen(){
2015-09-06 13:42:57 +00:00
$this->query->select()->from('table')->where("field1", "value1")->and()->open()->where("field2", "value2")->or()->where("field3", "value3")->close();
$this->assertEquals('SELECT * FROM table WHERE field1 = ? AND ( field2 = ? OR field3 = ? )', $this->query->getSql());
$this->assertEquals(array('value1', 'value2', 'value3'), $this->query->getBinds());
}
public function testSelectWhereOr(){
2015-09-06 13:42:57 +00:00
$this->query->select()->from('table')->where("field1", "value1")->or()->where("field2", "value2");
$this->assertEquals('SELECT * FROM table WHERE field1 = ? OR field2 = ?', $this->query->getSql());
$this->assertEquals(array('value1', 'value2'), $this->query->getBinds());
}
public function testSelectWhereOrOpen(){
2015-09-06 13:42:57 +00:00
$this->query->select()->from('table')->where("field1", "value1")->or()->open()->where("field2", "value2")->and()->where("field3", "value3")->close();
$this->assertEquals('SELECT * FROM table WHERE field1 = ? OR ( field2 = ? AND field3 = ? )', $this->query->getSql());
$this->assertEquals(array('value1', 'value2', 'value3'), $this->query->getBinds());
}
public function testSelectWhereOpen(){
$this->query->select()->from('table')
2015-09-06 13:42:57 +00:00
->where()->open()->where("field1", "value1")->and()->where("field2", "value2")->close()
->or()->where('field3', 'value3');
$this->assertEquals('SELECT * FROM table WHERE ( field1 = ? AND field2 = ? ) OR field3 = ?', $this->query->getSql());
$this->assertEquals(array('value1', 'value2', 'value3'), $this->query->getBinds());
}
2015-09-06 13:42:57 +00:00
/*
* Order by
*/
2015-09-06 13:42:57 +00:00
public function testSelectOrderASC(){
$this->query->select()->from('table')->order('field');
2015-09-06 13:42:57 +00:00
$this->assertEquals('SELECT * FROM table ORDER BY field ASC', $this->query->getSql());
}
public function testSelectOrderDESC(){
$this->query->select()->from('table')->order('-field');
2015-09-06 13:42:57 +00:00
$this->assertEquals('SELECT * FROM table ORDER BY field DESC', $this->query->getSql());
}
public function testSelectOrderMultiple(){
$this->query->select()->from('table')->order('field1', '-field2');
2015-09-06 13:42:57 +00:00
$this->assertEquals('SELECT * FROM table ORDER BY field1 ASC, field2 DESC', $this->query->getSql());
}
2015-09-06 13:42:57 +00:00
/*
* Limit
*/
2015-09-06 13:42:57 +00:00
public function testSelectLimit()
{
$this->query->select()->from('table')->limit(5, 10);
2015-09-06 13:42:57 +00:00
$this->assertEquals('SELECT * FROM table LIMIT 10, 5', $this->query->getSql());
}
/*
* Having
*/
public function testSelectHaving(){
$this->query->select()->from('table')->having("field", "value");
2015-09-06 13:42:57 +00:00
$this->assertEquals('SELECT * FROM table HAVING field = ?', $this->query->getSql());
$this->assertEquals(array('value'), $this->query->getBinds());
}
public function testSelectHavingLike(){
$this->query->select()->from('table')->having("field", "like", '%value%');
2015-09-06 13:42:57 +00:00
$this->assertEquals('SELECT * FROM table HAVING field LIKE ?', $this->query->getSql());
$this->assertEquals(array('%value%'), $this->query->getBinds());
}
public function testSelectHavingBetween(){
$this->query->select()->from('table')->having("field", "between", array(2, 4));
2015-09-06 13:42:57 +00:00
$this->assertEquals('SELECT * FROM table HAVING field BETWEEN ? AND ?', $this->query->getSql());
$this->assertEquals(array(2, 4), $this->query->getBinds());
}
public function testSelectHavingIn(){
$this->query->select()->from('table')->having("field", "in", array(2, 3, 4));
2015-09-06 13:42:57 +00:00
$this->assertEquals('SELECT * FROM table HAVING field IN (?,?,?)', $this->query->getSql());
$this->assertEquals(array(2, 3, 4), $this->query->getBinds());
}
public function testSelectHavingNot(){
$this->query->select()->from('table')->having("field", "<>", "value");
2015-09-06 13:42:57 +00:00
$this->assertEquals('SELECT * FROM table HAVING field <> ?', $this->query->getSql());
$this->assertEquals(array('value'), $this->query->getBinds());
}
public function testSelectHavingGreater(){
$this->query->select()->from('table')->having("field", ">", "value");
2015-09-06 13:42:57 +00:00
$this->assertEquals('SELECT * FROM table HAVING field > ?', $this->query->getSql());
$this->assertEquals(array('value'), $this->query->getBinds());
}
public function testSelectHavingGreaterEqual(){
$this->query->select()->from('table')->having("field", ">=", "value");
2015-09-06 13:42:57 +00:00
$this->assertEquals('SELECT * FROM table HAVING field >= ?', $this->query->getSql());
$this->assertEquals(array('value'), $this->query->getBinds());
}
public function testSelectHavingSmaller(){
$this->query->select()->from('table')->having("field", "<", "value");
2015-09-06 13:42:57 +00:00
$this->assertEquals('SELECT * FROM table HAVING field < ?', $this->query->getSql());
$this->assertEquals(array('value'), $this->query->getBinds());
}
public function testSelectHavingSmallerEqual(){
$this->query->select()->from('table')->having("field", "<=", "value");
2015-09-06 13:42:57 +00:00
$this->assertEquals('SELECT * FROM table HAVING field <= ?', $this->query->getSql());
$this->assertEquals(array('value'), $this->query->getBinds());
}
public function testSelectHavingAnd(){
2015-09-06 13:42:57 +00:00
$this->query->select()->from('table')->having("field1", "value1")->and()->where("field2", "value2");
$this->assertEquals('SELECT * FROM table HAVING field1 = ? AND field2 = ?', $this->query->getSql());
$this->assertEquals(array('value1', 'value2'), $this->query->getBinds());
}
public function testSelectHavingAndOpen(){
2015-09-06 13:42:57 +00:00
$this->query->select()->from('table')->having("field1", "value1")->and()->open()->where("field2", "value2")->or()->where("field3", "value3")->close();
$this->assertEquals('SELECT * FROM table HAVING field1 = ? AND ( field2 = ? OR field3 = ? )', $this->query->getSql());
$this->assertEquals(array('value1', 'value2', 'value3'), $this->query->getBinds());
}
public function testSelectHavingOr(){
2015-09-06 13:42:57 +00:00
$this->query->select()->from('table')->having("field1", "value1")->or()->where("field2", "value2");
$this->assertEquals('SELECT * FROM table HAVING field1 = ? OR field2 = ?', $this->query->getSql());
$this->assertEquals(array('value1', 'value2'), $this->query->getBinds());
}
public function testSelectHavingOrOpen(){
2015-09-06 13:42:57 +00:00
$this->query->select()->from('table')->having("field1", "value1")->or()->open()->where("field2", "value2")->and()->where("field3", "value3")->close();
$this->assertEquals('SELECT * FROM table HAVING field1 = ? OR ( field2 = ? AND field3 = ? )', $this->query->getSql());
$this->assertEquals(array('value1', 'value2', 'value3'), $this->query->getBinds());
}
public function testSelectHavingOpen(){
$this->query->select()->from('table')
2015-09-06 13:42:57 +00:00
->having()->open()->where("field1", "value1")->and()->where("field2", "value2")->close()
->or()->where('field3', 'value3');
$this->assertEquals('SELECT * FROM table HAVING ( field1 = ? AND field2 = ? ) OR field3 = ?', $this->query->getSql());
$this->assertEquals(array('value1', 'value2', 'value3'), $this->query->getBinds());
}
/*
* Update
*/
public function testUpdateSimple(){
$this->query->setTable('table')->update()->set(array('field' => 'value'));
2015-09-06 13:42:57 +00:00
$this->assertEquals('UPDATE table SET field=?', $this->query->getSql());
$this->assertEquals(array('value'), $this->query->getBinds());
}
public function testUpdateMultiple(){
$this->query->setTable('table')->update()->set(array('field1' => 'value1', "field2" => 'value2'));
2015-09-06 13:42:57 +00:00
$this->assertEquals('UPDATE table SET field1=?, field2=?', $this->query->getSql());
$this->assertEquals(array('value1', 'value2'), $this->query->getBinds());
}
/*
* Delete
*/
public function testDeleteSimple(){
$this->query->setTable('table')->delete();
2015-09-06 13:42:57 +00:00
$this->assertEquals('DELETE FROM table', $this->query->getSql());
}
/*
* Insert
*/
public function testInsertSimple(){
$this->query->insert(array('field' => 'value'), 'table');
2015-09-06 13:42:57 +00:00
$this->assertEquals('INSERT INTO table (field) VALUES (?)', $this->query->getSql());
$this->assertEquals(array('value'), $this->query->getBinds());
}
public function testInsertMultiple(){
$this->query->insert(array('field1' => 'value1', 'field2' => 'value2'), 'table');
2015-09-06 13:42:57 +00:00
$this->assertEquals('INSERT INTO table (field1,field2) VALUES (?,?)', $this->query->getSql());
$this->assertEquals(array('value1', 'value2'), $this->query->getBinds());
}
/*
* Replace
*/
public function testReplaceSimple(){
$this->query->setTable('table')->replace(array('field' => 'value'));
2015-09-06 13:42:57 +00:00
$this->assertEquals('REPLACE INTO table (field) VALUES (?)', $this->query->getSql());
$this->assertEquals(array('value'), $this->query->getBinds());
}
public function testReplaceMultiple(){
$this->query->setTable('table')->replace(array('field1' => 'value1', 'field2' => 'value2'));
2015-09-06 13:42:57 +00:00
$this->assertEquals('REPLACE INTO table (field1,field2) VALUES (?,?)', $this->query->getSql());
$this->assertEquals(array('value1', 'value2'), $this->query->getBinds());
}
2015-05-14 11:30:40 +00:00
/*
* Joins
*/
public function testJoin(){
$this->query->select()->from('table')->join('other')->on("field", "value")->where("field", "value2");
2015-09-06 13:42:57 +00:00
$this->assertEquals('SELECT * FROM table JOIN other ON field = ? WHERE field = ?', $this->query->getSql());
2015-05-14 11:30:40 +00:00
$this->assertEquals(array('value', 'value2'), $this->query->getBinds());
}
public function testJoinLeft(){
$this->query->select()->from('table')->left_join('other')->on("field", "value")->where("field", "value2");
2015-09-06 13:42:57 +00:00
$this->assertEquals('SELECT * FROM table LEFT JOIN other ON field = ? WHERE field = ?', $this->query->getSql());
2015-05-14 11:30:40 +00:00
$this->assertEquals(array('value', 'value2'), $this->query->getBinds());
}
public function testJoinRight(){
$this->query->select()->from('table')->right_join('other')->on("field", "value")->where("field", "value2");
2015-09-06 13:42:57 +00:00
$this->assertEquals('SELECT * FROM table RIGHT JOIN other ON field = ? WHERE field = ?', $this->query->getSql());
2015-05-14 11:30:40 +00:00
$this->assertEquals(array('value', 'value2'), $this->query->getBinds());
}
public function testJoinFull(){
$this->query->select()->from('table')->full_join('other')->on("field", "value")->where("field", "value2");
2015-09-06 13:42:57 +00:00
$this->assertEquals('SELECT * FROM table FULL JOIN other ON field = ? WHERE field = ?', $this->query->getSql());
2015-05-14 11:30:40 +00:00
$this->assertEquals(array('value', 'value2'), $this->query->getBinds());
}
public function testJoinAdvanced(){
$this->query->select()->from('table')
->left_join('other_a')->on("field", "value")
->right_join('other_b')->on("field", "value2")
->full_join('other_c')->on("field", "value3")
->where("field", "value4");
2015-09-06 13:42:57 +00:00
$this->assertEquals('SELECT * FROM table LEFT JOIN other_a ON field = ? RIGHT JOIN other_b ON field = ? FULL JOIN other_c ON field = ? WHERE field = ?', $this->query->getSql());
2015-05-14 11:30:40 +00:00
$this->assertEquals(array('value', 'value2', 'value3', 'value4'), $this->query->getBinds());
}
/**
* Inline joins
*/
public function testJoinInline(){
$this->query->select()->from('table t', 'other o')->where("o.field", "value");
2015-09-06 13:42:57 +00:00
$this->assertEquals('SELECT * FROM table t, other o WHERE o.field = ?', $this->query->getSql());
2015-05-14 11:30:40 +00:00
$this->assertEquals(array('value'), $this->query->getBinds());
}
public function testJoinInlineAdvanced(){
$this->query->select()->from('table t', 'other o')->where("o.field = t.field");
2015-09-06 13:42:57 +00:00
$this->assertEquals('SELECT * FROM table t, other o WHERE o.field = t.field', $this->query->getSql());
2015-05-14 11:30:40 +00:00
$this->assertEmpty($this->query->getBinds());
}
public function testJoinAllInOne(){
$this->query->select()
->from('table t', 'other o')
->left_join('third th')->on('th.field = o.field')
->where("o.field = t.field")
2015-09-06 13:42:57 +00:00
->and()->where('t.thing', '>', 25);
$this->assertEquals('SELECT * FROM table t, other o LEFT JOIN third th ON th.field = o.field WHERE o.field = t.field AND t.thing > ?', $this->query->getSql());
2015-05-14 11:30:40 +00:00
$this->assertEquals(array(25), $this->query->getBinds());
}
2015-09-06 13:42:57 +00:00
/**
* Group by
*/
public function testSelectGroupBy(){
$this->query->groupBy('field1', 'field2');
$this->assertEquals('GROUP BY field1, field2', $this->query->getSql());
}
}