2008-08-09 06:38:44 +02:00
< ? php
class SQLQueryTest extends SapphireTest {
static $fixture_file = null ;
2010-04-12 04:03:16 +02:00
protected $extraDataObjects = array (
'SQLQueryTest_DO' ,
);
2008-08-09 06:38:44 +02:00
function testEmptyQueryReturnsNothing () {
$query = new SQLQuery ();
$this -> assertEquals ( '' , $query -> sql ());
}
function testSelectFromBasicTable () {
$query = new SQLQuery ();
$query -> from [] = " MyTable " ;
$this -> assertEquals ( " SELECT * FROM MyTable " , $query -> sql ());
$query -> from [] = " MyJoin " ;
2008-08-09 06:53:34 +02:00
$this -> assertEquals ( " SELECT * FROM MyTable MyJoin " , $query -> sql ());
2008-08-09 06:38:44 +02:00
}
function testSelectFromUserSpecifiedFields () {
$query = new SQLQuery ();
$query -> select = array ( " Name " , " Title " , " Description " );
$query -> from [] = " MyTable " ;
$this -> assertEquals ( " SELECT Name, Title, Description FROM MyTable " , $query -> sql ());
}
function testSelectWithWhereClauseFilter () {
$query = new SQLQuery ();
$query -> select = array ( " Name " , " Meta " );
$query -> from [] = " MyTable " ;
$query -> where [] = " Name = 'Name' " ;
$query -> where [] = " Meta = 'Test' " ;
$this -> assertEquals ( " SELECT Name, Meta FROM MyTable WHERE (Name = 'Name') AND (Meta = 'Test') " , $query -> sql ());
}
function testSelectWithConstructorParameters () {
$query = new SQLQuery ( array ( " Foo " , " Bar " ), " FooBarTable " );
$this -> assertEquals ( " SELECT Foo, Bar FROM FooBarTable " , $query -> sql ());
$query = new SQLQuery ( array ( " Foo " , " Bar " ), " FooBarTable " , array ( " Foo = 'Boo' " ));
$this -> assertEquals ( " SELECT Foo, Bar FROM FooBarTable WHERE (Foo = 'Boo') " , $query -> sql ());
}
function testSelectWithChainedMethods () {
$query = new SQLQuery ();
$query -> select ( " Name " , " Meta " ) -> from ( " MyTable " ) -> where ( " Name " , " Name " ) -> where ( " Meta " , " Test " );
$this -> assertEquals ( " SELECT Name, Meta FROM MyTable WHERE (Name = 'Name') AND (Meta = 'Test') " , $query -> sql ());
}
function testSelectWithChainedFilterParameters () {
$query = new SQLQuery ();
$query -> select ( array ( " Name " , " Meta " )) -> from ( " MyTable " );
$query -> where ( " Name = 'Name' " ) -> where ( " Meta " , " Test " ) -> where ( " Beta " , " != " , " Gamma " );
$this -> assertEquals ( " SELECT Name, Meta FROM MyTable WHERE (Name = 'Name') AND (Meta = 'Test') AND (Beta != 'Gamma') " , $query -> sql ());
}
function testSelectWithPredicateFilters () {
$query = new SQLQuery ();
2008-08-09 07:57:44 +02:00
$query -> select ( array ( " Name " )) -> from ( " SQLQueryTest_DO " );
2008-08-09 06:38:44 +02:00
$match = new ExactMatchFilter ( " Name " , " Value " );
2008-08-09 07:57:44 +02:00
$match -> setModel ( 'SQLQueryTest_DO' );
2008-08-09 06:38:44 +02:00
$match -> apply ( $query );
2008-08-09 07:57:44 +02:00
2008-08-09 06:38:44 +02:00
$match = new PartialMatchFilter ( " Meta " , " Value " );
2008-08-09 07:57:44 +02:00
$match -> setModel ( 'SQLQueryTest_DO' );
2008-08-09 06:38:44 +02:00
$match -> apply ( $query );
2008-08-09 07:57:44 +02:00
2009-03-29 23:50:47 +02:00
$this -> assertEquals ( " SELECT Name FROM SQLQueryTest_DO WHERE ( \" SQLQueryTest_DO \" . \" Name \" = 'Value') AND ( \" SQLQueryTest_DO \" . \" Meta \" LIKE '%Value%') " , $query -> sql ());
2008-08-09 06:38:44 +02:00
}
function testSelectWithLimitClause () {
2009-06-16 07:33:03 +02:00
// These are MySQL specific :-S
if ( DB :: getConn () instanceof MySQLDatabase ) {
// numeric limit
$query = new SQLQuery ();
$query -> from [] = " MyTable " ;
$query -> limit ( " 99 " );
$this -> assertEquals ( " SELECT * FROM MyTable LIMIT 99 " , $query -> sql ());
2008-08-09 07:57:44 +02:00
2009-06-16 07:33:03 +02:00
// array limit
$query = new SQLQuery ();
$query -> from [] = " MyTable " ;
$query -> limit ( array ( 'limit' => 99 ));
$this -> assertEquals ( " SELECT * FROM MyTable LIMIT 99 " , $query -> sql ());
2008-08-09 07:57:44 +02:00
2009-06-16 07:33:03 +02:00
// array limit with start (MySQL specific)
$query = new SQLQuery ();
$query -> from [] = " MyTable " ;
$query -> limit ( array ( 'limit' => 99 , 'start' => 97 ));
$this -> assertEquals ( " SELECT * FROM MyTable LIMIT 99 OFFSET 97 " , $query -> sql ());
}
2008-08-09 07:57:44 +02:00
}
function testSelectWithOrderbyClause () {
// numeric limit
$query = new SQLQuery ();
$query -> from [] = " MyTable " ;
$query -> orderby ( 'MyName ASC' );
// can't escape as we don't know if ASC or DESC is appended
$this -> assertEquals ( " SELECT * FROM MyTable ORDER BY MyName ASC " , $query -> sql ());
// array limit
$query = new SQLQuery ();
$query -> from [] = " MyTable " ;
$query -> orderby ( array ( 'sort' => 'MyName' ));
2008-11-22 04:51:04 +01:00
$this -> assertEquals ( 'SELECT * FROM MyTable ORDER BY "MyName"' , $query -> sql ());
2008-08-09 07:57:44 +02:00
// array limit with start (MySQL specific)
$query = new SQLQuery ();
$query -> from [] = " MyTable " ;
$query -> orderby ( array ( 'sort' => 'MyName' , 'dir' => 'desc' ));
2008-11-22 04:51:04 +01:00
$this -> assertEquals ( 'SELECT * FROM MyTable ORDER BY "MyName" DESC' , $query -> sql ());
2008-08-09 06:38:44 +02:00
}
2008-08-09 07:57:44 +02:00
function testSelectWithComplexOrderbyClause () {
// @todo Test "ORDER BY RANDOM() ASC,MyName DESC" etc.
}
2009-01-10 12:35:50 +01:00
function testFiltersOnID () {
$query = new SQLQuery ();
$query -> where [] = " ID = 5 " ;
$this -> assertTrue (
$query -> filtersOnID (),
" filtersOnID() is true with simple unquoted column name "
);
$query = new SQLQuery ();
$query -> where [] = " ID=5 " ;
$this -> assertTrue (
$query -> filtersOnID (),
" filtersOnID() is true with simple unquoted column name and no spaces in equals sign "
);
/*
$query = new SQLQuery ();
$query -> where [] = " Foo='Bar' AND ID=5 " ;
$this -> assertTrue (
$query -> filtersOnID (),
" filtersOnID() is true with combined SQL statements "
);
*/
$query = new SQLQuery ();
$query -> where [] = " Identifier = 5 " ;
$this -> assertFalse (
$query -> filtersOnID (),
" filtersOnID() is false with custom column name (starting with 'id') "
);
$query = new SQLQuery ();
$query -> where [] = " ParentID = 5 " ;
$this -> assertFalse (
$query -> filtersOnID (),
" filtersOnID() is false with column name ending in 'ID' "
);
$query = new SQLQuery ();
$query -> where [] = " MyTable.ID = 5 " ;
$this -> assertTrue (
$query -> filtersOnID (),
" filtersOnID() is true with table and column name "
);
$query = new SQLQuery ();
2009-09-30 00:03:30 +02:00
$query -> where [] = " MyTable.ID= 5 " ;
2009-01-10 12:35:50 +01:00
$this -> assertTrue (
$query -> filtersOnID (),
" filtersOnID() is true with table and quoted column name "
);
}
function testFiltersOnFK () {
$query = new SQLQuery ();
$query -> where [] = " ID = 5 " ;
$this -> assertFalse (
$query -> filtersOnFK (),
" filtersOnFK() is true with simple unquoted column name "
);
$query = new SQLQuery ();
$query -> where [] = " Identifier = 5 " ;
$this -> assertFalse (
$query -> filtersOnFK (),
" filtersOnFK() is false with custom column name (starting with 'id') "
);
$query = new SQLQuery ();
$query -> where [] = " MyTable.ParentID = 5 " ;
$this -> assertTrue (
$query -> filtersOnFK (),
" filtersOnFK() is true with table and column name "
);
$query = new SQLQuery ();
$query -> where [] = " MyTable.`ParentID`= 5 " ;
$this -> assertTrue (
$query -> filtersOnFK (),
" filtersOnFK() is true with table and quoted column name "
);
}
2008-08-09 07:57:44 +02:00
}
class SQLQueryTest_DO extends DataObject implements TestOnly {
static $db = array (
" Name " => " Varchar " ,
" Meta " => " Varchar " ,
);
2008-08-09 06:38:44 +02:00
}
2009-04-29 01:52:15 +02:00
?>