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

/test/testsuite/runtime/query/ModelCriteriaSelectTest.php

https://github.com/1989gaurav/Propel
PHP | 383 lines | 313 code | 49 blank | 21 comment | 0 complexity | 07fb5ca992b2a5b1f7697e83611d5ab2 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. require_once dirname(__FILE__) . '/../../../tools/helpers/bookstore/BookstoreTestBase.php';
  10. require_once dirname(__FILE__) . '/../../../tools/helpers/bookstore/BookstoreDataPopulator.php';
  11. /**
  12. * Test class for ModelCriteria select() method.
  13. *
  14. * @author Francois Zaninotto
  15. * @version $Id: ModelCriteriaTest.php 1842 2010-07-22 22:39:40Z KRavEN $
  16. * @package runtime.query
  17. */
  18. class ModelCriteriaSelectTest extends BookstoreTestBase
  19. {
  20. /**
  21. * @expectedException PropelException
  22. */
  23. public function testSelectThrowsExceptionWhenCalledWithAnEmptyString()
  24. {
  25. $c = new ModelCriteria('bookstore', 'Book');
  26. $c->select('');
  27. }
  28. /**
  29. * @expectedException PropelException
  30. */
  31. public function testSelectThrowsExceptionWhenCalledWithAnEmptyArray()
  32. {
  33. $c = new ModelCriteria('bookstore', 'Book');
  34. $c->select(array());
  35. }
  36. public function testSelectStringNoResult()
  37. {
  38. $c = new ModelCriteria('bookstore', 'Book');
  39. $c->where('Book.Title = ?', 'kdjfhlkdsh');
  40. $c->select('Title');
  41. $titles = $c->find($this->con);
  42. $expectedSQL = 'SELECT book.TITLE AS "Title" FROM `book` WHERE book.TITLE = \'kdjfhlkdsh\'';
  43. $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'find() called after select(string) selects a single column');
  44. $this->assertTrue($titles instanceof PropelArrayCollection, 'find() called after select(string) returns a PropelArrayCollection object');
  45. $this->assertTrue(is_array($titles->getData()), 'find() called after select(string) returns an empty PropelArrayCollection object');
  46. $this->assertEquals(0, count($titles), 'find() called after select(string) returns an empty array if no record is found');
  47. $c = new ModelCriteria('bookstore', 'Book');
  48. $c->where('Book.Title = ?', 'kdjfhlkdsh');
  49. $c->select('Title');
  50. $title = $c->findOne();
  51. $this->assertTrue(is_null($title), 'findOne() called after select(string) returns null when no record is found');
  52. }
  53. public function testSelectStringAcceptsColumnNames()
  54. {
  55. $c = new ModelCriteria('bookstore', 'Book');
  56. $c->select('Title');
  57. $titles = $c->find();
  58. $expectedSQL = 'SELECT book.TITLE AS "Title" FROM `book`';
  59. $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'select() accepts short column names');
  60. $c = new ModelCriteria('bookstore', 'Book');
  61. $c->select('Book.Title');
  62. $titles = $c->find();
  63. $expectedSQL = 'SELECT book.TITLE AS "Book.Title" FROM `book`';
  64. $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'select() accepts complete column names');
  65. $c = new ModelCriteria('bookstore', 'Book', 'b');
  66. $c->select('b.Title');
  67. $titles = $c->find();
  68. $expectedSQL = 'SELECT book.TITLE AS "b.Title" FROM `book`';
  69. $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'select() accepts complete column names with table aliases');
  70. }
  71. public function testSelectStringFind()
  72. {
  73. BookstoreDataPopulator::depopulate($this->con);
  74. BookstoreDataPopulator::populate($this->con);
  75. $c = new ModelCriteria('bookstore', 'Book');
  76. $c->select('Title');
  77. $titles = $c->find($this->con);
  78. $this->assertEquals($titles->count(), 4, 'find() called after select(string) returns an array with one row for each record');
  79. $this->assertEquals($titles->shift(), 'Harry Potter and the Order of the Phoenix', 'find() called after select(string) returns an array of column values');
  80. $this->assertEquals($titles->shift(), 'Quicksilver', 'find() called after select(string) returns an array of column values');
  81. $c = new ModelCriteria('bookstore', 'Author');
  82. $c->where('Author.FirstName = ?', 'Neal');
  83. $c->select('FirstName');
  84. $authors = $c->find($this->con);
  85. $this->assertEquals($authors->count(), 1, 'find() called after select(string) allows for where() statements');
  86. $expectedSQL = "SELECT author.FIRST_NAME AS \"FirstName\" FROM `author` WHERE author.FIRST_NAME = 'Neal'";
  87. $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'find() called after select(string) allows for where() statements');
  88. }
  89. public function testSelectStringFindOne()
  90. {
  91. BookstoreDataPopulator::depopulate($this->con);
  92. BookstoreDataPopulator::populate($this->con);
  93. $c = new ModelCriteria('bookstore', 'Book');
  94. $c->select('Title');
  95. $title = $c->findOne($this->con);
  96. $expectedSQL = 'SELECT book.TITLE AS "Title" FROM `book` LIMIT 1';
  97. $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'findOne() called after select(string) selects a single column and requests a single row');
  98. $this->assertTrue(is_string($title),'findOne() called after select(string) returns a string');
  99. $this->assertEquals($title, 'Harry Potter and the Order of the Phoenix', 'findOne() called after select(string) returns the column value of the first row matching the query');
  100. $c = new ModelCriteria('bookstore', 'Author');
  101. $c->where('Author.FirstName = ?', 'Neal');
  102. $c->select('FirstName');
  103. $author = $c->findOne($this->con);
  104. $this->assertEquals(count($author), 1, 'findOne() called after select(string) allows for where() statements');
  105. $expectedSQL = "SELECT author.FIRST_NAME AS \"FirstName\" FROM `author` WHERE author.FIRST_NAME = 'Neal' LIMIT 1";
  106. $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'findOne() called after select(string) allows for where() statements');
  107. }
  108. public function testSelectStringJoin()
  109. {
  110. BookstoreDataPopulator::depopulate($this->con);
  111. BookstoreDataPopulator::populate($this->con);
  112. $c = new ModelCriteria('bookstore', 'Book');
  113. $c->join('Book.Author');
  114. $c->where('Author.FirstName = ?', 'Neal');
  115. $c->select('Title');
  116. $titles = $c->find($this->con);
  117. $this->assertEquals($titles->count(), 1, 'find() called after select(string) allows for join() statements');
  118. $expectedSQL = "SELECT book.TITLE AS \"Title\" FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) WHERE author.FIRST_NAME = 'Neal'";
  119. $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'find() called after select(string) allows for join() statements');
  120. $c = new ModelCriteria('bookstore', 'Book');
  121. $c->join('Book.Author');
  122. $c->where('Author.FirstName = ?', 'Neal');
  123. $c->select('Author.FirstName');
  124. $titles = $c->find($this->con);
  125. $this->assertEquals($titles->shift(), 'Neal', 'find() called after select(string) will return values from the joined table using complete column names');
  126. $c = new ModelCriteria('bookstore', 'Book');
  127. $c->join('Book.Author');
  128. $c->where('Author.FirstName = ?', 'Neal');
  129. $c->select('Title');
  130. $title = $c->findOne($this->con);
  131. $this->assertEquals(count($title), 1, 'findOne() called after select(string) allows for join() statements');
  132. $expectedSQL = "SELECT book.TITLE AS \"Title\" FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) WHERE author.FIRST_NAME = 'Neal' LIMIT 1";
  133. $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'findOne() called after select(string) allows for where() statements');
  134. $c = new ModelCriteria('bookstore', 'Book');
  135. $c->join('Book.Author');
  136. $c->where('Author.FirstName = ?', 'Neal');
  137. $c->select('Author.FirstName');
  138. $title = $c->findOne($this->con);
  139. $this->assertEquals($title, 'Neal', 'findOne() called after select(string) will return values from the joined table using complete column names');
  140. }
  141. public function testSelectStringWildcard()
  142. {
  143. BookstoreDataPopulator::depopulate($this->con);
  144. BookstoreDataPopulator::populate($this->con);
  145. $c = new ModelCriteria('bookstore', 'Book');
  146. $c->select('*');
  147. $book = $c->findOne($this->con);
  148. $expectedSQL = 'SELECT book.ID AS "Book.Id", book.TITLE AS "Book.Title", book.ISBN AS "Book.ISBN", book.PRICE AS "Book.Price", book.PUBLISHER_ID AS "Book.PublisherId", book.AUTHOR_ID AS "Book.AuthorId" FROM `book` LIMIT 1';
  149. $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'select(\'*\') selects all the columns from the main object');
  150. $this->assertTrue(is_array($book), 'findOne() called after select(\'*\') returns an array');
  151. $this->assertEquals(array_keys($book), array('Book.Id', 'Book.Title', 'Book.ISBN', 'Book.Price', 'Book.PublisherId', 'Book.AuthorId'), 'select(\'*\') returns all the columns from the main object, in complete form');
  152. }
  153. public function testSelectArrayFind()
  154. {
  155. BookstoreDataPopulator::depopulate($this->con);
  156. BookstoreDataPopulator::populate($this->con);
  157. // fix for a bug/limitation in pdo_dblib where it truncates columnnames to a maximum of 31 characters when doing PDO::FETCH_ASSOC
  158. $c = new ModelCriteria('bookstore', 'BookstoreEmployeeAccount');
  159. $c->select(array('BookstoreEmployeeAccount.Authenticator', 'BookstoreEmployeeAccount.Password'));
  160. $account = $c->findOne($this->con);
  161. $this->assertEquals($account, array('BookstoreEmployeeAccount.Authenticator' => 'Password', 'BookstoreEmployeeAccount.Password' => 'johnp4ss'), 'select() does not mind long column names');
  162. $c = new ModelCriteria('bookstore', 'Author');
  163. $c->where('Author.FirstName = ?', 'Neal');
  164. $c->select(array('FirstName', 'LastName'));
  165. $authors = $c->find($this->con);
  166. $this->assertEquals($authors->count(), 1, 'find() called after select(array) allows for where() statements');
  167. $expectedSQL = "SELECT author.FIRST_NAME AS \"FirstName\", author.LAST_NAME AS \"LastName\" FROM `author` WHERE author.FIRST_NAME = 'Neal'";
  168. $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'find() called after select(array) allows for where() statements');
  169. }
  170. public function testSelectArrayFindOne()
  171. {
  172. BookstoreDataPopulator::depopulate($this->con);
  173. BookstoreDataPopulator::populate($this->con);
  174. $c = new ModelCriteria('bookstore', 'Author');
  175. $c->where('Author.FirstName = ?', 'Neal');
  176. $c->select(array('FirstName', 'LastName'));
  177. $author = $c->findOne($this->con);
  178. $this->assertEquals(count($author), 2, 'findOne() called after select(array) allows for where() statements');
  179. $expectedSQL = "SELECT author.FIRST_NAME AS \"FirstName\", author.LAST_NAME AS \"LastName\" FROM `author` WHERE author.FIRST_NAME = 'Neal' LIMIT 1";
  180. $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'findOne() called after select(array) allows for where() statements');
  181. }
  182. public function testSelectArrayJoin()
  183. {
  184. BookstoreDataPopulator::depopulate($this->con);
  185. BookstoreDataPopulator::populate($this->con);
  186. $c = new ModelCriteria('bookstore', 'Book');
  187. $c->join('Book.Author');
  188. $c->where('Author.FirstName = ?', 'Neal');
  189. $c->select(array('Title', 'ISBN'));
  190. $titles = $c->find($this->con);
  191. $this->assertEquals($titles->count(), 1, 'find() called after select(array) allows for join() statements');
  192. $expectedSQL = "SELECT book.TITLE AS \"Title\", book.ISBN AS \"ISBN\" FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) WHERE author.FIRST_NAME = 'Neal'";
  193. $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'find() called after select(array) allows for join() statements');
  194. $c = new ModelCriteria('bookstore', 'Book');
  195. $c->join('Book.Author');
  196. $c->where('Author.FirstName = ?', 'Neal');
  197. $c->select(array('Author.FirstName', 'Author.LastName'));
  198. $titles = $c->find($this->con);
  199. $this->assertEquals(array_values($titles->shift()), array('Neal', 'Stephenson'), 'find() called after select(array) will return values from the joined table using complete column names');
  200. $c = new ModelCriteria('bookstore', 'Book');
  201. $c->join('Book.Author');
  202. $c->where('Author.FirstName = ?', 'Neal');
  203. $c->select(array('Title', 'ISBN'));
  204. $title = $c->findOne($this->con);
  205. $this->assertEquals(count($title), 2, 'findOne() called after select(array) allows for join() statements');
  206. $expectedSQL = "SELECT book.TITLE AS \"Title\", book.ISBN AS \"ISBN\" FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) WHERE author.FIRST_NAME = 'Neal' LIMIT 1";
  207. $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'findOne() called after select(array) allows for join() statements');
  208. $c = new ModelCriteria('bookstore', 'Book');
  209. $c->join('Book.Author');
  210. $c->where('Author.FirstName = ?', 'Neal');
  211. $c->select(array('Author.FirstName', 'Author.LastName'));
  212. $title = $c->findOne($this->con);
  213. $this->assertEquals(array_values($title), array('Neal', 'Stephenson'), 'findOne() called after select(array) will return values from the joined table using complete column names');
  214. }
  215. public function testSelectArrayRelation()
  216. {
  217. BookstoreDataPopulator::depopulate($this->con);
  218. BookstoreDataPopulator::populate($this->con);
  219. $c = new ModelCriteria('bookstore', 'Book');
  220. $c->join('Book.Author');
  221. $c->orderBy('Book.Title');
  222. $c->select(array('Author.LastName', 'Book.Title'));
  223. $rows = $c->find($this->con);
  224. $expectedSQL = 'SELECT author.LAST_NAME AS "Author.LastName", book.TITLE AS "Book.Title" FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) ORDER BY book.TITLE ASC';
  225. $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'select(array) can select columns from several tables (many-to-one)');
  226. $expectedRows = array(
  227. array(
  228. 'Author.LastName' => 'Byron',
  229. 'Book.Title' => 'Don Juan',
  230. ),
  231. array(
  232. 'Author.LastName' => 'Rowling',
  233. 'Book.Title' => 'Harry Potter and the Order of the Phoenix',
  234. ),
  235. array(
  236. 'Author.LastName' => 'Stephenson',
  237. 'Book.Title' => 'Quicksilver',
  238. ),
  239. array(
  240. 'Author.LastName' => 'Grass',
  241. 'Book.Title' => 'The Tin Drum',
  242. ),
  243. );
  244. $this->assertEquals(serialize($rows->getData()), serialize($expectedRows), 'find() called after select(array) returns columns from several tables (many-to-one');
  245. $c = new ModelCriteria('bookstore', 'Book');
  246. $c->join('Book.Author');
  247. $c->select(array('Author.LastName', 'Book.Title'));
  248. $c->orderBy('Book.Id');
  249. $c->orderBy('Author.Id');
  250. $rows = $c->find($this->con);
  251. $expectedSQL = 'SELECT author.LAST_NAME AS "Author.LastName", book.TITLE AS "Book.Title" FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) ORDER BY book.ID ASC,author.ID ASC';
  252. $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'select(array) can select columns from several tables (many-to-one)');
  253. $expectedRows = array (
  254. array (
  255. 'Author.LastName' => 'Rowling',
  256. 'Book.Title' => 'Harry Potter and the Order of the Phoenix',
  257. ),
  258. array (
  259. 'Author.LastName' => 'Stephenson',
  260. 'Book.Title' => 'Quicksilver',
  261. ),
  262. array (
  263. 'Author.LastName' => 'Byron',
  264. 'Book.Title' => 'Don Juan',
  265. ),
  266. array (
  267. 'Author.LastName' => 'Grass',
  268. 'Book.Title' => 'The Tin Drum',
  269. )
  270. );
  271. $this->assertEquals(serialize($rows->getData()), serialize($expectedRows), 'find() called after select(array) returns columns from several tables (many-to-one');
  272. }
  273. public function testSelectArrayWithColumn()
  274. {
  275. BookstoreDataPopulator::depopulate($this->con);
  276. BookstoreDataPopulator::populate($this->con);
  277. $c = new ModelCriteria('bookstore', 'Book');
  278. $c->join('Book.Author');
  279. $c->withColumn('LOWER(Book.Title)', 'LowercaseTitle');
  280. $c->select(array('LowercaseTitle', 'Book.Title'));
  281. $c->orderBy('Book.Title');
  282. $rows = $c->find($this->con);
  283. $expectedSQL = 'SELECT LOWER(book.TITLE) AS LowercaseTitle, book.TITLE AS "Book.Title" FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) ORDER BY book.TITLE ASC';
  284. $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'find() called after select(array) can cope with a column added with withColumn()');
  285. $expectedRows = array (
  286. array (
  287. 'LowercaseTitle' => 'don juan',
  288. 'Book.Title' => 'Don Juan',
  289. ),
  290. array (
  291. 'LowercaseTitle' => 'harry potter and the order of the phoenix',
  292. 'Book.Title' => 'Harry Potter and the Order of the Phoenix',
  293. ),
  294. array (
  295. 'LowercaseTitle' => 'quicksilver',
  296. 'Book.Title' => 'Quicksilver',
  297. ),
  298. array (
  299. 'LowercaseTitle' => 'the tin drum',
  300. 'Book.Title' => 'The Tin Drum',
  301. ),
  302. );
  303. $this->assertEquals(serialize($rows->getData()), serialize($expectedRows), 'find() called after select(array) can cope with a column added with withColumn()');
  304. }
  305. public function testGetSelectReturnsNullByDefault()
  306. {
  307. $c = new ModelCriteria('bookstore', 'Book');
  308. $this->assertNull($c->getSelect());
  309. }
  310. public function testGetSelectReturnsStringWhenSelectingASingleColumn()
  311. {
  312. $c = new ModelCriteria('bookstore', 'Book');
  313. $c->select('Title');
  314. $this->assertEquals('Title', $c->getSelect());
  315. }
  316. public function testGetSelectReturnsArrayWhenSelectingSeveralColumns()
  317. {
  318. $c = new ModelCriteria('bookstore', 'Book');
  319. $c->select(array('Id', 'Title'));
  320. $this->assertEquals(array('Id', 'Title'), $c->getSelect());
  321. }
  322. public function testGetSelectReturnsArrayWhenSelectingASingleColumnAsArray()
  323. {
  324. $c = new ModelCriteria('bookstore', 'Book');
  325. $c->select(array('Title'));
  326. $this->assertEquals(array('Title'), $c->getSelect());
  327. }
  328. public function testGetSelectReturnsArrayWhenSelectingAllColumns()
  329. {
  330. $c = new ModelCriteria('bookstore', 'Book');
  331. $c->select('*');
  332. $this->assertEquals(array('Book.Id', 'Book.Title', 'Book.ISBN', 'Book.Price', 'Book.PublisherId', 'Book.AuthorId'), $c->getSelect());
  333. }
  334. }