2015-05-06 19:38:31 +00:00
< ? php
2015-10-11 18:14:49 +00:00
/**
2016-05-07 17:22:09 +00:00
* FuzeWorks .
2015-10-11 18:14:49 +00:00
*
* 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
2016-05-07 17:22:09 +00:00
* @ copyright Copyright ( c ) 2013 - 2016 , Techfuze . ( http :// techfuze . net )
2015-10-11 18:14:49 +00:00
* @ copyright Copyright ( c ) 1996 - 2015 , Free Software Foundation , Inc . ( http :// www . fsf . org / )
* @ license http :// opensource . org / licenses / GPL - 3.0 GPLv3 License
2016-05-07 17:22:09 +00:00
*
2015-10-11 18:14:49 +00:00
* @ link http :// fuzeworks . techfuze . net
* @ since Version 0.0 . 1
2016-05-07 17:22:09 +00:00
*
2015-10-11 18:14:49 +00:00
* @ version Version 0.0 . 1
*/
2015-09-06 13:42:57 +00:00
use \FuzeWorks\Modules ;
use \Module\DatabaseUtils\Query ;
2015-05-06 19:38:31 +00:00
2016-05-07 17:22:09 +00:00
class QueryTests extends \CoreTestAbstract
{
2015-05-06 19:38:31 +00:00
/**
* @ var Query
*/
public $query ;
2015-09-06 13:42:57 +00:00
public $core ;
2015-05-06 19:38:31 +00:00
/**
* @ before
*/
2016-05-07 17:22:09 +00:00
protected function setUp ()
{
2015-09-06 13:42:57 +00:00
Modules :: get ( 'core/databaseutils' );
$this -> query = new Query ();
}
2016-05-07 17:22:09 +00:00
public function testConstructor ()
{
2015-09-06 13:42:57 +00:00
$query = new Query ( 'table' );
$this -> assertEquals ( 'table' , $query -> getTable ());
2015-05-06 19:38:31 +00:00
}
/*
* 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 ());
2015-05-06 19:38:31 +00:00
}
2016-05-07 17:22:09 +00:00
public function testSelectSimpleDefaultTable ()
{
$this -> query -> setTable ( 'table' ) -> select () -> from ();
2015-09-06 13:42:57 +00:00
$this -> assertEquals ( 'SELECT * FROM table' , $this -> query -> getSql ());
2015-05-06 19:38:31 +00:00
}
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
}
2016-05-07 17:22:09 +00:00
public function testSelectSimpleOneField ()
{
2015-05-06 19:38:31 +00:00
$this -> query -> select ( 'field1' ) -> from ( 'table' );
2015-09-06 13:42:57 +00:00
$this -> assertEquals ( 'SELECT field1 FROM table' , $this -> query -> getSql ());
2015-05-06 19:38:31 +00:00
}
2016-05-07 17:22:09 +00:00
public function testSelectSimpleTwoFields ()
{
2015-05-06 19:38:31 +00:00
$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 ());
2015-05-06 19:38:31 +00:00
}
/*
* Where
*/
2016-05-07 17:22:09 +00:00
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 ());
2015-05-06 19:38:31 +00:00
$this -> assertEquals ( array ( 'value' ), $this -> query -> getBinds ());
}
2016-05-07 17:22:09 +00:00
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 ());
2015-05-06 19:38:31 +00:00
$this -> assertEquals ( array ( '%value%' ), $this -> query -> getBinds ());
}
2016-05-07 17:22:09 +00:00
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 ());
2015-05-06 19:38:31 +00:00
$this -> assertEquals ( array ( 2 , 4 ), $this -> query -> getBinds ());
}
2016-05-07 17:22:09 +00:00
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 ());
2015-05-06 19:38:31 +00:00
$this -> assertEquals ( array ( 2 , 3 , 4 ), $this -> query -> getBinds ());
}
2015-09-06 13:42:57 +00:00
/**
* @ todo Make this work !
2016-05-07 17:22:09 +00:00
*
2015-09-06 13:42:57 +00:00
* @ 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 ());
} */
2016-05-07 17:22:09 +00:00
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 ());
2015-05-06 19:38:31 +00:00
$this -> assertEquals ( array ( 'value' ), $this -> query -> getBinds ());
}
2015-09-06 13:42:57 +00:00
2016-05-07 17:22:09 +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 ());
2015-05-06 19:38:31 +00:00
$this -> assertEquals ( array ( 'value' ), $this -> query -> getBinds ());
}
2015-09-06 13:42:57 +00:00
2016-05-07 17:22:09 +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 ());
2015-05-06 19:38:31 +00:00
$this -> assertEquals ( array ( 'value' ), $this -> query -> getBinds ());
}
2015-09-06 13:42:57 +00:00
2016-05-07 17:22:09 +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 ());
2015-05-06 19:38:31 +00:00
$this -> assertEquals ( array ( 'value' ), $this -> query -> getBinds ());
}
2015-09-06 13:42:57 +00:00
2016-05-07 17:22:09 +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 ());
2015-05-06 19:38:31 +00:00
$this -> assertEquals ( array ( 'value' ), $this -> query -> getBinds ());
}
2015-09-06 13:42:57 +00:00
2016-05-07 17:22:09 +00:00
public function testSelectWhereAnd ()
{
$this -> query -> select () -> from ( 'table' ) -> where ( 'field1' , 'value1' ) -> and () -> where ( 'field2' , 'value2' );
2015-09-06 13:42:57 +00:00
$this -> assertEquals ( 'SELECT * FROM table WHERE field1 = ? AND field2 = ?' , $this -> query -> getSql ());
2015-05-06 19:38:31 +00:00
$this -> assertEquals ( array ( 'value1' , 'value2' ), $this -> query -> getBinds ());
}
2016-05-07 17:22:09 +00:00
public function testSelectWhereAndOpen ()
{
$this -> query -> select () -> from ( 'table' ) -> where ( 'field1' , 'value1' ) -> and () -> open () -> where ( 'field2' , 'value2' ) -> or () -> where ( 'field3' , 'value3' ) -> close ();
2015-09-06 13:42:57 +00:00
$this -> assertEquals ( 'SELECT * FROM table WHERE field1 = ? AND ( field2 = ? OR field3 = ? )' , $this -> query -> getSql ());
2015-05-06 19:38:31 +00:00
$this -> assertEquals ( array ( 'value1' , 'value2' , 'value3' ), $this -> query -> getBinds ());
}
2016-05-07 17:22:09 +00:00
public function testSelectWhereOr ()
{
$this -> query -> select () -> from ( 'table' ) -> where ( 'field1' , 'value1' ) -> or () -> where ( 'field2' , 'value2' );
2015-09-06 13:42:57 +00:00
$this -> assertEquals ( 'SELECT * FROM table WHERE field1 = ? OR field2 = ?' , $this -> query -> getSql ());
2015-05-06 19:38:31 +00:00
$this -> assertEquals ( array ( 'value1' , 'value2' ), $this -> query -> getBinds ());
}
2016-05-07 17:22:09 +00:00
public function testSelectWhereOrOpen ()
{
$this -> query -> select () -> from ( 'table' ) -> where ( 'field1' , 'value1' ) -> or () -> open () -> where ( 'field2' , 'value2' ) -> and () -> where ( 'field3' , 'value3' ) -> close ();
2015-09-06 13:42:57 +00:00
$this -> assertEquals ( 'SELECT * FROM table WHERE field1 = ? OR ( field2 = ? AND field3 = ? )' , $this -> query -> getSql ());
2015-05-06 19:38:31 +00:00
$this -> assertEquals ( array ( 'value1' , 'value2' , 'value3' ), $this -> query -> getBinds ());
}
2016-05-07 17:22:09 +00:00
public function testSelectWhereOpen ()
{
2015-05-06 19:38:31 +00:00
$this -> query -> select () -> from ( 'table' )
2016-05-07 17:22:09 +00:00
-> where () -> open () -> where ( 'field1' , 'value1' ) -> and () -> where ( 'field2' , 'value2' ) -> close ()
2015-09-06 13:42:57 +00:00
-> or () -> where ( 'field3' , 'value3' );
$this -> assertEquals ( 'SELECT * FROM table WHERE ( field1 = ? AND field2 = ? ) OR field3 = ?' , $this -> query -> getSql ());
2015-05-06 19:38:31 +00:00
$this -> assertEquals ( array ( 'value1' , 'value2' , 'value3' ), $this -> query -> getBinds ());
}
2015-09-06 13:42:57 +00:00
2015-05-06 19:38:31 +00:00
/*
* Order by
*/
2015-09-06 13:42:57 +00:00
2016-05-07 17:22:09 +00:00
public function testSelectOrderASC ()
{
2015-05-06 19:38:31 +00:00
$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 ());
2015-05-06 19:38:31 +00:00
}
2016-05-07 17:22:09 +00:00
public function testSelectOrderDESC ()
{
2015-05-06 19:38:31 +00:00
$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 ());
2015-05-06 19:38:31 +00:00
}
2016-05-07 17:22:09 +00:00
public function testSelectOrderMultiple ()
{
2015-05-06 19:38:31 +00:00
$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-05-06 19:38:31 +00:00
}
2015-09-06 13:42:57 +00:00
2015-05-06 19:38:31 +00:00
/*
* Limit
*/
2015-09-06 13:42:57 +00:00
2015-05-06 19:38:31 +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 ());
2015-05-06 19:38:31 +00:00
}
/*
* Having
*/
2016-05-07 17:22:09 +00:00
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 ());
2015-05-06 19:38:31 +00:00
$this -> assertEquals ( array ( 'value' ), $this -> query -> getBinds ());
}
2016-05-07 17:22:09 +00:00
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 ());
2015-05-06 19:38:31 +00:00
$this -> assertEquals ( array ( '%value%' ), $this -> query -> getBinds ());
}
2016-05-07 17:22:09 +00:00
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 ());
2015-05-06 19:38:31 +00:00
$this -> assertEquals ( array ( 2 , 4 ), $this -> query -> getBinds ());
}
2016-05-07 17:22:09 +00:00
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 ());
2015-05-06 19:38:31 +00:00
$this -> assertEquals ( array ( 2 , 3 , 4 ), $this -> query -> getBinds ());
}
2016-05-07 17:22:09 +00:00
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 ());
2015-05-06 19:38:31 +00:00
$this -> assertEquals ( array ( 'value' ), $this -> query -> getBinds ());
}
2016-05-07 17:22:09 +00:00
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 ());
2015-05-06 19:38:31 +00:00
$this -> assertEquals ( array ( 'value' ), $this -> query -> getBinds ());
}
2016-05-07 17:22:09 +00:00
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 ());
2015-05-06 19:38:31 +00:00
$this -> assertEquals ( array ( 'value' ), $this -> query -> getBinds ());
}
2016-05-07 17:22:09 +00:00
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 ());
2015-05-06 19:38:31 +00:00
$this -> assertEquals ( array ( 'value' ), $this -> query -> getBinds ());
}
2016-05-07 17:22:09 +00:00
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 ());
2015-05-06 19:38:31 +00:00
$this -> assertEquals ( array ( 'value' ), $this -> query -> getBinds ());
}
2016-05-07 17:22:09 +00:00
public function testSelectHavingAnd ()
{
$this -> query -> select () -> from ( 'table' ) -> having ( 'field1' , 'value1' ) -> and () -> where ( 'field2' , 'value2' );
2015-09-06 13:42:57 +00:00
$this -> assertEquals ( 'SELECT * FROM table HAVING field1 = ? AND field2 = ?' , $this -> query -> getSql ());
2015-05-06 19:38:31 +00:00
$this -> assertEquals ( array ( 'value1' , 'value2' ), $this -> query -> getBinds ());
}
2016-05-07 17:22:09 +00:00
public function testSelectHavingAndOpen ()
{
$this -> query -> select () -> from ( 'table' ) -> having ( 'field1' , 'value1' ) -> and () -> open () -> where ( 'field2' , 'value2' ) -> or () -> where ( 'field3' , 'value3' ) -> close ();
2015-09-06 13:42:57 +00:00
$this -> assertEquals ( 'SELECT * FROM table HAVING field1 = ? AND ( field2 = ? OR field3 = ? )' , $this -> query -> getSql ());
2015-05-06 19:38:31 +00:00
$this -> assertEquals ( array ( 'value1' , 'value2' , 'value3' ), $this -> query -> getBinds ());
}
2016-05-07 17:22:09 +00:00
public function testSelectHavingOr ()
{
$this -> query -> select () -> from ( 'table' ) -> having ( 'field1' , 'value1' ) -> or () -> where ( 'field2' , 'value2' );
2015-09-06 13:42:57 +00:00
$this -> assertEquals ( 'SELECT * FROM table HAVING field1 = ? OR field2 = ?' , $this -> query -> getSql ());
2015-05-06 19:38:31 +00:00
$this -> assertEquals ( array ( 'value1' , 'value2' ), $this -> query -> getBinds ());
}
2016-05-07 17:22:09 +00:00
public function testSelectHavingOrOpen ()
{
$this -> query -> select () -> from ( 'table' ) -> having ( 'field1' , 'value1' ) -> or () -> open () -> where ( 'field2' , 'value2' ) -> and () -> where ( 'field3' , 'value3' ) -> close ();
2015-09-06 13:42:57 +00:00
$this -> assertEquals ( 'SELECT * FROM table HAVING field1 = ? OR ( field2 = ? AND field3 = ? )' , $this -> query -> getSql ());
2015-05-06 19:38:31 +00:00
$this -> assertEquals ( array ( 'value1' , 'value2' , 'value3' ), $this -> query -> getBinds ());
}
2016-05-07 17:22:09 +00:00
public function testSelectHavingOpen ()
{
2015-05-06 19:38:31 +00:00
$this -> query -> select () -> from ( 'table' )
2016-05-07 17:22:09 +00:00
-> having () -> open () -> where ( 'field1' , 'value1' ) -> and () -> where ( 'field2' , 'value2' ) -> close ()
2015-09-06 13:42:57 +00:00
-> or () -> where ( 'field3' , 'value3' );
$this -> assertEquals ( 'SELECT * FROM table HAVING ( field1 = ? AND field2 = ? ) OR field3 = ?' , $this -> query -> getSql ());
2015-05-06 19:38:31 +00:00
$this -> assertEquals ( array ( 'value1' , 'value2' , 'value3' ), $this -> query -> getBinds ());
}
/*
* Update
*/
2016-05-07 17:22:09 +00:00
public function testUpdateSimple ()
{
2016-01-23 13:06:16 +00:00
$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 ());
2015-05-06 19:38:31 +00:00
$this -> assertEquals ( array ( 'value' ), $this -> query -> getBinds ());
}
2016-05-07 17:22:09 +00:00
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 ());
2015-05-06 19:38:31 +00:00
$this -> assertEquals ( array ( 'value1' , 'value2' ), $this -> query -> getBinds ());
}
/*
* Delete
*/
2016-05-07 17:22:09 +00:00
public function testDeleteSimple ()
{
2016-01-23 13:06:16 +00:00
$this -> query -> setTable ( 'table' ) -> delete ();
2015-09-06 13:42:57 +00:00
$this -> assertEquals ( 'DELETE FROM table' , $this -> query -> getSql ());
2015-05-06 19:38:31 +00:00
}
/*
* Insert
*/
2016-05-07 17:22:09 +00:00
public function testInsertSimple ()
{
2015-05-06 19:38:31 +00:00
$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 ());
2015-05-06 19:38:31 +00:00
$this -> assertEquals ( array ( 'value' ), $this -> query -> getBinds ());
}
2016-05-07 17:22:09 +00:00
public function testInsertMultiple ()
{
2015-05-06 19:38:31 +00:00
$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
*/
2016-05-07 17:22:09 +00:00
public function testReplaceSimple ()
{
2016-01-23 13:06:16 +00:00
$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 ());
}
2016-05-07 17:22:09 +00:00
public function testReplaceMultiple ()
{
2016-01-23 13:06:16 +00:00
$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 ());
2015-05-06 19:38:31 +00:00
$this -> assertEquals ( array ( 'value1' , 'value2' ), $this -> query -> getBinds ());
}
2015-05-14 11:30:40 +00:00
/*
* Joins
*/
2016-05-07 17:22:09 +00:00
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 ());
}
2016-05-07 17:22:09 +00:00
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 ());
}
2016-05-07 17:22:09 +00:00
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 ());
}
2016-05-07 17:22:09 +00:00
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 ());
}
2016-05-07 17:22:09 +00:00
public function testJoinAdvanced ()
{
2015-05-14 11:30:40 +00:00
$this -> query -> select () -> from ( 'table' )
2016-05-07 17:22:09 +00:00
-> 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 ());
}
/**
2016-05-07 17:22:09 +00:00
* Inline joins .
2015-05-14 11:30:40 +00:00
*/
2016-05-07 17:22:09 +00:00
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 ());
}
2016-05-07 17:22:09 +00:00
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 ());
}
2016-05-07 17:22:09 +00:00
public function testJoinAllInOne ()
{
2015-05-14 11:30:40 +00:00
$this -> query -> select ()
-> from ( 'table t' , 'other o' )
-> left_join ( 'third th' ) -> on ( 'th.field = o.field' )
2016-05-07 17:22:09 +00:00
-> 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
/**
2016-05-07 17:22:09 +00:00
* Group by .
2015-09-06 13:42:57 +00:00
*/
2016-05-07 17:22:09 +00:00
public function testSelectGroupBy ()
{
2015-09-06 13:42:57 +00:00
$this -> query -> groupBy ( 'field1' , 'field2' );
$this -> assertEquals ( 'GROUP BY field1, field2' , $this -> query -> getSql ());
}
2016-05-07 17:22:09 +00:00
}