DqlGenerationTest.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. /*
  3. * $Id$
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.doctrine-project.org>.
  20. */
  21. namespace Doctrine\Tests\ORM\Query;
  22. require_once __DIR__ . '/../../TestInit.php';
  23. /**
  24. * Test case for testing the saving and referencing of query identifiers.
  25. *
  26. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  27. * @author Janne Vanhala <jpvanhal@cc.hut.fi>
  28. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  29. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  30. * @link http://www.doctrine-project.org
  31. * @since 2.0
  32. * @version $Revision$
  33. */
  34. class DqlGenerationTest extends \Doctrine\Tests\OrmTestCase
  35. {
  36. protected function setUp() {
  37. $this->markTestSkipped('Not yet implemented.');
  38. }
  39. protected function createQuery()
  40. {
  41. return $this->_em->createQuery();
  42. }
  43. public function testSelect()
  44. {
  45. $query = $this->createQuery();
  46. // select and from
  47. $query->setDql('FROM User u');
  48. $this->assertEquals('FROM User u', $query->getDql()); // Internally we use SELECT * FROM User u to process the DQL
  49. $query->free();
  50. $query->select()->from('User u');
  51. $this->assertEquals('SELECT * FROM User u', $query->getDql());
  52. $query->free();
  53. $query->select('u.*')->from('User u');
  54. $this->assertEquals('SELECT u.* FROM User u', $query->getDql());
  55. $query->free();
  56. $query->select('u.id')->from('User u');
  57. $this->assertEquals('SELECT u.id FROM User u', $query->getDql());
  58. $query->free();
  59. $query->select('u.id, u.name')->from('User u');
  60. $this->assertEquals('SELECT u.id, u.name FROM User u', $query->getDql());
  61. $query->free();
  62. $query->select('u.name AS myCustomName')->from('User u');
  63. $this->assertEquals('SELECT u.name AS myCustomName FROM User u', $query->getDql());
  64. $query->free();
  65. $query->select('u.id')->select('u.name')->from('User u');
  66. $this->assertEquals('SELECT u.id, u.name FROM User u', $query->getDql());
  67. $query->free();
  68. }
  69. public function testSelectDistinct()
  70. {
  71. $query = $this->createQuery();
  72. $query->select()->distinct()->from('User u');
  73. $this->assertEquals('SELECT DISTINCT * FROM User u', $query->getDql());
  74. $query->free();
  75. $query->select('u.name')->distinct(false)->from('User u');
  76. $this->assertEquals('SELECT u.name FROM User u', $query->getDql());
  77. $query->free();
  78. $query->select()->distinct(false)->from('User u');
  79. $this->assertEquals('SELECT * FROM User u', $query->getDql());
  80. $query->free();
  81. $query->select('u.name')->distinct()->from('User u');
  82. $this->assertEquals('SELECT DISTINCT u.name FROM User u', $query->getDql());
  83. $query->free();
  84. $query->select('u.name, u.email')->distinct()->from('User u');
  85. $this->assertEquals('SELECT DISTINCT u.name, u.email FROM User u', $query->getDql());
  86. $query->free();
  87. $query->select('u.name')->select('u.email')->distinct()->from('User u');
  88. $this->assertEquals('SELECT DISTINCT u.name, u.email FROM User u', $query->getDql());
  89. $query->free();
  90. $query->select('DISTINCT u.name')->from('User u');
  91. $this->assertEquals('SELECT DISTINCT u.name FROM User u', $query->getDql());
  92. $query->free();
  93. $query->select('DISTINCT u.name, u.email')->from('User u');
  94. $this->assertEquals('SELECT DISTINCT u.name, u.email FROM User u', $query->getDql());
  95. $query->free();
  96. $query->select('DISTINCT u.name')->select('u.email')->from('User u');
  97. $this->assertEquals('SELECT DISTINCT u.name, u.email FROM User u', $query->getDql());
  98. $query->free();
  99. }
  100. public function testSelectJoin()
  101. {
  102. $query = $this->createQuery();
  103. $query->select('u.*')->from('User u')->join('u.Group g')->where('g.id = ?', 1);
  104. $this->assertEquals('SELECT u.* FROM User u INNER JOIN u.Group g WHERE g.id = ?', $query->getDql());
  105. $this->assertEquals(array(1), $query->getParams());
  106. $query->free();
  107. $query->select('u.*')->from('User u')->innerJoin('u.Group g')->where('g.id = ?', 1);
  108. $this->assertEquals('SELECT u.* FROM User u INNER JOIN u.Group g WHERE g.id = ?', $query->getDql());
  109. $this->assertEquals(array(1), $query->getParams());
  110. $query->free();
  111. $query->select('u.*')->from('User u')->leftJoin('u.Group g')->where('g.id IS NULL');
  112. $this->assertEquals('SELECT u.* FROM User u LEFT JOIN u.Group g WHERE g.id IS NULL', $query->getDql());
  113. $query->free();
  114. $query->select('u.*')->from('User u')->leftJoin('u.UserGroup ug')->leftJoin('ug.Group g')->where('g.name = ?', 'admin');
  115. $this->assertEquals('SELECT u.* FROM User u LEFT JOIN u.UserGroup ug LEFT JOIN ug.Group g WHERE g.name = ?', $query->getDql());
  116. $query->free();
  117. }
  118. public function testSelectWhere()
  119. {
  120. $query = $this->createQuery();
  121. $query->select('u.name')->from('User u')->where('u.id = ?', 1);
  122. $this->assertEquals('SELECT u.name FROM User u WHERE u.id = ?', $query->getDql());
  123. $this->assertEquals(array(1), $query->getParams());
  124. $query->free();
  125. $query->select('u.name')->from('User u')->where('u.id = ? AND u.type != ?', array(1, 'admin'));
  126. $this->assertEquals('SELECT u.name FROM User u WHERE u.id = ? AND u.type != ?', $query->getDql());
  127. $this->assertEquals(array(1, 'admin'), $query->getParams());
  128. $query->free();
  129. $query->select('u.name')->from('User u')->where('u.id = ?', 1)->andWhere('u.type != ?', 'admin');
  130. $this->assertEquals('SELECT u.name FROM User u WHERE u.id = ? AND u.type != ?', $query->getDql());
  131. $this->assertEquals(array(1, 'admin'), $query->getParams());
  132. $query->free();
  133. $query->select('u.name')->from('User u')->where('( u.id = ?', 1)->andWhere('u.type != ? )', 'admin');
  134. $this->assertEquals('SELECT u.name FROM User u WHERE ( u.id = ? AND u.type != ? )', $query->getDql());
  135. $this->assertEquals(array(1, 'admin'), $query->getParams());
  136. $query->free();
  137. $query->select('u.name')->from('User u')->where('u.id = ? OR u.type != ?', array(1, 'admin'));
  138. $this->assertEquals('SELECT u.name FROM User u WHERE u.id = ? OR u.type != ?', $query->getDql());
  139. $this->assertEquals(array(1, 'admin'), $query->getParams());
  140. $query->free();
  141. $query->select('u.name')->from('User u')->where('u.id = ?', 1)->orWhere('u.type != ?', 'admin');
  142. $this->assertEquals('SELECT u.name FROM User u WHERE u.id = ? OR u.type != ?', $query->getDql());
  143. $this->assertEquals(array(1, 'admin'), $query->getParams());
  144. $query->free();
  145. $query->select('u.name')->from('User u')->andwhere('u.id = ?', 1)->andWhere('u.type != ?', 'admin')->orWhere('u.email = ?', 'admin@localhost');
  146. $this->assertEquals('SELECT u.name FROM User u WHERE u.id = ? AND u.type != ? OR u.email = ?', $query->getDql());
  147. $this->assertEquals(array(1, 'admin', 'admin@localhost'), $query->getParams());
  148. $query->free();
  149. }
  150. public function testSelectWhereIn()
  151. {
  152. $query = $this->createQuery();
  153. $query->select('u.name')->from('User u')->whereIn('u.id', array(1, 2, 3, 4, 5));
  154. $this->assertEquals('SELECT u.name FROM User u WHERE u.id IN (?, ?, ?, ?, ?)', $query->getDql());
  155. $this->assertEquals(array(1, 2, 3, 4, 5), $query->getParams());
  156. $query->free();
  157. $query->select('u.name')->from('User u')->whereNotIn('u.id', array(1, 2, 3));
  158. $this->assertEquals('SELECT u.name FROM User u WHERE u.id NOT IN (?, ?, ?)', $query->getDql());
  159. $this->assertEquals(array(1, 2, 3), $query->getParams());
  160. $query->free();
  161. $query->select('u.name')->from('User u')->where('u.type = ?', 'admin')->andWhereIn('u.id', array(1, 2));
  162. $this->assertEquals('SELECT u.name FROM User u WHERE u.type = ? AND u.id IN (?, ?)', $query->getDql());
  163. $this->assertEquals(array('admin', 1, 2), $query->getParams());
  164. $query->free();
  165. $query->select('u.name')->from('User u')->where('u.type = ?', 'admin')->andWhereNotIn('u.id', array(1, 2));
  166. $this->assertEquals('SELECT u.name FROM User u WHERE u.type = ? AND u.id NOT IN (?, ?)', $query->getDql());
  167. $this->assertEquals(array('admin', 1, 2), $query->getParams());
  168. $query->free();
  169. $query->select('u.name')->from('User u')->whereIn('u.type', array('admin', 'moderator'))->andWhereNotIn('u.id', array(1, 2, 3, 4));
  170. $this->assertEquals('SELECT u.name FROM User u WHERE u.type IN (?, ?) AND u.id NOT IN (?, ?, ?, ?)', $query->getDql());
  171. $this->assertEquals(array('admin', 'moderator', 1, 2, 3, 4), $query->getParams());
  172. $query->free();
  173. $query->select('u.name')->from('User u')->whereIn('u.type', array('admin', 'moderator'))->orWhereIn('u.id', array(1, 2, 3, 4));
  174. $this->assertEquals('SELECT u.name FROM User u WHERE u.type IN (?, ?) OR u.id IN (?, ?, ?, ?)', $query->getDql());
  175. $this->assertEquals(array('admin', 'moderator', 1, 2, 3, 4), $query->getParams());
  176. $query->free();
  177. $query->select('u.name')->from('User u')->whereIn('u.type', array('admin', 'moderator'))->andWhereNotIn('u.id', array(1, 2))->orWhereNotIn('u.type', array('admin', 'moderator'))->andWhereNotIn('u.email', array('user@localhost', 'guest@localhost'));
  178. $this->assertEquals('SELECT u.name FROM User u WHERE u.type IN (?, ?) AND u.id NOT IN (?, ?) OR u.type NOT IN (?, ?) AND u.email NOT IN (?, ?)', $query->getDql());
  179. $this->assertEquals(array('admin', 'moderator', 1, 2, 'admin', 'moderator', 'user@localhost', 'guest@localhost'), $query->getParams());
  180. $query->free();
  181. }
  182. public function testDelete()
  183. {
  184. $query = $this->createQuery();
  185. $query->setDql('DELETE CmsUser u');
  186. $this->assertEquals('DELETE CmsUser u', $query->getDql());
  187. $query->free();
  188. $query->delete()->from('CmsUser u');
  189. $this->assertEquals('DELETE FROM CmsUser u', $query->getDql());
  190. $query->free();
  191. $query->delete()->from('CmsUser u')->where('u.id = ?', 1);
  192. $this->assertEquals('DELETE FROM CmsUser u WHERE u.id = ?', $query->getDql());
  193. $query->free();
  194. $query->delete()->from('CmsUser u')->where('u.username = ?', 'gblanco')->orWhere('u.name = ?', 'Guilherme');
  195. $this->assertEquals('DELETE FROM CmsUser u WHERE u.username = ? OR u.name = ?', $query->getDql());
  196. $query->free();
  197. }
  198. }