ExprTest.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\Tests\ORM\Query;
  20. use Doctrine\ORM\Query\Expr;
  21. use Doctrine\ORM\Query;
  22. require_once __DIR__ . '/../../TestInit.php';
  23. /**
  24. * Test case for the DQL Expr class used for generating DQL snippets through
  25. * a programmatic interface
  26. *
  27. * @author Jonathan H. Wage <jonwage@gmail.com>
  28. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  29. * @link http://www.phpdoctrine.org
  30. * @since 2.0
  31. * @version $Revision$
  32. */
  33. class ExprTest extends \Doctrine\Tests\OrmTestCase
  34. {
  35. private $_em;
  36. protected function setUp()
  37. {
  38. $this->_em = $this->_getTestEntityManager();
  39. $this->_expr = new Expr;
  40. }
  41. public function testAvgExpr()
  42. {
  43. $this->assertEquals('AVG(u.id)', (string) $this->_expr->avg('u.id'));
  44. }
  45. public function testMaxExpr()
  46. {
  47. $this->assertEquals('MAX(u.id)', (string) $this->_expr->max('u.id'));
  48. }
  49. public function testMinExpr()
  50. {
  51. $this->assertEquals('MIN(u.id)', (string) $this->_expr->min('u.id'));
  52. }
  53. public function testCountExpr()
  54. {
  55. $this->assertEquals('MAX(u.id)', (string) $this->_expr->max('u.id'));
  56. }
  57. public function testCountDistinctExpr()
  58. {
  59. $this->assertEquals('COUNT(DISTINCT u.id)', (string) $this->_expr->countDistinct('u.id'));
  60. }
  61. public function testExistsExpr()
  62. {
  63. $qb = $this->_em->createQueryBuilder();
  64. $qb->select('u')->from('User', 'u')->where('u.name = ?1');
  65. $this->assertEquals('EXISTS(SELECT u FROM User u WHERE u.name = ?1)', (string) $this->_expr->exists($qb));
  66. }
  67. public function testAllExpr()
  68. {
  69. $qb = $this->_em->createQueryBuilder();
  70. $qb->select('u')->from('User', 'u')->where('u.name = ?1');
  71. $this->assertEquals('ALL(SELECT u FROM User u WHERE u.name = ?1)', (string) $this->_expr->all($qb));
  72. }
  73. public function testSomeExpr()
  74. {
  75. $qb = $this->_em->createQueryBuilder();
  76. $qb->select('u')->from('User', 'u')->where('u.name = ?1');
  77. $this->assertEquals('SOME(SELECT u FROM User u WHERE u.name = ?1)', (string) $this->_expr->some($qb));
  78. }
  79. public function testAnyExpr()
  80. {
  81. $qb = $this->_em->createQueryBuilder();
  82. $qb->select('u')->from('User', 'u')->where('u.name = ?1');
  83. $this->assertEquals('ANY(SELECT u FROM User u WHERE u.name = ?1)', (string) $this->_expr->any($qb));
  84. }
  85. public function testNotExpr()
  86. {
  87. $qb = $this->_em->createQueryBuilder();
  88. $qb->select('u')->from('User', 'u')->where('u.name = ?1');
  89. $this->assertEquals('NOT(SELECT u FROM User u WHERE u.name = ?1)', (string) $this->_expr->not($qb));
  90. }
  91. public function testAndExpr()
  92. {
  93. $this->assertEquals('1 = 1 AND 2 = 2', (string) $this->_expr->andx((string) $this->_expr->eq(1, 1), (string) $this->_expr->eq(2, 2)));
  94. }
  95. public function testIntelligentParenthesisPreventionAndExpr()
  96. {
  97. $this->assertEquals(
  98. '1 = 1 AND 2 = 2',
  99. (string) $this->_expr->andx($this->_expr->orx($this->_expr->andx($this->_expr->eq(1, 1))), (string) $this->_expr->eq(2, 2))
  100. );
  101. }
  102. public function testOrExpr()
  103. {
  104. $this->assertEquals('1 = 1 OR 2 = 2', (string) $this->_expr->orx((string) $this->_expr->eq(1, 1), (string) $this->_expr->eq(2, 2)));
  105. }
  106. public function testAbsExpr()
  107. {
  108. $this->assertEquals('ABS(1)', (string) $this->_expr->abs(1));
  109. }
  110. public function testProdExpr()
  111. {
  112. $this->assertEquals('1 * 2', (string) $this->_expr->prod(1, 2));
  113. }
  114. public function testDiffExpr()
  115. {
  116. $this->assertEquals('1 - 2', (string) $this->_expr->diff(1, 2));
  117. }
  118. public function testSumExpr()
  119. {
  120. $this->assertEquals('1 + 2', (string) $this->_expr->sum(1, 2));
  121. }
  122. public function testQuotientExpr()
  123. {
  124. $this->assertEquals('10 / 2', (string) $this->_expr->quot(10, 2));
  125. }
  126. public function testScopeInArithmeticExpr()
  127. {
  128. $this->assertEquals('(100 - 20) / 2', (string) $this->_expr->quot($this->_expr->diff(100, 20), 2));
  129. $this->assertEquals('100 - (20 / 2)', (string) $this->_expr->diff(100, $this->_expr->quot(20, 2)));
  130. }
  131. public function testSquareRootExpr()
  132. {
  133. $this->assertEquals('SQRT(1)', (string) $this->_expr->sqrt(1));
  134. }
  135. public function testEqualExpr()
  136. {
  137. $this->assertEquals('1 = 1', (string) $this->_expr->eq(1, 1));
  138. }
  139. public function testLikeExpr()
  140. {
  141. $this->assertEquals('a.description LIKE :description', (string) $this->_expr->like('a.description', ':description'));
  142. }
  143. public function testConcatExpr()
  144. {
  145. $this->assertEquals('CONCAT(u.first_name, u.last_name)', (string) $this->_expr->concat('u.first_name', 'u.last_name'));
  146. }
  147. public function testSubstringExpr()
  148. {
  149. $this->assertEquals('SUBSTRING(a.title, 0, 25)', (string) $this->_expr->substring('a.title', 0, 25));
  150. }
  151. /**
  152. * @group regression
  153. * @group DDC-612
  154. */
  155. public function testSubstringExprAcceptsTwoArguments()
  156. {
  157. $this->assertEquals('SUBSTRING(a.title, 5)', (string) $this->_expr->substring('a.title', 5));
  158. }
  159. public function testLowerExpr()
  160. {
  161. $this->assertEquals('LOWER(u.first_name)', (string) $this->_expr->lower('u.first_name'));
  162. }
  163. public function testUpperExpr()
  164. {
  165. $this->assertEquals('UPPER(u.first_name)', (string) $this->_expr->upper('u.first_name'));
  166. }
  167. public function testLengthExpr()
  168. {
  169. $this->assertEquals('LENGTH(u.first_name)', (string) $this->_expr->length('u.first_name'));
  170. }
  171. public function testGreaterThanExpr()
  172. {
  173. $this->assertEquals('5 > 2', (string) $this->_expr->gt(5, 2));
  174. }
  175. public function testLessThanExpr()
  176. {
  177. $this->assertEquals('2 < 5', (string) $this->_expr->lt(2, 5));
  178. }
  179. public function testStringLiteralExpr()
  180. {
  181. $this->assertEquals("'word'", (string) $this->_expr->literal('word'));
  182. }
  183. public function testNumericLiteralExpr()
  184. {
  185. $this->assertEquals(5, (string) $this->_expr->literal(5));
  186. }
  187. /**
  188. * @group regression
  189. * @group DDC-610
  190. */
  191. public function testLiteralExprProperlyQuotesStrings()
  192. {
  193. $this->assertEquals("'00010001'", (string) $this->_expr->literal('00010001'));
  194. }
  195. public function testGreaterThanOrEqualToExpr()
  196. {
  197. $this->assertEquals('5 >= 2', (string) $this->_expr->gte(5, 2));
  198. }
  199. public function testLessThanOrEqualTo()
  200. {
  201. $this->assertEquals('2 <= 5', (string) $this->_expr->lte(2, 5));
  202. }
  203. public function testBetweenExpr()
  204. {
  205. $this->assertEquals('u.id BETWEEN 3 AND 6', (string) $this->_expr->between('u.id', 3, 6));
  206. }
  207. public function testTrimExpr()
  208. {
  209. $this->assertEquals('TRIM(u.id)', (string) $this->_expr->trim('u.id'));
  210. }
  211. public function testIsNullExpr()
  212. {
  213. $this->assertEquals('u.id IS NULL', (string) $this->_expr->isNull('u.id'));
  214. }
  215. public function testIsNotNullExpr()
  216. {
  217. $this->assertEquals('u.id IS NOT NULL', (string) $this->_expr->isNotNull('u.id'));
  218. }
  219. public function testInExpr()
  220. {
  221. $this->assertEquals('u.id IN(1, 2, 3)', (string) $this->_expr->in('u.id', array(1, 2, 3)));
  222. }
  223. public function testInLiteralExpr()
  224. {
  225. $this->assertEquals("u.type IN('foo', 'bar')", (string) $this->_expr->in('u.type', array('foo', 'bar')));
  226. }
  227. public function testNotInExpr()
  228. {
  229. $this->assertEquals('u.id NOT IN(1, 2, 3)', (string) $this->_expr->notIn('u.id', array(1, 2, 3)));
  230. }
  231. public function testNotInLiteralExpr()
  232. {
  233. $this->assertEquals("u.type NOT IN('foo', 'bar')", (string) $this->_expr->notIn('u.type', array('foo', 'bar')));
  234. }
  235. public function testAndxOrxExpr()
  236. {
  237. $andExpr = $this->_expr->andx();
  238. $andExpr->add($this->_expr->eq(1, 1));
  239. $andExpr->add($this->_expr->lt(1, 5));
  240. $orExpr = $this->_expr->orx();
  241. $orExpr->add($andExpr);
  242. $orExpr->add($this->_expr->eq(1, 1));
  243. $this->assertEquals('(1 = 1 AND 1 < 5) OR 1 = 1', (string) $orExpr);
  244. }
  245. public function testOrxExpr()
  246. {
  247. $orExpr = $this->_expr->orx();
  248. $orExpr->add($this->_expr->eq(1, 1));
  249. $orExpr->add($this->_expr->lt(1, 5));
  250. $this->assertEquals('1 = 1 OR 1 < 5', (string) $orExpr);
  251. }
  252. public function testOrderByCountExpr()
  253. {
  254. $orderExpr = $this->_expr->desc('u.username');
  255. $this->assertEquals($orderExpr->count(), 1);
  256. $this->assertEquals('u.username DESC', (string) $orderExpr);
  257. }
  258. public function testOrderByOrder()
  259. {
  260. $orderExpr = $this->_expr->desc('u.username');
  261. $this->assertEquals('u.username DESC', (string) $orderExpr);
  262. }
  263. public function testOrderByAsc()
  264. {
  265. $orderExpr = $this->_expr->asc('u.username');
  266. $this->assertEquals('u.username ASC', (string) $orderExpr);
  267. }
  268. /**
  269. * @expectedException \InvalidArgumentException
  270. */
  271. public function testAddThrowsException()
  272. {
  273. $orExpr = $this->_expr->orx();
  274. $orExpr->add($this->_expr->quot(5, 2));
  275. }
  276. }