2006-10-03 20:03:19 +04:00
< ? php
2007-05-15 18:36:07 +04:00
/*
* $Id $
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* " AS IS " AND ANY EXPRESS OR IMPLIED WARRANTIES , INCLUDING , BUT NOT
* LIMITED TO , THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED . IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT , INDIRECT , INCIDENTAL ,
* SPECIAL , EXEMPLARY , OR CONSEQUENTIAL DAMAGES ( INCLUDING , BUT NOT
* LIMITED TO , PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ; LOSS OF USE ,
* DATA , OR PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY , OR TORT
* ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE .
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL . For more information , see
* < http :// www . phpdoctrine . com >.
*/
/**
* Doctrine_Query_Subquery_TestCase
* This test case is used for testing DQL WHERE part functionality
*
* @ package Doctrine
* @ author Konsta Vesterinen < kvesteri @ cc . hut . fi >
* @ license http :// www . opensource . org / licenses / lgpl - license . php LGPL
* @ category Object Relational Mapping
* @ link www . phpdoctrine . com
* @ since 1.0
* @ version $Revision $
*/
2007-05-15 19:08:23 +04:00
class Doctrine_Query_Where_TestCase extends Doctrine_UnitTestCase
{
2006-10-03 20:03:19 +04:00
public function prepareData () { }
public function prepareTables () {
$this -> tables = array ( 'entity' );
parent :: prepareTables ();
}
2006-10-10 22:09:20 +04:00
2007-05-15 19:08:23 +04:00
public function testDirectParameterSetting ()
{
2006-10-03 20:03:19 +04:00
$this -> connection -> clear ();
$user = new User ();
$user -> name = 'someone' ;
$user -> save ();
2007-05-16 23:20:55 +04:00
$q = new Doctrine_Query ();
2006-10-08 23:10:21 +04:00
2007-05-15 18:36:07 +04:00
$q -> from ( 'User' ) -> addWhere ( 'User.id = ?' , 1 );
2006-10-03 20:03:19 +04:00
$users = $q -> execute ();
2006-10-08 23:10:21 +04:00
2006-10-03 20:03:19 +04:00
$this -> assertEqual ( $users -> count (), 1 );
$this -> assertEqual ( $users [ 0 ] -> name , 'someone' );
}
2006-11-08 22:12:16 +03:00
2007-05-15 19:08:23 +04:00
public function testDirectMultipleParameterSetting ()
{
2006-10-03 20:03:19 +04:00
$user = new User ();
2006-10-08 23:10:21 +04:00
$user -> name = 'someone.2' ;
2006-10-03 20:03:19 +04:00
$user -> save ();
2007-05-16 23:20:55 +04:00
$q = new Doctrine_Query ();
2006-10-08 23:10:21 +04:00
2007-05-15 19:08:23 +04:00
$q -> from ( 'User' ) -> addWhere ( 'User.id IN (?, ?)' , array ( 1 , 2 ));
2006-10-03 20:03:19 +04:00
$users = $q -> execute ();
2006-10-08 23:10:21 +04:00
2006-10-03 20:03:19 +04:00
$this -> assertEqual ( $users -> count (), 2 );
$this -> assertEqual ( $users [ 0 ] -> name , 'someone' );
2006-10-08 23:10:21 +04:00
$this -> assertEqual ( $users [ 1 ] -> name , 'someone.2' );
}
2007-05-15 19:08:23 +04:00
public function testDirectMultipleParameterSetting2 ()
{
2007-05-16 23:20:55 +04:00
$q = new Doctrine_Query ();
2006-11-08 22:12:16 +03:00
2007-05-15 19:08:23 +04:00
$q -> from ( 'User' ) -> where ( 'User.id IN (?, ?)' , array ( 1 , 2 ));
$this -> assertEqual ( $q -> getQuery (), 'SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id FROM entity e WHERE e.id IN (?, ?) AND (e.type = 0)' );
2006-11-08 22:12:16 +03:00
$users = $q -> execute ();
$this -> assertEqual ( $users -> count (), 2 );
$this -> assertEqual ( $users [ 0 ] -> name , 'someone' );
$this -> assertEqual ( $users [ 1 ] -> name , 'someone.2' );
// the parameters and where part should be reseted
2007-05-15 19:08:23 +04:00
$q -> where ( 'User.id IN (?, ?)' , array ( 1 , 2 ));
2006-11-08 22:12:16 +03:00
$users = $q -> execute ();
2006-10-12 00:44:21 +04:00
2006-11-08 22:12:16 +03:00
$this -> assertEqual ( $users -> count (), 2 );
$this -> assertEqual ( $users [ 0 ] -> name , 'someone' );
$this -> assertEqual ( $users [ 1 ] -> name , 'someone.2' );
}
2007-05-15 19:08:23 +04:00
public function testNotInExpression ()
{
2007-05-16 23:20:55 +04:00
$q = new Doctrine_Query ();
2006-10-10 22:09:20 +04:00
2006-10-12 00:44:21 +04:00
$q -> from ( 'User u' ) -> addWhere ( 'u.id NOT IN (?)' , array ( 1 ));
$users = $q -> execute ();
$this -> assertEqual ( $users -> count (), 1 );
$this -> assertEqual ( $users [ 0 ] -> name , 'someone.2' );
}
2007-05-15 19:08:23 +04:00
public function testExistsExpression ()
{
2007-05-16 23:20:55 +04:00
$q = new Doctrine_Query ();
2006-10-12 00:44:21 +04:00
$user = new User ();
$user -> name = 'someone with a group' ;
$user -> Group [ 0 ] -> name = 'some group' ;
$user -> save ();
// find all users which have groups
try {
2007-05-15 19:08:23 +04:00
$q -> from ( 'User u' ) -> where ( 'EXISTS (SELECT Groupuser.id FROM Groupuser WHERE Groupuser.user_id = u.id)' );
2006-10-12 00:44:21 +04:00
$this -> pass ();
} catch ( Doctrine_Exception $e ) {
$this -> fail ();
}
$users = $q -> execute ();
$this -> assertEqual ( $users -> count (), 1 );
$this -> assertEqual ( $users [ 0 ] -> name , 'someone with a group' );
}
2007-05-15 19:08:23 +04:00
public function testNotExistsExpression ()
{
2007-05-16 23:20:55 +04:00
$q = new Doctrine_Query ();
2006-10-12 00:44:21 +04:00
// find all users which don't have groups
try {
2007-05-15 19:08:23 +04:00
$q -> from ( 'User u' ) -> where ( 'NOT EXISTS (SELECT Groupuser.id FROM Groupuser WHERE Groupuser.user_id = u.id)' );
2006-10-12 00:44:21 +04:00
$this -> pass ();
} catch ( Doctrine_Exception $e ) {
$this -> fail ();
}
$users = $q -> execute ();
$this -> assertEqual ( $users -> count (), 2 );
$this -> assertEqual ( $users [ 0 ] -> name , 'someone' );
2006-11-05 22:24:28 +03:00
$this -> assertEqual ( $users [ 1 ] -> name , 'someone.2' );
2006-10-12 00:44:21 +04:00
}
2007-05-15 19:08:23 +04:00
public function testComponentAliases ()
{
2007-05-16 23:20:55 +04:00
$q = new Doctrine_Query ();
2006-10-10 22:09:20 +04:00
2007-05-15 18:36:07 +04:00
$q -> from ( 'User u' ) -> addWhere ( 'u.id IN (?, ?)' , array ( 1 , 2 ));
2007-05-15 19:08:23 +04:00
2006-10-10 22:09:20 +04:00
$users = $q -> execute ();
$this -> assertEqual ( $users -> count (), 2 );
$this -> assertEqual ( $users [ 0 ] -> name , 'someone' );
2006-10-12 00:44:21 +04:00
$this -> assertEqual ( $users [ 1 ] -> name , 'someone.2' );
}
2007-05-15 19:08:23 +04:00
public function testComponentAliases2 ()
{
2007-05-16 23:20:55 +04:00
$q = new Doctrine_Query ();
2006-10-12 00:44:21 +04:00
$q -> from ( 'User u' ) -> addWhere ( 'u.name = ?' , array ( 'someone' ));
$users = $q -> execute ();
$this -> assertEqual ( $users -> count (), 1 );
$this -> assertEqual ( $users [ 0 ] -> name , 'someone' );
}
2007-05-15 19:08:23 +04:00
public function testOperatorWithNoTrailingSpaces ()
{
2007-05-16 23:20:55 +04:00
$q = new Doctrine_Query ();
2006-10-08 23:10:21 +04:00
2007-05-15 19:08:23 +04:00
$q -> select ( 'User.id' ) -> from ( 'User' ) -> where ( " User.name='someone' " );
2006-10-08 23:10:21 +04:00
$users = $q -> execute ();
$this -> assertEqual ( $users -> count (), 1 );
2006-11-05 22:24:28 +03:00
$this -> assertEqual ( $q -> getQuery (), " SELECT e.id AS e__id FROM entity e WHERE e.name = 'someone' AND (e.type = 0) " );
2006-10-08 23:10:21 +04:00
}
2007-05-15 19:08:23 +04:00
public function testOperatorWithNoTrailingSpaces2 ()
{
2007-05-16 23:20:55 +04:00
$q = new Doctrine_Query ();
2006-10-08 23:10:21 +04:00
2007-05-15 19:08:23 +04:00
$q -> select ( 'User.id' ) -> from ( 'User' ) -> where ( " User.name='foo.bar' " );
2006-10-08 23:10:21 +04:00
$users = $q -> execute ();
$this -> assertEqual ( $users -> count (), 0 );
2006-11-05 22:24:28 +03:00
$this -> assertEqual ( $q -> getQuery (), " SELECT e.id AS e__id FROM entity e WHERE e.name = 'foo.bar' AND (e.type = 0) " );
2006-10-08 23:10:21 +04:00
}
2007-05-15 19:08:23 +04:00
public function testOperatorWithSingleTrailingSpace ()
{
2007-05-16 23:20:55 +04:00
$q = new Doctrine_Query ();
2006-10-08 23:10:21 +04:00
2007-05-15 19:08:23 +04:00
$q -> select ( 'User.id' ) -> from ( 'User' ) -> where ( " User.name= 'foo.bar' " );
2006-10-08 23:10:21 +04:00
$users = $q -> execute ();
$this -> assertEqual ( $users -> count (), 0 );
2006-11-05 22:24:28 +03:00
$this -> assertEqual ( $q -> getQuery (), " SELECT e.id AS e__id FROM entity e WHERE e.name = 'foo.bar' AND (e.type = 0) " );
2006-10-08 23:10:21 +04:00
}
2007-05-15 19:08:23 +04:00
public function testOperatorWithSingleTrailingSpace2 ()
{
2007-05-16 23:20:55 +04:00
$q = new Doctrine_Query ();
2007-05-15 19:08:23 +04:00
$q -> select ( 'User.id' ) -> from ( 'User' ) -> where ( " User.name ='foo.bar' " );
2006-10-08 23:10:21 +04:00
$users = $q -> execute ();
$this -> assertEqual ( $users -> count (), 0 );
2006-11-05 22:24:28 +03:00
$this -> assertEqual ( $q -> getQuery (), " SELECT e.id AS e__id FROM entity e WHERE e.name = 'foo.bar' AND (e.type = 0) " );
2006-10-03 20:03:19 +04:00
}
2007-05-18 03:13:58 +04:00
public function testEnumValuesWorkInPlaceholders ()
{
$e = new EnumTest ;
$e -> status = 'verified' ;
$e -> save ();
$q = new Doctrine_Query ();
$q -> select ( 'e.*' ) -> from ( 'EnumTest e' ) -> where ( 'e.status = ?' );
$this -> assertEqual ( count ( $q -> getEnumParams ()), 1 );
$q -> execute ( array ( 'verified' ));
}
public function testEnumValuesWorkWithMultiplePlaceholders ()
{
$q = new Doctrine_Query ();
2006-10-12 00:44:21 +04:00
2007-05-18 03:13:58 +04:00
$q -> select ( 'e.*' ) -> from ( 'EnumTest e' ) -> where ( 'e.id = ? AND e.status = ?' );
$p = $q -> getEnumParams ();
$this -> assertEqual ( array_keys ( $p ), array ( 0 , 1 ));
$this -> assertTrue ( empty ( $p [ 0 ]));
$q -> execute ( array ( 1 , 'verified' ));
}
public function testEnumValuesWorkWithMultipleNamedPlaceholders ()
{
$q = new Doctrine_Query ();
$q -> select ( 'e.*' ) -> from ( 'EnumTest e' ) -> where ( 'e.id = :id AND e.status = :status' );
$p = $q -> getEnumParams ();
$this -> assertEqual ( array_keys ( $p ), array ( ':id' , ':status' ));
$this -> assertTrue ( empty ( $p [ ':id' ]));
$q -> execute ( array ( 1 , 'verified' ));
}
2006-10-03 20:03:19 +04:00
}
?>