Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Query / UpdateSqlGenerationTest.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 use Doctrine\DBAL\Types\Type as DBALType;
25
26 require_once __DIR__ . '/../../TestInit.php';
27
28 /**
29  * Test case for testing the saving and referencing of query identifiers.
30  *
31  * @author      Guilherme Blanco <guilhermeblanco@hotmail.com>
32  * @author      Janne Vanhala <jpvanhal@cc.hut.fi>
33  * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
34  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
35  * @link        http://www.phpdoctrine.org
36  * @since       2.0
37  * @version     $Revision$
38  * @todo        1) [romanb] We  might want to split the SQL generation tests into multiple
39  *              testcases later since we'll have a lot of them and we might want to have special SQL
40  *              generation tests for some dbms specific SQL syntaxes.
41  */
42 class UpdateSqlGenerationTest extends \Doctrine\Tests\OrmTestCase
43 {
44     private $_em;
45
46     protected function setUp() {
47         if (DBALType::hasType('negative_to_positive')) {
48             DBALType::overrideType('negative_to_positive', 'Doctrine\Tests\DbalTypes\NegativeToPositiveType');
49         } else {
50             DBALType::addType('negative_to_positive', 'Doctrine\Tests\DbalTypes\NegativeToPositiveType');
51         }
52
53         $this->_em = $this->_getTestEntityManager();
54     }
55
56     public function assertSqlGeneration($dqlToBeTested, $sqlToBeConfirmed)
57     {
58         try {
59             $query = $this->_em->createQuery($dqlToBeTested);
60             parent::assertEquals($sqlToBeConfirmed, $query->getSql());
61             $query->free();
62         } catch (\Exception $e) {
63             $this->fail($e->getMessage());
64         }
65     }
66
67     public function testSupportsQueriesWithoutWhere()
68     {
69         $this->assertSqlGeneration(
70             'UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.name = ?1',
71             'UPDATE cms_users SET name = ?'
72         );
73     }
74
75     public function testSupportsMultipleFieldsWithoutWhere()
76     {
77         $this->assertSqlGeneration(
78             'UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.name = ?1, u.username = ?2',
79             'UPDATE cms_users SET name = ?, username = ?'
80         );
81     }
82
83     public function testSupportsWhereClauses()
84     {
85         $this->assertSqlGeneration(
86             'UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.name = ?1 WHERE u.id = ?2',
87             'UPDATE cms_users SET name = ? WHERE id = ?'
88         );
89     }
90
91     public function testSupportsWhereClausesOnTheUpdatedField()
92     {
93         $this->assertSqlGeneration(
94             'UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.name = ?1 WHERE u.name = ?2',
95             'UPDATE cms_users SET name = ? WHERE name = ?'
96         );
97     }
98
99     public function testSupportsMultipleWhereClause()
100     {
101         $this->assertSqlGeneration(
102             'UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.name = ?1 WHERE u.name = ?2 AND u.status = ?3',
103             'UPDATE cms_users SET name = ? WHERE name = ? AND status = ?'
104         );
105     }
106
107     public function testSupportsInClause()
108     {
109         $this->assertSqlGeneration(
110             'UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.name = ?1 WHERE u.id IN (1, 3, 4)',
111             'UPDATE cms_users SET name = ? WHERE id IN (1, 3, 4)'
112         );
113     }
114
115     public function testSupportsParametrizedInClause()
116     {
117         $this->assertSqlGeneration(
118             'UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.name = ?1 WHERE u.id IN (?2, ?3, ?4)',
119             'UPDATE cms_users SET name = ? WHERE id IN (?, ?, ?)'
120         );
121     }
122
123     public function testSupportsNotInClause()
124     {
125         $this->assertSqlGeneration(
126             'UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.name = ?1 WHERE u.id NOT IN (1, 3, 4)',
127             'UPDATE cms_users SET name = ? WHERE id NOT IN (1, 3, 4)'
128         );
129     }
130
131     public function testSupportsGreatherThanClause()
132     {
133         $this->assertSqlGeneration(
134             'UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.status = ?1 WHERE u.id > ?2',
135             'UPDATE cms_users SET status = ? WHERE id > ?'
136         );
137     }
138
139     public function testSupportsGreatherThanOrEqualToClause()
140     {
141         $this->assertSqlGeneration(
142             'UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.status = ?1 WHERE u.id >= ?2',
143             'UPDATE cms_users SET status = ? WHERE id >= ?'
144         );
145     }
146
147     public function testSupportsLessThanClause()
148     {
149         $this->assertSqlGeneration(
150             'UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.status = ?1 WHERE u.id < ?2',
151             'UPDATE cms_users SET status = ? WHERE id < ?'
152         );
153     }
154
155     public function testSupportsLessThanOrEqualToClause()
156     {
157         $this->assertSqlGeneration(
158             'UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.status = ?1 WHERE u.id <= ?2',
159             'UPDATE cms_users SET status = ? WHERE id <= ?'
160         );
161     }
162
163     public function testSupportsBetweenClause()
164     {
165         $this->assertSqlGeneration(
166             'UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.status = ?1 WHERE u.id BETWEEN :from AND :to',
167             'UPDATE cms_users SET status = ? WHERE id BETWEEN ? AND ?'
168         );
169     }
170
171     public function testSingleValuedAssociationFieldInWhere()
172     {
173         $this->assertSqlGeneration(
174             "UPDATE Doctrine\Tests\Models\CMS\CmsPhonenumber p SET p.phonenumber = 1234 WHERE p.user = ?1",
175             "UPDATE cms_phonenumbers SET phonenumber = 1234 WHERE user_id = ?"
176         );
177     }
178
179     public function testSingleValuedAssociationFieldInSetClause()
180     {
181         $this->assertSqlGeneration(
182             "update Doctrine\Tests\Models\CMS\CmsComment c set c.article = null where c.article=?1",
183             "UPDATE cms_comments SET article_id = NULL WHERE article_id = ?"
184         );
185     }
186
187     /**
188      * @group DDC-980
189      */
190     public function testSubselectTableAliasReferencing()
191     {
192         $this->assertSqlGeneration(
193             "UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.status = 'inactive' WHERE SIZE(u.groups) = 10",
194             "UPDATE cms_users SET status = 'inactive' WHERE (SELECT COUNT(*) FROM cms_users_groups c0_ WHERE c0_.user_id = cms_users.id) = 10"
195         );
196     }
197
198     public function testCustomTypeValueSqlCompletelyIgnoredInUpdateStatements()
199     {
200         $this->assertSqlGeneration(
201             'UPDATE Doctrine\Tests\Models\CustomType\CustomTypeParent p SET p.customInteger = 1 WHERE p.id = 1',
202             'UPDATE customtype_parents SET customInteger = 1 WHERE id = 1'
203         );
204     }
205 }