1
0
mirror of synced 2024-12-15 23:56:02 +03:00
doctrine2/tests/Doctrine/Tests/ORM/Query/LanguageRecognitionTest.php

322 lines
12 KiB
PHP
Raw Normal View History

2008-05-24 22:18:37 +04:00
<?php
namespace Doctrine\Tests\ORM\Query;
require_once __DIR__ . '/../../TestInit.php';
class LanguageRecognitionTest extends \Doctrine\Tests\OrmTestCase
2008-05-24 22:18:37 +04:00
{
private $_em;
protected function setUp()
{
$this->_em = $this->_getTestEntityManager();
}
public function assertValidDql($dql, $debug = false)
2008-05-24 22:18:37 +04:00
{
try {
$query = $this->_em->createQuery($dql);
2008-05-24 22:18:37 +04:00
$parserResult = $query->parse();
} catch (\Exception $e) {
if ($debug) {
echo $e->getTraceAsString() . PHP_EOL;
}
2008-05-24 22:18:37 +04:00
$this->fail($e->getMessage());
}
}
public function assertInvalidDql($dql, $debug = false)
2008-05-24 22:18:37 +04:00
{
try {
$query = $this->_em->createQuery($dql);
2008-05-24 22:18:37 +04:00
$query->setDql($dql);
$parserResult = $query->parse();
$this->fail('No syntax errors were detected, when syntax errors were expected');
} catch (\Exception $e) {
//echo $e->getMessage() . PHP_EOL;
if ($debug) {
echo $e->getMessage() . PHP_EOL;
echo $e->getTraceAsString() . PHP_EOL;
}
2008-05-24 22:18:37 +04:00
// It was expected!
}
}
public function testEmptyQueryString()
{
$this->assertInvalidDql('');
}
public function testPlainFromClauseWithAlias()
{
$this->assertValidDql('SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u');
2008-05-24 22:18:37 +04:00
}
public function testSelectSingleComponentWithAsterisk()
{
$this->assertValidDql('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u');
2008-05-24 22:18:37 +04:00
}
public function testInvalidSelectSingleComponentWithAsterisk()
{
$this->assertInvalidDql('SELECT p FROM Doctrine\Tests\Models\CMS\CmsUser u');
2008-05-24 22:18:37 +04:00
}
public function testSelectSingleComponentWithMultipleColumns()
{
$this->assertValidDql('SELECT u.name, u.username FROM Doctrine\Tests\Models\CMS\CmsUser u');
2008-05-24 22:18:37 +04:00
}
public function testSelectMultipleComponentsWithAsterisk()
{
$this->assertValidDql('SELECT u, p FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.phonenumbers p');
2008-05-24 22:18:37 +04:00
}
public function testSelectDistinctIsSupported()
{
$this->assertValidDql('SELECT DISTINCT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u');
2008-05-24 22:18:37 +04:00
}
public function testAggregateFunctionInSelect()
{
$this->assertValidDql('SELECT COUNT(u.id) FROM Doctrine\Tests\Models\CMS\CmsUser u');
2008-05-24 22:18:37 +04:00
}
public function testAggregateFunctionWithDistinctInSelect()
{
$this->assertValidDql('SELECT COUNT(DISTINCT u.name) FROM Doctrine\Tests\Models\CMS\CmsUser u');
2008-05-24 22:18:37 +04:00
}
2009-03-15 00:19:50 +03:00
2008-05-24 22:18:37 +04:00
public function testFunctionalExpressionsSupportedInWherePart()
{
$this->assertValidDql("SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE TRIM(u.name) = 'someone'");
2008-05-24 22:18:37 +04:00
}
2009-03-15 00:19:50 +03:00
2008-05-24 22:18:37 +04:00
public function testArithmeticExpressionsSupportedInWherePart()
{
$this->assertValidDql('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE ((u.id + 5000) * u.id + 3) < 10000000');
2008-05-24 22:18:37 +04:00
}
2009-03-15 00:19:50 +03:00
2008-05-24 22:18:37 +04:00
public function testInExpressionSupportedInWherePart()
{
$this->assertValidDql('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id IN (1, 2)');
2008-05-24 22:18:37 +04:00
}
public function testNotInExpressionSupportedInWherePart()
{
$this->assertValidDql('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id NOT IN (1)');
2008-05-24 22:18:37 +04:00
}
2009-03-17 01:12:38 +03:00
2009-03-15 00:19:50 +03:00
public function testExistsExpressionSupportedInWherePart()
2008-05-24 22:18:37 +04:00
{
$this->assertValidDql('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE EXISTS (SELECT p.phonenumber FROM Doctrine\Tests\Models\CMS\CmsPhonenumber p WHERE p.phonenumber = 1234)');
2008-05-24 22:18:37 +04:00
}
2009-03-19 15:43:48 +03:00
2008-05-24 22:18:37 +04:00
public function testNotExistsExpressionSupportedInWherePart()
{
$this->assertValidDql('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE NOT EXISTS (SELECT p.phonenumber FROM Doctrine\Tests\Models\CMS\CmsPhonenumber p WHERE p.phonenumber = 1234)');
2008-05-24 22:18:37 +04:00
}
2009-03-19 15:43:48 +03:00
public function testAggregateFunctionInHavingClause()
2008-05-24 22:18:37 +04:00
{
2009-03-19 15:43:48 +03:00
$this->assertValidDql('SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.phonenumbers p HAVING COUNT(p.phonenumber) > 2');
$this->assertValidDql("SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.phonenumbers p HAVING MAX(u.name) = 'romanb'");
2008-05-24 22:18:37 +04:00
}
2009-03-19 15:43:48 +03:00
public function testLeftJoin()
2008-05-24 22:18:37 +04:00
{
2009-03-19 15:43:48 +03:00
$this->assertValidDql('SELECT u, p FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.phonenumbers p');
2008-05-24 22:18:37 +04:00
}
2009-03-19 15:43:48 +03:00
public function testJoin()
2008-05-24 22:18:37 +04:00
{
2009-03-19 15:43:48 +03:00
$this->assertValidDql('SELECT u,p FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.phonenumbers p');
2008-05-24 22:18:37 +04:00
}
2009-03-19 15:43:48 +03:00
public function testInnerJoin()
2008-05-24 22:18:37 +04:00
{
2009-03-19 15:43:48 +03:00
$this->assertValidDql('SELECT u, p FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.phonenumbers p');
2008-05-24 22:18:37 +04:00
}
2009-03-19 15:43:48 +03:00
public function testMultipleLeftJoin()
2008-05-24 22:18:37 +04:00
{
2009-03-19 15:43:48 +03:00
$this->assertValidDql('SELECT u, a, p FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a LEFT JOIN u.phonenumbers p');
2008-05-24 22:18:37 +04:00
}
2009-03-19 15:43:48 +03:00
public function testMultipleInnerJoin()
2008-05-24 22:18:37 +04:00
{
2009-03-19 15:43:48 +03:00
$this->assertValidDql('SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.articles a INNER JOIN u.phonenumbers p');
2008-05-24 22:18:37 +04:00
}
2009-03-19 15:43:48 +03:00
public function testMixingOfJoins()
2008-05-24 22:18:37 +04:00
{
2009-03-19 15:43:48 +03:00
$this->assertValidDql('SELECT u.name, a.topic, p.phonenumber FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.articles a LEFT JOIN u.phonenumbers p');
2008-05-24 22:18:37 +04:00
}
2009-03-19 15:43:48 +03:00
public function testOrderBySingleColumn()
2008-05-24 22:18:37 +04:00
{
2009-03-19 15:43:48 +03:00
$this->assertValidDql('SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY u.name');
2008-05-24 22:18:37 +04:00
}
2009-03-19 15:43:48 +03:00
public function testOrderBySingleColumnAscending()
2008-05-24 22:18:37 +04:00
{
2009-03-19 15:43:48 +03:00
$this->assertValidDql('SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY u.name ASC');
2008-05-24 22:18:37 +04:00
}
2009-03-19 15:43:48 +03:00
public function testOrderBySingleColumnDescending()
2008-05-24 22:18:37 +04:00
{
2009-03-19 15:43:48 +03:00
$this->assertValidDql('SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY u.name DESC');
2008-05-24 22:18:37 +04:00
}
2009-03-19 15:43:48 +03:00
public function testOrderByMultipleColumns()
2008-05-24 22:18:37 +04:00
{
2009-03-19 15:43:48 +03:00
$this->assertValidDql('SELECT u.name, u.username FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY u.username DESC, u.name DESC');
2008-05-24 22:18:37 +04:00
}
2009-03-19 15:43:48 +03:00
public function testSubselectInInExpression()
2008-05-24 22:18:37 +04:00
{
2009-03-19 15:43:48 +03:00
$this->assertValidDql("SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id NOT IN (SELECT u2.id FROM Doctrine\Tests\Models\CMS\CmsUser u2 WHERE u2.name = 'zYne')");
2008-05-24 22:18:37 +04:00
}
2009-03-19 15:43:48 +03:00
public function testSubselectInSelectPart()
2008-05-24 22:18:37 +04:00
{
2009-03-19 15:43:48 +03:00
$this->assertValidDql("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'");
2008-05-24 22:18:37 +04:00
}
2009-03-19 15:43:48 +03:00
public function testPositionalInputParameter()
2008-05-24 22:18:37 +04:00
{
2009-03-21 22:58:52 +03:00
$this->assertValidDql('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1');
2008-05-24 22:18:37 +04:00
}
2009-03-19 15:43:48 +03:00
public function testNamedInputParameter()
2008-05-24 22:18:37 +04:00
{
2009-03-19 15:43:48 +03:00
$this->assertValidDql('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :id');
2008-05-24 22:18:37 +04:00
}
2009-03-19 15:43:48 +03:00
public function testJoinConditionsSupported()
2008-05-24 22:18:37 +04:00
{
2009-03-19 15:43:48 +03:00
$this->assertValidDql("SELECT u.name, p FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.phonenumbers p ON p.phonenumber = '123 123'");
2008-05-24 22:18:37 +04:00
}
2009-03-19 15:43:48 +03:00
public function testIndexByClauseWithOneComponent()
2008-05-24 22:18:37 +04:00
{
2009-03-19 15:43:48 +03:00
$this->assertValidDql('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u INDEX BY u.id');
2008-05-24 22:18:37 +04:00
}
2009-03-19 15:43:48 +03:00
public function testIndexBySupportsJoins()
2008-05-24 22:18:37 +04:00
{
2009-03-19 15:43:48 +03:00
$this->assertValidDql('SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a INDEX BY a.id'); // INDEX BY is now referring to articles
2008-05-24 22:18:37 +04:00
}
2009-03-19 15:43:48 +03:00
public function testIndexBySupportsJoins2()
2008-05-24 22:18:37 +04:00
{
2009-03-19 15:43:48 +03:00
$this->assertValidDql('SELECT u, p FROM Doctrine\Tests\Models\CMS\CmsUser u INDEX BY u.id LEFT JOIN u.phonenumbers p INDEX BY p.phonenumber');
2008-05-24 22:18:37 +04:00
}
2009-03-19 15:43:48 +03:00
public function testBetweenExpressionSupported()
2008-05-24 22:18:37 +04:00
{
2009-03-19 15:43:48 +03:00
$this->assertValidDql("SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name BETWEEN 'jepso' AND 'zYne'");
2008-05-24 22:18:37 +04:00
}
2009-03-19 15:43:48 +03:00
public function testNotBetweenExpressionSupported()
2008-05-24 22:18:37 +04:00
{
2009-03-19 15:43:48 +03:00
$this->assertValidDql("SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name NOT BETWEEN 'jepso' AND 'zYne'");
2008-05-24 22:18:37 +04:00
}
2009-03-19 15:43:48 +03:00
public function testLikeExpression()
2008-05-24 22:18:37 +04:00
{
2009-03-19 15:43:48 +03:00
$this->assertValidDql("SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name LIKE 'z%'");
2008-05-24 22:18:37 +04:00
}
2009-03-19 15:43:48 +03:00
public function testNotLikeExpression()
2008-05-24 22:18:37 +04:00
{
2009-03-19 15:43:48 +03:00
$this->assertValidDql("SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name NOT LIKE 'z%'");
2008-05-24 22:18:37 +04:00
}
2009-03-19 15:43:48 +03:00
public function testLikeExpressionWithCustomEscapeCharacter()
2008-05-24 22:18:37 +04:00
{
2009-03-19 15:43:48 +03:00
$this->assertValidDql("SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name LIKE 'z|%' ESCAPE '|'");
2008-05-24 22:18:37 +04:00
}
/*
2009-03-19 15:43:48 +03:00
public function testImplicitJoinInWhereOnSingleValuedAssociationPathExpression()
2008-05-24 22:18:37 +04:00
{
2009-03-19 15:43:48 +03:00
// This should be allowed because avatar is a single-value association.
// SQL: SELECT ... FROM forum_user fu INNER JOIN forum_avatar fa ON fu.avatar_id = fa.id WHERE fa.id = ?
$this->assertValidDql("SELECT u FROM Doctrine\Tests\Models\Forum\ForumUser u WHERE u.avatar.id = ?");
2008-05-24 22:18:37 +04:00
}
*/
2009-03-19 15:43:48 +03:00
public function testImplicitJoinInWhereOnCollectionValuedPathExpression()
2008-05-24 22:18:37 +04:00
{
2009-03-19 15:43:48 +03:00
// This should be forbidden, because articles is a collection
$this->assertInvalidDql("SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.articles.title = ?");
2008-05-24 22:18:37 +04:00
}
2009-03-19 15:43:48 +03:00
public function testInvalidSyntaxIsRejected()
2008-05-24 22:18:37 +04:00
{
2009-03-19 15:43:48 +03:00
$this->assertInvalidDql("FOOBAR CmsUser");
//$this->assertInvalidDql("DELETE FROM Doctrine\Tests\Models\CMS\CmsUser.articles");
//$this->assertInvalidDql("DELETE FROM Doctrine\Tests\Models\CMS\CmsUser cu WHERE cu.articles.id > ?");
$this->assertInvalidDql("SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.articles.comments");
2009-03-19 15:43:48 +03:00
// Currently UNDEFINED OFFSET error
$this->assertInvalidDql("SELECT c FROM CmsUser.articles.comments c");
2008-05-24 22:18:37 +04:00
}
2009-03-19 15:43:48 +03:00
public function testUpdateWorksWithOneField()
2008-05-24 22:18:37 +04:00
{
2009-03-19 15:43:48 +03:00
$this->assertValidDql("UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.name = 'someone'");
2008-05-24 22:18:37 +04:00
}
2009-03-19 15:43:48 +03:00
public function testUpdateWorksWithMultipleFields()
2008-05-24 22:18:37 +04:00
{
2009-03-19 15:43:48 +03:00
$this->assertValidDql("UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.name = 'someone', u.username = 'some'");
2008-05-24 22:18:37 +04:00
}
2009-03-19 15:43:48 +03:00
public function testUpdateSupportsConditions()
2008-05-24 22:18:37 +04:00
{
2009-03-19 15:43:48 +03:00
$this->assertValidDql("UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.name = 'someone' WHERE u.id = 5");
2008-05-24 22:18:37 +04:00
}
2009-03-19 15:43:48 +03:00
public function testDeleteAll()
2008-05-24 22:18:37 +04:00
{
2009-03-19 15:43:48 +03:00
$this->assertValidDql('DELETE FROM Doctrine\Tests\Models\CMS\CmsUser');
2008-05-24 22:18:37 +04:00
}
2009-03-19 15:43:48 +03:00
public function testDeleteWithCondition()
2008-05-24 22:18:37 +04:00
{
2009-03-19 15:43:48 +03:00
$this->assertValidDql('DELETE FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = 3');
2008-05-24 22:18:37 +04:00
}
2009-03-19 15:43:48 +03:00
/**
* TODO: Hydration can't deal with this currently but it should be allowed.
* Also, generated SQL looks like: "... FROM cms_user, cms_article ..." which
* may not work on all dbms.
*
* The main use case for this generalized style of join is when a join condition
* does not involve a foreign key relationship that is mapped to an entity relationship.
*/
public function testImplicitJoinWithCartesianProductAndConditionInWhere()
2008-05-24 22:18:37 +04:00
{
2009-03-19 15:43:48 +03:00
$this->assertValidDql("SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u, Doctrine\Tests\Models\CMS\CmsArticle a WHERE u.name = a.topic");
2008-05-24 22:18:37 +04:00
}
public function testAllExpressionWithCorrelatedSubquery()
2008-05-24 22:18:37 +04:00
{
$this->assertValidDql('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id > ALL (SELECT u2.id FROM Doctrine\Tests\Models\CMS\CmsUser u2 WHERE u2.name = u.name)');
2008-05-24 22:18:37 +04:00
}
public function testCustomJoinsAndWithKeywordSupported()
2008-05-24 22:18:37 +04:00
{
$this->assertValidDql('SELECT u, p FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.phonenumbers p WITH p.phonenumber = 123 WHERE u.id = 1');
2008-05-24 22:18:37 +04:00
}
public function testAnyExpressionWithCorrelatedSubquery()
2008-05-24 22:18:37 +04:00
{
$this->assertValidDql('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id > ANY (SELECT u2.id FROM Doctrine\Tests\Models\CMS\CmsUser u2 WHERE u2.name = u.name)');
2008-05-24 22:18:37 +04:00
}
public function testSomeExpressionWithCorrelatedSubquery()
2008-05-24 22:18:37 +04:00
{
$this->assertValidDql('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id > SOME (SELECT u2.id FROM Doctrine\Tests\Models\CMS\CmsUser u2 WHERE u2.name = u.name)');
2008-05-24 22:18:37 +04:00
}
}