QueryDqlFunctionTest.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional;
  3. use Doctrine\Tests\Models\Company\CompanyManager;
  4. require_once __DIR__ . '/../../TestInit.php';
  5. /**
  6. * Functional Query tests.
  7. *
  8. * @author robo
  9. */
  10. class QueryDqlFunctionTest extends \Doctrine\Tests\OrmFunctionalTestCase
  11. {
  12. protected function setUp()
  13. {
  14. $this->useModelSet('company');
  15. parent::setUp();
  16. $this->generateFixture();
  17. }
  18. public function testAggregateSum()
  19. {
  20. $salarySum = $this->_em->createQuery('SELECT SUM(m.salary) AS salary FROM Doctrine\Tests\Models\Company\CompanyManager m')
  21. ->getSingleResult();
  22. $this->assertEquals(1500000, $salarySum['salary']);
  23. }
  24. public function testAggregateAvg()
  25. {
  26. $salaryAvg = $this->_em->createQuery('SELECT AVG(m.salary) AS salary FROM Doctrine\Tests\Models\Company\CompanyManager m')
  27. ->getSingleResult();
  28. $this->assertEquals(375000, round($salaryAvg['salary'], 0));
  29. }
  30. public function testAggregateMin()
  31. {
  32. $salary = $this->_em->createQuery('SELECT MIN(m.salary) AS salary FROM Doctrine\Tests\Models\Company\CompanyManager m')
  33. ->getSingleResult();
  34. $this->assertEquals(100000, $salary['salary']);
  35. }
  36. public function testAggregateMax()
  37. {
  38. $salary = $this->_em->createQuery('SELECT MAX(m.salary) AS salary FROM Doctrine\Tests\Models\Company\CompanyManager m')
  39. ->getSingleResult();
  40. $this->assertEquals(800000, $salary['salary']);
  41. }
  42. public function testAggregateCount()
  43. {
  44. $managerCount = $this->_em->createQuery('SELECT COUNT(m.id) AS managers FROM Doctrine\Tests\Models\Company\CompanyManager m')
  45. ->getSingleResult();
  46. $this->assertEquals(4, $managerCount['managers']);
  47. }
  48. public function testFunctionAbs()
  49. {
  50. $result = $this->_em->createQuery('SELECT m, ABS(m.salary * -1) AS abs FROM Doctrine\Tests\Models\Company\CompanyManager m')
  51. ->getResult();
  52. $this->assertEquals(4, count($result));
  53. $this->assertEquals(100000, $result[0]['abs']);
  54. $this->assertEquals(200000, $result[1]['abs']);
  55. $this->assertEquals(400000, $result[2]['abs']);
  56. $this->assertEquals(800000, $result[3]['abs']);
  57. }
  58. public function testFunctionConcat()
  59. {
  60. $arg = $this->_em->createQuery('SELECT m, CONCAT(m.name, m.department) AS namedep FROM Doctrine\Tests\Models\Company\CompanyManager m')
  61. ->getArrayResult();
  62. $this->assertEquals(4, count($arg));
  63. $this->assertEquals('Roman B.IT', $arg[0]['namedep']);
  64. $this->assertEquals('Benjamin E.HR', $arg[1]['namedep']);
  65. $this->assertEquals('Guilherme B.Complaint Department', $arg[2]['namedep']);
  66. $this->assertEquals('Jonathan W.Administration', $arg[3]['namedep']);
  67. }
  68. public function testFunctionLength()
  69. {
  70. $result = $this->_em->createQuery('SELECT m, LENGTH(CONCAT(m.name, m.department)) AS namedeplength FROM Doctrine\Tests\Models\Company\CompanyManager m')
  71. ->getArrayResult();
  72. $this->assertEquals(4, count($result));
  73. $this->assertEquals(10, $result[0]['namedeplength']);
  74. $this->assertEquals(13, $result[1]['namedeplength']);
  75. $this->assertEquals(32, $result[2]['namedeplength']);
  76. $this->assertEquals(25, $result[3]['namedeplength']);
  77. }
  78. public function testFunctionLocate()
  79. {
  80. $dql = "SELECT m, LOCATE('e', LOWER(m.name)) AS loc, LOCATE('e', LOWER(m.name), 7) AS loc2 ".
  81. "FROM Doctrine\Tests\Models\Company\CompanyManager m";
  82. $result = $this->_em->createQuery($dql)
  83. ->getArrayResult();
  84. $this->assertEquals(4, count($result));
  85. $this->assertEquals(0, $result[0]['loc']);
  86. $this->assertEquals(2, $result[1]['loc']);
  87. $this->assertEquals(6, $result[2]['loc']);
  88. $this->assertEquals(0, $result[3]['loc']);
  89. $this->assertEquals(0, $result[0]['loc2']);
  90. $this->assertEquals(10, $result[1]['loc2']);
  91. $this->assertEquals(9, $result[2]['loc2']);
  92. $this->assertEquals(0, $result[3]['loc2']);
  93. }
  94. public function testFunctionLower()
  95. {
  96. $result = $this->_em->createQuery("SELECT m, LOWER(m.name) AS lowername FROM Doctrine\Tests\Models\Company\CompanyManager m")
  97. ->getArrayResult();
  98. $this->assertEquals(4, count($result));
  99. $this->assertEquals('roman b.', $result[0]['lowername']);
  100. $this->assertEquals('benjamin e.', $result[1]['lowername']);
  101. $this->assertEquals('guilherme b.', $result[2]['lowername']);
  102. $this->assertEquals('jonathan w.', $result[3]['lowername']);
  103. }
  104. public function testFunctionMod()
  105. {
  106. $result = $this->_em->createQuery("SELECT m, MOD(m.salary, 3500) AS amod FROM Doctrine\Tests\Models\Company\CompanyManager m")
  107. ->getArrayResult();
  108. $this->assertEquals(4, count($result));
  109. $this->assertEquals(2000, $result[0]['amod']);
  110. $this->assertEquals(500, $result[1]['amod']);
  111. $this->assertEquals(1000, $result[2]['amod']);
  112. $this->assertEquals(2000, $result[3]['amod']);
  113. }
  114. public function testFunctionSqrt()
  115. {
  116. $result = $this->_em->createQuery("SELECT m, SQRT(m.salary) AS sqrtsalary FROM Doctrine\Tests\Models\Company\CompanyManager m")
  117. ->getArrayResult();
  118. $this->assertEquals(4, count($result));
  119. $this->assertEquals(316, round($result[0]['sqrtsalary']));
  120. $this->assertEquals(447, round($result[1]['sqrtsalary']));
  121. $this->assertEquals(632, round($result[2]['sqrtsalary']));
  122. $this->assertEquals(894, round($result[3]['sqrtsalary']));
  123. }
  124. public function testFunctionUpper()
  125. {
  126. $result = $this->_em->createQuery("SELECT m, UPPER(m.name) AS uppername FROM Doctrine\Tests\Models\Company\CompanyManager m")
  127. ->getArrayResult();
  128. $this->assertEquals(4, count($result));
  129. $this->assertEquals('ROMAN B.', $result[0]['uppername']);
  130. $this->assertEquals('BENJAMIN E.', $result[1]['uppername']);
  131. $this->assertEquals('GUILHERME B.', $result[2]['uppername']);
  132. $this->assertEquals('JONATHAN W.', $result[3]['uppername']);
  133. }
  134. public function testFunctionSubstring()
  135. {
  136. $dql = "SELECT m, SUBSTRING(m.name, 1, 3) AS str1, SUBSTRING(m.name, 5) AS str2 ".
  137. "FROM Doctrine\Tests\Models\Company\CompanyManager m";
  138. $result = $this->_em->createQuery($dql)
  139. ->getArrayResult();
  140. $this->assertEquals(4, count($result));
  141. $this->assertEquals('Rom', $result[0]['str1']);
  142. $this->assertEquals('Ben', $result[1]['str1']);
  143. $this->assertEquals('Gui', $result[2]['str1']);
  144. $this->assertEquals('Jon', $result[3]['str1']);
  145. $this->assertEquals('n B.', $result[0]['str2']);
  146. $this->assertEquals('amin E.', $result[1]['str2']);
  147. $this->assertEquals('herme B.', $result[2]['str2']);
  148. $this->assertEquals('than W.', $result[3]['str2']);
  149. }
  150. public function testFunctionTrim()
  151. {
  152. $dql = "SELECT m, TRIM(TRAILING '.' FROM m.name) AS str1, ".
  153. " TRIM(LEADING '.' FROM m.name) AS str2, TRIM(CONCAT(' ', CONCAT(m.name, ' '))) AS str3 ".
  154. "FROM Doctrine\Tests\Models\Company\CompanyManager m";
  155. $result = $this->_em->createQuery($dql)->getArrayResult();
  156. $this->assertEquals(4, count($result));
  157. $this->assertEquals('Roman B', $result[0]['str1']);
  158. $this->assertEquals('Benjamin E', $result[1]['str1']);
  159. $this->assertEquals('Guilherme B', $result[2]['str1']);
  160. $this->assertEquals('Jonathan W', $result[3]['str1']);
  161. $this->assertEquals('Roman B.', $result[0]['str2']);
  162. $this->assertEquals('Benjamin E.', $result[1]['str2']);
  163. $this->assertEquals('Guilherme B.', $result[2]['str2']);
  164. $this->assertEquals('Jonathan W.', $result[3]['str2']);
  165. $this->assertEquals('Roman B.', $result[0]['str3']);
  166. $this->assertEquals('Benjamin E.', $result[1]['str3']);
  167. $this->assertEquals('Guilherme B.', $result[2]['str3']);
  168. $this->assertEquals('Jonathan W.', $result[3]['str3']);
  169. }
  170. public function testOperatorAdd()
  171. {
  172. $result = $this->_em->createQuery('SELECT m, m.salary+2500 AS add FROM Doctrine\Tests\Models\Company\CompanyManager m')
  173. ->getResult();
  174. $this->assertEquals(4, count($result));
  175. $this->assertEquals(102500, $result[0]['add']);
  176. $this->assertEquals(202500, $result[1]['add']);
  177. $this->assertEquals(402500, $result[2]['add']);
  178. $this->assertEquals(802500, $result[3]['add']);
  179. }
  180. public function testOperatorSub()
  181. {
  182. $result = $this->_em->createQuery('SELECT m, m.salary-2500 AS sub FROM Doctrine\Tests\Models\Company\CompanyManager m')
  183. ->getResult();
  184. $this->assertEquals(4, count($result));
  185. $this->assertEquals(97500, $result[0]['sub']);
  186. $this->assertEquals(197500, $result[1]['sub']);
  187. $this->assertEquals(397500, $result[2]['sub']);
  188. $this->assertEquals(797500, $result[3]['sub']);
  189. }
  190. public function testOperatorMultiply()
  191. {
  192. $result = $this->_em->createQuery('SELECT m, m.salary*2 AS op FROM Doctrine\Tests\Models\Company\CompanyManager m')
  193. ->getResult();
  194. $this->assertEquals(4, count($result));
  195. $this->assertEquals(200000, $result[0]['op']);
  196. $this->assertEquals(400000, $result[1]['op']);
  197. $this->assertEquals(800000, $result[2]['op']);
  198. $this->assertEquals(1600000, $result[3]['op']);
  199. }
  200. /**
  201. * @group test
  202. */
  203. public function testOperatorDiv()
  204. {
  205. $result = $this->_em->createQuery('SELECT m, (m.salary/0.5) AS op FROM Doctrine\Tests\Models\Company\CompanyManager m')
  206. ->getResult();
  207. $this->assertEquals(4, count($result));
  208. $this->assertEquals(200000, $result[0]['op']);
  209. $this->assertEquals(400000, $result[1]['op']);
  210. $this->assertEquals(800000, $result[2]['op']);
  211. $this->assertEquals(1600000, $result[3]['op']);
  212. }
  213. public function testConcatFunction()
  214. {
  215. $arg = $this->_em->createQuery('SELECT CONCAT(m.name, m.department) AS namedep FROM Doctrine\Tests\Models\Company\CompanyManager m order by namedep desc')
  216. ->getArrayResult();
  217. $this->assertEquals(4, count($arg));
  218. $this->assertEquals('Roman B.IT', $arg[0]['namedep']);
  219. $this->assertEquals('Jonathan W.Administration', $arg[1]['namedep']);
  220. $this->assertEquals('Guilherme B.Complaint Department', $arg[2]['namedep']);
  221. $this->assertEquals('Benjamin E.HR', $arg[3]['namedep']);
  222. }
  223. /**
  224. * @group DDC-1014
  225. */
  226. public function testDateDiff()
  227. {
  228. $query = $this->_em->createQuery("SELECT DATE_DIFF(CURRENT_TIMESTAMP(), DATE_ADD(CURRENT_TIMESTAMP(), 10, 'day')) AS diff FROM Doctrine\Tests\Models\Company\CompanyManager m");
  229. $arg = $query->getArrayResult();
  230. $this->assertEquals(-10, $arg[0]['diff'], "Should be roughly -10 (or -9)", 1);
  231. $query = $this->_em->createQuery("SELECT DATE_DIFF(DATE_ADD(CURRENT_TIMESTAMP(), 10, 'day'), CURRENT_TIMESTAMP()) AS diff FROM Doctrine\Tests\Models\Company\CompanyManager m");
  232. $arg = $query->getArrayResult();
  233. $this->assertEquals(10, $arg[0]['diff'], "Should be roughly 10 (or 9)", 1);
  234. }
  235. /**
  236. * @group DDC-1014
  237. */
  238. public function testDateAdd()
  239. {
  240. $arg = $this->_em->createQuery("SELECT DATE_ADD(CURRENT_TIMESTAMP(), 10, 'day') AS add FROM Doctrine\Tests\Models\Company\CompanyManager m")
  241. ->getArrayResult();
  242. $this->assertTrue(strtotime($arg[0]['add']) > 0);
  243. $arg = $this->_em->createQuery("SELECT DATE_ADD(CURRENT_TIMESTAMP(), 10, 'month') AS add FROM Doctrine\Tests\Models\Company\CompanyManager m")
  244. ->getArrayResult();
  245. $this->assertTrue(strtotime($arg[0]['add']) > 0);
  246. }
  247. /**
  248. * @group DDC-1014
  249. */
  250. public function testDateSub()
  251. {
  252. $arg = $this->_em->createQuery("SELECT DATE_SUB(CURRENT_TIMESTAMP(), 10, 'day') AS add FROM Doctrine\Tests\Models\Company\CompanyManager m")
  253. ->getArrayResult();
  254. $this->assertTrue(strtotime($arg[0]['add']) > 0);
  255. $arg = $this->_em->createQuery("SELECT DATE_SUB(CURRENT_TIMESTAMP(), 10, 'month') AS add FROM Doctrine\Tests\Models\Company\CompanyManager m")
  256. ->getArrayResult();
  257. $this->assertTrue(strtotime($arg[0]['add']) > 0);
  258. }
  259. protected function generateFixture()
  260. {
  261. $manager1 = new CompanyManager();
  262. $manager1->setName('Roman B.');
  263. $manager1->setTitle('Foo');
  264. $manager1->setDepartment('IT');
  265. $manager1->setSalary(100000);
  266. $manager2 = new CompanyManager();
  267. $manager2->setName('Benjamin E.');
  268. $manager2->setTitle('Foo');
  269. $manager2->setDepartment('HR');
  270. $manager2->setSalary(200000);
  271. $manager3 = new CompanyManager();
  272. $manager3->setName('Guilherme B.');
  273. $manager3->setTitle('Foo');
  274. $manager3->setDepartment('Complaint Department');
  275. $manager3->setSalary(400000);
  276. $manager4 = new CompanyManager();
  277. $manager4->setName('Jonathan W.');
  278. $manager4->setTitle('Foo');
  279. $manager4->setDepartment('Administration');
  280. $manager4->setSalary(800000);
  281. $this->_em->persist($manager1);
  282. $this->_em->persist($manager2);
  283. $this->_em->persist($manager3);
  284. $this->_em->persist($manager4);
  285. $this->_em->flush();
  286. $this->_em->clear();
  287. }
  288. }