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-02-18 13:39:51 +03:00
/*
2008-05-24 22:18:37 +04:00
public function testFunctionalExpressionsSupportedInWherePart ()
{
2009-02-18 10:59:11 +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-02-18 13:39:51 +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-02-18 13:39:51 +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-02-18 13:39:51 +03:00
*/
/* public function testExistsExpressionSupportedInWherePart ()
2008-05-24 22:18:37 +04:00
{
2009-02-18 13:39:51 +03:00
$this -> assertValidDql ( 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE EXISTS (SELECT p.user_id FROM Doctrine\Tests\Models\CMS\CmsPhonenumber p WHERE p.user_id = u.id)' );
2008-05-24 22:18:37 +04:00
}
public function testNotExistsExpressionSupportedInWherePart ()
{
2009-02-18 10:59:11 +03:00
$this -> assertValidDql ( 'SELECT u.* FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE NOT EXISTS (SELECT p.user_id FROM Doctrine\Tests\Models\CMS\CmsPhonenumber p WHERE p.user_id = u.id)' );
2008-05-24 22:18:37 +04:00
}
public function testLiteralValueAsInOperatorOperandIsSupported ()
{
2009-02-18 10:59:11 +03:00
$this -> assertValidDql ( 'SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE 1 IN (1, 2)' );
2008-05-24 22:18:37 +04:00
}
public function testUpdateWorksWithOneColumn ()
{
2009-02-18 10:59:11 +03:00
$this -> assertValidDql ( " UPDATE Doctrine \T ests \ Models \ CMS \ CmsUser u SET u.name = 'someone' " );
2008-05-24 22:18:37 +04:00
}
public function testUpdateWorksWithMultipleColumns ()
{
2009-02-18 10:59:11 +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
}
public function testUpdateSupportsConditions ()
{
2009-02-18 10:59:11 +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
}
public function testDeleteAll ()
{
2009-02-18 10:59:11 +03:00
$this -> assertValidDql ( 'DELETE FROM Doctrine\Tests\Models\CMS\CmsUser' );
2008-05-24 22:18:37 +04:00
}
public function testDeleteWithCondition ()
{
2009-02-18 10:59:11 +03:00
$this -> assertValidDql ( 'DELETE FROM Doctrine\Tests\Models\CMS\CmsUser WHERE id = 3' );
2008-05-24 22:18:37 +04:00
}
public function testAdditionExpression ()
{
2009-02-18 10:59:11 +03:00
$this -> assertValidDql ( 'SELECT u.*, (u.id + u.id) addition FROM Doctrine\Tests\Models\CMS\CmsUser u' );
2008-05-24 22:18:37 +04:00
}
public function testSubtractionExpression ()
{
2009-02-18 10:59:11 +03:00
$this -> assertValidDql ( 'SELECT u.*, (u.id - u.id) subtraction FROM Doctrine\Tests\Models\CMS\CmsUser u' );
2008-05-24 22:18:37 +04:00
}
public function testDivisionExpression ()
{
2009-02-18 10:59:11 +03:00
$this -> assertValidDql ( 'SELECT u.*, (u.id/u.id) division FROM Doctrine\Tests\Models\CMS\CmsUser u' );
2008-05-24 22:18:37 +04:00
}
public function testMultiplicationExpression ()
{
2009-02-18 10:59:11 +03:00
$this -> assertValidDql ( 'SELECT u.*, (u.id * u.id) multiplication FROM Doctrine\Tests\Models\CMS\CmsUser u' );
2008-05-24 22:18:37 +04:00
}
public function testNegationExpression ()
{
2009-02-18 10:59:11 +03:00
$this -> assertValidDql ( 'SELECT u.*, -u.id negation FROM Doctrine\Tests\Models\CMS\CmsUser u' );
2008-05-24 22:18:37 +04:00
}
public function testExpressionWithPrecedingPlusSign ()
{
$this -> assertValidDql ( 'SELECT u.*, +u.id FROM CmsUser u' );
}
public function testAggregateFunctionInHavingClause ()
{
2009-02-18 10:59:11 +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) = 'zYne' " );
2008-05-24 22:18:37 +04:00
}
public function testMultipleAggregateFunctionsInHavingClause ()
{
2009-02-18 10:59:11 +03:00
$this -> assertValidDql ( " SELECT u.name FROM Doctrine \T ests \ Models \ CMS \ CmsUser u LEFT JOIN u.phonenumbers p HAVING MAX(u.name) = 'zYne' " );
2008-05-24 22:18:37 +04:00
}
public function testLeftJoin ()
{
2009-02-18 10:59:11 +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
}
public function testJoin ()
{
2009-02-18 10:59:11 +03:00
$this -> assertValidDql ( 'SELECT u.* FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.phonenumbers' );
2008-05-24 22:18:37 +04:00
}
public function testInnerJoin ()
{
2009-02-18 10:59:11 +03:00
$this -> assertValidDql ( 'SELECT u.*, u.phonenumbers.* FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.phonenumbers' );
2008-05-24 22:18:37 +04:00
}
public function testMultipleLeftJoin ()
{
2009-02-18 10:59:11 +03:00
$this -> assertValidDql ( 'SELECT u.articles.*, u.phonenumbers.* FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles LEFT JOIN u.phonenumbers' );
2008-05-24 22:18:37 +04:00
}
public function testMultipleInnerJoin ()
{
2009-02-18 10:59:11 +03:00
$this -> assertValidDql ( 'SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.articles INNER JOIN u.phonenumbers' );
2008-05-24 22:18:37 +04:00
}
public function testMultipleInnerJoin2 ()
{
2009-02-18 10:59:11 +03:00
$this -> assertValidDql ( 'SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.articles, u.phonenumbers' );
2008-05-24 22:18:37 +04:00
}
public function testMixingOfJoins ()
{
2009-02-18 10:59:11 +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
}
2008-05-25 02:04:14 +04:00
2008-05-24 22:18:37 +04:00
public function testMixingOfJoins2 ()
{
2009-02-18 10:59:11 +03:00
$this -> assertInvalidDql ( 'SELECT u.name, u.articles.topic, c.text FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.articles.comments c' );
2008-05-24 22:18:37 +04:00
}
2008-05-25 02:04:14 +04:00
2008-05-24 22:18:37 +04:00
public function testOrderBySingleColumn ()
{
2009-02-18 10:59:11 +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
}
public function testOrderBySingleColumnAscending ()
{
2009-02-18 10:59:11 +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
}
public function testOrderBySingleColumnDescending ()
{
2009-02-18 10:59:11 +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
}
public function testOrderByMultipleColumns ()
{
2009-02-18 10:59:11 +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
}
public function testOrderByWithFunctionExpression ()
{
2009-02-18 10:59:11 +03:00
$this -> assertValidDql ( 'SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY COALESCE(u.id, u.name) DESC' );
2009-02-18 13:39:51 +03:00
} */
2008-05-25 02:04:14 +04:00
/*
2008-05-24 22:18:37 +04:00
public function testSubselectInInExpression ()
{
$this -> assertValidDql ( " SELECT * FROM CmsUser u WHERE u.id NOT IN (SELECT u2.id FROM CmsUser u2 WHERE u2.name = 'zYne') " );
}
2008-05-25 02:04:14 +04:00
2008-05-24 22:18:37 +04:00
public function testSubselectInSelectPart ()
{
// Semantical error: Unknown query component u (probably in subselect)
$this -> assertValidDql ( " SELECT u.name, (SELECT COUNT(p.phonenumber) FROM CmsPhonenumber p WHERE p.user_id = u.id) pcount FROM CmsUser u WHERE u.name = 'zYne' LIMIT 1 " );
}
2009-02-18 13:39:51 +03:00
*//*
2008-05-24 22:18:37 +04:00
public function testPositionalInputParameter ()
{
2009-02-18 10:59:11 +03:00
$this -> assertValidDql ( 'SELECT * FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?' );
2008-05-24 22:18:37 +04:00
}
public function testNamedInputParameter ()
{
2009-02-18 10:59:11 +03:00
$this -> assertValidDql ( 'SELECT * FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :id' );
2009-02-18 13:39:51 +03:00
} */
2008-05-24 22:18:37 +04:00
/*
public function testCustomJoinsAndWithKeywordSupported ()
{
// We need existant classes here, otherwise semantical will always fail
$this -> assertValidDql ( 'SELECT c.*, c2.*, d.* FROM Record_Country c INNER JOIN c.City c2 WITH c2.id = 2 WHERE c.id = 1' );
}
*/
2009-02-18 13:39:51 +03:00
/*
2008-05-24 22:18:37 +04:00
public function testJoinConditionsSupported ()
{
2009-02-18 10:59:11 +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
}
public function testIndexByClauseWithOneComponent ()
{
2009-02-18 10:59:11 +03:00
$this -> assertValidDql ( 'SELECT * FROM Doctrine\Tests\Models\CMS\CmsUser u INDEX BY id' );
2008-05-24 22:18:37 +04:00
}
public function testIndexBySupportsJoins ()
{
2009-02-18 10:59:11 +03:00
$this -> assertValidDql ( 'SELECT u.*, u.articles.* FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles INDEX BY id' ); // INDEX BY is now referring to articles
2008-05-24 22:18:37 +04:00
}
public function testIndexBySupportsJoins2 ()
{
2009-02-18 10:59:11 +03:00
$this -> assertValidDql ( 'SELECT u.*, p.* FROM Doctrine\Tests\Models\CMS\CmsUser u INDEX BY id LEFT JOIN u.phonenumbers p INDEX BY phonenumber' );
2008-05-24 22:18:37 +04:00
}
public function testBetweenExpressionSupported ()
{
2009-02-18 10:59:11 +03:00
$this -> assertValidDql ( " SELECT * FROM Doctrine \T ests \ Models \ CMS \ CmsUser u WHERE u.name BETWEEN 'jepso' AND 'zYne' " );
2008-05-24 22:18:37 +04:00
}
public function testNotBetweenExpressionSupported ()
{
2009-02-18 10:59:11 +03:00
$this -> assertValidDql ( " SELECT * FROM Doctrine \T ests \ Models \ CMS \ CmsUser u WHERE u.name NOT BETWEEN 'jepso' AND 'zYne' " );
2008-05-24 22:18:37 +04:00
}
2009-02-18 13:39:51 +03:00
*/
2008-08-31 22:27:16 +04:00
/* public function testAllExpressionWithCorrelatedSubquery ()
2008-05-24 22:18:37 +04:00
{
// We need existant classes here, otherwise semantical will always fail
2008-08-31 22:27:16 +04:00
$this -> assertValidDql ( 'SELECT * FROM CompanyEmployee e WHERE e.salary > ALL (SELECT m.salary FROM CompanyManager m WHERE m.department = e.department)' , true );
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
{
// We need existant classes here, otherwise semantical will always fail
$this -> assertValidDql ( 'SELECT * FROM Employee e WHERE e.salary > ANY (SELECT m.salary FROM Manager m WHERE m.department = e.department)' );
}
2008-08-31 22:27:16 +04:00
public function testSomeExpressionWithCorrelatedSubquery ()
2008-05-24 22:18:37 +04:00
{
// We need existant classes here, otherwise semantical will always fail
$this -> assertValidDql ( 'SELECT * FROM Employee e WHERE e.salary > SOME (SELECT m.salary FROM Manager m WHERE m.department = e.department)' );
}
2009-02-18 13:39:51 +03:00
*//*
2008-05-24 22:18:37 +04:00
public function testLikeExpression ()
{
2009-02-18 10:59:11 +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
}
public function testNotLikeExpression ()
{
2009-02-18 10:59:11 +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
}
public function testLikeExpressionWithCustomEscapeCharacter ()
{
2009-02-18 10:59:11 +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-02-18 13:39:51 +03:00
*/
2008-08-31 22:27:16 +04: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 .
*/
2009-02-18 13:39:51 +03:00
/* public function testImplicitJoinWithCartesianProductAndConditionInWhere ()
2008-08-31 22:27:16 +04:00
{
2009-02-18 10:59:11 +03:00
$this -> assertValidDql ( " SELECT u.*, a.* FROM Doctrine \T ests \ Models \ CMS \ CmsUser u, CmsArticle a WHERE u.name = a.topic " );
2008-08-31 22:27:16 +04:00
}
public function testImplicitJoinInWhereOnSingleValuedAssociationPathExpression ()
{
// 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 = ?
2008-09-07 17:48:40 +04:00
//$this->assertValidDql("SELECT u.* FROM ForumUser u WHERE u.avatar.id = ?");
2008-08-31 22:27:16 +04:00
}
public function testImplicitJoinInWhereOnCollectionValuedPathExpression ()
{
// This should be forbidden, because articles is a collection
2009-02-18 10:59:11 +03:00
$this -> assertInvalidDql ( " SELECT u.* FROM Doctrine \T ests \ Models \ CMS \ CmsUser u WHERE u.articles.title = ? " );
2008-08-31 22:27:16 +04:00
}
2008-05-27 08:52:50 +04:00
public function testInvalidSyntaxIsRejected ()
{
$this -> assertInvalidDql ( " FOOBAR CmsUser " );
2009-02-18 10:59:11 +03:00
$this -> assertInvalidDql ( " DELETE FROM Doctrine \T ests \ Models \ CMS \ CmsUser.articles " );
$this -> assertInvalidDql ( " DELETE FROM Doctrine \T ests \ Models \ CMS \ CmsUser cu WHERE cu.articles.id > ? " );
$this -> assertInvalidDql ( " SELECT user FROM Doctrine \T ests \ Models \ CMS \ CmsUser user " );
2008-08-31 22:27:16 +04:00
// Error message here is: Relation 'comments' does not exist in component 'CmsUser'
// This means it is intepreted as:
// SELECT u.* FROM CmsUser u JOIN u.articles JOIN u.comments
// This seems wrong. "JOIN u.articles.comments" should not be allowed.
2009-02-18 10:59:11 +03:00
$this -> assertInvalidDql ( " SELECT u.* FROM Doctrine \T ests \ Models \ CMS \ CmsUser u JOIN u.articles.comments " );
2008-08-31 22:27:16 +04:00
// Currently UNDEFINED OFFSET error
2008-09-07 17:48:40 +04:00
//$this->assertInvalidDql("SELECT * FROM CmsUser.articles.comments");
2009-02-18 13:39:51 +03:00
} */
2009-02-18 10:59:11 +03:00
}