Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / CustomTreeWalkersTest.php
1 <?php
2 /*
3  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14  *
15  * This software consists of voluntary contributions made by many individuals
16  * and is licensed under the LGPL. For more information, see
17  * <http://www.doctrine-project.org>.
18  */
19
20 namespace Doctrine\Tests\ORM\Functional;
21
22 use Doctrine\ORM\Query;
23
24 require_once __DIR__ . '/../../TestInit.php';
25
26 /**
27  * Test case for custom AST walking and modification.
28  *
29  * @author      Roman Borschel <roman@code-factory.org>
30  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
31  * @link        http://www.doctrine-project.org
32  * @since       2.0
33  */
34 class CustomTreeWalkersTest extends \Doctrine\Tests\OrmTestCase
35 {
36     private $_em;
37
38     protected function setUp()
39     {
40         $this->_em = $this->_getTestEntityManager();
41     }
42
43     public function assertSqlGeneration($dqlToBeTested, $sqlToBeConfirmed)
44     {
45         try {
46             $query = $this->_em->createQuery($dqlToBeTested);
47             $query->setHint(Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\Tests\ORM\Functional\CustomTreeWalker'))
48                   ->useQueryCache(false);
49
50             $this->assertEquals($sqlToBeConfirmed, $query->getSql());
51             $query->free();
52         } catch (\Exception $e) {
53             $this->fail($e->getMessage() . ' at "' . $e->getFile() . '" on line ' . $e->getLine());
54
55         }
56     }
57
58     public function testSupportsQueriesWithoutWhere()
59     {
60         $this->assertSqlGeneration(
61             'select u from Doctrine\Tests\Models\CMS\CmsUser u',
62             "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, c0_.email_id AS email_id4 FROM cms_users c0_ WHERE c0_.id = 1"
63         );
64     }
65
66     public function testSupportsQueriesWithMultipleConditionalExpressions()
67     {
68         $this->assertSqlGeneration(
69             'select u from Doctrine\Tests\Models\CMS\CmsUser u where u.name = :name or u.name = :otherName',
70             "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, c0_.email_id AS email_id4 FROM cms_users c0_ WHERE (c0_.name = ? OR c0_.name = ?) AND c0_.id = 1"
71         );
72     }
73
74     public function testSupportsQueriesWithSimpleConditionalExpression()
75     {
76         $this->assertSqlGeneration(
77             'select u from Doctrine\Tests\Models\CMS\CmsUser u where u.name = :name',
78             "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, c0_.email_id AS email_id4 FROM cms_users c0_ WHERE c0_.name = ? AND c0_.id = 1"
79         );
80     }
81 }
82
83 class CustomTreeWalker extends Query\TreeWalkerAdapter
84 {
85     public function walkSelectStatement(Query\AST\SelectStatement $selectStatement)
86     {
87         // Get the DQL aliases of all the classes we want to modify
88         $dqlAliases = array();
89
90         foreach ($this->_getQueryComponents() as $dqlAlias => $comp) {
91             // Hard-coded check just for demonstration: We want to modify the query if
92             // it involves the CmsUser class.
93             if ($comp['metadata']->name == 'Doctrine\Tests\Models\CMS\CmsUser') {
94                 $dqlAliases[] = $dqlAlias;
95             }
96         }
97
98         // Create our conditions for all involved classes
99         $factors = array();
100         foreach ($dqlAliases as $alias) {
101             $pathExpr = new Query\AST\PathExpression(Query\AST\PathExpression::TYPE_STATE_FIELD, $alias, 'id');
102             $pathExpr->type = Query\AST\PathExpression::TYPE_STATE_FIELD;
103             $comparisonExpr = new Query\AST\ComparisonExpression($pathExpr, '=', 1);
104
105             $condPrimary = new Query\AST\ConditionalPrimary;
106             $condPrimary->simpleConditionalExpression = $comparisonExpr;
107
108             $factor = new Query\AST\ConditionalFactor($condPrimary);
109             $factors[] = $factor;
110         }
111
112         if (($whereClause = $selectStatement->whereClause) !== null) {
113             // There is already a WHERE clause, so append the conditions
114             $condExpr = $whereClause->conditionalExpression;
115
116             // Since Phase 1 AST optimizations were included, we need to re-add the ConditionalExpression
117             if ( ! ($condExpr instanceof Query\AST\ConditionalExpression)) {
118                 $condExpr = new Query\AST\ConditionalExpression(array($condExpr));
119
120                 $whereClause->conditionalExpression = $condExpr;
121             }
122
123             $existingTerms = $whereClause->conditionalExpression->conditionalTerms;
124
125             if (count($existingTerms) > 1) {
126                 // More than one term, so we need to wrap all these terms in a single root term
127                 // i.e: "WHERE u.name = :foo or u.other = :bar" => "WHERE (u.name = :foo or u.other = :bar) AND <our condition>"
128
129                 $primary = new Query\AST\ConditionalPrimary;
130                 $primary->conditionalExpression = new Query\AST\ConditionalExpression($existingTerms);
131                 $existingFactor = new Query\AST\ConditionalFactor($primary);
132                 $term = new Query\AST\ConditionalTerm(array_merge(array($existingFactor), $factors));
133
134                 $selectStatement->whereClause->conditionalExpression->conditionalTerms = array($term);
135             } else {
136                 // Just one term so we can simply append our factors to that term
137                 $singleTerm = $selectStatement->whereClause->conditionalExpression->conditionalTerms[0];
138
139                 // Since Phase 1 AST optimizations were included, we need to re-add the ConditionalExpression
140                 if ( ! ($singleTerm instanceof Query\AST\ConditionalTerm)) {
141                     $singleTerm = new Query\AST\ConditionalTerm(array($singleTerm));
142
143                     $selectStatement->whereClause->conditionalExpression->conditionalTerms[0] = $singleTerm;
144                 }
145
146                 $singleTerm->conditionalFactors = array_merge($singleTerm->conditionalFactors, $factors);
147                 $selectStatement->whereClause->conditionalExpression->conditionalTerms = array($singleTerm);
148             }
149         } else {
150             // Create a new WHERE clause with our factors
151             $term = new Query\AST\ConditionalTerm($factors);
152             $condExpr = new Query\AST\ConditionalExpression(array($term));
153             $whereClause = new Query\AST\WhereClause($condExpr);
154             $selectStatement->whereClause = $whereClause;
155         }
156     }
157 }