2006-12-28 14:19:05 +03:00
< ? php
2008-01-10 16:17:05 +03:00
/*
2006-12-28 14:19:05 +03: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_Expression_TestCase
*
* @ package Doctrine
* @ license http :// www . opensource . org / licenses / lgpl - license . php LGPL
* @ author Konsta Vesterinen < kvesteri @ cc . hut . fi >
* @ version $Revision $
* @ category Object Relational Mapping
* @ link www . phpdoctrine . com
* @ since 1.0
*/
2008-01-10 16:17:05 +03:00
class Doctrine_Query_Expression_TestCase extends Doctrine_UnitTestCase
2007-06-19 02:13:14 +04:00
{
2008-01-10 16:17:05 +03:00
public function testUnknownExpressionInSelectClauseThrowsException ()
2007-06-19 02:13:14 +04:00
{
2006-12-28 14:19:05 +03:00
$q = new Doctrine_Query ();
2008-01-10 16:17:05 +03:00
2006-12-28 14:19:05 +03:00
try {
$q -> parseQuery ( 'SELECT SOMEUNKNOWNFUNC(u.name, " ", u.loginname) FROM User u' );
2008-01-10 16:17:05 +03:00
2007-06-19 02:13:14 +04:00
$q -> getQuery ();
2006-12-28 14:19:05 +03:00
$this -> fail ();
} catch ( Doctrine_Query_Exception $e ) {
$this -> pass ();
}
}
2007-06-19 02:13:14 +04:00
2008-01-10 16:17:05 +03:00
public function testUnknownColumnWithinFunctionInSelectClauseThrowsException ()
2007-06-19 02:13:14 +04:00
{
2006-12-28 14:19:05 +03:00
$q = new Doctrine_Query ();
2008-01-10 16:17:05 +03:00
2006-12-28 14:19:05 +03:00
try {
2008-01-10 16:17:05 +03:00
$q -> parseQuery ( 'SELECT u.name || u.unknown FROM User u' );
2007-06-04 22:21:36 +04:00
$q -> execute ();
2006-12-28 14:19:05 +03:00
$this -> fail ();
} catch ( Doctrine_Query_Exception $e ) {
$this -> pass ();
}
}
2007-06-19 02:13:14 +04:00
2008-01-10 16:17:05 +03:00
public function testConcatIsSupportedInSelectClause ()
2007-06-19 02:13:14 +04:00
{
2006-12-28 14:19:05 +03:00
$q = new Doctrine_Query ();
2008-01-10 16:17:05 +03:00
2006-12-28 14:19:05 +03:00
$q -> parseQuery ( 'SELECT CONCAT(u.name, u.loginname) FROM User u' );
2008-01-10 16:17:05 +03:00
$this -> assertEqual ( $q -> getQuery (), 'SELECT e.name || e.loginname AS e__0 FROM entity e WHERE (e.type = 0)' );
2006-12-28 14:19:05 +03:00
}
2007-06-19 02:13:14 +04:00
2008-01-10 16:17:05 +03:00
public function testConcatInSelectClauseSupportsLiteralStrings ()
2007-06-19 02:13:14 +04:00
{
2006-12-28 14:19:05 +03:00
$q = new Doctrine_Query ();
2008-01-10 16:17:05 +03:00
2006-12-28 14:19:05 +03:00
$q -> parseQuery ( " SELECT CONCAT(u.name, 'The Man') FROM User u " );
2008-01-10 16:17:05 +03:00
$this -> assertEqual ( $q -> getQuery (), " SELECT e.name || 'The Man' AS e__0 FROM entity e WHERE (e.type = 0) " );
2006-12-28 14:19:05 +03:00
}
2007-06-19 02:13:14 +04:00
2008-01-10 16:17:05 +03:00
public function testConcatInSelectClauseSupportsMoreThanTwoArgs ()
2007-06-19 02:13:14 +04:00
{
2006-12-28 14:19:05 +03:00
$q = new Doctrine_Query ();
2008-01-10 16:17:05 +03:00
2006-12-28 14:19:05 +03:00
$q -> parseQuery ( " SELECT CONCAT(u.name, 'The Man', u.loginname) FROM User u " );
2008-01-10 16:17:05 +03:00
$this -> assertEqual ( $q -> getQuery (), " SELECT e.name || 'The Man' || e.loginname AS e__0 FROM entity e WHERE (e.type = 0) " );
2006-12-28 14:19:05 +03:00
}
2007-07-09 16:36:21 +04:00
public function testNonPortableFunctionsAreSupported ()
{
$query = new Doctrine_Query ();
// we are using stored procedure here, so adjust portability settings
$this -> conn -> setAttribute ( Doctrine :: ATTR_PORTABILITY , Doctrine :: PORTABILITY_ALL ^ Doctrine :: PORTABILITY_EXPR );
$lat = '13.23' ;
$lon = '33.23' ;
$radius = '33' ;
$query -> select ( " l.*, i18n.*, GeoDistKM(l.lat, l.lon, $lat , $lon ) distance " )
2008-01-10 16:17:05 +03:00
-> from ( 'Location l, l.LocationI18n i18n' )
2007-07-09 16:36:21 +04:00
-> where ( 'l.id <> ? AND i18n.culture = ?' , array ( 1 , 'en' ))
-> having ( " distance < $radius " )
-> orderby ( 'distance ASC' )
-> groupby ( 'l.id' )
-> limit ( 5 );
$this -> assertEqual ( $query -> getSql (), " SELECT l.id AS l__id, l.lat AS l__lat, l.lon AS l__lon, l2.name AS l2__name, l2.id AS l2__id, l2.culture AS l2__culture, GeoDistKM(l.lat, l.lon, 13.23, 33.23) AS l__0 FROM location l LEFT JOIN location_i18n l2 ON l.id = l2.id WHERE l.id IN (SELECT DISTINCT l3.id FROM location l3 LEFT JOIN location_i18n l4 ON l3.id = l4.id WHERE (l3.id <> ? AND l4.culture = ?) GROUP BY l3.id HAVING l__0 < 33 ORDER BY l__0 ASC LIMIT 5) AND (l.id <> ? AND l2.culture = ?) GROUP BY l.id HAVING l__0 < 33 ORDER BY l__0 ASC " );
$this -> conn -> setAttribute ( Doctrine :: ATTR_PORTABILITY , Doctrine :: PORTABILITY_ALL );
}
}