2008-05-24 22:18:37 +04:00
< ? php
2009-02-18 10:59:11 +03:00
namespace Doctrine\Tests\ORM\Query ;
2008-05-25 02:04:14 +04:00
2009-02-18 10:59:11 +03:00
require_once __DIR__ . '/../../TestInit.php' ;
class LanguageRecognitionTest extends \Doctrine\Tests\OrmTestCase
2008-05-24 22:18:37 +04:00
{
2009-02-18 10:59:11 +03:00
private $_em ;
protected function setUp ()
{
$this -> _em = $this -> _getTestEntityManager ();
}
2008-08-31 22:27:16 +04:00
public function assertValidDql ( $dql , $debug = false )
2008-05-24 22:18:37 +04:00
{
try {
2009-02-18 13:39:51 +03:00
$query = $this -> _em -> createQuery ( $dql );
2008-05-24 22:18:37 +04:00
$parserResult = $query -> parse ();
2009-02-18 13:39:51 +03:00
} catch ( \Exception $e ) {
2008-08-31 22:27:16 +04:00
if ( $debug ) {
echo $e -> getTraceAsString () . PHP_EOL ;
}
2008-05-24 22:18:37 +04:00
$this -> fail ( $e -> getMessage ());
}
}
2008-08-31 22:27:16 +04:00
public function assertInvalidDql ( $dql , $debug = false )
2008-05-24 22:18:37 +04:00
{
try {
2009-02-18 13:39:51 +03:00
$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' );
2009-02-18 13:39:51 +03:00
} catch ( \Exception $e ) {
2008-08-31 22:27:16 +04:00
//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 ()
{
2009-02-18 10:59:11 +03:00
$this -> assertValidDql ( 'SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u' );
2008-05-24 22:18:37 +04:00
}
public function testSelectSingleComponentWithAsterisk ()
{
2009-02-18 13:39:51 +03:00
$this -> assertValidDql ( 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u' );
2008-05-24 22:18:37 +04:00
}
public function testInvalidSelectSingleComponentWithAsterisk ()
{
2009-02-18 13:39:51 +03:00
$this -> assertInvalidDql ( 'SELECT p FROM Doctrine\Tests\Models\CMS\CmsUser u' );
2008-05-24 22:18:37 +04:00
}
public function testSelectSingleComponentWithMultipleColumns ()
{
2009-02-18 10:59:11 +03:00
$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 ()
{
2009-02-18 13:39:51 +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
}
public function testSelectDistinctIsSupported ()
{
2009-02-18 10:59:11 +03:00
$this -> assertValidDql ( 'SELECT DISTINCT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u' );
2008-05-24 22:18:37 +04:00
}
public function testAggregateFunctionInSelect ()
{
2009-02-18 10:59:11 +03:00
$this -> assertValidDql ( 'SELECT COUNT(u.id) FROM Doctrine\Tests\Models\CMS\CmsUser u' );
2008-05-24 22:18:37 +04:00
}
public function testAggregateFunctionWithDistinctInSelect ()
{
2009-02-18 10:59:11 +03:00
$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 ()
{
2009-03-28 20:10:41 +03:00
$this -> assertValidDql ( " SELECT u.name FROM Doctrine \T ests \ 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 ()
{
2009-02-18 13:39:51 +03:00
$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 ()
{
2009-02-18 13:39:51 +03:00
$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 ()
{
2009-02-18 13:39:51 +03:00
$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
{
2009-03-21 15:49:58 +03: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 ()
{
2009-03-21 15:49:58 +03:00
$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 \T ests \ 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 \T ests \ Models \ CMS \ CmsUser u WHERE u.id NOT IN (SELECT u2.id FROM Doctrine \T ests \ 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 \T ests \ Models \ CMS \ CmsPhonenumber p WHERE p.phonenumber = 1234) pcount FROM Doctrine \T ests \ 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 \T ests \ 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 \T ests \ 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 \T ests \ Models \ CMS \ CmsUser u WHERE u.name NOT BETWEEN 'jepso' AND 'zYne' " );
2008-05-24 22:18:37 +04:00
}
2008-05-25 02:04:14 +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 \T ests \ Models \ CMS \ CmsUser u WHERE u.name LIKE 'z%' " );
2008-05-24 22:18:37 +04:00
}
2008-05-25 02:04:14 +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 \T ests \ 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 \T ests \ Models \ CMS \ CmsUser u WHERE u.name LIKE 'z|%' ESCAPE '|' " );
2008-05-24 22:18:37 +04:00
}
2009-03-21 15:49:58 +03: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 \T ests \ Models \ Forum \ ForumUser u WHERE u.avatar.id = ? " );
2008-05-24 22:18:37 +04:00
}
2009-03-21 15:49:58 +03: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 \T ests \ 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 \T ests \ Models \ CMS \ CmsUser u JOIN u.articles.comments " );
2008-05-25 02:04:14 +04:00
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 \T ests \ 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 \T ests \ 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 \T ests \ 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 \T ests \ Models \ CMS \ CmsUser u, Doctrine \T ests \ Models \ CMS \ CmsArticle a WHERE u.name = a.topic " );
2008-05-24 22:18:37 +04:00
}
2009-03-28 20:10:41 +03:00
public function testAllExpressionWithCorrelatedSubquery ()
2008-05-24 22:18:37 +04:00
{
2009-03-28 20:10:41 +03: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
}
2009-03-28 20:10:41 +03:00
public function testCustomJoinsAndWithKeywordSupported ()
2008-05-24 22:18:37 +04:00
{
2009-03-28 20:10:41 +03: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
}
2008-08-31 22:27:16 +04:00
public function testAnyExpressionWithCorrelatedSubquery ()
2008-05-24 22:18:37 +04:00
{
2009-03-28 20:10:41 +03: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
}
2008-08-31 22:27:16 +04:00
public function testSomeExpressionWithCorrelatedSubquery ()
2008-05-24 22:18:37 +04:00
{
2009-03-28 20:10:41 +03: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
}
2009-02-18 10:59:11 +03:00
}