PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/bluebox/libraries/doctrine/tests/Query/LimitTestCase.php

https://github.com/robertleeplummerjr/bluebox
PHP | 329 lines | 221 code | 73 blank | 35 comment | 0 complexity | d810b4a9dee613e904fc77a7156b0cb3 MD5 | raw file
  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.phpdoctrine.org>.
  20. */
  21. /**
  22. * Doctrine_Query_Limit_TestCase
  23. *
  24. * This test case is used for testing DQL LIMIT clause
  25. *
  26. * @package Doctrine
  27. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  28. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  29. * @category Object Relational Mapping
  30. * @link www.phpdoctrine.org
  31. * @since 1.0
  32. * @version $Revision$
  33. */
  34. class Doctrine_Query_Limit_TestCase extends Doctrine_UnitTestCase
  35. {
  36. public function prepareTables()
  37. {
  38. $this->tables[] = "Photo";
  39. $this->tables[] = "Tag";
  40. $this->tables[] = "Phototag";
  41. parent::prepareTables();
  42. }
  43. public function testLimitWithNormalManyToMany()
  44. {
  45. $coll = new Doctrine_Collection($this->connection->getTable("Photo"));
  46. $tag = new Tag();
  47. $tag->tag = "Some tag";
  48. $coll[0]->Tag[0] = $tag;
  49. $coll[0]->name = "photo 1";
  50. $coll[1]->Tag[0] = $tag;
  51. $coll[1]->name = "photo 2";
  52. $coll[2]->Tag[0] = $tag;
  53. $coll[2]->name = "photo 3";
  54. $coll[3]->Tag[0]->tag = "Other tag";
  55. $coll[3]->name = "photo 4";
  56. $this->connection->flush();
  57. $q = new Doctrine_Query();
  58. $q->from('Photo')->where('Photo.Tag.id = ?')->orderby('Photo.id DESC')->limit(100);
  59. $photos = $q->execute(array(1));
  60. $this->assertEqual($photos->count(), 3);
  61. $this->assertEqual($q->getSqlQuery(),
  62. "SELECT p.id AS p__id, p.name AS p__name FROM photo p LEFT JOIN phototag p2 ON (p.id = p2.photo_id) LEFT JOIN tag t ON t.id = p2.tag_id WHERE p.id IN (SELECT DISTINCT p3.id FROM photo p3 LEFT JOIN phototag p4 ON (p3.id = p4.photo_id) LEFT JOIN tag t2 ON t2.id = p4.tag_id WHERE t2.id = ? ORDER BY p3.id DESC LIMIT 100) AND t.id = ? ORDER BY p.id DESC");
  63. }
  64. public function testLimitWithOneToOneLeftJoin()
  65. {
  66. $q = new Doctrine_Query();
  67. $q->select('u.id, e.*')->from('User u, u.Email e')->limit(5);
  68. $users = $q->execute();
  69. $this->assertEqual($users->count(), 5);
  70. $this->assertEqual($q->getSqlQuery(), "SELECT e.id AS e__id, e2.id AS e2__id, e2.address AS e2__address FROM entity e LEFT JOIN email e2 ON e.email_id = e2.id WHERE (e.type = 0) LIMIT 5");
  71. }
  72. public function testLimitWithOneToOneInnerJoin()
  73. {
  74. $q = new Doctrine_Query();
  75. $q->select('u.id, e.*')->from('User u, u:Email e')->limit(5);
  76. $users = $q->execute();
  77. $this->assertEqual($users->count(), 5);
  78. $this->assertEqual($q->getSqlQuery(), "SELECT e.id AS e__id, e2.id AS e2__id, e2.address AS e2__address FROM entity e INNER JOIN email e2 ON e.email_id = e2.id WHERE (e.type = 0) LIMIT 5");
  79. }
  80. public function testLimitWithOneToManyLeftJoin()
  81. {
  82. $q = new Doctrine_Query();
  83. $q->select('u.id, p.*')->from('User u, u.Phonenumber p')->limit(5);
  84. $sql = $q->getSqlQuery();
  85. $this->assertEqual($q->getSqlQuery(),
  86. 'SELECT e.id AS e__id, p.id AS p__id, p.phonenumber AS p__phonenumber, p.entity_id AS p__entity_id FROM entity e LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE e.id IN (SELECT DISTINCT e2.id FROM entity e2 LEFT JOIN phonenumber p2 ON e2.id = p2.entity_id WHERE (e2.type = 0) LIMIT 5) AND (e.type = 0)');
  87. $users = $q->execute();
  88. $count = $this->conn->count();
  89. $this->assertEqual($users->count(), 5);
  90. $users[0]->Phonenumber[0];
  91. $this->assertEqual($count, $this->conn->count());
  92. $q->offset(2);
  93. $users = $q->execute();
  94. $count = $this->conn->count();
  95. $this->assertEqual($users->count(), 5);
  96. $users[3]->Phonenumber[0];
  97. $this->assertEqual($count, $this->conn->count());
  98. }
  99. public function testLimitWithOneToManyLeftJoinAndCondition()
  100. {
  101. $q = new Doctrine_Query();
  102. $q->select('User.name')->from('User')->where("User.Phonenumber.phonenumber LIKE '%123%'")->limit(5);
  103. $users = $q->execute();
  104. $this->assertEqual($users->count(), 5);
  105. $this->assertEqual($users[0]->name, 'zYne');
  106. $this->assertEqual($users[1]->name, 'Arnold Schwarzenegger');
  107. $this->assertEqual($users[2]->name, 'Michael Caine');
  108. $this->assertEqual($users[3]->name, 'Sylvester Stallone');
  109. $this->assertEqual($users[4]->name, 'Jean Reno');
  110. $this->assertEqual($q->getSqlQuery(),
  111. "SELECT e.id AS e__id, e.name AS e__name FROM entity e LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE e.id IN (SELECT DISTINCT e2.id FROM entity e2 LEFT JOIN phonenumber p2 ON e2.id = p2.entity_id WHERE p2.phonenumber LIKE '%123%' AND (e2.type = 0) LIMIT 5) AND p.phonenumber LIKE '%123%' AND (e.type = 0)");
  112. }
  113. public function testLimitWithOneToManyLeftJoinAndOrderBy()
  114. {
  115. $q = new Doctrine_Query();
  116. $q->select('User.name')->distinct()->from('User')->where("User.Phonenumber.phonenumber LIKE '%123%'")->orderby('User.Email.address')->limit(5);
  117. $users = $q->execute();
  118. $this->assertEqual($users[0]->name, 'Arnold Schwarzenegger');
  119. $this->assertEqual($users[1]->name, 'Michael Caine');
  120. $this->assertEqual($users[2]->name, 'Jean Reno');
  121. $this->assertEqual($users[3]->name, 'Sylvester Stallone');
  122. $this->assertEqual($users[4]->name, 'zYne');
  123. $this->assertEqual($users->count(), 5);
  124. }
  125. public function testLimitWithOneToManyInnerJoin()
  126. {
  127. $q = new Doctrine_Query();
  128. $q->select('u.id, p.*')->from('User u INNER JOIN u.Phonenumber p');
  129. $q->limit(5);
  130. $sql = $q->getSqlQuery();
  131. $users = $q->execute();
  132. $count = $this->conn->count();
  133. $this->assertEqual($users->count(), 5);
  134. $users[0]->Phonenumber[0];
  135. $this->assertEqual($count, $this->conn->count());
  136. $q->offset(2);
  137. $users = $q->execute();
  138. $count = $this->conn->count();
  139. $this->assertEqual($users->count(), 5);
  140. $users[3]->Phonenumber[0];
  141. $this->assertEqual($count, $this->conn->count());
  142. $this->assertEqual($q->getSqlQuery(),
  143. 'SELECT e.id AS e__id, p.id AS p__id, p.phonenumber AS p__phonenumber, p.entity_id AS p__entity_id FROM entity e INNER JOIN phonenumber p ON e.id = p.entity_id WHERE e.id IN (SELECT DISTINCT e2.id FROM entity e2 INNER JOIN phonenumber p2 ON e2.id = p2.entity_id WHERE (e2.type = 0) LIMIT 5 OFFSET 2) AND (e.type = 0)');
  144. }
  145. public function testLimitWithPreparedQueries()
  146. {
  147. $q = new Doctrine_Query();
  148. $q->select('u.id, p.id')->from('User u LEFT JOIN u.Phonenumber p');
  149. $q->where('u.name = ?', array('zYne'));
  150. $q->limit(5);
  151. $users = $q->execute();
  152. $this->assertEqual($users->count(), 1);
  153. $count = $this->conn->count();
  154. $users[0]->Phonenumber[0];
  155. $this->assertEqual($count, $this->conn->count());
  156. $this->assertEqual($q->getSqlQuery(),
  157. 'SELECT e.id AS e__id, p.id AS p__id FROM entity e LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE e.id IN (SELECT DISTINCT e2.id FROM entity e2 LEFT JOIN phonenumber p2 ON e2.id = p2.entity_id WHERE e2.name = ? AND (e2.type = 0) LIMIT 5) AND e.name = ? AND (e.type = 0)');
  158. $q = new Doctrine_Query();
  159. $q->select('u.id, p.id')->from('User u LEFT JOIN u.Phonenumber p');
  160. $q->where("u.name LIKE ? OR u.name LIKE ?");
  161. $q->limit(5);
  162. $users = $q->execute(array('%zYne%', '%Arnold%'));
  163. $this->assertEqual($users->count(), 2);
  164. $count = $this->conn->count();
  165. $users[0]->Phonenumber[0];
  166. $this->assertEqual($count, $this->conn->count());
  167. $this->assertEqual($q->getSqlQuery(),
  168. "SELECT e.id AS e__id, p.id AS p__id FROM entity e LEFT JOIN phonenumber p ON"
  169. . " e.id = p.entity_id WHERE e.id IN (SELECT DISTINCT e2.id FROM entity e2 LEFT JOIN phonenumber p2"
  170. . " ON e2.id = p2.entity_id WHERE (e2.name LIKE ? OR e2.name LIKE ?) AND (e2.type = 0) LIMIT 5) AND "
  171. . "(e.name LIKE ? OR e.name LIKE ?) AND (e.type = 0)");
  172. }
  173. public function testConnectionFlushing()
  174. {
  175. $q = new Doctrine_Query();
  176. $q->from('User.Phonenumber');
  177. $q->where('User.name = ?', array('zYne'));
  178. $q->limit(5);
  179. $users = $q->execute();
  180. $this->assertEqual($q->getSqlQuery(), 'SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id, p.id AS p__id, p.phonenumber AS p__phonenumber, p.entity_id AS p__entity_id FROM entity e LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE e.id IN (SELECT DISTINCT e2.id FROM entity e2 LEFT JOIN phonenumber p2 ON e2.id = p2.entity_id WHERE e2.name = ? AND (e2.type = 0) LIMIT 5) AND e.name = ? AND (e.type = 0)');
  181. $this->assertEqual($users->count(), 1);
  182. //$this->connection->flush();
  183. }
  184. public function testLimitWithManyToManyColumnAggInheritanceLeftJoin()
  185. {
  186. $q = new Doctrine_Query();
  187. $q->from('User.Group')->limit(5);
  188. $users = $q->execute();
  189. $this->assertEqual($users->count(), 5);
  190. $user = $this->objTable->find(5);
  191. $user->Group[1]->name = "Tough guys inc.";
  192. $user->Group[2]->name = "Terminators";
  193. $user2 = $this->objTable->find(4);
  194. //$user2->Group = $user->Group;
  195. $user2->Group = new Doctrine_Collection('Group');
  196. $user2->Group[] = $user->Group[0];
  197. $user2->Group[] = $user->Group[1];
  198. $user2->Group[] = $user->Group[2];
  199. $user3 = $this->objTable->find(6);
  200. //$user3->Group = $user->Group;
  201. $user3->Group = new Doctrine_Collection('Group');
  202. $user3->Group[] = $user->Group[0];
  203. $user3->Group[] = $user->Group[1];
  204. $user3->Group[] = $user->Group[2];
  205. $this->assertEqual($user->Group[0]->name, "Action Actors");
  206. $this->assertEqual(count($user->Group), 3);
  207. $this->assertEqual(count($user2->Group), 3);
  208. $this->assertEqual(count($user3->Group), 3);
  209. $this->connection->flush();
  210. $this->assertEqual($user->Group[0]->name, "Action Actors");
  211. $this->assertEqual(count($user->Group), 3);
  212. $q = new Doctrine_Query();
  213. $q->from("User")->where("User.Group.id = ?")->orderby("User.id ASC")->limit(5);
  214. $users = $q->execute(array($user->Group[1]->id));
  215. $this->assertEqual($users->count(), 3);
  216. $this->connection->clear();
  217. $q = new Doctrine_Query();
  218. $q->from('User')->where('User.Group.id = ?')->orderby('User.id DESC');
  219. $users = $q->execute(array($user->Group[1]->id));
  220. $this->assertEqual($users->count(), 3);
  221. }
  222. public function testLimitAttribute()
  223. {
  224. $this->manager->setAttribute(Doctrine::ATTR_QUERY_LIMIT, Doctrine::LIMIT_ROWS);
  225. $this->connection->clear();
  226. $q = new Doctrine_Query();
  227. $q->from('User')->where('User.Group.name = ?')->orderby('User.id DESC')->limit(5);
  228. $users = $q->execute(array('Tough guys inc.'));
  229. $this->assertEqual($users->count(), 3);
  230. $this->assertEqual($q->getSqlQuery(), "SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id FROM entity e LEFT JOIN groupuser g ON (e.id = g.user_id) LEFT JOIN entity e2 ON e2.id = g.group_id AND e2.type = 1 WHERE e2.name = ? AND (e.type = 0) ORDER BY e.id DESC LIMIT 5");
  231. $this->manager->setAttribute(Doctrine::ATTR_QUERY_LIMIT, Doctrine::LIMIT_RECORDS);
  232. }
  233. public function testLimitWithManyToManyAndColumnAggregationInheritance()
  234. {
  235. $q = new Doctrine_Query();
  236. $q->from('User u, u.Group g')->where('g.id > 1')->orderby('u.name DESC')->limit(10);
  237. }
  238. public function testLimitNoticesOrderbyJoins()
  239. {
  240. $q = new Doctrine_Query();
  241. $q->from('Photo p')
  242. ->leftJoin('p.Tag t')
  243. ->orderby('t.id DESC')->limit(10);
  244. $this->assertEqual($q->getSqlQuery(), "SELECT p.id AS p__id, p.name AS p__name, t.id AS t__id, t.tag AS t__tag FROM photo p LEFT JOIN phototag p2 ON (p.id = p2.photo_id) LEFT JOIN tag t ON t.id = p2.tag_id WHERE p.id IN (SELECT DISTINCT p3.id FROM photo p3 LEFT JOIN phototag p4 ON (p3.id = p4.photo_id) LEFT JOIN tag t2 ON t2.id = p4.tag_id ORDER BY t2.id DESC LIMIT 10) ORDER BY t.id DESC");
  245. }
  246. public function testLimitSubqueryNotNeededIfSelectSingleFieldDistinct()
  247. {
  248. $q = new Doctrine_Query();
  249. $q->select('u.id')->distinct()->from('User u LEFT JOIN u.Phonenumber p');
  250. $q->where('u.name = ?');
  251. $q->limit(5);
  252. $users = $q->execute(array('zYne'));
  253. $this->assertEqual(1, $users->count());
  254. $this->assertEqual($q->getSqlQuery(), "SELECT DISTINCT e.id AS e__id FROM entity e LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE e.name = ? AND (e.type = 0) LIMIT 5");
  255. }
  256. }