2012-10-11 17:25:39 +04:00
< ? php
/*
* 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
2012-10-12 11:33:40 +04:00
* and is licensed under the MIT license . For more information , see
2012-10-11 17:25:39 +04:00
* < http :// www . doctrine - project . org >.
*/
2012-10-12 11:33:40 +04:00
namespace Doctrine\Tests\ORM\Query ;
2012-10-11 17:25:39 +04:00
use Doctrine\ORM\Query ;
/**
* Test case for custom AST walking and adding new joins .
*
* @ author Lukasz Cybula < lukasz . cybula @ fsi . pl >
2012-10-12 11:33:40 +04:00
* @ license MIT
2012-10-11 17:25:39 +04:00
* @ link http :// www . doctrine - project . org
*/
class CustomTreeWalkersJoinTest extends \Doctrine\Tests\OrmTestCase
{
2012-10-12 11:33:40 +04:00
private $em ;
2012-10-11 17:25:39 +04:00
protected function setUp ()
{
2012-10-12 11:33:40 +04:00
$this -> em = $this -> _getTestEntityManager ();
2012-10-11 17:25:39 +04:00
}
public function assertSqlGeneration ( $dqlToBeTested , $sqlToBeConfirmed )
{
try {
2012-10-12 11:33:40 +04:00
$query = $this -> em -> createQuery ( $dqlToBeTested );
$query -> setHint ( Query :: HINT_CUSTOM_TREE_WALKERS , array ( 'Doctrine\Tests\ORM\Query\CustomTreeWalkerJoin' ))
2012-10-11 17:25:39 +04:00
-> useQueryCache ( false );
$this -> assertEquals ( $sqlToBeConfirmed , $query -> getSql ());
$query -> free ();
} catch ( \Exception $e ) {
$this -> fail ( $e -> getMessage () . ' at "' . $e -> getFile () . '" on line ' . $e -> getLine ());
}
}
public function testAddsJoin ()
{
$this -> assertSqlGeneration (
'select u from Doctrine\Tests\Models\CMS\CmsUser u' ,
2014-03-21 22:39:51 +04:00
" SELECT c0_.id AS id_0, c0_.status AS status_1, c0_.username AS username_2, c0_.name AS name_3, c1_.id AS id_4, c1_.country AS country_5, c1_.zip AS zip_6, c1_.city AS city_7, c0_.email_id AS email_id_8, c1_.user_id AS user_id_9 FROM cms_users c0_ LEFT JOIN cms_addresses c1_ ON c0_.id = c1_.user_id "
2012-10-11 17:25:39 +04:00
);
}
public function testDoesNotAddJoin ()
{
$this -> assertSqlGeneration (
'select a from Doctrine\Tests\Models\CMS\CmsAddress a' ,
2014-03-21 22:39:51 +04:00
" SELECT c0_.id AS id_0, c0_.country AS country_1, c0_.zip AS zip_2, c0_.city AS city_3, c0_.user_id AS user_id_4 FROM cms_addresses c0_ "
2012-10-11 17:25:39 +04:00
);
}
}
2012-10-12 11:33:40 +04:00
class CustomTreeWalkerJoin extends Query\TreeWalkerAdapter
2012-10-11 17:25:39 +04:00
{
public function walkSelectStatement ( Query\AST\SelectStatement $selectStatement )
{
foreach ( $selectStatement -> fromClause -> identificationVariableDeclarations as $identificationVariableDeclaration ) {
2014-04-21 00:27:57 +04:00
$rangeVariableDecl = $identificationVariableDeclaration -> rangeVariableDeclaration ;
if ( $rangeVariableDecl -> abstractSchemaName !== 'Doctrine\Tests\Models\CMS\CmsUser' ) {
continue ;
2012-10-11 17:25:39 +04:00
}
2014-04-21 00:27:57 +04:00
$this -> modifySelectStatement ( $selectStatement , $identificationVariableDeclaration );
2012-10-11 17:25:39 +04:00
}
}
2014-04-21 00:27:57 +04:00
private function modifySelectStatement ( Query\AST\SelectStatement $selectStatement , $identificationVariableDecl )
{
$rangeVariableDecl = $identificationVariableDecl -> rangeVariableDeclaration ;
$joinAssocPathExpression = new Query\AST\JoinAssociationPathExpression ( $rangeVariableDecl -> aliasIdentificationVariable , 'address' );
$joinAssocDeclaration = new Query\AST\JoinAssociationDeclaration ( $joinAssocPathExpression , $rangeVariableDecl -> aliasIdentificationVariable . 'a' , null );
$join = new Query\AST\Join ( Query\AST\Join :: JOIN_TYPE_LEFT , $joinAssocDeclaration );
$selectExpression = new Query\AST\SelectExpression ( $rangeVariableDecl -> aliasIdentificationVariable . 'a' , null , false );
$identificationVariableDecl -> joins [] = $join ;
$selectStatement -> selectClause -> selectExpressions [] = $selectExpression ;
$entityManager = $this -> _getQuery () -> getEntityManager ();
$userMetadata = $entityManager -> getClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsUser' );
$addressMetadata = $entityManager -> getClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsAddress' );
$this -> setQueryComponent ( $rangeVariableDecl -> aliasIdentificationVariable . 'a' ,
array (
'metadata' => $addressMetadata ,
'parent' => $rangeVariableDecl -> aliasIdentificationVariable ,
'relation' => $userMetadata -> getAssociationMapping ( 'address' ),
'map' => null ,
'nestingLevel' => 0 ,
'token' => null ,
)
);
}
2012-10-11 17:25:39 +04:00
}