Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Query / DeleteSqlGenerationTest.php
1 <?php
2 /*
3  *  $Id$
4  *
5  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16  *
17  * This software consists of voluntary contributions made by many individuals
18  * and is licensed under the LGPL. For more information, see
19  * <http://www.doctrine-project.org>.
20  */
21
22 namespace Doctrine\Tests\ORM\Query;
23
24 require_once __DIR__ . '/../../TestInit.php';
25
26 /**
27  * Test case for testing the saving and referencing of query identifiers.
28  *
29  * @author      Guilherme Blanco <guilhermeblanco@hotmail.com>
30  * @author      Janne Vanhala <jpvanhal@cc.hut.fi>
31  * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
32  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
33  * @link        http://www.phpdoctrine.org
34  * @since       2.0
35  * @version     $Revision$
36  * @todo        1) [romanb] We  might want to split the SQL generation tests into multiple
37  *              testcases later since we'll have a lot of them and we might want to have special SQL
38  *              generation tests for some dbms specific SQL syntaxes.
39  */
40 class DeleteSqlGenerationTest extends \Doctrine\Tests\OrmTestCase
41 {
42     private $_em;
43
44     protected function setUp() {
45         $this->_em = $this->_getTestEntityManager();
46     }
47
48     public function assertSqlGeneration($dqlToBeTested, $sqlToBeConfirmed)
49     {
50         try {
51             $query = $this->_em->createQuery($dqlToBeTested);
52             parent::assertEquals($sqlToBeConfirmed, $query->getSql());
53             $query->free();
54         } catch (\Exception $e) {
55             $this->fail($e->getMessage());
56         }
57     }
58
59     public function testSupportsDeleteWithoutWhereAndFrom()
60     {
61         $this->assertSqlGeneration(
62             'DELETE Doctrine\Tests\Models\CMS\CmsUser u',
63             'DELETE FROM cms_users'
64         );
65     }
66
67     public function testSupportsDeleteWithoutWhere()
68     {
69         $this->assertSqlGeneration(
70             'DELETE FROM Doctrine\Tests\Models\CMS\CmsUser u',
71             'DELETE FROM cms_users'
72         );
73     }
74
75     public function testSupportsWhereClause()
76     {
77         $this->assertSqlGeneration(
78             'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1',
79             'DELETE FROM cms_users WHERE id = ?'
80         );
81     }
82
83     public function testSupportsWhereOrExpressions()
84     {
85         $this->assertSqlGeneration(
86             'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = ?1 OR u.name = ?2',
87             'DELETE FROM cms_users WHERE username = ? OR name = ?'
88         );
89     }
90
91     public function testSupportsWhereNestedConditionalExpressions()
92     {
93         $this->assertSqlGeneration(
94             'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1 OR ( u.username = ?2 OR u.name = ?3)',
95             'DELETE FROM cms_users WHERE id = ? OR (username = ? OR name = ?)'
96         );
97
98         //$this->assertSqlGeneration(
99         //    'DELETE FROM Doctrine\Tests\Models\CMS\CmsUser WHERE id = ?1',
100         //    'DELETE FROM cms_users WHERE id = ?'
101         //);
102     }
103
104     public function testIsCaseAgnostic()
105     {
106         $this->assertSqlGeneration(
107             "delete from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1",
108             "DELETE FROM cms_users WHERE username = ?"
109         );
110     }
111
112     public function testSupportsAndCondition()
113     {
114         $this->assertSqlGeneration(
115             "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = ?1 AND u.name = ?2",
116             "DELETE FROM cms_users WHERE username = ? AND name = ?"
117         );
118     }
119
120     public function testSupportsWhereNot()
121     {
122         $this->assertSqlGeneration(
123             "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE NOT u.id != ?1",
124             "DELETE FROM cms_users WHERE NOT id <> ?"
125         );
126     }
127
128     public function testSupportsWhereNotWithParentheses()
129     {
130         $this->assertSqlGeneration(
131             "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE NOT ( u.id != ?1 )",
132             "DELETE FROM cms_users WHERE NOT (id <> ?)"
133         );
134     }
135
136     public function testSupportsWhereNotWithAndExpression()
137     {
138         $this->assertSqlGeneration(
139             "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE NOT ( u.id != ?1 AND u.username = ?2 )",
140             "DELETE FROM cms_users WHERE NOT (id <> ? AND username = ?)"
141         );
142     }
143
144     // ConditionalPrimary was already tested (see testSupportsWhereClause() and testSupportsWhereNot())
145
146     public function testSupportsGreaterThanComparisonClause()
147     {
148         // id = ? was already tested (see testDeleteWithWhere())
149         $this->assertSqlGeneration(
150             "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id > ?1",
151             "DELETE FROM cms_users WHERE id > ?"
152         );
153     }
154
155     public function testSupportsGreaterThanOrEqualToComparisonClause()
156     {
157         $this->assertSqlGeneration(
158             "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id >= ?1",
159             "DELETE FROM cms_users WHERE id >= ?"
160         );
161     }
162
163     public function testSupportsLessThanComparisonClause()
164     {
165         $this->assertSqlGeneration(
166             "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id < ?1",
167             "DELETE FROM cms_users WHERE id < ?"
168         );
169     }
170
171     public function testSupportsLessThanOrEqualToComparisonClause()
172     {
173         $this->assertSqlGeneration(
174             "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id <= ?1",
175             "DELETE FROM cms_users WHERE id <= ?"
176         );
177     }
178
179     public function testSupportsNotEqualToComparisonClause()
180     {
181         $this->assertSqlGeneration(
182             "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id <> ?1",
183             "DELETE FROM cms_users WHERE id <> ?"
184         );
185     }
186
187     public function testSupportsNotEqualToComparisonClauseExpressedWithExclamationMark()
188     {
189         $this->assertSqlGeneration(
190             "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id != ?1",
191             "DELETE FROM cms_users WHERE id <> ?"
192         );
193     }
194
195     public function testSupportsNotBetweenClause()
196     {
197         $this->assertSqlGeneration(
198             "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id NOT BETWEEN ?1 AND ?2",
199             "DELETE FROM cms_users WHERE id NOT BETWEEN ? AND ?"
200         );
201     }
202
203     public function testSupportsBetweenClauseUsedWithAndClause()
204     {
205         $this->assertSqlGeneration(
206             "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id BETWEEN ?1 AND ?2 AND u.username != ?3",
207             "DELETE FROM cms_users WHERE id BETWEEN ? AND ? AND username <> ?"
208         );
209     }
210
211     public function testSupportsNotLikeClause()
212     {
213         // "WHERE" Expression LikeExpression
214         $this->assertSqlGeneration(
215             'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username NOT LIKE ?1',
216             'DELETE FROM cms_users WHERE username NOT LIKE ?'
217         );
218     }
219
220     public function testSupportsLikeClauseWithEscapeExpression()
221     {
222         $this->assertSqlGeneration(
223             "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username LIKE ?1 ESCAPE '\\'",
224             "DELETE FROM cms_users WHERE username LIKE ? ESCAPE '\\'"
225         );
226     }
227
228     public function testSupportsIsNullClause()
229     {
230         // "WHERE" Expression NullComparisonExpression
231         $this->assertSqlGeneration(
232             'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name IS NULL',
233             'DELETE FROM cms_users WHERE name IS NULL'
234         );
235     }
236
237     public function testSupportsIsNotNullClause()
238     {
239         $this->assertSqlGeneration(
240             'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name IS NOT NULL',
241             'DELETE FROM cms_users WHERE name IS NOT NULL'
242         );
243     }
244
245     public function testSupportsAtomExpressionAsClause()
246     {
247         $this->assertSqlGeneration(
248             'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE 1 = 1',
249             'DELETE FROM cms_users WHERE 1 = 1'
250         );
251     }
252
253     public function testSupportsParameterizedAtomExpression()
254     {
255         $this->assertSqlGeneration(
256             'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE ?1 = 1',
257             'DELETE FROM cms_users WHERE ? = 1'
258         );
259     }
260
261     public function testSupportsInClause()
262     {
263         $this->assertSqlGeneration(
264             'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id IN ( ?1, ?2, ?3, ?4 )',
265             'DELETE FROM cms_users WHERE id IN (?, ?, ?, ?)'
266         );
267     }
268
269     public function testSupportsNotInClause()
270     {
271         $this->assertSqlGeneration(
272             'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id NOT IN ( ?1, ?2 )',
273             'DELETE FROM cms_users WHERE id NOT IN (?, ?)'
274         );
275     }
276
277     /**
278      * @group DDC-980
279      */
280     public function testSubselectTableAliasReferencing()
281     {
282         $this->assertSqlGeneration(
283             'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE SIZE(u.groups) = 10',
284             'DELETE FROM cms_users WHERE (SELECT COUNT(*) FROM cms_users_groups c0_ WHERE c0_.user_id = cms_users.id) = 10'
285         );
286     }
287 }