LanguageRecognitionTest.php 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. <?php
  2. namespace Doctrine\Tests\ORM\Query;
  3. use Doctrine\ORM\Query,
  4. Doctrine\ORM\Query\QueryException;
  5. require_once __DIR__ . '/../../TestInit.php';
  6. class LanguageRecognitionTest extends \Doctrine\Tests\OrmTestCase
  7. {
  8. private $_em;
  9. protected function setUp()
  10. {
  11. $this->_em = $this->_getTestEntityManager();
  12. }
  13. public function assertValidDQL($dql, $debug = false)
  14. {
  15. try {
  16. $parserResult = $this->parseDql($dql);
  17. } catch (QueryException $e) {
  18. if ($debug) {
  19. echo $e->getTraceAsString() . PHP_EOL;
  20. }
  21. $this->fail($e->getMessage());
  22. }
  23. }
  24. public function assertInvalidDQL($dql, $debug = false)
  25. {
  26. try {
  27. $parserResult = $this->parseDql($dql);
  28. $this->fail('No syntax errors were detected, when syntax errors were expected');
  29. } catch (QueryException $e) {
  30. if ($debug) {
  31. echo $e->getMessage() . PHP_EOL;
  32. echo $e->getTraceAsString() . PHP_EOL;
  33. }
  34. }
  35. }
  36. public function parseDql($dql, $hints = array())
  37. {
  38. $query = $this->_em->createQuery($dql);
  39. $query->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);
  40. $query->setDql($dql);
  41. foreach ($hints as $key => $value) {
  42. $query->setHint($key, $value);
  43. }
  44. $parser = new \Doctrine\ORM\Query\Parser($query);
  45. // We do NOT test SQL output here. That only unnecessarily slows down the tests!
  46. $parser->setCustomOutputTreeWalker('Doctrine\Tests\Mocks\MockTreeWalker');
  47. return $parser->parse();
  48. }
  49. public function testEmptyQueryString()
  50. {
  51. $this->assertInvalidDQL('');
  52. }
  53. public function testPlainFromClauseWithAlias()
  54. {
  55. $this->assertValidDQL('SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u');
  56. }
  57. public function testSelectSingleComponentWithAsterisk()
  58. {
  59. $this->assertValidDQL('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u');
  60. }
  61. public function testSelectSingleComponentWithMultipleColumns()
  62. {
  63. $this->assertValidDQL('SELECT u.name, u.username FROM Doctrine\Tests\Models\CMS\CmsUser u');
  64. }
  65. public function testSelectMultipleComponentsUsingMultipleFrom()
  66. {
  67. $this->assertValidDQL('SELECT u, p FROM Doctrine\Tests\Models\CMS\CmsUser u, Doctrine\Tests\Models\CMS\CmsPhonenumber p WHERE u = p.user');
  68. }
  69. public function testSelectMultipleComponentsWithAsterisk()
  70. {
  71. $this->assertValidDQL('SELECT u, p FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.phonenumbers p');
  72. }
  73. public function testSelectDistinctIsSupported()
  74. {
  75. $this->assertValidDQL('SELECT DISTINCT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u');
  76. }
  77. public function testAggregateFunctionInSelect()
  78. {
  79. $this->assertValidDQL('SELECT COUNT(u.id) FROM Doctrine\Tests\Models\CMS\CmsUser u');
  80. }
  81. public function testDuplicatedAliasInAggregateFunction()
  82. {
  83. $this->assertInvalidDQL('SELECT COUNT(u.id) AS num, SUM(u.id) AS num FROM Doctrine\Tests\Models\CMS\CmsUser u');
  84. }
  85. public function testAggregateFunctionWithDistinctInSelect()
  86. {
  87. $this->assertValidDQL('SELECT COUNT(DISTINCT u.name) FROM Doctrine\Tests\Models\CMS\CmsUser u');
  88. }
  89. public function testFunctionalExpressionsSupportedInWherePart()
  90. {
  91. $this->assertValidDQL("SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE TRIM(u.name) = 'someone'");
  92. }
  93. public function testArithmeticExpressionsSupportedInWherePart()
  94. {
  95. $this->assertValidDQL('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE ((u.id + 5000) * u.id + 3) < 10000000');
  96. }
  97. public function testInExpressionSupportedInWherePart()
  98. {
  99. $this->assertValidDQL('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id IN (1, 2)');
  100. }
  101. public function testInExpressionWithoutSpacesSupportedInWherePart()
  102. {
  103. $this->assertValidDQL('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id IN (1,2,3)');
  104. }
  105. public function testNotInExpressionSupportedInWherePart()
  106. {
  107. $this->assertValidDQL('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id NOT IN (1)');
  108. }
  109. public function testInExpressionWithSingleValuedAssociationPathExpression()
  110. {
  111. $this->assertValidDQL("SELECT u FROM Doctrine\Tests\Models\Forum\ForumUser u WHERE u.avatar IN (?1, ?2)");
  112. }
  113. public function testInvalidInExpressionWithCollectionValuedAssociationPathExpression()
  114. {
  115. $this->assertInvalidDQL("SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.phonenumbers IN (?1, ?2)");
  116. }
  117. public function testInstanceOfExpressionSupportedInWherePart()
  118. {
  119. $this->assertValidDQL('SELECT u FROM Doctrine\Tests\Models\Company\CompanyPerson u WHERE u INSTANCE OF Doctrine\Tests\Models\Company\CompanyEmployee');
  120. }
  121. public function testInstanceOfExpressionWithInputParamSupportedInWherePart()
  122. {
  123. $this->assertValidDQL('SELECT u FROM Doctrine\Tests\Models\Company\CompanyPerson u WHERE u INSTANCE OF ?1');
  124. }
  125. public function testNotInstanceOfExpressionSupportedInWherePart()
  126. {
  127. $this->assertValidDQL('SELECT u FROM Doctrine\Tests\Models\Company\CompanyPerson u WHERE u NOT INSTANCE OF ?1');
  128. }
  129. public function testExistsExpressionSupportedInWherePart()
  130. {
  131. $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)');
  132. }
  133. public function testNotExistsExpressionSupportedInWherePart()
  134. {
  135. $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)');
  136. }
  137. public function testAggregateFunctionInHavingClause()
  138. {
  139. $this->assertValidDQL('SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.phonenumbers p HAVING COUNT(p.phonenumber) > 2');
  140. $this->assertValidDQL("SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.phonenumbers p HAVING MAX(u.name) = 'romanb'");
  141. }
  142. public function testLeftJoin()
  143. {
  144. $this->assertValidDQL('SELECT u, p FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.phonenumbers p');
  145. }
  146. public function testJoin()
  147. {
  148. $this->assertValidDQL('SELECT u,p FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.phonenumbers p');
  149. }
  150. public function testInnerJoin()
  151. {
  152. $this->assertValidDQL('SELECT u, p FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.phonenumbers p');
  153. }
  154. public function testMultipleLeftJoin()
  155. {
  156. $this->assertValidDQL('SELECT u, a, p FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.articles a LEFT JOIN u.phonenumbers p');
  157. }
  158. public function testMultipleInnerJoin()
  159. {
  160. $this->assertValidDQL('SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.articles a INNER JOIN u.phonenumbers p');
  161. }
  162. public function testMixingOfJoins()
  163. {
  164. $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');
  165. }
  166. public function testOrderBySingleColumn()
  167. {
  168. $this->assertValidDQL('SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY u.name');
  169. }
  170. public function testOrderBySingleColumnAscending()
  171. {
  172. $this->assertValidDQL('SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY u.name ASC');
  173. }
  174. public function testOrderBySingleColumnDescending()
  175. {
  176. $this->assertValidDQL('SELECT u.name FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY u.name DESC');
  177. }
  178. public function testOrderByMultipleColumns()
  179. {
  180. $this->assertValidDQL('SELECT u.name, u.username FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY u.username DESC, u.name DESC');
  181. }
  182. public function testSubselectInInExpression()
  183. {
  184. $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')");
  185. }
  186. public function testSubselectInSelectPart()
  187. {
  188. $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'");
  189. }
  190. public function testArithmeticExpressionInSelectPart()
  191. {
  192. $this->assertValidDQL("SELECT SUM(u.id) / COUNT(u.id) FROM Doctrine\Tests\Models\CMS\CmsUser u");
  193. }
  194. public function testArithmeticExpressionInSubselectPart()
  195. {
  196. $this->assertValidDQL("SELECT (SELECT SUM(u.id) / COUNT(u.id) FROM Doctrine\Tests\Models\CMS\CmsUser u2) value FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name = 'jon'");
  197. }
  198. public function testArithmeticExpressionWithParenthesisInSubselectPart()
  199. {
  200. $this->assertValidDQL("SELECT (SELECT (SUM(u.id) / COUNT(u.id)) FROM Doctrine\Tests\Models\CMS\CmsUser u2) value FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name = 'jon'");
  201. }
  202. /**
  203. * @group DDC-1079
  204. */
  205. public function testSelectLiteralInSubselect()
  206. {
  207. $this->assertValidDQL('SELECT (SELECT 1 FROM Doctrine\Tests\Models\CMS\CmsUser u2) value FROM Doctrine\Tests\Models\CMS\CmsUser u');
  208. $this->assertValidDQL('SELECT (SELECT 0 FROM Doctrine\Tests\Models\CMS\CmsUser u2) value FROM Doctrine\Tests\Models\CMS\CmsUser u');
  209. }
  210. /**
  211. * @group DDC-1077
  212. */
  213. public function testConstantValueInSelect()
  214. {
  215. $this->assertValidDQL("SELECT u.name, 'foo' AS bar FROM Doctrine\Tests\Models\CMS\CmsUser u", true);
  216. }
  217. public function testDuplicateAliasInSubselectPart()
  218. {
  219. $this->assertInvalidDQL("SELECT (SELECT SUM(u.id) / COUNT(u.id) AS foo FROM Doctrine\Tests\Models\CMS\CmsUser u2) foo FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name = 'jon'");
  220. }
  221. public function testPositionalInputParameter()
  222. {
  223. $this->assertValidDQL('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1');
  224. }
  225. public function testNamedInputParameter()
  226. {
  227. $this->assertValidDQL('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = :id');
  228. }
  229. public function testJoinConditionOverrideNotSupported()
  230. {
  231. $this->assertInvalidDQL("SELECT u.name, p FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.phonenumbers p ON p.phonenumber = '123 123'");
  232. }
  233. public function testIndexByClauseWithOneComponent()
  234. {
  235. $this->assertValidDQL('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u INDEX BY u.id');
  236. }
  237. public function testIndexBySupportsJoins()
  238. {
  239. $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
  240. }
  241. public function testIndexBySupportsJoins2()
  242. {
  243. $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');
  244. }
  245. public function testBetweenExpressionSupported()
  246. {
  247. $this->assertValidDQL("SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name BETWEEN 'jepso' AND 'zYne'");
  248. }
  249. public function testNotBetweenExpressionSupported()
  250. {
  251. $this->assertValidDQL("SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name NOT BETWEEN 'jepso' AND 'zYne'");
  252. }
  253. public function testLikeExpression()
  254. {
  255. $this->assertValidDQL("SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name LIKE 'z%'");
  256. }
  257. public function testNotLikeExpression()
  258. {
  259. $this->assertValidDQL("SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name NOT LIKE 'z%'");
  260. }
  261. public function testLikeExpressionWithCustomEscapeCharacter()
  262. {
  263. $this->assertValidDQL("SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name LIKE 'z|%' ESCAPE '|'");
  264. }
  265. public function testFieldComparisonWithoutAlias()
  266. {
  267. $this->assertInvalidDQL("SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE id = 1");
  268. }
  269. public function testDuplicatedAliasDeclaration()
  270. {
  271. $this->assertInvalidDQL("SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u INNER JOIN u.articles u WHERE u.id = 1");
  272. }
  273. public function testImplicitJoinInWhereOnSingleValuedAssociationPathExpression()
  274. {
  275. // This should be allowed because avatar is a single-value association.
  276. // SQL: SELECT ... FROM forum_user fu INNER JOIN forum_avatar fa ON fu.avatar_id = fa.id WHERE fa.id = ?
  277. $this->assertValidDQL("SELECT u FROM Doctrine\Tests\Models\Forum\ForumUser u JOIN u.avatar a WHERE a.id = ?1");
  278. }
  279. public function testImplicitJoinInWhereOnCollectionValuedPathExpression()
  280. {
  281. // This should be forbidden, because articles is a collection
  282. $this->assertInvalidDQL("SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.articles a WHERE a.title = ?");
  283. }
  284. public function testInvalidSyntaxIsRejected()
  285. {
  286. $this->assertInvalidDQL("FOOBAR CmsUser");
  287. $this->assertInvalidDQL("DELETE FROM Doctrine\Tests\Models\CMS\CmsUser.articles");
  288. $this->assertInvalidDQL("SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.articles.comments");
  289. // Currently UNDEFINED OFFSET error
  290. $this->assertInvalidDQL("SELECT c FROM CmsUser.articles.comments c");
  291. }
  292. public function testUpdateWorksWithOneField()
  293. {
  294. $this->assertValidDQL("UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.name = 'someone'");
  295. }
  296. public function testUpdateWorksWithMultipleFields()
  297. {
  298. $this->assertValidDQL("UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.name = 'someone', u.username = 'some'");
  299. }
  300. public function testUpdateSupportsConditions()
  301. {
  302. $this->assertValidDQL("UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.name = 'someone' WHERE u.id = 5");
  303. }
  304. public function testDeleteAll()
  305. {
  306. $this->assertValidDQL('DELETE FROM Doctrine\Tests\Models\CMS\CmsUser u');
  307. }
  308. public function testDeleteWithCondition()
  309. {
  310. $this->assertValidDQL('DELETE FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = 3');
  311. }
  312. /**
  313. * The main use case for this generalized style of join is when a join condition
  314. * does not involve a foreign key relationship that is mapped to an entity relationship.
  315. */
  316. public function testImplicitJoinWithCartesianProductAndConditionInWhere()
  317. {
  318. $this->assertValidDQL("SELECT u, a FROM Doctrine\Tests\Models\CMS\CmsUser u, Doctrine\Tests\Models\CMS\CmsArticle a WHERE u.name = a.topic");
  319. }
  320. public function testAllExpressionWithCorrelatedSubquery()
  321. {
  322. $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)');
  323. }
  324. public function testCustomJoinsAndWithKeywordSupported()
  325. {
  326. $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');
  327. }
  328. public function testAnyExpressionWithCorrelatedSubquery()
  329. {
  330. $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)');
  331. }
  332. public function testSomeExpressionWithCorrelatedSubquery()
  333. {
  334. $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)');
  335. }
  336. public function testArithmeticExpressionWithoutParenthesisInWhereClause()
  337. {
  338. $this->assertValidDQL('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE SIZE(u.phonenumbers) + 1 > 10');
  339. }
  340. public function testMemberOfExpression()
  341. {
  342. $this->assertValidDQL('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE :param MEMBER OF u.phonenumbers');
  343. //$this->assertValidDQL("SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE 'Joe' MEMBER OF u.nicknames");
  344. }
  345. public function testSizeFunction()
  346. {
  347. $this->assertValidDQL('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE SIZE(u.phonenumbers) > 1');
  348. }
  349. public function testEmptyCollectionComparisonExpression()
  350. {
  351. $this->assertValidDQL('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.phonenumbers IS EMPTY');
  352. }
  353. public function testSingleValuedAssociationFieldInWhere()
  354. {
  355. $this->assertValidDQL('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.address = ?1');
  356. $this->assertValidDQL('SELECT p FROM Doctrine\Tests\Models\CMS\CmsPhonenumber p WHERE p.user = ?1');
  357. }
  358. public function testBooleanLiteralInWhere()
  359. {
  360. $this->assertValidDQL('SELECT b FROM Doctrine\Tests\Models\Generic\BooleanModel b WHERE b.booleanField = true');
  361. }
  362. public function testSubqueryInSelectExpression()
  363. {
  364. $this->assertValidDQL('select u, (select max(p.phonenumber) from Doctrine\Tests\Models\CMS\CmsPhonenumber p) maxId from Doctrine\Tests\Models\CMS\CmsUser u');
  365. }
  366. public function testUsageOfQComponentOutsideSubquery()
  367. {
  368. $this->assertInvalidDQL('select u, (select max(p.phonenumber) from Doctrine\Tests\Models\CMS\CmsPhonenumber p) maxId from Doctrine\Tests\Models\CMS\CmsUser u WHERE p.user = ?1');
  369. }
  370. public function testUnknownAbstractSchemaName()
  371. {
  372. $this->assertInvalidDQL('SELECT u FROM UnknownClassName u');
  373. }
  374. public function testCorrectPartialObjectLoad()
  375. {
  376. $this->assertValidDQL('SELECT PARTIAL u.{id,name} FROM Doctrine\Tests\Models\CMS\CmsUser u');
  377. }
  378. public function testIncorrectPartialObjectLoadBecauseOfMissingIdentifier()
  379. {
  380. $this->assertInvalidDQL('SELECT PARTIAL u.{name} FROM Doctrine\Tests\Models\CMS\CmsUser u');
  381. }
  382. public function testScalarExpressionInSelect()
  383. {
  384. $this->assertValidDQL('SELECT u, 42 + u.id AS someNumber FROM Doctrine\Tests\Models\CMS\CmsUser u');
  385. }
  386. public function testInputParameterInSelect()
  387. {
  388. $this->assertValidDQL('SELECT u, u.id + ?1 AS someNumber FROM Doctrine\Tests\Models\CMS\CmsUser u');
  389. }
  390. /**
  391. * @group DDC-1091
  392. */
  393. public function testCustomFunctionsReturningStringInStringPrimary()
  394. {
  395. $this->_em->getConfiguration()->addCustomStringFunction('CC', 'Doctrine\ORM\Query\AST\Functions\ConcatFunction');
  396. $this->assertValidDQL("SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE CC('%', u.name) LIKE '%foo%'", true);
  397. }
  398. /**
  399. * @group DDC-505
  400. */
  401. public function testDQLKeywordInJoinIsAllowed()
  402. {
  403. $this->assertValidDQL('SELECT u FROM ' . __NAMESPACE__ . '\DQLKeywordsModelUser u JOIN u.group g');
  404. }
  405. /**
  406. * @group DDC-505
  407. */
  408. public function testDQLKeywordInConditionIsAllowed()
  409. {
  410. $this->assertValidDQL('SELECT g FROM ' . __NAMESPACE__ . '\DQLKeywordsModelGroup g WHERE g.from=0');
  411. }
  412. /* The exception is currently thrown in the SQLWalker, not earlier.
  413. public function testInverseSideSingleValuedAssociationPathNotAllowed()
  414. {
  415. $this->assertInvalidDQL('SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.address = ?1');
  416. }
  417. */
  418. /**
  419. * @group DDC-617
  420. */
  421. public function testSelectOnlyNonRootEntityAlias()
  422. {
  423. $this->assertInvalidDQL('SELECT g FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.groups g');
  424. }
  425. /**
  426. * @group DDC-1108
  427. */
  428. public function testInputParameterSingleChar()
  429. {
  430. $this->assertValidDQL('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name = :q');
  431. }
  432. /**
  433. * @group DDC-1053
  434. */
  435. public function testGroupBy()
  436. {
  437. $this->assertValidDQL('SELECT g.id, count(u.id) FROM Doctrine\Tests\Models\CMS\CmsGroup g JOIN g.users u GROUP BY g.id');
  438. }
  439. /**
  440. * @group DDC-1053
  441. */
  442. public function testGroupByIdentificationVariable()
  443. {
  444. $this->assertValidDQL('SELECT g, count(u.id) FROM Doctrine\Tests\Models\CMS\CmsGroup g JOIN g.users u GROUP BY g');
  445. }
  446. /**
  447. * @group DDC-1053
  448. */
  449. public function testGroupByUnknownIdentificationVariable()
  450. {
  451. $this->assertInvalidDQL('SELECT g, count(u.id) FROM Doctrine\Tests\Models\CMS\CmsGroup g JOIN g.users u GROUP BY m');
  452. }
  453. /**
  454. * @group DDC-117
  455. */
  456. public function testSizeOfForeignKeyOneToManyPrimaryKeyEntity()
  457. {
  458. $this->assertValidDQL("SELECT a, t FROM Doctrine\Tests\Models\DDC117\DDC117Article a JOIN a.translations t WHERE SIZE(a.translations) > 0");
  459. }
  460. /**
  461. * @group DDC-117
  462. */
  463. public function testSizeOfForeignKeyManyToManyPrimaryKeyEntity()
  464. {
  465. $this->assertValidDQL("SELECT e, t FROM Doctrine\Tests\Models\DDC117\DDC117Editor e JOIN e.reviewingTranslations t WHERE SIZE(e.reviewingTranslations) > 0");
  466. }
  467. public function testCaseSupportContainingNullIfExpression()
  468. {
  469. $this->assertValidDQL("SELECT u.id, NULLIF(u.name, u.name) AS shouldBeNull FROM Doctrine\Tests\Models\CMS\CmsUser u");
  470. }
  471. public function testCaseSupportContainingCoalesceExpression()
  472. {
  473. $this->assertValidDQL("select COALESCE(NULLIF(u.name, ''), u.username) as Display FROM Doctrine\Tests\Models\CMS\CmsUser u");
  474. }
  475. }
  476. /** @Entity */
  477. class DQLKeywordsModelUser
  478. {
  479. /** @Id @Column(type="integer") @GeneratedValue */
  480. private $id;
  481. /** @OneToOne(targetEntity="DQLKeywordsModelGroup") */
  482. private $group;
  483. }
  484. /** @Entity */
  485. class DQLKeywordsModelGroup
  486. {
  487. /** @Id @Column(type="integer") @GeneratedValue */
  488. private $id;
  489. /** @Column */
  490. private $from;
  491. }