DeleteSqlGenerationTest.php 9.18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?php
/*
 *  $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
romanb's avatar
romanb committed
19
 * <http://www.doctrine-project.org>.
20
 */
romanb's avatar
romanb committed
21 22 23 24 25

namespace Doctrine\Tests\ORM\Query;

require_once __DIR__ . '/../../TestInit.php';

26 27 28 29 30 31 32 33
/**
 * Test case for testing the saving and referencing of query identifiers.
 *
 * @author      Guilherme Blanco <guilhermeblanco@hotmail.com>
 * @author      Janne Vanhala <jpvanhal@cc.hut.fi>
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @link        http://www.phpdoctrine.org
34
 * @since       2.0
35 36 37 38 39
 * @version     $Revision$
 * @todo        1) [romanb] We  might want to split the SQL generation tests into multiple
 *              testcases later since we'll have a lot of them and we might want to have special SQL
 *              generation tests for some dbms specific SQL syntaxes.
 */
romanb's avatar
romanb committed
40
class DeleteSqlGenerationTest extends \Doctrine\Tests\OrmTestCase
41
{
romanb's avatar
romanb committed
42 43 44 45 46 47
    private $_em;

    protected function setUp() {
        $this->_em = $this->_getTestEntityManager();
    }

48
    public function assertSqlGeneration($dqlToBeTested, $sqlToBeConfirmed)
49
    {
50
        try {
romanb's avatar
romanb committed
51
            $query = $this->_em->createQuery($dqlToBeTested);
52 53
            parent::assertEquals($sqlToBeConfirmed, $query->getSql());
            $query->free();
romanb's avatar
romanb committed
54
        } catch (\Exception $e) {
55 56 57 58
            $this->fail($e->getMessage());
        }
    }

59
    public function testSupportsDeleteWithoutWhereAndFrom()
60 61
    {
        $this->assertSqlGeneration(
romanb's avatar
romanb committed
62
            'DELETE Doctrine\Tests\Models\CMS\CmsUser u',
63
            'DELETE FROM cms_users'
64
        );
65 66 67 68
    }

    public function testSupportsDeleteWithoutWhere()
    {
69
        $this->assertSqlGeneration(
romanb's avatar
romanb committed
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(
romanb's avatar
romanb committed
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(
romanb's avatar
romanb committed
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 92
    public function testSupportsWhereNestedConditionalExpressions()
    {
93
        $this->assertSqlGeneration(
romanb's avatar
romanb committed
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 99 100 101
        //$this->assertSqlGeneration(
        //    'DELETE FROM Doctrine\Tests\Models\CMS\CmsUser WHERE id = ?1',
        //    'DELETE FROM cms_users WHERE id = ?'
        //);
102 103
    }

104
    public function testIsCaseAgnostic()
105
    {
106
        $this->assertSqlGeneration(
romanb's avatar
romanb committed
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(
romanb's avatar
romanb committed
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(
romanb's avatar
romanb committed
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 129
    public function testSupportsWhereNotWithParentheses()
    {
130
        $this->assertSqlGeneration(
romanb's avatar
romanb committed
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 137
    public function testSupportsWhereNotWithAndExpression()
    {
138
        $this->assertSqlGeneration(
romanb's avatar
romanb committed
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(
romanb's avatar
romanb committed
150
            "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id > ?1",
151
            "DELETE FROM cms_users WHERE id > ?"
152
        );
153
    }
154

155 156
    public function testSupportsGreaterThanOrEqualToComparisonClause()
    {
157
        $this->assertSqlGeneration(
romanb's avatar
romanb committed
158
            "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id >= ?1",
159
            "DELETE FROM cms_users WHERE id >= ?"
160
        );
161
    }
162

163 164
    public function testSupportsLessThanComparisonClause()
    {
165
        $this->assertSqlGeneration(
romanb's avatar
romanb committed
166
            "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id < ?1",
167
            "DELETE FROM cms_users WHERE id < ?"
168
        );
169
    }
170

171 172
    public function testSupportsLessThanOrEqualToComparisonClause()
    {
173
        $this->assertSqlGeneration(
romanb's avatar
romanb committed
174
            "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id <= ?1",
175
            "DELETE FROM cms_users WHERE id <= ?"
176
        );
177
    }
178

179 180
    public function testSupportsNotEqualToComparisonClause()
    {
181
        $this->assertSqlGeneration(
romanb's avatar
romanb committed
182
            "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id <> ?1",
183
            "DELETE FROM cms_users WHERE id <> ?"
184
        );
185
    }
186

187 188
    public function testSupportsNotEqualToComparisonClauseExpressedWithExclamationMark()
    {
189
        $this->assertSqlGeneration(
romanb's avatar
romanb committed
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(
romanb's avatar
romanb committed
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 204
    public function testSupportsBetweenClauseUsedWithAndClause()
    {
205
        $this->assertSqlGeneration(
romanb's avatar
romanb committed
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 221
    public function testSupportsLikeClauseWithEscapeExpression()
    {
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 238
    public function testSupportsIsNotNullClause()
    {
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 254
    public function testSupportsParameterizedAtomExpression()
    {
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(
romanb's avatar
romanb committed
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 270
    public function testSupportsNotInClause()
    {
271
        $this->assertSqlGeneration(
romanb's avatar
romanb committed
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
    }
}