PageRenderTime 48ms CodeModel.GetById 4ms app.highlight 33ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/doctrine/orm/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php

https://github.com/DevAlien/DriverMaps
PHP | 1807 lines | 1331 code | 220 blank | 256 comment | 12 complexity | 093e6a5d4a921e5b6a6ac3e087e411a8 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1<?php
   2
   3namespace Doctrine\Tests\ORM\Query;
   4
   5use Doctrine\DBAL\Types\Type as DBALType;
   6use Doctrine\ORM\Query;
   7
   8require_once __DIR__ . '/../../TestInit.php';
   9
  10class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase
  11{
  12    private $_em;
  13
  14    protected function setUp()
  15    {
  16        $this->_em = $this->_getTestEntityManager();
  17    }
  18
  19    /**
  20     * Assert a valid SQL generation.
  21     *
  22     * @param string $dqlToBeTested
  23     * @param string $sqlToBeConfirmed
  24     * @param array $queryHints
  25     * @param array $queryParams
  26     */
  27    public function assertSqlGeneration($dqlToBeTested, $sqlToBeConfirmed, array $queryHints = array(), array $queryParams = array())
  28    {
  29        try {
  30            $query = $this->_em->createQuery($dqlToBeTested);
  31
  32            foreach ($queryParams AS $name => $value) {
  33                $query->setParameter($name, $value);
  34            }
  35
  36            $query->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true)
  37                  ->useQueryCache(false);
  38
  39            foreach ($queryHints AS $name => $value) {
  40                $query->setHint($name, $value);
  41            }
  42
  43            $sqlGenerated = $query->getSQL();
  44
  45            parent::assertEquals(
  46                $sqlToBeConfirmed,
  47                $sqlGenerated,
  48                sprintf('"%s" is not equal of "%s"', $sqlGenerated, $sqlToBeConfirmed)
  49            );
  50
  51            $query->free();
  52        } catch (\Exception $e) {
  53            $this->fail($e->getMessage() ."\n".$e->getTraceAsString());
  54        }
  55    }
  56
  57    /**
  58     * Asser an invalid SQL generation.
  59     *
  60     * @param string $dqlToBeTested
  61     * @param string $expectedException
  62     * @param array $queryHints
  63     * @param array $queryParams
  64     */
  65    public function assertInvalidSqlGeneration($dqlToBeTested, $expectedException, array $queryHints = array(), array $queryParams = array())
  66    {
  67        $this->setExpectedException($expectedException);
  68
  69        $query = $this->_em->createQuery($dqlToBeTested);
  70
  71        foreach ($queryParams AS $name => $value) {
  72            $query->setParameter($name, $value);
  73        }
  74
  75        $query->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true)
  76              ->useQueryCache(false);
  77
  78        foreach ($queryHints AS $name => $value) {
  79            $query->setHint($name, $value);
  80        }
  81
  82        $sql = $query->getSql();
  83        $query->free();
  84
  85        // If we reached here, test failed
  86        $this->fail($sql);
  87    }
  88
  89
  90    public function testSupportsSelectForAllFields()
  91    {
  92        $this->assertSqlGeneration(
  93            'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u',
  94            'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_'
  95        );
  96    }
  97
  98    public function testSupportsSelectForOneField()
  99    {
 100        $this->assertSqlGeneration(
 101            'SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u',
 102            'SELECT c0_.id AS id0 FROM cms_users c0_'
 103        );
 104    }
 105
 106    public function testSupportsSelectForOneNestedField()
 107    {
 108        $this->assertSqlGeneration(
 109            'SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsArticle a JOIN a.user u',
 110            'SELECT c0_.id AS id0 FROM cms_articles c1_ INNER JOIN cms_users c0_ ON c1_.user_id = c0_.id'
 111        );
 112    }
 113
 114    public function testSupportsSelectForAllNestedField()
 115    {
 116        $this->assertSqlGeneration(
 117            'SELECT a FROM Doctrine\Tests\Models\CMS\CmsArticle a JOIN a.user u ORDER BY u.name ASC',
 118            'SELECT c0_.id AS id0, c0_.topic AS topic1, c0_.text AS text2, c0_.version AS version3 FROM cms_articles c0_ INNER JOIN cms_users c1_ ON c0_.user_id = c1_.id ORDER BY c1_.name ASC'
 119        );
 120    }
 121
 122    public function testSupportsSelectForMultipleColumnsOfASingleComponent()
 123    {
 124        $this->assertSqlGeneration(
 125            'SELECT u.username, u.name FROM Doctrine\Tests\Models\CMS\CmsUser u',
 126            'SELECT c0_.username AS username0, c0_.name AS name1 FROM cms_users c0_'
 127        );
 128    }
 129
 130    public function testSupportsSelectUsingMultipleFromComponents()
 131    {
 132        $this->assertSqlGeneration(
 133            'SELECT u, p FROM Doctrine\Tests\Models\CMS\CmsUser u, Doctrine\Tests\Models\CMS\CmsPhonenumber p WHERE u = p.user',
 134            'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, c1_.phonenumber AS phonenumber4 FROM cms_users c0_, cms_phonenumbers c1_ WHERE c0_.id = c1_.user_id'
 135        );
 136    }
 137
 138    public function testSupportsJoinOnMultipleComponents()
 139    {
 140        $this->assertSqlGeneration(
 141            'SELECT u, p FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN Doctrine\Tests\Models\CMS\CmsPhonenumber p WITH u = p.user',
 142            'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, c1_.phonenumber AS phonenumber4 FROM cms_users c0_ INNER JOIN cms_phonenumbers c1_ ON (c0_.id = c1_.user_id)'
 143        );
 144    }
 145
 146    public function testSupportsJoinOnMultipleComponentsWithJoinedInheritanceType()
 147    {
 148        $this->assertSqlGeneration(
 149            'SELECT e FROM Doctrine\Tests\Models\Company\CompanyEmployee e JOIN Doctrine\Tests\Models\Company\CompanyManager m WITH e.id = m.id',
 150            'SELECT c0_.id AS id0, c0_.name AS name1, c1_.salary AS salary2, c1_.department AS department3, c1_.startDate AS startDate4, c0_.discr AS discr5 FROM company_employees c1_ INNER JOIN company_persons c0_ ON c1_.id = c0_.id INNER JOIN company_managers c2_ INNER JOIN company_employees c3_ ON c2_.id = c3_.id INNER JOIN company_persons c4_ ON c2_.id = c4_.id AND (c0_.id = c4_.id)'
 151        );
 152
 153        $this->assertSqlGeneration(
 154            'SELECT e FROM Doctrine\Tests\Models\Company\CompanyEmployee e LEFT JOIN Doctrine\Tests\Models\Company\CompanyManager m WITH e.id = m.id',
 155            'SELECT c0_.id AS id0, c0_.name AS name1, c1_.salary AS salary2, c1_.department AS department3, c1_.startDate AS startDate4, c0_.discr AS discr5 FROM company_employees c1_ INNER JOIN company_persons c0_ ON c1_.id = c0_.id LEFT JOIN company_managers c2_ INNER JOIN company_employees c3_ ON c2_.id = c3_.id INNER JOIN company_persons c4_ ON c2_.id = c4_.id ON (c0_.id = c4_.id)'
 156        );
 157    }
 158
 159    public function testSupportsSelectWithCollectionAssociationJoin()
 160    {
 161        $this->assertSqlGeneration(
 162            'SELECT u, p FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.phonenumbers p',
 163            'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, c1_.phonenumber AS phonenumber4 FROM cms_users c0_ INNER JOIN cms_phonenumbers c1_ ON c0_.id = c1_.user_id'
 164        );
 165    }
 166
 167    public function testSupportsSelectWithSingleValuedAssociationJoin()
 168    {
 169        $this->assertSqlGeneration(
 170            'SELECT u, a FROM Doctrine\Tests\Models\Forum\ForumUser u JOIN u.avatar a',
 171            'SELECT f0_.id AS id0, f0_.username AS username1, f1_.id AS id2 FROM forum_users f0_ INNER JOIN forum_avatars f1_ ON f0_.avatar_id = f1_.id'
 172        );
 173    }
 174
 175    public function testSelectCorrelatedSubqueryComplexMathematicalExpression()
 176    {
 177        $this->assertSqlGeneration(
 178            'SELECT (SELECT (count(p.phonenumber)+5)*10 FROM Doctrine\Tests\Models\CMS\CmsPhonenumber p JOIN p.user ui WHERE ui.id = u.id) AS c FROM Doctrine\Tests\Models\CMS\CmsUser u',
 179            'SELECT (SELECT (count(c0_.phonenumber) + 5) * 10 AS sclr1 FROM cms_phonenumbers c0_ INNER JOIN cms_users c1_ ON c0_.user_id = c1_.id WHERE c1_.id = c2_.id) AS sclr0 FROM cms_users c2_'
 180        );
 181    }
 182
 183    public function testSelectComplexMathematicalExpression()
 184    {
 185        $this->assertSqlGeneration(
 186            'SELECT (count(p.phonenumber)+5)*10 FROM Doctrine\Tests\Models\CMS\CmsPhonenumber p JOIN p.user ui WHERE ui.id = ?1',
 187            'SELECT (count(c0_.phonenumber) + 5) * 10 AS sclr0 FROM cms_phonenumbers c0_ INNER JOIN cms_users c1_ ON c0_.user_id = c1_.id WHERE c1_.id = ?'
 188        );
 189    }
 190
 191    /* NOT (YET?) SUPPORTED.
 192       Can be supported if SimpleSelectExpresion supports SingleValuedPathExpression instead of StateFieldPathExpression.
 193
 194    public function testSingleAssociationPathExpressionInSubselect()
 195    {
 196        $this->assertSqlGeneration(
 197            'SELECT (SELECT p.user FROM Doctrine\Tests\Models\CMS\CmsPhonenumber p WHERE p.user = u) user_id FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1',
 198            'SELECT (SELECT c0_.user_id FROM cms_phonenumbers c0_ WHERE c0_.user_id = c1_.id) AS sclr0 FROM cms_users c1_ WHERE c1_.id = ?'
 199        );
 200    }*/
 201
 202    /**
 203     * @group DDC-1077
 204     */
 205    public function testConstantValueInSelect()
 206    {
 207        $this->assertSqlGeneration(
 208            "SELECT u.name, 'foo' AS bar FROM Doctrine\Tests\Models\CMS\CmsUser u",
 209            "SELECT c0_.name AS name0, 'foo' AS sclr1 FROM cms_users c0_"
 210        );
 211    }
 212
 213    public function testSupportsOrderByWithAscAsDefault()
 214    {
 215        $this->assertSqlGeneration(
 216            'SELECT u FROM Doctrine\Tests\Models\Forum\ForumUser u ORDER BY u.id',
 217            'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ ORDER BY f0_.id ASC'
 218        );
 219    }
 220
 221    public function testSupportsOrderByAsc()
 222    {
 223        $this->assertSqlGeneration(
 224            'SELECT u FROM Doctrine\Tests\Models\Forum\ForumUser u ORDER BY u.id asc',
 225            'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ ORDER BY f0_.id ASC'
 226        );
 227    }
 228    public function testSupportsOrderByDesc()
 229    {
 230        $this->assertSqlGeneration(
 231            'SELECT u FROM Doctrine\Tests\Models\Forum\ForumUser u ORDER BY u.id desc',
 232            'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ ORDER BY f0_.id DESC'
 233        );
 234    }
 235
 236    public function testSupportsSelectDistinct()
 237    {
 238        $this->assertSqlGeneration(
 239            'SELECT DISTINCT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u',
 240            'SELECT DISTINCT c0_.name AS name0 FROM cms_users c0_'
 241        );
 242    }
 243
 244    public function testSupportsAggregateFunctionInSelectedFields()
 245    {
 246        $this->assertSqlGeneration(
 247            'SELECT COUNT(u.id) FROM Doctrine\Tests\Models\CMS\CmsUser u GROUP BY u.id',
 248            'SELECT COUNT(c0_.id) AS sclr0 FROM cms_users c0_ GROUP BY c0_.id'
 249        );
 250    }
 251
 252    public function testSupportsAggregateFunctionWithSimpleArithmetic()
 253    {
 254        $this->assertSqlGeneration(
 255            'SELECT MAX(u.id + 4) * 2 FROM Doctrine\Tests\Models\CMS\CmsUser u',
 256            'SELECT MAX(c0_.id + 4) * 2 AS sclr0 FROM cms_users c0_'
 257        );
 258    }
 259
 260    public function testSupportsWhereClauseWithPositionalParameter()
 261    {
 262        $this->assertSqlGeneration(
 263            'select u from Doctrine\Tests\Models\Forum\ForumUser u where u.id = ?1',
 264            'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ WHERE f0_.id = ?'
 265        );
 266    }
 267
 268    public function testSupportsWhereClauseWithNamedParameter()
 269    {
 270        $this->assertSqlGeneration(
 271            'select u from Doctrine\Tests\Models\Forum\ForumUser u where u.username = :name',
 272            'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ WHERE f0_.username = ?'
 273        );
 274    }
 275
 276    public function testSupportsWhereAndClauseWithNamedParameters()
 277    {
 278        $this->assertSqlGeneration(
 279            'select u from Doctrine\Tests\Models\Forum\ForumUser u where u.username = :name and u.username = :name2',
 280            'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ WHERE f0_.username = ? AND f0_.username = ?'
 281        );
 282    }
 283
 284    public function testSupportsCombinedWhereClauseWithNamedParameter()
 285    {
 286        $this->assertSqlGeneration(
 287            'select u from Doctrine\Tests\Models\Forum\ForumUser u where (u.username = :name OR u.username = :name2) AND u.id = :id',
 288            'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ WHERE (f0_.username = ? OR f0_.username = ?) AND f0_.id = ?'
 289        );
 290    }
 291
 292    public function testSupportsAggregateFunctionInASelectDistinct()
 293    {
 294        $this->assertSqlGeneration(
 295            'SELECT COUNT(DISTINCT u.name) FROM Doctrine\Tests\Models\CMS\CmsUser u',
 296            'SELECT COUNT(DISTINCT c0_.name) AS sclr0 FROM cms_users c0_'
 297        );
 298    }
 299
 300    // Ticket #668
 301    public function testSupportsASqlKeywordInAStringLiteralParam()
 302    {
 303        $this->assertSqlGeneration(
 304            "SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name LIKE '%foo OR bar%'",
 305            "SELECT c0_.name AS name0 FROM cms_users c0_ WHERE c0_.name LIKE '%foo OR bar%'"
 306        );
 307    }
 308
 309    public function testSupportsArithmeticExpressionsInWherePart()
 310    {
 311        $this->assertSqlGeneration(
 312            'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE ((u.id + 5000) * u.id + 3) < 10000000',
 313            'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE (c0_.id + 5000) * c0_.id + 3 < 10000000'
 314        );
 315    }
 316
 317    public function testSupportsMultipleEntitiesInFromClause()
 318    {
 319        $this->assertSqlGeneration(
 320            'SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u, Doctrine\Tests\Models\CMS\CmsArticle a JOIN a.user u2 WHERE u.id = u2.id',
 321            'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, c1_.id AS id4, c1_.topic AS topic5, c1_.text AS text6, c1_.version AS version7 FROM cms_users c0_, cms_articles c1_ INNER JOIN cms_users c2_ ON c1_.user_id = c2_.id WHERE c0_.id = c2_.id'
 322        );
 323    }
 324
 325    public function testSupportsMultipleEntitiesInFromClauseUsingPathExpression()
 326    {
 327        $this->assertSqlGeneration(
 328            'SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u, Doctrine\Tests\Models\CMS\CmsArticle a WHERE u.id = a.user',
 329            'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, c1_.id AS id4, c1_.topic AS topic5, c1_.text AS text6, c1_.version AS version7 FROM cms_users c0_, cms_articles c1_ WHERE c0_.id = c1_.user_id'
 330        );
 331    }
 332
 333    public function testSupportsPlainJoinWithoutClause()
 334    {
 335        $this->assertSqlGeneration(
 336            'SELECT u.id, a.id from Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a',
 337            'SELECT c0_.id AS id0, c1_.id AS id1 FROM cms_users c0_ LEFT JOIN cms_articles c1_ ON c0_.id = c1_.user_id'
 338        );
 339        $this->assertSqlGeneration(
 340            'SELECT u.id, a.id from Doctrine\Tests\Models\CMS\CmsUser u JOIN u.articles a',
 341            'SELECT c0_.id AS id0, c1_.id AS id1 FROM cms_users c0_ INNER JOIN cms_articles c1_ ON c0_.id = c1_.user_id'
 342        );
 343    }
 344
 345    /**
 346     * @group DDC-135
 347     */
 348    public function testSupportsJoinAndWithClauseRestriction()
 349    {
 350        $this->assertSqlGeneration(
 351            "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a WITH a.topic LIKE '%foo%'",
 352            "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ LEFT JOIN cms_articles c1_ ON c0_.id = c1_.user_id AND (c1_.topic LIKE '%foo%')"
 353        );
 354        $this->assertSqlGeneration(
 355            "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.articles a WITH a.topic LIKE '%foo%'",
 356            "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ INNER JOIN cms_articles c1_ ON c0_.id = c1_.user_id AND (c1_.topic LIKE '%foo%')"
 357        );
 358    }
 359
 360    /**
 361     * @group DDC-135
 362     * @group DDC-177
 363     */
 364    public function testJoinOnClause_NotYetSupported_ThrowsException()
 365    {
 366        $this->setExpectedException('Doctrine\ORM\Query\QueryException');
 367
 368        $sql = $this->_em->createQuery(
 369            "SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a ON a.topic LIKE '%foo%'"
 370        )->getSql();
 371    }
 372
 373    public function testSupportsMultipleJoins()
 374    {
 375        $this->assertSqlGeneration(
 376            'SELECT u.id, a.id, p.phonenumber, c.id from Doctrine\Tests\Models\CMS\CmsUser u JOIN u.articles a JOIN u.phonenumbers p JOIN a.comments c',
 377            'SELECT c0_.id AS id0, c1_.id AS id1, c2_.phonenumber AS phonenumber2, c3_.id AS id3 FROM cms_users c0_ INNER JOIN cms_articles c1_ ON c0_.id = c1_.user_id INNER JOIN cms_phonenumbers c2_ ON c0_.id = c2_.user_id INNER JOIN cms_comments c3_ ON c1_.id = c3_.article_id'
 378        );
 379    }
 380
 381    public function testSupportsTrimFunction()
 382    {
 383        $this->assertSqlGeneration(
 384            "SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE TRIM(TRAILING ' ' FROM u.name) = 'someone'",
 385            "SELECT c0_.name AS name0 FROM cms_users c0_ WHERE TRIM(TRAILING ' ' FROM c0_.name) = 'someone'"
 386        );
 387    }
 388
 389    // Ticket 894
 390    public function testSupportsBetweenClauseWithPositionalParameters()
 391    {
 392        $this->assertSqlGeneration(
 393            "SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id BETWEEN ?1 AND ?2",
 394            "SELECT c0_.name AS name0 FROM cms_users c0_ WHERE c0_.id BETWEEN ? AND ?"
 395        );
 396    }
 397
 398    /**
 399     * @group DDC-1802
 400     */
 401    public function testSupportsNotBetweenForSizeFunction()
 402    {
 403        $this->assertSqlGeneration(
 404            "SELECT m.name FROM Doctrine\Tests\Models\StockExchange\Market m WHERE SIZE(m.stocks) NOT BETWEEN ?1 AND ?2",
 405            "SELECT e0_.name AS name0 FROM exchange_markets e0_ WHERE (SELECT COUNT(*) FROM exchange_stocks e1_ WHERE e1_.market_id = e0_.id) NOT BETWEEN ? AND ?"
 406        );
 407    }
 408
 409    public function testSupportsFunctionalExpressionsInWherePart()
 410    {
 411        $this->assertSqlGeneration(
 412            "SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE TRIM(u.name) = 'someone'",
 413            // String quoting in the SQL usually depends on the database platform.
 414            // This test works with a mock connection which uses ' for string quoting.
 415            "SELECT c0_.name AS name0 FROM cms_users c0_ WHERE TRIM(c0_.name) = 'someone'"
 416        );
 417    }
 418
 419    public function testSupportsInstanceOfExpressionsInWherePart()
 420    {
 421        $this->assertSqlGeneration(
 422            "SELECT u FROM Doctrine\Tests\Models\Company\CompanyPerson u WHERE u INSTANCE OF Doctrine\Tests\Models\Company\CompanyEmployee",
 423            "SELECT c0_.id AS id0, c0_.name AS name1, c0_.discr AS discr2 FROM company_persons c0_ WHERE c0_.discr IN ('employee')"
 424        );
 425    }
 426
 427    public function testSupportsInstanceOfExpressionInWherePartWithMultipleValues()
 428    {
 429        $this->assertSqlGeneration(
 430            "SELECT u FROM Doctrine\Tests\Models\Company\CompanyPerson u WHERE u INSTANCE OF (Doctrine\Tests\Models\Company\CompanyEmployee, \Doctrine\Tests\Models\Company\CompanyManager)",
 431            "SELECT c0_.id AS id0, c0_.name AS name1, c0_.discr AS discr2 FROM company_persons c0_ WHERE c0_.discr IN ('employee', 'manager')"
 432        );
 433    }
 434
 435    /**
 436     * @group DDC-1194
 437     */
 438    public function testSupportsInstanceOfExpressionsInWherePartPrefixedSlash()
 439    {
 440        $this->assertSqlGeneration(
 441            "SELECT u FROM Doctrine\Tests\Models\Company\CompanyPerson u WHERE u INSTANCE OF \Doctrine\Tests\Models\Company\CompanyEmployee",
 442            "SELECT c0_.id AS id0, c0_.name AS name1, c0_.discr AS discr2 FROM company_persons c0_ WHERE c0_.discr IN ('employee')"
 443        );
 444    }
 445
 446    /**
 447     * @group DDC-1194
 448     */
 449    public function testSupportsInstanceOfExpressionsInWherePartWithUnrelatedClass()
 450    {
 451        $this->assertInvalidSqlGeneration(
 452            "SELECT u FROM Doctrine\Tests\Models\Company\CompanyPerson u WHERE u INSTANCE OF \Doctrine\Tests\Models\CMS\CmsUser",
 453            "Doctrine\ORM\Query\QueryException"
 454        );
 455    }
 456
 457    public function testSupportsInstanceOfExpressionsInWherePartInDeeperLevel()
 458    {
 459        $this->assertSqlGeneration(
 460            "SELECT u FROM Doctrine\Tests\Models\Company\CompanyEmployee u WHERE u INSTANCE OF Doctrine\Tests\Models\Company\CompanyManager",
 461            "SELECT c0_.id AS id0, c0_.name AS name1, c1_.salary AS salary2, c1_.department AS department3, c1_.startDate AS startDate4, c0_.discr AS discr5 FROM company_employees c1_ INNER JOIN company_persons c0_ ON c1_.id = c0_.id WHERE c0_.discr IN ('manager')"
 462        );
 463    }
 464
 465    public function testSupportsInstanceOfExpressionsInWherePartInDeepestLevel()
 466    {
 467        $this->assertSqlGeneration(
 468            "SELECT u FROM Doctrine\Tests\Models\Company\CompanyManager u WHERE u INSTANCE OF Doctrine\Tests\Models\Company\CompanyManager",
 469            "SELECT c0_.id AS id0, c0_.name AS name1, c1_.salary AS salary2, c1_.department AS department3, c1_.startDate AS startDate4, c2_.title AS title5, c0_.discr AS discr6 FROM company_managers c2_ INNER JOIN company_employees c1_ ON c2_.id = c1_.id INNER JOIN company_persons c0_ ON c2_.id = c0_.id WHERE c0_.discr IN ('manager')"
 470        );
 471    }
 472
 473    public function testSupportsInstanceOfExpressionsUsingInputParameterInWherePart()
 474    {
 475        $this->assertSqlGeneration(
 476            "SELECT u FROM Doctrine\Tests\Models\Company\CompanyPerson u WHERE u INSTANCE OF ?1",
 477            "SELECT c0_.id AS id0, c0_.name AS name1, c0_.discr AS discr2 FROM company_persons c0_ WHERE c0_.discr IN (?)",
 478            array(), array(1 => $this->_em->getClassMetadata('Doctrine\Tests\Models\Company\CompanyEmployee'))
 479        );
 480    }
 481
 482    // Ticket #973
 483    public function testSupportsSingleValuedInExpressionWithoutSpacesInWherePart()
 484    {
 485        $this->assertSqlGeneration(
 486            "SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE IDENTITY(u.email) IN(46)",
 487            "SELECT c0_.name AS name0 FROM cms_users c0_ WHERE c0_.email_id IN (46)"
 488        );
 489    }
 490
 491    public function testSupportsMultipleValuedInExpressionInWherePart()
 492    {
 493        $this->assertSqlGeneration(
 494            'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id IN (1, 2)',
 495            'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE c0_.id IN (1, 2)'
 496        );
 497    }
 498
 499    public function testSupportsNotInExpressionInWherePart()
 500    {
 501        $this->assertSqlGeneration(
 502            'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE :id NOT IN (1)',
 503            'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE ? NOT IN (1)'
 504        );
 505    }
 506
 507    /**
 508     * @group DDC-1802
 509     */
 510    public function testSupportsNotInExpressionForModFunction()
 511    {
 512        $this->assertSqlGeneration(
 513            "SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE MOD(u.id, 5) NOT IN(1,3,4)",
 514            "SELECT c0_.name AS name0 FROM cms_users c0_ WHERE MOD(c0_.id, 5) NOT IN (1, 3, 4)"
 515        );
 516    }
 517
 518    public function testInExpressionWithSingleValuedAssociationPathExpressionInWherePart()
 519    {
 520        $this->assertSqlGeneration(
 521            'SELECT u FROM Doctrine\Tests\Models\Forum\ForumUser u WHERE u.avatar IN (?1, ?2)',
 522            'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ WHERE f0_.avatar_id IN (?, ?)'
 523        );
 524    }
 525
 526    public function testInvalidInExpressionWithSingleValuedAssociationPathExpressionOnInverseSide()
 527    {
 528        // We do not support SingleValuedAssociationPathExpression on inverse side
 529        $this->assertInvalidSqlGeneration(
 530            "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.address IN (?1, ?2)",
 531            "Doctrine\ORM\Query\QueryException"
 532        );
 533    }
 534
 535    public function testSupportsConcatFunctionForMysqlAndPostgresql()
 536    {
 537        $connMock = $this->_em->getConnection();
 538        $orgPlatform = $connMock->getDatabasePlatform();
 539
 540        $connMock->setDatabasePlatform(new \Doctrine\DBAL\Platforms\MySqlPlatform);
 541        $this->assertSqlGeneration(
 542            "SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE CONCAT(u.name, 's') = ?1",
 543            "SELECT c0_.id AS id0 FROM cms_users c0_ WHERE CONCAT(c0_.name, 's') = ?"
 544        );
 545        $this->assertSqlGeneration(
 546            "SELECT CONCAT(u.id, u.name) FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1",
 547            "SELECT CONCAT(c0_.id, c0_.name) AS sclr0 FROM cms_users c0_ WHERE c0_.id = ?"
 548        );
 549
 550        $connMock->setDatabasePlatform(new \Doctrine\DBAL\Platforms\PostgreSqlPlatform);
 551        $this->assertSqlGeneration(
 552            "SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE CONCAT(u.name, 's') = ?1",
 553            "SELECT c0_.id AS id0 FROM cms_users c0_ WHERE c0_.name || 's' = ?"
 554        );
 555        $this->assertSqlGeneration(
 556            "SELECT CONCAT(u.id, u.name) FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1",
 557            "SELECT c0_.id || c0_.name AS sclr0 FROM cms_users c0_ WHERE c0_.id = ?"
 558        );
 559
 560        $connMock->setDatabasePlatform($orgPlatform);
 561    }
 562
 563    public function testSupportsExistsExpressionInWherePartWithCorrelatedSubquery()
 564    {
 565        $this->assertSqlGeneration(
 566            'SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE EXISTS (SELECT p.phonenumber FROM Doctrine\Tests\Models\CMS\CmsPhonenumber p WHERE p.phonenumber = u.id)',
 567            'SELECT c0_.id AS id0 FROM cms_users c0_ WHERE EXISTS (SELECT c1_.phonenumber FROM cms_phonenumbers c1_ WHERE c1_.phonenumber = c0_.id)'
 568        );
 569    }
 570
 571    /**
 572     * @group DDC-593
 573     */
 574    public function testSubqueriesInComparisonExpression()
 575    {
 576        $this->assertSqlGeneration(
 577            'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE (u.id >= (SELECT u2.id FROM Doctrine\Tests\Models\CMS\CmsUser u2 WHERE u2.name = :name)) AND (u.id <= (SELECT u3.id FROM Doctrine\Tests\Models\CMS\CmsUser u3 WHERE u3.name = :name))',
 578            'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE (c0_.id >= (SELECT c1_.id FROM cms_users c1_ WHERE c1_.name = ?)) AND (c0_.id <= (SELECT c2_.id FROM cms_users c2_ WHERE c2_.name = ?))'
 579        );
 580    }
 581
 582    public function testSupportsMemberOfExpressionOneToMany()
 583    {
 584        // "Get all users who have $phone as a phonenumber." (*cough* doesnt really make sense...)
 585        $q = $this->_em->createQuery('SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE :param MEMBER OF u.phonenumbers');
 586        $q->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);
 587
 588        $phone = new \Doctrine\Tests\Models\CMS\CmsPhonenumber;
 589        $phone->phonenumber = 101;
 590        $q->setParameter('param', $phone);
 591
 592        $this->assertEquals(
 593            'SELECT c0_.id AS id0 FROM cms_users c0_ WHERE EXISTS (SELECT 1 FROM cms_phonenumbers c1_ WHERE c0_.id = c1_.user_id AND c1_.phonenumber = ?)',
 594            $q->getSql()
 595        );
 596    }
 597
 598    public function testSupportsMemberOfExpressionManyToMany()
 599    {
 600        // "Get all users who are members of $group."
 601        $q = $this->_em->createQuery('SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE :param MEMBER OF u.groups');
 602        $q->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);
 603
 604        $group = new \Doctrine\Tests\Models\CMS\CmsGroup;
 605        $group->id = 101;
 606        $q->setParameter('param', $group);
 607
 608        $this->assertEquals(
 609            'SELECT c0_.id AS id0 FROM cms_users c0_ WHERE EXISTS (SELECT 1 FROM cms_users_groups c1_ INNER JOIN cms_groups c2_ ON c1_.group_id = c2_.id WHERE c1_.user_id = c0_.id AND c2_.id = ?)',
 610            $q->getSql()
 611        );
 612    }
 613
 614    public function testSupportsMemberOfExpressionSelfReferencing()
 615    {
 616        // "Get all persons who have $person as a friend."
 617        // Tough one: Many-many self-referencing ("friends") with class table inheritance
 618        $q = $this->_em->createQuery('SELECT p FROM Doctrine\Tests\Models\Company\CompanyPerson p WHERE :param MEMBER OF p.friends');
 619        $person = new \Doctrine\Tests\Models\Company\CompanyPerson;
 620        $this->_em->getClassMetadata(get_class($person))->setIdentifierValues($person, array('id' => 101));
 621        $q->setParameter('param', $person);
 622        $this->assertEquals(
 623            'SELECT c0_.id AS id0, c0_.name AS name1, c1_.title AS title2, c2_.salary AS salary3, c2_.department AS department4, c2_.startDate AS startDate5, c0_.discr AS discr6, c0_.spouse_id AS spouse_id7, c1_.car_id AS car_id8 FROM company_persons c0_ LEFT JOIN company_managers c1_ ON c0_.id = c1_.id LEFT JOIN company_employees c2_ ON c0_.id = c2_.id WHERE EXISTS (SELECT 1 FROM company_persons_friends c3_ INNER JOIN company_persons c4_ ON c3_.friend_id = c4_.id WHERE c3_.person_id = c0_.id AND c4_.id = ?)',
 624            $q->getSql()
 625        );
 626    }
 627
 628    public function testSupportsMemberOfWithSingleValuedAssociation()
 629    {
 630        // Impossible example, but it illustrates the purpose
 631        $q = $this->_em->createQuery('SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.email MEMBER OF u.groups');
 632
 633        $this->assertEquals(
 634            'SELECT c0_.id AS id0 FROM cms_users c0_ WHERE EXISTS (SELECT 1 FROM cms_users_groups c1_ INNER JOIN cms_groups c2_ ON c1_.group_id = c2_.id WHERE c1_.user_id = c0_.id AND c2_.id = c0_.email_id)',
 635            $q->getSql()
 636        );
 637    }
 638
 639    public function testSupportsMemberOfWithIdentificationVariable()
 640    {
 641        // Impossible example, but it illustrates the purpose
 642        $q = $this->_em->createQuery('SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u MEMBER OF u.groups');
 643
 644        $this->assertEquals(
 645            'SELECT c0_.id AS id0 FROM cms_users c0_ WHERE EXISTS (SELECT 1 FROM cms_users_groups c1_ INNER JOIN cms_groups c2_ ON c1_.group_id = c2_.id WHERE c1_.user_id = c0_.id AND c2_.id = c0_.id)',
 646            $q->getSql()
 647        );
 648    }
 649
 650    public function testSupportsCurrentDateFunction()
 651    {
 652        $q = $this->_em->createQuery('SELECT d.id FROM Doctrine\Tests\Models\Generic\DateTimeModel d WHERE d.datetime > current_date()');
 653        $q->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);
 654        $this->assertEquals('SELECT d0_.id AS id0 FROM date_time_model d0_ WHERE d0_.col_datetime > CURRENT_DATE', $q->getSql());
 655    }
 656
 657    public function testSupportsCurrentTimeFunction()
 658    {
 659        $q = $this->_em->createQuery('SELECT d.id FROM Doctrine\Tests\Models\Generic\DateTimeModel d WHERE d.time > current_time()');
 660        $q->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);
 661        $this->assertEquals('SELECT d0_.id AS id0 FROM date_time_model d0_ WHERE d0_.col_time > CURRENT_TIME', $q->getSql());
 662    }
 663
 664    public function testSupportsCurrentTimestampFunction()
 665    {
 666        $q = $this->_em->createQuery('SELECT d.id FROM Doctrine\Tests\Models\Generic\DateTimeModel d WHERE d.datetime > current_timestamp()');
 667        $q->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);
 668        $this->assertEquals('SELECT d0_.id AS id0 FROM date_time_model d0_ WHERE d0_.col_datetime > CURRENT_TIMESTAMP', $q->getSql());
 669    }
 670
 671    public function testExistsExpressionInWhereCorrelatedSubqueryAssocCondition()
 672    {
 673        $this->assertSqlGeneration(
 674            // DQL
 675            // The result of this query consists of all employees whose spouses are also employees.
 676            'SELECT DISTINCT emp FROM Doctrine\Tests\Models\CMS\CmsEmployee emp
 677                WHERE EXISTS (
 678                    SELECT spouseEmp
 679                    FROM Doctrine\Tests\Models\CMS\CmsEmployee spouseEmp
 680                    WHERE spouseEmp = emp.spouse)',
 681            // SQL
 682            'SELECT DISTINCT c0_.id AS id0, c0_.name AS name1 FROM cms_employees c0_'
 683                . ' WHERE EXISTS ('
 684                    . 'SELECT c1_.id FROM cms_employees c1_ WHERE c1_.id = c0_.spouse_id'
 685                    . ')'
 686        );
 687    }
 688
 689    public function testExistsExpressionWithSimpleSelectReturningScalar()
 690    {
 691        $this->assertSqlGeneration(
 692            // DQL
 693            // The result of this query consists of all employees whose spouses are also employees.
 694            'SELECT DISTINCT emp FROM Doctrine\Tests\Models\CMS\CmsEmployee emp
 695                WHERE EXISTS (
 696                    SELECT 1
 697                    FROM Doctrine\Tests\Models\CMS\CmsEmployee spouseEmp
 698                    WHERE spouseEmp = emp.spouse)',
 699            // SQL
 700            'SELECT DISTINCT c0_.id AS id0, c0_.name AS name1 FROM cms_employees c0_'
 701                . ' WHERE EXISTS ('
 702                    . 'SELECT 1 AS sclr2 FROM cms_employees c1_ WHERE c1_.id = c0_.spouse_id'
 703                    . ')'
 704        );
 705    }
 706
 707    public function testLimitFromQueryClass()
 708    {
 709        $q = $this->_em
 710            ->createQuery('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u')
 711            ->setMaxResults(10);
 712
 713        $this->assertEquals('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_ LIMIT 10', $q->getSql());
 714    }
 715
 716    public function testLimitAndOffsetFromQueryClass()
 717    {
 718        $q = $this->_em
 719            ->createQuery('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u')
 720            ->setMaxResults(10)
 721            ->setFirstResult(0);
 722
 723        $this->assertEquals('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_ LIMIT 10 OFFSET 0', $q->getSql());
 724    }
 725
 726    public function testSizeFunction()
 727    {
 728        $this->assertSqlGeneration(
 729            "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE SIZE(u.phonenumbers) > 1",
 730            "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE (SELECT COUNT(*) FROM cms_phonenumbers c1_ WHERE c1_.user_id = c0_.id) > 1"
 731        );
 732    }
 733
 734    public function testSizeFunctionSupportsManyToMany()
 735    {
 736        $this->assertSqlGeneration(
 737            "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE SIZE(u.groups) > 1",
 738            "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE (SELECT COUNT(*) FROM cms_users_groups c1_ WHERE c1_.user_id = c0_.id) > 1"
 739        );
 740    }
 741
 742    public function testEmptyCollectionComparisonExpression()
 743    {
 744        $this->assertSqlGeneration(
 745            "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.phonenumbers IS EMPTY",
 746            "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE (SELECT COUNT(*) FROM cms_phonenumbers c1_ WHERE c1_.user_id = c0_.id) = 0"
 747        );
 748        $this->assertSqlGeneration(
 749            "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.phonenumbers IS NOT EMPTY",
 750            "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE (SELECT COUNT(*) FROM cms_phonenumbers c1_ WHERE c1_.user_id = c0_.id) > 0"
 751        );
 752    }
 753
 754    public function testNestedExpressions()
 755    {
 756        $this->assertSqlGeneration(
 757            "select u from Doctrine\Tests\Models\CMS\CmsUser u where u.id > 10 and u.id < 42 and ((u.id * 2) > 5)",
 758            "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE c0_.id > 10 AND c0_.id < 42 AND (c0_.id * 2 > 5)"
 759        );
 760    }
 761
 762    public function testNestedExpressions2()
 763    {
 764        $this->assertSqlGeneration(
 765            "select u from Doctrine\Tests\Models\CMS\CmsUser u where (u.id > 10) and (u.id < 42 and ((u.id * 2) > 5)) or u.id <> 42",
 766            "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE (c0_.id > 10) AND (c0_.id < 42 AND (c0_.id * 2 > 5)) OR c0_.id <> 42"
 767        );
 768    }
 769
 770    public function testNestedExpressions3()
 771    {
 772        $this->assertSqlGeneration(
 773            "select u from Doctrine\Tests\Models\CMS\CmsUser u where (u.id > 10) and (u.id between 1 and 10 or u.id in (1, 2, 3, 4, 5))",
 774            "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE (c0_.id > 10) AND (c0_.id BETWEEN 1 AND 10 OR c0_.id IN (1, 2, 3, 4, 5))"
 775        );
 776    }
 777
 778    public function testOrderByCollectionAssociationSize()
 779    {
 780        $this->assertSqlGeneration(
 781            "select u, size(u.articles) as numArticles from Doctrine\Tests\Models\CMS\CmsUser u order by numArticles",
 782            "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, (SELECT COUNT(*) FROM cms_articles c1_ WHERE c1_.user_id = c0_.id) AS sclr4 FROM cms_users c0_ ORDER BY sclr4 ASC"
 783        );
 784    }
 785
 786    public function testOrderBySupportsSingleValuedPathExpressionOwningSide()
 787    {
 788        $this->assertSqlGeneration(
 789            "select a from Doctrine\Tests\Models\CMS\CmsArticle a order by a.user",
 790            "SELECT c0_.id AS id0, c0_.topic AS topic1, c0_.text AS text2, c0_.version AS version3 FROM cms_articles c0_ ORDER BY c0_.user_id ASC"
 791        );
 792    }
 793
 794    /**
 795     * @expectedException Doctrine\ORM\Query\QueryException
 796     */
 797    public function testOrderBySupportsSingleValuedPathExpressionInverseSide()
 798    {
 799        $q = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u order by u.address");
 800        $q->getSQL();
 801    }
 802
 803    public function testBooleanLiteralInWhereOnSqlite()
 804    {
 805        $oldPlat = $this->_em->getConnection()->getDatabasePlatform();
 806        $this->_em->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\SqlitePlatform);
 807
 808        $this->assertSqlGeneration(
 809            "SELECT b FROM Doctrine\Tests\Models\Generic\BooleanModel b WHERE b.booleanField = true",
 810            "SELECT b0_.id AS id0, b0_.booleanField AS booleanField1 FROM boolean_model b0_ WHERE b0_.booleanField = 1"
 811        );
 812
 813        $this->assertSqlGeneration(
 814            "SELECT b FROM Doctrine\Tests\Models\Generic\BooleanModel b WHERE b.booleanField = false",
 815            "SELECT b0_.id AS id0, b0_.booleanField AS booleanField1 FROM boolean_model b0_ WHERE b0_.booleanField = 0"
 816        );
 817
 818        $this->_em->getConnection()->setDatabasePlatform($oldPlat);
 819    }
 820
 821    public function testBooleanLiteralInWhereOnPostgres()
 822    {
 823        $oldPlat = $this->_em->getConnection()->getDatabasePlatform();
 824        $this->_em->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\PostgreSqlPlatform);
 825
 826        $this->assertSqlGeneration(
 827            "SELECT b FROM Doctrine\Tests\Models\Generic\BooleanModel b WHERE b.booleanField = true",
 828            "SELECT b0_.id AS id0, b0_.booleanField AS booleanfield1 FROM boolean_model b0_ WHERE b0_.booleanField = true"
 829        );
 830
 831        $this->assertSqlGeneration(
 832            "SELECT b FROM Doctrine\Tests\Models\Generic\BooleanModel b WHERE b.booleanField = false",
 833            "SELECT b0_.id AS id0, b0_.booleanField AS booleanfield1 FROM boolean_model b0_ WHERE b0_.booleanField = false"
 834        );
 835
 836        $this->_em->getConnection()->setDatabasePlatform($oldPlat);
 837    }
 838
 839    public function testSingleValuedAssociationFieldInWhere()
 840    {
 841        $this->assertSqlGeneration(
 842            "SELECT p FROM Doctrine\Tests\Models\CMS\CmsPhonenumber p WHERE p.user = ?1",
 843            "SELECT c0_.phonenumber AS phonenumber0 FROM cms_phonenumbers c0_ WHERE c0_.user_id = ?"
 844        );
 845    }
 846
 847    public function testSingleValuedAssociationNullCheckOnOwningSide()
 848    {
 849        $this->assertSqlGeneration(
 850            "SELECT a FROM Doctrine\Tests\Models\CMS\CmsAddress a WHERE a.user IS NULL",
 851            "SELECT c0_.id AS id0, c0_.country AS country1, c0_.zip AS zip2, c0_.city AS city3 FROM cms_addresses c0_ WHERE c0_.user_id IS NULL"
 852        );
 853    }
 854
 855    // Null check on inverse side has to happen through explicit JOIN.
 856    // "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.address IS NULL"
 857    // where the CmsUser is the inverse side is not supported.
 858    public function testSingleValuedAssociationNullCheckOnInverseSide()
 859    {
 860        $this->assertSqlGeneration(
 861            "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.address a WHERE a.id IS NULL",
 862            "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ LEFT JOIN cms_addresses c1_ ON c0_.id = c1_.user_id WHERE c1_.id IS NULL"
 863        );
 864    }
 865
 866    /**
 867     * @group DDC-339
 868     * @group DDC-1572
 869     */
 870    public function testStringFunctionLikeExpression()
 871    {
 872        $this->assertSqlGeneration(
 873            "SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE LOWER(u.name) LIKE '%foo OR bar%'",
 874            "SELECT c0_.name AS name0 FROM cms_users c0_ WHERE LOWER(c0_.name) LIKE '%foo OR bar%'"
 875        );
 876        $this->assertSqlGeneration(
 877            "SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE LOWER(u.name) LIKE :str",
 878            "SELECT c0_.name AS name0 FROM cms_users c0_ WHERE LOWER(c0_.name) LIKE ?"
 879        );
 880        $this->assertSqlGeneration(
 881            "SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE CONCAT(UPPER(u.name), '_moo') LIKE :str",
 882            "SELECT c0_.name AS name0 FROM cms_users c0_ WHERE UPPER(c0_.name) || '_moo' LIKE ?"
 883        );
 884
 885        // DDC-1572
 886        $this->assertSqlGeneration(
 887            "SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE UPPER(u.name) LIKE UPPER(:str)",
 888            "SELECT c0_.name AS name0 FROM cms_users c0_ WHERE UPPER(c0_.name) LIKE UPPER(?)"
 889        );
 890        $this->assertSqlGeneration(
 891            "SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE UPPER(LOWER(u.name)) LIKE UPPER(LOWER(:str))",
 892            "SELECT c0_.name AS name0 FROM cms_users c0_ WHERE UPPER(LOWER(c0_.name)) LIKE UPPER(LOWER(?))"
 893        );
 894        $this->assertSqlGeneration(
 895            "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a WITH a.topic LIKE u.name",
 896            "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ LEFT JOIN cms_articles c1_ ON c0_.id = c1_.user_id AND (c1_.topic LIKE c0_.name)"
 897        );
 898    }
 899
 900    /**
 901     * @group DDC-1802
 902     */
 903    public function testStringFunctionNotLikeExpression()
 904    {
 905        $this->assertSqlGeneration(
 906                "SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE LOWER(u.name) NOT LIKE '%foo OR bar%'",
 907                "SELECT c0_.name AS name0 FROM cms_users c0_ WHERE LOWER(c0_.name) NOT LIKE '%foo OR bar%'"
 908        );
 909
 910        $this->assertSqlGeneration(
 911                "SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE UPPER(LOWER(u.name)) NOT LIKE UPPER(LOWER(:str))",
 912                "SELECT c0_.name AS name0 FROM cms_users c0_ WHERE UPPER(LOWER(c0_.name)) NOT LIKE UPPER(LOWER(?))"
 913        );
 914        $this->assertSqlGeneration(
 915                "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a WITH a.topic NOT LIKE u.name",
 916                "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ LEFT JOIN cms_articles c1_ ON c0_.id = c1_.user_id AND (c1_.topic NOT LIKE c0_.name)"
 917        );
 918    }
 919
 920    /**
 921     * @group DDC-338
 922     */
 923    public function testOrderedCollectionFetchJoined()
 924    {
 925        $this->assertSqlGeneration(
 926            "SELECT r, l FROM Doctrine\Tests\Models\Routing\RoutingRoute r JOIN r.legs l",
 927            "SELECT r0_.id AS id0, r1_.id AS id1, r1_.departureDate AS departureDate2, r1_.arrivalDate AS arrivalDate3 FROM RoutingRoute r0_ INNER JOIN RoutingRouteLegs r2_ ON r0_.id = r2_.route_id INNER JOIN RoutingLeg r1_ ON r1_.id = r2_.leg_id ".
 928            "ORDER BY r1_.departureDate ASC"
 929        );
 930    }
 931
 932    public function testSubselectInSelect()
 933    {
 934        $this->assertSqlGeneration(
 935            "SELECT u.name, (SELECT COUNT(p.phonenumber) FROM Doctrine\Tests\Models\CMS\CmsPhonenumber p WHERE p.phonenumber = 1234) pcount FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name = 'jon'",
 936            "SELECT c0_.name AS name0, (SELECT COUNT(c1_.phonenumber) AS dctrn__1 FROM cms_phonenumbers c1_ WHERE c1_.phonenumber = 1234) AS sclr1 FROM cms_users c0_ WHERE c0_.name = 'jon'"
 937        );
 938    }
 939
 940    /**
 941     * @group locking
 942     * @group DDC-178
 943     */
 944    public function testPessimisticWriteLockQueryHint()
 945    {
 946        if ($this->_em->getConnection()->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\SqlitePlatform) {
 947            $this->markTestSkipped('SqLite does not support Row locking at all.');
 948        }
 949
 950        $this->assertSqlGeneration(
 951            "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = 'gblanco'",
 952            "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 ".
 953            "FROM cms_users c0_ WHERE c0_.username = 'gblanco' FOR UPDATE",
 954            array(Query::HINT_LOCK_MODE => \Doctrine\DBAL\LockMode::PESSIMISTIC_WRITE)
 955        );
 956    }
 957
 958    /**
 959     * @group locking
 960     * @group DDC-178
 961     */
 962    public function testPessimisticReadLockQueryHintPostgreSql()
 963    {
 964        $this->_em->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\PostgreSqlPlatform);
 965
 966        $this->assertSqlGeneration(
 967            "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = 'gblanco'",
 968            "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 ".
 969            "FROM cms_users c0_ WHERE c0_.username = 'gblanco' FOR SHARE",
 970            array(Query::HINT_LOCK_MODE => \Doctrine\DBAL\LockMode::PESSIMISTIC_READ)
 971                );
 972    }
 973
 974    /**
 975     * @group DDC-1693
 976     * @group locking
 977     */
 978    public function testLockModeNoneQueryHint()
 979    {
 980        $this->assertSqlGeneration(
 981            "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = 'gblanco'",
 982            "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 ".
 983            "FROM cms_users c0_ WHERE c0_.username = 'gblanco'",
 984            array(Query::HINT_LOCK_MODE => \Doctrine\DBAL\LockMode::NONE)
 985                );
 986    }
 987
 988    /**
 989     * @group DDC-430
 990     */
 991    public function testSupportSelectWithMoreThan10InputParameters()
 992    {
 993        $this->assertSqlGeneration(
 994            "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1 OR u.id = ?2 OR u.id = ?3 OR u.id = ?4 OR u.id = ?5 OR u.id = ?6 OR u.id = ?7 OR u.id = ?8 OR u.id = ?9 OR u.id = ?10 OR u.id = ?11",
 995            "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE c0_.id = ? OR c0_.id = ? OR c0_.id = ? OR c0_.id = ? OR c0_.id = ? OR c0_.id = ? OR c0_.id = ? OR c0_.id = ? OR c0_.id = ? OR c0_.id = ? OR c0_.id = ?"
 996        );
 997    }
 998
 999    /**
1000     * @group locking
1001     * @group DDC-178
1002     */
1003    public function testPessimisticReadLockQueryHintMySql()
1004    {
1005        $this->_em->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\MySqlPlatform);
1006
1007        $this->assertSqlGeneration(
1008            "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = 'gblanco'",
1009            "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 ".
1010            "FROM cms_users c0_ WHERE c0_.username = 'gblanco' LOCK IN SHARE MODE",
1011            array(Query::HINT_LOCK_MODE => \Doctrine\DBAL\LockMode::PESSIMISTIC_READ)
1012        );
1013    }
1014
1015    /**
1016     * @group locking
1017     * @group DDC-178
1018     */
1019    public function testPessimisticReadLockQueryHintOracle()
1020    {
1021        $this->_em->getConnection()->setDatabasePlatform(new \Doctrine\DBAL\Platforms\OraclePlatform);
1022
1023        $this->assertSqlGeneration(
1024            "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = 'gblanco'",
1025            "SELECT c0_.id AS ID0, c0_.status AS STATUS1, c0_.username AS USERNAME2, c0_.name AS NAME3 ".
1026            "FROM cms_users c0_ WHERE c0_.username = 'gblanco' FOR UPDATE",
1027            array(Query::HINT_LOCK_MODE => \Doctrine\DBAL\LockMode::PESSIMISTIC_READ)
1028        );
1029    }
1030
1031    /**
1032     * @group DDC-431
1033     */
1034    public function testSupportToCustomDQLFunctions()
1035    {
1036        $config = $this->_em->getConfiguration();
1037        $config->addCustomNumericFunction('MYABS', 'Doctrine\Tests\ORM\Query\MyAbsFunction');
1038
1039        $this->assertSqlGeneration(
1040            'SELECT MYABS(p.phonenumber) FROM Doctrine\Tests\Models\CMS\CmsPhonenumber p',
1041            'SELECT ABS(c0_.phonenumber) AS sclr0 FROM cms_phonenumbers c0_'
1042        );
1043
1044        $config->setCustomNumericFunctions(array());
1045    }
1046
1047    /**
1048     * @group DDC-826
1049     */
1050    public function testMappedSuperclassAssociationJoin()
1051    {
1052        $this->assertSqlGeneration(
1053            'SELECT f FROM Doctrine\Tests\Models\DirectoryTree\File f JOIN f.parentDirectory d WHERE f.id = ?1',
1054            'SELECT f0_.id AS id0, f0_.extension AS extension1, f0_.name AS name2 FROM "file" f0_ INNER JOIN Directory d1_ ON f0_.parentDirectory_id = d1_.id WHERE f0_.id = ?'
1055        );
1056    }
1057
1058    /**
1059     * @group DDC-1053
1060     */
1061    public function testGroupBy()
1062    {
1063        $this->assertSqlGeneration(
1064            'SELECT g.id, count(u.id) FROM Doctrine\Tests\Models\CMS\CmsGroup g JOIN g.users u GROUP BY g.id',
1065            'SELECT c0_.id AS id0, count(c1_.id) AS sclr1 FROM cms_groups c0_ INNER JOIN cms_users_groups c2_ ON c0_.id = c2_.group_id INNER JOIN cms_users c1_ ON c1_.id = c2_.user_id GROUP BY c0_.id'
1066        );
1067    }
1068
1069    /**
1070     * @group DDC-1053
1071     */
1072    public function testGroupByIdentificationVariable()
1073    {
1074        $this->assertSqlGeneration(
1075            'SELECT g, count(u.id) FROM Doctrine\Tests\Models\CMS\CmsGroup g JOIN g.users u GROUP BY g',
1076            'SELECT c0_.id AS id0, c0_.name AS name1, count(c1_.id) AS sclr2 FROM cms_groups c0_ INNER JOIN cms_users_groups c2_ ON c0_.id = c2_.group_id INNER JOIN cms_users c1_ ON c1_.id = c2_.user_…

Large files files are truncated, but you can click here to view the full file