QueryTest.php 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. namespace Cake\Test\TestCase\Database;
  16. use Cake\Core\Configure;
  17. use Cake\Database\Expression\IdentifierExpression;
  18. use Cake\Database\Query;
  19. use Cake\Datasource\ConnectionManager;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * Tests Query class
  23. *
  24. */
  25. class QueryTest extends TestCase {
  26. public $fixtures = ['core.article', 'core.author', 'core.comment'];
  27. const ARTICLE_COUNT = 3;
  28. const AUTHOR_COUNT = 4;
  29. const COMMENT_COUNT = 6;
  30. public function setUp() {
  31. parent::setUp();
  32. $this->connection = ConnectionManager::get('test');
  33. $this->autoQuote = $this->connection->driver()->autoQuoting();
  34. }
  35. public function tearDown() {
  36. parent::tearDown();
  37. $this->connection->driver()->autoQuoting($this->autoQuote);
  38. unset($this->connection);
  39. }
  40. /**
  41. * Tests that it is possible to obtain expression results from a query
  42. *
  43. * @return void
  44. */
  45. public function testSelectFieldsOnly() {
  46. $this->connection->driver()->autoQuoting(false);
  47. $query = new Query($this->connection);
  48. $result = $query->select('1 + 1')->execute();
  49. $this->assertInstanceOf('Cake\Database\StatementInterface', $result);
  50. $this->assertEquals([2], $result->fetch());
  51. //This new field should be appended
  52. $result = $query->select(array('1 + 3'))->execute();
  53. $this->assertInstanceOf('Cake\Database\StatementInterface', $result);
  54. $this->assertEquals([2, 4], $result->fetch());
  55. //This should now overwrite all previous fields
  56. $result = $query->select(array('1 + 2', '1 + 5'), true)->execute();
  57. $this->assertEquals([3, 6], $result->fetch());
  58. }
  59. /**
  60. * Tests that it is possible to pass a closure as fields in select()
  61. *
  62. * @return void
  63. */
  64. public function testSelectClosure() {
  65. $this->connection->driver()->autoQuoting(false);
  66. $query = new Query($this->connection);
  67. $result = $query->select(function($q) use ($query) {
  68. $this->assertSame($query, $q);
  69. return ['1 + 2', '1 + 5'];
  70. })->execute();
  71. $this->assertEquals([3, 6], $result->fetch());
  72. }
  73. /**
  74. * Tests it is possible to select fields from tables with no conditions
  75. *
  76. * @return void
  77. */
  78. public function testSelectFieldsFromTable() {
  79. $query = new Query($this->connection);
  80. $result = $query->select(array('body', 'author_id'))->from('articles')->execute();
  81. $this->assertEquals(array('body' => 'First Article Body', 'author_id' => 1), $result->fetch('assoc'));
  82. $this->assertEquals(array('body' => 'Second Article Body', 'author_id' => 3), $result->fetch('assoc'));
  83. //Append more tables to next execution
  84. $result = $query->select('name')->from(array('authors'))->order(['name' => 'desc', 'articles.id' => 'asc'])->execute();
  85. $this->assertEquals(array('body' => 'First Article Body', 'author_id' => 1, 'name' => 'nate'), $result->fetch('assoc'));
  86. $this->assertEquals(array('body' => 'Second Article Body', 'author_id' => 3, 'name' => 'nate'), $result->fetch('assoc'));
  87. $this->assertEquals(array('body' => 'Third Article Body', 'author_id' => 1, 'name' => 'nate'), $result->fetch('assoc'));
  88. // Overwrite tables and only fetch from authors
  89. $result = $query->select('name', true)->from('authors', true)->order(['name' => 'desc'], true)->execute();
  90. $this->assertEquals(array('nate'), $result->fetch());
  91. $this->assertEquals(array('mariano'), $result->fetch());
  92. $this->assertCount(4, $result);
  93. }
  94. /**
  95. * Tests it is possible to select aliased fields
  96. *
  97. * @return void
  98. */
  99. public function testSelectAliasedFieldsFromTable() {
  100. $query = new Query($this->connection);
  101. $result = $query->select(['text' => 'body', 'author_id'])->from('articles')->execute();
  102. $this->assertEquals(array('text' => 'First Article Body', 'author_id' => 1), $result->fetch('assoc'));
  103. $this->assertEquals(array('text' => 'Second Article Body', 'author_id' => 3), $result->fetch('assoc'));
  104. $query = new Query($this->connection);
  105. $result = $query->select(['text' => 'body', 'author' => 'author_id'])->from('articles')->execute();
  106. $this->assertEquals(array('text' => 'First Article Body', 'author' => 1), $result->fetch('assoc'));
  107. $this->assertEquals(array('text' => 'Second Article Body', 'author' => 3), $result->fetch('assoc'));
  108. $query = new Query($this->connection);
  109. $query->select(['text' => 'body'])->select(['author_id', 'foo' => 'body']);
  110. $result = $query->from('articles')->execute();
  111. $this->assertEquals(array('foo' => 'First Article Body', 'text' => 'First Article Body', 'author_id' => 1), $result->fetch('assoc'));
  112. $this->assertEquals(array('foo' => 'Second Article Body', 'text' => 'Second Article Body', 'author_id' => 3), $result->fetch('assoc'));
  113. $query = new Query($this->connection);
  114. $exp = $query->newExpr()->add('1 + 1');
  115. $comp = $query->newExpr()->add(['author_id +' => 2]);
  116. $result = $query->select(['text' => 'body', 'two' => $exp, 'three' => $comp])
  117. ->from('articles')->execute();
  118. $this->assertEquals(array('text' => 'First Article Body', 'two' => 2, 'three' => 3), $result->fetch('assoc'));
  119. $this->assertEquals(array('text' => 'Second Article Body', 'two' => 2, 'three' => 5), $result->fetch('assoc'));
  120. }
  121. /**
  122. * Tests that tables can also be aliased and referenced in the select clause using such alias
  123. *
  124. * @return void
  125. */
  126. public function testSelectAliasedTables() {
  127. $query = new Query($this->connection);
  128. $result = $query->select(['text' => 'a.body', 'a.author_id'])
  129. ->from(['a' => 'articles'])->execute();
  130. $this->assertEquals(['text' => 'First Article Body', 'author_id' => 1], $result->fetch('assoc'));
  131. $this->assertEquals(['text' => 'Second Article Body', 'author_id' => 3], $result->fetch('assoc'));
  132. $result = $query->select(['name' => 'b.name'])->from(['b' => 'authors'])
  133. ->order(['text' => 'desc', 'name' => 'desc'])
  134. ->execute();
  135. $this->assertEquals(
  136. ['text' => 'Third Article Body', 'author_id' => 1, 'name' => 'nate'],
  137. $result->fetch('assoc')
  138. );
  139. $this->assertEquals(
  140. ['text' => 'Third Article Body', 'author_id' => 1, 'name' => 'mariano'],
  141. $result->fetch('assoc')
  142. );
  143. }
  144. /**
  145. * Tests it is possible to add joins to a select query
  146. *
  147. * @return void
  148. */
  149. public function testSelectWithJoins() {
  150. $query = new Query($this->connection);
  151. $result = $query
  152. ->select(['title', 'name'])
  153. ->from('articles')
  154. ->join(['table' => 'authors', 'alias' => 'a', 'conditions' => 'author_id = a.id'])
  155. ->order(['title' => 'asc'])
  156. ->execute();
  157. $this->assertCount(3, $result);
  158. $this->assertEquals(array('title' => 'First Article', 'name' => 'mariano'), $result->fetch('assoc'));
  159. $this->assertEquals(array('title' => 'Second Article', 'name' => 'larry'), $result->fetch('assoc'));
  160. $result = $query->join('authors', [], true)->execute();
  161. $this->assertCount(12, $result, 'Cross join results in 12 records');
  162. $result = $query->join([
  163. ['table' => 'authors', 'type' => 'INNER', 'conditions' => 'author_id = authors.id']
  164. ], [], true)->execute();
  165. $this->assertCount(3, $result);
  166. $this->assertEquals(array('title' => 'First Article', 'name' => 'mariano'), $result->fetch('assoc'));
  167. $this->assertEquals(array('title' => 'Second Article', 'name' => 'larry'), $result->fetch('assoc'));
  168. }
  169. /**
  170. * Tests it is possible to add joins to a select query using array or expression as conditions
  171. *
  172. * @return void
  173. */
  174. public function testSelectWithJoinsConditions() {
  175. $query = new Query($this->connection);
  176. $result = $query
  177. ->select(['title', 'name'])
  178. ->from('articles')
  179. ->join(['table' => 'authors', 'alias' => 'a', 'conditions' => ['author_id = a.id']])
  180. ->order(['title' => 'asc'])
  181. ->execute();
  182. $this->assertEquals(array('title' => 'First Article', 'name' => 'mariano'), $result->fetch('assoc'));
  183. $this->assertEquals(array('title' => 'Second Article', 'name' => 'larry'), $result->fetch('assoc'));
  184. $query = new Query($this->connection);
  185. $conditions = $query->newExpr()->add('author_id = a.id');
  186. $result = $query
  187. ->select(['title', 'name'])
  188. ->from('articles')
  189. ->join(['table' => 'authors', 'alias' => 'a', 'conditions' => $conditions])
  190. ->order(['title' => 'asc'])
  191. ->execute();
  192. $this->assertEquals(array('title' => 'First Article', 'name' => 'mariano'), $result->fetch('assoc'));
  193. $this->assertEquals(array('title' => 'Second Article', 'name' => 'larry'), $result->fetch('assoc'));
  194. $query = new Query($this->connection);
  195. $time = new \DateTime('2007-03-18 10:50:00');
  196. $types = ['created' => 'datetime'];
  197. $result = $query
  198. ->select(['title', 'comment' => 'c.comment'])
  199. ->from('articles')
  200. ->join(['table' => 'comments', 'alias' => 'c', 'conditions' => ['created <=' => $time]], $types)
  201. ->execute();
  202. $this->assertEquals(['title' => 'First Article', 'comment' => 'First Comment for First Article'], $result->fetch('assoc'));
  203. }
  204. /**
  205. * Tests that joins can be aliased using array keys
  206. *
  207. * @return void
  208. */
  209. public function testSelectAliasedJoins() {
  210. $query = new Query($this->connection);
  211. $result = $query
  212. ->select(['title', 'name'])
  213. ->from('articles')
  214. ->join(['a' => 'authors'])
  215. ->order(['name' => 'desc', 'articles.id' => 'asc'])
  216. ->execute();
  217. $this->assertEquals(array('title' => 'First Article', 'name' => 'nate'), $result->fetch('assoc'));
  218. $this->assertEquals(array('title' => 'Second Article', 'name' => 'nate'), $result->fetch('assoc'));
  219. $query = new Query($this->connection);
  220. $conditions = $query->newExpr()->add('author_id = a.id');
  221. $result = $query
  222. ->select(['title', 'name'])
  223. ->from('articles')
  224. ->join(['a' => ['table' => 'authors', 'conditions' => $conditions]])
  225. ->order(['title' => 'asc'])
  226. ->execute();
  227. $this->assertEquals(array('title' => 'First Article', 'name' => 'mariano'), $result->fetch('assoc'));
  228. $this->assertEquals(array('title' => 'Second Article', 'name' => 'larry'), $result->fetch('assoc'));
  229. $query = new Query($this->connection);
  230. $time = new \DateTime('2007-03-18 10:45:23');
  231. $types = ['created' => 'datetime'];
  232. $result = $query
  233. ->select(['title', 'name' => 'c.comment'])
  234. ->from('articles')
  235. ->join(['c' => ['table' => 'comments', 'conditions' => ['created' => $time]]], $types)
  236. ->execute();
  237. $this->assertEquals(array('title' => 'First Article', 'name' => 'First Comment for First Article'), $result->fetch('assoc'));
  238. }
  239. /**
  240. * Tests it is possible to filter a query by using simple AND joined conditions
  241. *
  242. * @return void
  243. */
  244. public function testSelectSimpleWhere() {
  245. $query = new Query($this->connection);
  246. $result = $query
  247. ->select(['title'])
  248. ->from('articles')
  249. ->where(['id' => 1, 'title' => 'First Article'])
  250. ->execute();
  251. $this->assertCount(1, $result);
  252. $query = new Query($this->connection);
  253. $result = $query
  254. ->select(['title'])
  255. ->from('articles')
  256. ->where(['id' => 100], ['id' => 'integer'])
  257. ->execute();
  258. $this->assertCount(0, $result);
  259. }
  260. /**
  261. * Tests using where conditions with operators and scalar values works
  262. *
  263. * @return void
  264. */
  265. public function testSelectWhereOperators() {
  266. $query = new Query($this->connection);
  267. $result = $query
  268. ->select(['title'])
  269. ->from('articles')
  270. ->where(['id >' => 1])
  271. ->execute();
  272. $this->assertCount(2, $result);
  273. $this->assertEquals(array('title' => 'Second Article'), $result->fetch('assoc'));
  274. $query = new Query($this->connection);
  275. $result = $query
  276. ->select(['title'])
  277. ->from('articles')
  278. ->where(['id <' => 2])
  279. ->execute();
  280. $this->assertCount(1, $result);
  281. $this->assertEquals(array('title' => 'First Article'), $result->fetch('assoc'));
  282. $query = new Query($this->connection);
  283. $result = $query
  284. ->select(['title'])
  285. ->from('articles')
  286. ->where(['id <=' => 2])
  287. ->execute();
  288. $this->assertCount(2, $result);
  289. $query = new Query($this->connection);
  290. $result = $query
  291. ->select(['title'])
  292. ->from('articles')
  293. ->where(['id >=' => 1])
  294. ->execute();
  295. $this->assertCount(3, $result);
  296. $query = new Query($this->connection);
  297. $result = $query
  298. ->select(['title'])
  299. ->from('articles')
  300. ->where(['id <=' => 1])
  301. ->execute();
  302. $this->assertCount(1, $result);
  303. $query = new Query($this->connection);
  304. $result = $query
  305. ->select(['title'])
  306. ->from('articles')
  307. ->where(['id !=' => 2])
  308. ->execute();
  309. $this->assertCount(2, $result);
  310. $this->assertEquals(array('title' => 'First Article'), $result->fetch('assoc'));
  311. $query = new Query($this->connection);
  312. $result = $query
  313. ->select(['title'])
  314. ->from('articles')
  315. ->where(['title LIKE' => 'First Article'])
  316. ->execute();
  317. $this->assertCount(1, $result);
  318. $this->assertEquals(array('title' => 'First Article'), $result->fetch('assoc'));
  319. $query = new Query($this->connection);
  320. $result = $query
  321. ->select(['title'])
  322. ->from('articles')
  323. ->where(['title like' => '%Article%'])
  324. ->execute();
  325. $this->assertCount(3, $result);
  326. $query = new Query($this->connection);
  327. $result = $query
  328. ->select(['title'])
  329. ->from('articles')
  330. ->where(['title not like' => '%Article%'])
  331. ->execute();
  332. $this->assertCount(0, $result);
  333. }
  334. /**
  335. * Tests selecting with conditions and specifying types for those
  336. *
  337. * @return void
  338. **/
  339. public function testSelectWhereTypes() {
  340. $query = new Query($this->connection);
  341. $result = $query
  342. ->select(['id'])
  343. ->from('comments')
  344. ->where(['created' => new \DateTime('2007-03-18 10:45:23')], ['created' => 'datetime'])
  345. ->execute();
  346. $this->assertCount(1, $result);
  347. $this->assertEquals(['id' => 1], $result->fetch('assoc'));
  348. $query = new Query($this->connection);
  349. $result = $query
  350. ->select(['id'])
  351. ->from('comments')
  352. ->where(['created >' => new \DateTime('2007-03-18 10:46:00')], ['created' => 'datetime'])
  353. ->execute();
  354. $this->assertCount(5, $result);
  355. $this->assertEquals(['id' => 2], $result->fetch('assoc'));
  356. $this->assertEquals(['id' => 3], $result->fetch('assoc'));
  357. $query = new Query($this->connection);
  358. $result = $query
  359. ->select(['id'])
  360. ->from('comments')
  361. ->where(
  362. [
  363. 'created >' => new \DateTime('2007-03-18 10:40:00'),
  364. 'created <' => new \DateTime('2007-03-18 10:46:00')
  365. ],
  366. ['created' => 'datetime']
  367. )
  368. ->execute();
  369. $this->assertCount(1, $result);
  370. $this->assertEquals(array('id' => 1), $result->fetch('assoc'));
  371. $query = new Query($this->connection);
  372. $result = $query
  373. ->select(['id'])
  374. ->from('comments')
  375. ->where(
  376. [
  377. 'id' => '3something-crazy',
  378. 'created <' => new \DateTime('2013-01-01 12:00')
  379. ],
  380. ['created' => 'datetime', 'id' => 'integer']
  381. )
  382. ->execute();
  383. $this->assertCount(1, $result);
  384. $this->assertEquals(['id' => 3], $result->fetch('assoc'));
  385. $query = new Query($this->connection);
  386. $result = $query
  387. ->select(['id'])
  388. ->from('comments')
  389. ->where(
  390. [
  391. 'id' => '1something-crazy',
  392. 'created <' => new \DateTime('2013-01-01 12:00')
  393. ],
  394. ['created' => 'datetime', 'id' => 'float']
  395. )
  396. ->execute();
  397. $this->assertCount(1, $result);
  398. $this->assertEquals(['id' => 1], $result->fetch('assoc'));
  399. }
  400. /**
  401. * Tests that passing an array type to any where condition will replace
  402. * the passed array accordingly as a proper IN condition
  403. *
  404. * @return void
  405. */
  406. public function testSelectWhereArrayType() {
  407. $query = new Query($this->connection);
  408. $result = $query
  409. ->select(['id'])
  410. ->from('comments')
  411. ->where(['id' => ['1', '3']], ['id' => 'integer[]'])
  412. ->execute();
  413. $this->assertCount(2, $result);
  414. $this->assertEquals(['id' => 1], $result->fetch('assoc'));
  415. $this->assertEquals(['id' => 3], $result->fetch('assoc'));
  416. }
  417. /**
  418. * Tests that Query::orWhere() can be used to concatenate conditions with OR
  419. *
  420. * @return void
  421. **/
  422. public function testSelectOrWhere() {
  423. $query = new Query($this->connection);
  424. $result = $query
  425. ->select(['id'])
  426. ->from('comments')
  427. ->where(['created' => new \DateTime('2007-03-18 10:45:23')], ['created' => 'datetime'])
  428. ->orWhere(['created' => new \DateTime('2007-03-18 10:47:23')], ['created' => 'datetime'])
  429. ->execute();
  430. $this->assertCount(2, $result);
  431. $this->assertEquals(['id' => 1], $result->fetch('assoc'));
  432. $this->assertEquals(['id' => 2], $result->fetch('assoc'));
  433. }
  434. /**
  435. * Tests that Query::andWhere() can be used to concatenate conditions with AND
  436. *
  437. * @return void
  438. **/
  439. public function testSelectAndWhere() {
  440. $query = new Query($this->connection);
  441. $result = $query
  442. ->select(['id'])
  443. ->from('comments')
  444. ->where(['created' => new \DateTime('2007-03-18 10:45:23')], ['created' => 'datetime'])
  445. ->andWhere(['id' => 1])
  446. ->execute();
  447. $this->assertCount(1, $result);
  448. $this->assertEquals(['id' => 1], $result->fetch('assoc'));
  449. $query = new Query($this->connection);
  450. $result = $query
  451. ->select(['id'])
  452. ->from('comments')
  453. ->where(['created' => new \DateTime('2007-03-18 10:50:55')], ['created' => 'datetime'])
  454. ->andWhere(['id' => 2])
  455. ->execute();
  456. $this->assertCount(0, $result);
  457. }
  458. /**
  459. * Tests that combining Query::andWhere() and Query::orWhere() produces
  460. * correct conditions nesting
  461. *
  462. * @return void
  463. **/
  464. public function testSelectExpressionNesting() {
  465. $query = new Query($this->connection);
  466. $result = $query
  467. ->select(['id'])
  468. ->from('comments')
  469. ->where(['created' => new \DateTime('2007-03-18 10:45:23')], ['created' => 'datetime'])
  470. ->orWhere(['id' => 2])
  471. ->andWhere(['created >=' => new \DateTime('2007-03-18 10:40:00')], ['created' => 'datetime'])
  472. ->execute();
  473. $this->assertCount(2, $result);
  474. $this->assertEquals(['id' => 1], $result->fetch('assoc'));
  475. $this->assertEquals(['id' => 2], $result->fetch('assoc'));
  476. $query = new Query($this->connection);
  477. $result = $query
  478. ->select(['id'])
  479. ->from('comments')
  480. ->where(['created' => new \DateTime('2007-03-18 10:45:23')], ['created' => 'datetime'])
  481. ->orWhere(['id' => 2])
  482. ->andWhere(['created >=' => new \DateTime('2007-03-18 10:40:00')], ['created' => 'datetime'])
  483. ->orWhere(['created' => new \DateTime('2007-03-18 10:49:23')], ['created' => 'datetime'])
  484. ->execute();
  485. $this->assertCount(3, $result);
  486. $this->assertEquals(['id' => 1], $result->fetch('assoc'));
  487. $this->assertEquals(['id' => 2], $result->fetch('assoc'));
  488. $this->assertEquals(['id' => 3], $result->fetch('assoc'));
  489. }
  490. /**
  491. * Tests that Query::orWhere() can be used without calling where() before
  492. *
  493. * @return void
  494. **/
  495. public function testSelectOrWhereNoPreviousCondition() {
  496. $query = new Query($this->connection);
  497. $result = $query
  498. ->select(['id'])
  499. ->from('comments')
  500. ->orWhere(['created' => new \DateTime('2007-03-18 10:45:23')], ['created' => 'datetime'])
  501. ->orWhere(['created' => new \DateTime('2007-03-18 10:47:23')], ['created' => 'datetime'])
  502. ->execute();
  503. $this->assertCount(2, $result);
  504. $this->assertEquals(['id' => 1], $result->fetch('assoc'));
  505. $this->assertEquals(['id' => 2], $result->fetch('assoc'));
  506. }
  507. /**
  508. * Tests that Query::andWhere() can be used without calling where() before
  509. *
  510. * @return void
  511. **/
  512. public function testSelectAndWhereNoPreviousCondition() {
  513. $query = new Query($this->connection);
  514. $result = $query
  515. ->select(['id'])
  516. ->from('comments')
  517. ->andWhere(['created' => new \DateTime('2007-03-18 10:45:23')], ['created' => 'datetime'])
  518. ->andWhere(['id' => 1])
  519. ->execute();
  520. $this->assertCount(1, $result);
  521. $this->assertEquals(['id' => 1], $result->fetch('assoc'));
  522. }
  523. /**
  524. * Tests that it is possible to pass a closure to where() to build a set of
  525. * conditions and return the expression to be used
  526. *
  527. * @return void
  528. */
  529. public function testSelectWhereUsingClosure() {
  530. $query = new Query($this->connection);
  531. $result = $query
  532. ->select(['id'])
  533. ->from('comments')
  534. ->where(function($exp) {
  535. return $exp->eq('id', 1);
  536. })
  537. ->execute();
  538. $this->assertCount(1, $result);
  539. $this->assertEquals(['id' => 1], $result->fetch('assoc'));
  540. $query = new Query($this->connection);
  541. $result = $query
  542. ->select(['id'])
  543. ->from('comments')
  544. ->where(function($exp) {
  545. return $exp
  546. ->eq('id', 1)
  547. ->eq('created', new \DateTime('2007-03-18 10:45:23'), 'datetime');
  548. })
  549. ->execute();
  550. $this->assertCount(1, $result);
  551. $this->assertEquals(['id' => 1], $result->fetch('assoc'));
  552. $query = new Query($this->connection);
  553. $result = $query
  554. ->select(['id'])
  555. ->from('comments')
  556. ->where(function($exp) {
  557. return $exp
  558. ->eq('id', 1)
  559. ->eq('created', new \DateTime('2021-12-30 15:00'), 'datetime');
  560. })
  561. ->execute();
  562. $this->assertCount(0, $result);
  563. }
  564. /**
  565. * Tests that it is possible to pass a closure to andWhere() to build a set of
  566. * conditions and return the expression to be used
  567. *
  568. * @return void
  569. */
  570. public function testSelectAndWhereUsingClosure() {
  571. $query = new Query($this->connection);
  572. $result = $query
  573. ->select(['id'])
  574. ->from('comments')
  575. ->where(['id' => '1'])
  576. ->andWhere(function($exp) {
  577. return $exp->eq('created', new \DateTime('2007-03-18 10:45:23'), 'datetime');
  578. })
  579. ->execute();
  580. $this->assertCount(1, $result);
  581. $this->assertEquals(['id' => 1], $result->fetch('assoc'));
  582. $query = new Query($this->connection);
  583. $result = $query
  584. ->select(['id'])
  585. ->from('comments')
  586. ->where(['id' => '1'])
  587. ->andWhere(function($exp) {
  588. return $exp->eq('created', new \DateTime('2022-12-21 12:00'), 'datetime');
  589. })
  590. ->execute();
  591. $this->assertCount(0, $result);
  592. }
  593. /**
  594. * Tests that it is possible to pass a closure to orWhere() to build a set of
  595. * conditions and return the expression to be used
  596. *
  597. * @return void
  598. */
  599. public function testSelectOrWhereUsingClosure() {
  600. $query = new Query($this->connection);
  601. $result = $query
  602. ->select(['id'])
  603. ->from('comments')
  604. ->where(['id' => '1'])
  605. ->orWhere(function($exp) {
  606. return $exp->eq('created', new \DateTime('2007-03-18 10:47:23'), 'datetime');
  607. })
  608. ->execute();
  609. $this->assertCount(2, $result);
  610. $this->assertEquals(['id' => 1], $result->fetch('assoc'));
  611. $this->assertEquals(['id' => 2], $result->fetch('assoc'));
  612. $query = new Query($this->connection);
  613. $result = $query
  614. ->select(['id'])
  615. ->from('comments')
  616. ->where(['id' => '1'])
  617. ->orWhere(function($exp) {
  618. return $exp
  619. ->eq('created', new \DateTime('2012-12-22 12:00'), 'datetime')
  620. ->eq('id', 3);
  621. })
  622. ->execute();
  623. $this->assertCount(1, $result);
  624. $this->assertEquals(['id' => 1], $result->fetch('assoc'));
  625. }
  626. /**
  627. * Tests using where conditions with operator methods
  628. *
  629. * @return void
  630. */
  631. public function testSelectWhereOperatorMethods() {
  632. $query = new Query($this->connection);
  633. $result = $query
  634. ->select(['title'])
  635. ->from('articles')
  636. ->where(function($exp) {
  637. return $exp->gt('id', 1);
  638. })
  639. ->execute();
  640. $this->assertCount(2, $result);
  641. $this->assertEquals(array('title' => 'Second Article'), $result->fetch('assoc'));
  642. $query = new Query($this->connection);
  643. $result = $query->select(['title'])
  644. ->from('articles')
  645. ->where(function($exp) {
  646. return $exp->lt('id', 2);
  647. })
  648. ->execute();
  649. $this->assertCount(1, $result);
  650. $this->assertEquals(array('title' => 'First Article'), $result->fetch('assoc'));
  651. $query = new Query($this->connection);
  652. $result = $query->select(['title'])
  653. ->from('articles')
  654. ->where(function($exp) {
  655. return $exp->lte('id', 2);
  656. })
  657. ->execute();
  658. $this->assertCount(2, $result);
  659. $query = new Query($this->connection);
  660. $result = $query
  661. ->select(['title'])
  662. ->from('articles')
  663. ->where(function($exp) {
  664. return $exp->gte('id', 1);
  665. })
  666. ->execute();
  667. $this->assertCount(3, $result);
  668. $query = new Query($this->connection);
  669. $result = $query
  670. ->select(['title'])
  671. ->from('articles')
  672. ->where(function($exp) {
  673. return $exp->lte('id', 1);
  674. })
  675. ->execute();
  676. $this->assertCount(1, $result);
  677. $query = new Query($this->connection);
  678. $result = $query
  679. ->select(['title'])
  680. ->from('articles')
  681. ->where(function($exp) {
  682. return $exp->notEq('id', 2);
  683. })
  684. ->execute();
  685. $this->assertCount(2, $result);
  686. $this->assertEquals(array('title' => 'First Article'), $result->fetch('assoc'));
  687. $query = new Query($this->connection);
  688. $result = $query
  689. ->select(['title'])
  690. ->from('articles')
  691. ->where(function($exp) {
  692. return $exp->like('title', 'First Article');
  693. })
  694. ->execute();
  695. $this->assertCount(1, $result);
  696. $this->assertEquals(array('title' => 'First Article'), $result->fetch('assoc'));
  697. $query = new Query($this->connection);
  698. $result = $query
  699. ->select(['title'])
  700. ->from('articles')
  701. ->where(function($exp) {
  702. return $exp->like('title', '%Article%');
  703. })
  704. ->execute();
  705. $this->assertCount(3, $result);
  706. $query = new Query($this->connection);
  707. $result = $query
  708. ->select(['title'])
  709. ->from('articles')
  710. ->where(function($exp) {
  711. return $exp->notLike('title', '%Article%');
  712. })
  713. ->execute();
  714. $this->assertCount(0, $result);
  715. $query = new Query($this->connection);
  716. $result = $query
  717. ->select(['id'])
  718. ->from('comments')
  719. ->where(function($exp) {
  720. return $exp->isNull('published');
  721. })
  722. ->execute();
  723. $this->assertCount(0, $result);
  724. $query = new Query($this->connection);
  725. $result = $query
  726. ->select(['id'])
  727. ->from('comments')
  728. ->where(function($exp) {
  729. return $exp->isNotNull('published');
  730. })
  731. ->execute();
  732. $this->assertCount(6, $result);
  733. $query = new Query($this->connection);
  734. $result = $query
  735. ->select(['id'])
  736. ->from('comments')
  737. ->where(function($exp) {
  738. return $exp->in('published', ['Y', 'N']);
  739. })
  740. ->execute();
  741. $this->assertCount(6, $result);
  742. $query = new Query($this->connection);
  743. $result = $query
  744. ->select(['id'])
  745. ->from('comments')
  746. ->where(function($exp) {
  747. return $exp->in(
  748. 'created',
  749. [new \DateTime('2007-03-18 10:45:23'), new \DateTime('2007-03-18 10:47:23')],
  750. 'datetime'
  751. );
  752. })
  753. ->execute();
  754. $this->assertCount(2, $result);
  755. $this->assertEquals(['id' => 1], $result->fetch('assoc'));
  756. $this->assertEquals(['id' => 2], $result->fetch('assoc'));
  757. $query = new Query($this->connection);
  758. $result = $query
  759. ->select(['id'])
  760. ->from('comments')
  761. ->where(function($exp) {
  762. return $exp->notIn(
  763. 'created',
  764. [new \DateTime('2007-03-18 10:45:23'), new \DateTime('2007-03-18 10:47:23')],
  765. 'datetime'
  766. );
  767. })
  768. ->execute();
  769. $this->assertCount(4, $result);
  770. $this->assertEquals(['id' => 3], $result->fetch('assoc'));
  771. }
  772. /**
  773. * Tests that calling "in" and "notIn" will cast the passed values to an array
  774. *
  775. * @return void
  776. */
  777. public function testInValueCast() {
  778. $query = new Query($this->connection);
  779. $result = $query
  780. ->select(['id'])
  781. ->from('comments')
  782. ->where(function($exp) {
  783. return $exp->in('created', '2007-03-18 10:45:23', 'datetime');
  784. })
  785. ->execute();
  786. $this->assertCount(1, $result);
  787. $this->assertEquals(['id' => 1], $result->fetch('assoc'));
  788. $query = new Query($this->connection);
  789. $result = $query
  790. ->select(['id'])
  791. ->from('comments')
  792. ->where(function($exp) {
  793. return $exp->notIn('created', '2007-03-18 10:45:23', 'datetime');
  794. })
  795. ->execute();
  796. $this->assertCount(5, $result);
  797. $this->assertEquals(['id' => 2], $result->fetch('assoc'));
  798. $this->assertEquals(['id' => 3], $result->fetch('assoc'));
  799. $this->assertEquals(['id' => 4], $result->fetch('assoc'));
  800. $this->assertEquals(['id' => 5], $result->fetch('assoc'));
  801. $query = new Query($this->connection);
  802. $result = $query
  803. ->select(['id'])
  804. ->from('comments')
  805. ->where(function($exp, $q) {
  806. return $exp->in(
  807. 'created',
  808. $q->newExpr()->add("'2007-03-18 10:45:23'"),
  809. 'datetime'
  810. );
  811. })
  812. ->execute();
  813. $this->assertCount(1, $result);
  814. $this->assertEquals(['id' => 1], $result->fetch('assoc'));
  815. $query = new Query($this->connection);
  816. $result = $query
  817. ->select(['id'])
  818. ->from('comments')
  819. ->where(function($exp, $q) {
  820. return $exp->notIn(
  821. 'created',
  822. $q->newExpr()->add("'2007-03-18 10:45:23'"),
  823. 'datetime'
  824. );
  825. })
  826. ->execute();
  827. $this->assertCount(5, $result);
  828. }
  829. /**
  830. * Tests that calling "in" and "notIn" will cast the passed values to an array
  831. *
  832. * @return void
  833. */
  834. public function testInValueCast2() {
  835. $query = new Query($this->connection);
  836. $result = $query
  837. ->select(['id'])
  838. ->from('comments')
  839. ->where(['created IN' => '2007-03-18 10:45:23'])
  840. ->execute();
  841. $this->assertCount(1, $result);
  842. $this->assertEquals(['id' => 1], $result->fetch('assoc'));
  843. $query = new Query($this->connection);
  844. $result = $query
  845. ->select(['id'])
  846. ->from('comments')
  847. ->where(['created NOT IN' => '2007-03-18 10:45:23'])
  848. ->execute();
  849. $this->assertCount(5, $result);
  850. }
  851. /**
  852. * Tests nesting query expressions both using arrays and closures
  853. *
  854. * @return void
  855. **/
  856. public function testSelectExpressionComposition() {
  857. $query = new Query($this->connection);
  858. $result = $query
  859. ->select(['id'])
  860. ->from('comments')
  861. ->where(function($exp) {
  862. $and = $exp->and_(['id' => 2, 'id >' => 1]);
  863. return $exp->add($and);
  864. })
  865. ->execute();
  866. $this->assertCount(1, $result);
  867. $this->assertEquals(['id' => 2], $result->fetch('assoc'));
  868. $query = new Query($this->connection);
  869. $result = $query
  870. ->select(['id'])
  871. ->from('comments')
  872. ->where(function($exp) {
  873. $and = $exp->and_(['id' => 2, 'id <' => 2]);
  874. return $exp->add($and);
  875. })
  876. ->execute();
  877. $this->assertCount(0, $result);
  878. $query = new Query($this->connection);
  879. $result = $query
  880. ->select(['id'])
  881. ->from('comments')
  882. ->where(function($exp) {
  883. $and = $exp->and_(function($and) {
  884. return $and->eq('id', 1)->gt('id', 0);
  885. });
  886. return $exp->add($and);
  887. })
  888. ->execute();
  889. $this->assertCount(1, $result);
  890. $this->assertEquals(['id' => 1], $result->fetch('assoc'));
  891. $query = new Query($this->connection);
  892. $result = $query
  893. ->select(['id'])
  894. ->from('comments')
  895. ->where(function($exp) {
  896. $or = $exp->or_(['id' => 1]);
  897. $and = $exp->and_(['id >' => 2, 'id <' => 4]);
  898. return $or->add($and);
  899. })
  900. ->execute();
  901. $this->assertCount(2, $result);
  902. $this->assertEquals(['id' => 1], $result->fetch('assoc'));
  903. $this->assertEquals(['id' => 3], $result->fetch('assoc'));
  904. $query = new Query($this->connection);
  905. $result = $query
  906. ->select(['id'])
  907. ->from('comments')
  908. ->where(function($exp) {
  909. $or = $exp->or_(function($or) {
  910. return $or->eq('id', 1)->eq('id', 2);
  911. });
  912. return $or;
  913. })
  914. ->execute();
  915. $this->assertCount(2, $result);
  916. $this->assertEquals(['id' => 1], $result->fetch('assoc'));
  917. $this->assertEquals(['id' => 2], $result->fetch('assoc'));
  918. }
  919. /**
  920. * Tests that conditions can be nested with an unary operator using the array notation
  921. * and the not() method
  922. *
  923. * @return void
  924. **/
  925. public function testSelectWhereNot() {
  926. $query = new Query($this->connection);
  927. $result = $query
  928. ->select(['id'])
  929. ->from('comments')
  930. ->where(function($exp) {
  931. return $exp->not(
  932. $exp->and_(['id' => 2, 'created' => new \DateTime('2007-03-18 10:47:23')], ['created' => 'datetime'])
  933. );
  934. })
  935. ->execute();
  936. $this->assertCount(5, $result);
  937. $this->assertEquals(['id' => 1], $result->fetch('assoc'));
  938. $this->assertEquals(['id' => 3], $result->fetch('assoc'));
  939. $query = new Query($this->connection);
  940. $result = $query
  941. ->select(['id'])
  942. ->from('comments')
  943. ->where(function($exp) {
  944. return $exp->not(
  945. $exp->and_(['id' => 2, 'created' => new \DateTime('2012-12-21 12:00')], ['created' => 'datetime'])
  946. );
  947. })
  948. ->execute();
  949. $this->assertCount(6, $result);
  950. $query = new Query($this->connection);
  951. $result = $query
  952. ->select(['id'])
  953. ->from('articles')
  954. ->where([
  955. 'not' => ['or' => ['id' => 1, 'id >' => 2], 'id' => 3]
  956. ])
  957. ->execute();
  958. $this->assertCount(2, $result);
  959. $this->assertEquals(['id' => 1], $result->fetch('assoc'));
  960. $this->assertEquals(['id' => 2], $result->fetch('assoc'));
  961. }
  962. /**
  963. * Tests order() method both with simple fields and expressions
  964. *
  965. * @return void
  966. **/
  967. public function testSelectOrderBy() {
  968. $query = new Query($this->connection);
  969. $result = $query
  970. ->select(['id'])
  971. ->from('articles')
  972. ->order(['id' => 'desc'])
  973. ->execute();
  974. $this->assertEquals(['id' => 3], $result->fetch('assoc'));
  975. $this->assertEquals(['id' => 2], $result->fetch('assoc'));
  976. $this->assertEquals(['id' => 1], $result->fetch('assoc'));
  977. $result = $query->order(['id' => 'asc'])->execute();
  978. $this->assertEquals(['id' => 1], $result->fetch('assoc'));
  979. $this->assertEquals(['id' => 2], $result->fetch('assoc'));
  980. $this->assertEquals(['id' => 3], $result->fetch('assoc'));
  981. $result = $query->order(['title' => 'asc'])->execute();
  982. $this->assertEquals(['id' => 1], $result->fetch('assoc'));
  983. $this->assertEquals(['id' => 2], $result->fetch('assoc'));
  984. $this->assertEquals(['id' => 3], $result->fetch('assoc'));
  985. $result = $query->order(['title' => 'asc'], true)->execute();
  986. $this->assertEquals(['id' => 1], $result->fetch('assoc'));
  987. $this->assertEquals(['id' => 2], $result->fetch('assoc'));
  988. $this->assertEquals(['id' => 3], $result->fetch('assoc'));
  989. $result = $query->order(['title' => 'asc', 'published' => 'asc'], true)
  990. ->execute();
  991. $this->assertEquals(['id' => 1], $result->fetch('assoc'));
  992. $this->assertEquals(['id' => 2], $result->fetch('assoc'));
  993. $this->assertEquals(['id' => 3], $result->fetch('assoc'));
  994. $expression = $query->newExpr()
  995. ->add(['(id + :offset) % 2']);
  996. $result = $query
  997. ->order([$expression, 'id' => 'desc'], true)
  998. ->bind(':offset', 1, null)
  999. ->execute();
  1000. $this->assertEquals(['id' => 3], $result->fetch('assoc'));
  1001. $this->assertEquals(['id' => 1], $result->fetch('assoc'));
  1002. $this->assertEquals(['id' => 2], $result->fetch('assoc'));
  1003. $result = $query
  1004. ->order($expression, true)
  1005. ->order(['id' => 'asc'])
  1006. ->bind(':offset', 1, null)
  1007. ->execute();
  1008. $this->assertEquals(['id' => 1], $result->fetch('assoc'));
  1009. $this->assertEquals(['id' => 3], $result->fetch('assoc'));
  1010. $this->assertEquals(['id' => 2], $result->fetch('assoc'));
  1011. }
  1012. /**
  1013. * Tests that group by fields can be passed similar to select fields
  1014. * and that it sends the correct query to the database
  1015. *
  1016. * @return void
  1017. **/
  1018. public function testSelectGroup() {
  1019. $query = new Query($this->connection);
  1020. $result = $query
  1021. ->select(['total' => 'count(author_id)', 'author_id'])
  1022. ->from('articles')
  1023. ->join(['table' => 'authors', 'alias' => 'a', 'conditions' => 'author_id = a.id'])
  1024. ->group('author_id')
  1025. ->execute();
  1026. $expected = [['total' => 2, 'author_id' => 1], ['total' => '1', 'author_id' => 3]];
  1027. $this->assertEquals($expected, $result->fetchAll('assoc'));
  1028. $result = $query->select(['total' => 'count(title)', 'name'], true)
  1029. ->group(['name'], true)
  1030. ->order(['total' => 'asc'])
  1031. ->execute();
  1032. $expected = [['total' => 1, 'name' => 'larry'], ['total' => 2, 'name' => 'mariano']];
  1033. $this->assertEquals($expected, $result->fetchAll('assoc'));
  1034. $result = $query->select(['articles.id'])
  1035. ->group(['articles.id'])
  1036. ->execute();
  1037. $this->assertCount(3, $result);
  1038. }
  1039. /**
  1040. * Tests that it is possible to select distinct rows
  1041. *
  1042. * @return void
  1043. */
  1044. public function testSelectDistinct() {
  1045. $query = new Query($this->connection);
  1046. $result = $query
  1047. ->select(['author_id'])
  1048. ->from(['a' => 'articles'])
  1049. ->execute();
  1050. $this->assertCount(3, $result);
  1051. $result = $query->distinct()->execute();
  1052. $this->assertCount(2, $result);
  1053. $result = $query->select(['id'])->distinct(false)->execute();
  1054. $this->assertCount(3, $result);
  1055. }
  1056. /**
  1057. * Tests that it is possible to select distinct rows, even filtering by one column
  1058. * this is testing that there is an specific implementation for DISTINCT ON
  1059. *
  1060. * @return void
  1061. */
  1062. public function testSelectDistinctON() {
  1063. $this->skipIf(
  1064. $this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver,
  1065. 'Not implemented yet in SqlServer'
  1066. );
  1067. $result = $query->select(['id'])->distinct(['author_id'])->execute();
  1068. $this->assertCount(2, $result);
  1069. }
  1070. /**
  1071. * Test use of modifiers in the query
  1072. *
  1073. * Testing the generated SQL since the modifiers are usually different per driver
  1074. *
  1075. * @return void
  1076. */
  1077. public function testSelectModifiers() {
  1078. $query = new Query($this->connection);
  1079. $result = $query
  1080. ->select(['city', 'state', 'country'])
  1081. ->from(['addresses'])
  1082. ->modifier('DISTINCTROW');
  1083. $this->assertQuotedQuery(
  1084. 'SELECT DISTINCTROW <city>, <state>, <country> FROM <addresses>',
  1085. $result->sql(),
  1086. true
  1087. );
  1088. $query = new Query($this->connection);
  1089. $result = $query
  1090. ->select(['city', 'state', 'country'])
  1091. ->from(['addresses'])
  1092. ->modifier(['DISTINCTROW', 'SQL_NO_CACHE']);
  1093. $this->assertQuotedQuery(
  1094. 'SELECT DISTINCTROW SQL_NO_CACHE <city>, <state>, <country> FROM <addresses>',
  1095. $result->sql(),
  1096. true
  1097. );
  1098. $query = new Query($this->connection);
  1099. $result = $query
  1100. ->select(['city', 'state', 'country'])
  1101. ->from(['addresses'])
  1102. ->modifier('DISTINCTROW')
  1103. ->modifier('SQL_NO_CACHE');
  1104. $this->assertQuotedQuery(
  1105. 'SELECT DISTINCTROW SQL_NO_CACHE <city>, <state>, <country> FROM <addresses>',
  1106. $result->sql(),
  1107. true
  1108. );
  1109. $query = new Query($this->connection);
  1110. $result = $query
  1111. ->select(['city', 'state', 'country'])
  1112. ->from(['addresses'])
  1113. ->modifier(['TOP 10']);
  1114. $this->assertQuotedQuery(
  1115. 'SELECT TOP 10 <city>, <state>, <country> FROM <addresses>',
  1116. $result->sql(),
  1117. true
  1118. );
  1119. }
  1120. /**
  1121. * Tests that having() behaves pretty much the same as the where() method
  1122. *
  1123. * @return void
  1124. */
  1125. public function testSelectHaving() {
  1126. $query = new Query($this->connection);
  1127. $result = $query
  1128. ->select(['total' => 'count(author_id)', 'author_id'])
  1129. ->from('articles')
  1130. ->join(['table' => 'authors', 'alias' => 'a', 'conditions' => 'author_id = a.id'])
  1131. ->group('author_id')
  1132. ->having(['count(author_id) <' => 2], ['count(author_id)' => 'integer'])
  1133. ->execute();
  1134. $expected = [['total' => 1, 'author_id' => 3]];
  1135. $this->assertEquals($expected, $result->fetchAll('assoc'));
  1136. $result = $query->having(['count(author_id)' => 2], ['count(author_id)' => 'integer'], true)
  1137. ->execute();
  1138. $expected = [['total' => 2, 'author_id' => 1]];
  1139. $this->assertEquals($expected, $result->fetchAll('assoc'));
  1140. $result = $query->having(function($e) {
  1141. return $e->add('count(author_id) = 1 + 1');
  1142. }, [], true)
  1143. ->execute();
  1144. $expected = [['total' => 2, 'author_id' => 1]];
  1145. $this->assertEquals($expected, $result->fetchAll('assoc'));
  1146. }
  1147. /**
  1148. * Tests that Query::orHaving() can be used to concatenate conditions with OR
  1149. * in the having clause
  1150. *
  1151. * @return void
  1152. */
  1153. public function testSelectOrHaving() {
  1154. $query = new Query($this->connection);
  1155. $result = $query
  1156. ->select(['total' => 'count(author_id)', 'author_id'])
  1157. ->from('articles')
  1158. ->join(['table' => 'authors', 'alias' => 'a', 'conditions' => 'author_id = a.id'])
  1159. ->group('author_id')
  1160. ->having(['count(author_id) >' => 2], ['count(author_id)' => 'integer'])
  1161. ->orHaving(['count(author_id) <' => 2], ['count(author_id)' => 'integer'])
  1162. ->execute();
  1163. $expected = [['total' => 1, 'author_id' => 3]];
  1164. $this->assertEquals($expected, $result->fetchAll('assoc'));
  1165. $query = new Query($this->connection);
  1166. $result = $query
  1167. ->select(['total' => 'count(author_id)', 'author_id'])
  1168. ->from('articles')
  1169. ->join(['table' => 'authors', 'alias' => 'a', 'conditions' => 'author_id = a.id'])
  1170. ->group('author_id')
  1171. ->having(['count(author_id) >' => 2], ['count(author_id)' => 'integer'])
  1172. ->orHaving(['count(author_id) <=' => 2], ['count(author_id)' => 'integer'])
  1173. ->execute();
  1174. $expected = [['total' => 2, 'author_id' => 1], ['total' => 1, 'author_id' => 3]];
  1175. $this->assertEquals($expected, $result->fetchAll('assoc'));
  1176. $query = new Query($this->connection);
  1177. $result = $query
  1178. ->select(['total' => 'count(author_id)', 'author_id'])
  1179. ->from('articles')
  1180. ->join(['table' => 'authors', 'alias' => 'a', 'conditions' => 'author_id = a.id'])
  1181. ->group('author_id')
  1182. ->having(['count(author_id) >' => 2], ['count(author_id)' => 'integer'])
  1183. ->orHaving(function($e) {
  1184. return $e->add('count(author_id) = 1 + 1');
  1185. })
  1186. ->execute();
  1187. $expected = [['total' => 2, 'author_id' => 1]];
  1188. $this->assertEquals($expected, $result->fetchAll('assoc'));
  1189. }
  1190. /**
  1191. * Tests that Query::andHaving() can be used to concatenate conditions with AND
  1192. * in the having clause
  1193. *
  1194. * @return void
  1195. */
  1196. public function testSelectAndHaving() {
  1197. $query = new Query($this->connection);
  1198. $result = $query
  1199. ->select(['total' => 'count(author_id)', 'author_id'])
  1200. ->from('articles')
  1201. ->join(['table' => 'authors', 'alias' => 'a', 'conditions' => 'author_id = a.id'])
  1202. ->group('author_id')
  1203. ->having(['count(author_id) >' => 2], ['count(author_id)' => 'integer'])
  1204. ->andHaving(['count(author_id) <' => 2], ['count(author_id)' => 'integer'])
  1205. ->execute();
  1206. $this->assertCount(0, $result);
  1207. $query = new Query($this->connection);
  1208. $result = $query
  1209. ->select(['total' => 'count(author_id)', 'author_id'])
  1210. ->from('articles')
  1211. ->join(['table' => 'authors', 'alias' => 'a', 'conditions' => 'author_id = a.id'])
  1212. ->group('author_id')
  1213. ->having(['count(author_id)' => 2], ['count(author_id)' => 'integer'])
  1214. ->andHaving(['count(author_id) >' => 1], ['count(author_id)' => 'integer'])
  1215. ->execute();
  1216. $expected = [['total' => 2, 'author_id' => 1]];
  1217. $this->assertEquals($expected, $result->fetchAll('assoc'));
  1218. $query = new Query($this->connection);
  1219. $result = $query
  1220. ->select(['total' => 'count(author_id)', 'author_id'])
  1221. ->from('articles')
  1222. ->join(['table' => 'authors', 'alias' => 'a', 'conditions' => 'author_id = a.id'])
  1223. ->group('author_id')
  1224. ->andHaving(function($e) {
  1225. return $e->add('count(author_id) = 2 - 1');
  1226. })
  1227. ->execute();
  1228. $expected = [['total' => 1, 'author_id' => 3]];
  1229. $this->assertEquals($expected, $result->fetchAll('assoc'));
  1230. }
  1231. /**
  1232. * Tests selecting rows using a limit clause
  1233. *
  1234. * @return void
  1235. */
  1236. public function testSelectLimit() {
  1237. $query = new Query($this->connection);
  1238. $result = $query->select('id')->from('articles')->limit(1)->execute();
  1239. $this->assertCount(1, $result);
  1240. $query = new Query($this->connection);
  1241. $result = $query->select('id')->from('articles')->limit(2)->execute();
  1242. $this->assertCount(2, $result);
  1243. }
  1244. /**
  1245. * Tests selecting rows combining a limit and offset clause
  1246. *
  1247. * @return void
  1248. */
  1249. public function testSelectOffset() {
  1250. $query = new Query($this->connection);
  1251. $result = $query->select('id')->from('comments')
  1252. ->limit(1)
  1253. ->offset(0)->execute();
  1254. $this->assertCount(1, $result);
  1255. $this->assertEquals(['id' => 1], $result->fetch('assoc'));
  1256. $query = new Query($this->connection);
  1257. $result = $query->select('id')->from('comments')
  1258. ->limit(1)
  1259. ->offset(1)
  1260. ->execute();
  1261. $this->assertCount(1, $result);
  1262. $this->assertEquals(['id' => 2], $result->fetch('assoc'));
  1263. $query = new Query($this->connection);
  1264. $result = $query->select('id')->from('comments')
  1265. ->limit(1)
  1266. ->offset(2)
  1267. ->execute();
  1268. $this->assertCount(1, $result);
  1269. $this->assertEquals(['id' => 3], $result->fetch('assoc'));
  1270. $query = new Query($this->connection);
  1271. $result = $query->select('id')->from('articles')
  1272. ->order(['id' => 'desc'])
  1273. ->limit(1)
  1274. ->offset(0)->execute();
  1275. $this->assertCount(1, $result);
  1276. $this->assertEquals(['id' => 3], $result->fetch('assoc'));
  1277. $result = $query->limit(2)->offset(1)->execute();
  1278. $this->assertCount(2, $result);
  1279. $this->assertEquals(['id' => 2], $result->fetch('assoc'));
  1280. $this->assertEquals(['id' => 1], $result->fetch('assoc'));
  1281. }
  1282. /**
  1283. * Test selecting rows using the page() method.
  1284. *
  1285. * @return void
  1286. */
  1287. public function testSelectPage() {
  1288. $query = new Query($this->connection);
  1289. $result = $query->select('id')->from('comments')
  1290. ->limit(1)
  1291. ->page(1)
  1292. ->execute();
  1293. $this->assertCount(1, $result);
  1294. $this->assertEquals(['id' => 1], $result->fetch('assoc'));
  1295. $query = new Query($this->connection);
  1296. $result = $query->select('id')->from('comments')
  1297. ->limit(1)
  1298. ->page(2)
  1299. ->execute();
  1300. $this->assertCount(1, $result);
  1301. $this->assertEquals(['id' => 2], $result->fetch('assoc'));
  1302. $query = new Query($this->connection);
  1303. $query->select('id')->from('comments')->page(1);
  1304. $this->assertEquals(25, $query->clause('limit'));
  1305. $this->assertEquals(0, $query->clause('offset'));
  1306. $query->select('id')->from('comments')->page(2);
  1307. $this->assertEquals(25, $query->clause('limit'));
  1308. $this->assertEquals(25, $query->clause('offset'));
  1309. }
  1310. /**
  1311. * Tests that Query objects can be included inside the select clause
  1312. * and be used as a normal field, including binding any passed parameter
  1313. *
  1314. * @return void
  1315. */
  1316. public function testSubqueryInSelect() {
  1317. $query = new Query($this->connection);
  1318. $subquery = (new Query($this->connection))
  1319. ->select('name')
  1320. ->from(['b' => 'authors'])
  1321. ->where(['b.id = a.id']);
  1322. $result = $query
  1323. ->select(['id', 'name' => $subquery])
  1324. ->from(['a' => 'comments'])->execute();
  1325. $expected = [
  1326. ['id' => 1, 'name' => 'mariano'],
  1327. ['id' => 2, 'name' => 'nate'],
  1328. ['id' => 3, 'name' => 'larry'],
  1329. ['id' => 4, 'name' => 'garrett'],
  1330. ['id' => 5, 'name' => null],
  1331. ['id' => 6, 'name' => null],
  1332. ];
  1333. $this->assertEquals($expected, $result->fetchAll('assoc'));
  1334. $query = new Query($this->connection);
  1335. $subquery = (new Query($this->connection))
  1336. ->select('name')
  1337. ->from(['b' => 'authors'])
  1338. ->where(['name' => 'mariano'], ['name' => 'string']);
  1339. $result = $query
  1340. ->select(['id', 'name' => $subquery])
  1341. ->from(['a' => 'articles'])->execute();
  1342. $expected = [
  1343. ['id' => 1, 'name' => 'mariano'],
  1344. ['id' => 2, 'name' => 'mariano'],
  1345. ['id' => 3, 'name' => 'mariano'],
  1346. ];
  1347. $this->assertEquals($expected, $result->fetchAll('assoc'));
  1348. }
  1349. /**
  1350. * Tests that Query objects can be included inside the from clause
  1351. * and be used as a normal table, including binding any passed parameter
  1352. *
  1353. * @return void
  1354. */
  1355. public function testSuqueryInFrom() {
  1356. $query = new Query($this->connection);
  1357. $subquery = (new Query($this->connection))
  1358. ->select(['id', 'comment'])
  1359. ->from('comments')
  1360. ->where(['created >' => new \DateTime('2007-03-18 10:45:23')], ['created' => 'datetime']);
  1361. $result = $query
  1362. ->select(['say' => 'comment'])
  1363. ->from(['b' => $subquery])
  1364. ->where(['id !=' => 3])
  1365. ->execute();
  1366. $expected = [
  1367. ['say' => 'Second Comment for First Article'],
  1368. ['say' => 'Fourth Comment for First Article'],
  1369. ['say' => 'First Comment for Second Article'],
  1370. ['say' => 'Second Comment for Second Article'],
  1371. ];
  1372. $this->assertEquals($expected, $result->fetchAll('assoc'));
  1373. }
  1374. /**
  1375. * Tests that Query objects can be included inside the where clause
  1376. * and be used as a normal condition, including binding any passed parameter
  1377. *
  1378. * @return void
  1379. */
  1380. public function testSubqueryInWhere() {
  1381. $query = new Query($this->connection);
  1382. $subquery = (new Query($this->connection))
  1383. ->select(['id'])
  1384. ->from('authors')
  1385. ->where(['id' => 1]);
  1386. $result = $query
  1387. ->select(['name'])
  1388. ->from(['authors'])
  1389. ->where(['id !=' => $subquery])
  1390. ->execute();
  1391. $expected = [
  1392. ['name' => 'nate'],
  1393. ['name' => 'larry'],
  1394. ['name' => 'garrett'],
  1395. ];
  1396. $this->assertEquals($expected, $result->fetchAll('assoc'));
  1397. $query = new Query($this->connection);
  1398. $subquery = (new Query($this->connection))
  1399. ->select(['id'])
  1400. ->from('comments')
  1401. ->where(['created >' => new \DateTime('2007-03-18 10:45:23')], ['created' => 'datetime']);
  1402. $result = $query
  1403. ->select(['name'])
  1404. ->from(['authors'])
  1405. ->where(['id not in' => $subquery])
  1406. ->execute();
  1407. $expected = [
  1408. ['name' => 'mariano'],
  1409. ];
  1410. $this->assertEquals($expected, $result->fetchAll('assoc'));
  1411. }
  1412. /**
  1413. * Tests that it is possible to use a subquery in a join clause
  1414. *
  1415. * @return void
  1416. */
  1417. public function testSubqueyInJoin() {
  1418. $subquery = (new Query($this->connection))->select('*')->from('authors');
  1419. $query = new Query($this->connection);
  1420. $result = $query
  1421. ->select(['title', 'name'])
  1422. ->from('articles')
  1423. ->join(['b' => $subquery])
  1424. ->execute();
  1425. $this->assertCount(self::ARTICLE_COUNT * self::AUTHOR_COUNT, $result, 'Cross join causes multiplication');
  1426. $subquery->where(['id' => 1]);
  1427. $result = $query->execute();
  1428. $this->assertCount(3, $result);
  1429. $query->join(['b' => ['table' => $subquery, 'conditions' => ['b.id = articles.id']]], [], true);
  1430. $result = $query->execute();
  1431. $this->assertCount(1, $result);
  1432. }
  1433. /**
  1434. * Tests that it is possible to one or multiple UNION statements in a query
  1435. *
  1436. * @return void
  1437. */
  1438. public function testUnion() {
  1439. $union = (new Query($this->connection))->select(['id', 'title'])->from(['a' => 'articles']);
  1440. $query = new Query($this->connection);
  1441. $result = $query->select(['id', 'comment'])
  1442. ->from(['c' => 'comments'])
  1443. ->union($union)
  1444. ->execute();
  1445. $this->assertCount(self::COMMENT_COUNT + self::ARTICLE_COUNT, $result);
  1446. $rows = $result->fetchAll();
  1447. $union->select(['foo' => 'id', 'bar' => 'title']);
  1448. $union = (new Query($this->connection))
  1449. ->select(['id', 'name', 'other' => 'id', 'nameish' => 'name'])
  1450. ->from(['b' => 'authors'])
  1451. ->where(['id ' => 1])
  1452. ->order(['id' => 'desc']);
  1453. $query->select(['foo' => 'id', 'bar' => 'comment'])->union($union);
  1454. $result = $query->execute();
  1455. $this->assertCount(self::COMMENT_COUNT + self::AUTHOR_COUNT, $result);
  1456. $this->assertNotEquals($rows, $result->fetchAll());
  1457. $union = (new Query($this->connection))
  1458. ->select(['id', 'title'])
  1459. ->from(['c' => 'articles']);
  1460. $query->select(['id', 'comment'], true)->union($union, true);
  1461. $result = $query->execute();
  1462. $this->assertCount(self::COMMENT_COUNT + self::ARTICLE_COUNT, $result);
  1463. $this->assertEquals($rows, $result->fetchAll());
  1464. }
  1465. /**
  1466. * Tests that UNION ALL can be built by setting the second param of union() to true
  1467. *
  1468. * @return void
  1469. */
  1470. public function testUnionAll() {
  1471. $union = (new Query($this->connection))->select(['id', 'title'])->from(['a' => 'articles']);
  1472. $query = new Query($this->connection);
  1473. $result = $query->select(['id', 'comment'])
  1474. ->from(['c' => 'comments'])
  1475. ->union($union)
  1476. ->execute();
  1477. $this->assertCount(self::ARTICLE_COUNT + self::COMMENT_COUNT, $result);
  1478. $rows = $result->fetchAll();
  1479. $union->select(['foo' => 'id', 'bar' => 'title']);
  1480. $union = (new Query($this->connection))
  1481. ->select(['id', 'name', 'other' => 'id', 'nameish' => 'name'])
  1482. ->from(['b' => 'authors'])
  1483. ->where(['id ' => 1])
  1484. ->order(['id' => 'desc']);
  1485. $query->select(['foo' => 'id', 'bar' => 'comment'])->unionAll($union);
  1486. $result = $query->execute();
  1487. $this->assertCount(1 + self::COMMENT_COUNT + self::ARTICLE_COUNT, $result);
  1488. $this->assertNotEquals($rows, $result->fetchAll());
  1489. }
  1490. /**
  1491. * Tests stacking decorators for results and resetting the list of decorators
  1492. *
  1493. * @return void
  1494. */
  1495. public function testDecorateResults() {
  1496. $query = new Query($this->connection);
  1497. $result = $query
  1498. ->select(['id', 'title'])
  1499. ->from('articles')
  1500. ->order(['id' => 'ASC'])
  1501. ->decorateResults(function($row) {
  1502. $row['modified_id'] = $row['id'] + 1;
  1503. return $row;
  1504. })
  1505. ->execute();
  1506. while ($row = $result->fetch('assoc')) {
  1507. $this->assertEquals($row['id'] + 1, $row['modified_id']);
  1508. }
  1509. $result = $query->decorateResults(function($row) {
  1510. $row['modified_id']--;
  1511. return $row;
  1512. })->execute();
  1513. while ($row = $result->fetch('assoc')) {
  1514. $this->assertEquals($row['id'], $row['modified_id']);
  1515. }
  1516. $result = $query
  1517. ->decorateResults(function($row) {
  1518. $row['foo'] = 'bar';
  1519. return $row;
  1520. }, true)
  1521. ->execute();
  1522. while ($row = $result->fetch('assoc')) {
  1523. $this->assertEquals('bar', $row['foo']);
  1524. $this->assertArrayNotHasKey('modified_id', $row);
  1525. }
  1526. $results = $query->decorateResults(null, true)->execute();
  1527. while ($row = $result->fetch('assoc')) {
  1528. $this->assertArrayNotHasKey('foo', $row);
  1529. $this->assertArrayNotHasKey('modified_id', $row);
  1530. }
  1531. }
  1532. /**
  1533. * Test a basic delete using from()
  1534. *
  1535. * @return void
  1536. */
  1537. public function testDeleteWithFrom() {
  1538. $query = new Query($this->connection);
  1539. $query->delete()
  1540. ->from('authors')
  1541. ->where('1 = 1');
  1542. $result = $query->sql();
  1543. $this->assertQuotedQuery('DELETE FROM <authors>', $result, true);
  1544. $result = $query->execute();
  1545. $this->assertInstanceOf('Cake\Database\StatementInterface', $result);
  1546. $this->assertCount(self::AUTHOR_COUNT, $result);
  1547. }
  1548. /**
  1549. * Test a basic delete with no from() call.
  1550. *
  1551. * @return void
  1552. */
  1553. public function testDeleteNoFrom() {
  1554. $query = new Query($this->connection);
  1555. $query->delete('authors')
  1556. ->where('1 = 1');
  1557. $result = $query->sql();
  1558. $this->assertQuotedQuery('DELETE FROM <authors>', $result, true);
  1559. $result = $query->execute();
  1560. $this->assertInstanceOf('Cake\Database\StatementInterface', $result);
  1561. $this->assertCount(self::AUTHOR_COUNT, $result);
  1562. }
  1563. /**
  1564. * Test setting select() & delete() modes.
  1565. *
  1566. * @return void
  1567. */
  1568. public function testSelectAndDeleteOnSameQuery() {
  1569. $query = new Query($this->connection);
  1570. $result = $query->select()
  1571. ->delete('authors')
  1572. ->where('1 = 1');
  1573. $result = $query->sql();
  1574. $this->assertQuotedQuery('DELETE FROM <authors>', $result, true);
  1575. $this->assertContains(' WHERE 1 = 1', $result);
  1576. }
  1577. /**
  1578. * Test a simple update.
  1579. *
  1580. * @return void
  1581. */
  1582. public function testUpdateSimple() {
  1583. $query = new Query($this->connection);
  1584. $query->update('authors')
  1585. ->set('name', 'mark')
  1586. ->where(['id' => 1]);
  1587. $result = $query->sql();
  1588. $this->assertQuotedQuery('UPDATE <authors> SET <name> = :', $result, true);
  1589. $result = $query->execute();
  1590. $this->assertCount(1, $result);
  1591. }
  1592. /**
  1593. * Test update with multiple fields.
  1594. *
  1595. * @return void
  1596. */
  1597. public function testUpdateMultipleFields() {
  1598. $query = new Query($this->connection);
  1599. $query->update('articles')
  1600. ->set('title', 'mark', 'string')
  1601. ->set('body', 'some text', 'string')
  1602. ->where(['id' => 1]);
  1603. $result = $query->sql();
  1604. $this->assertQuotedQuery(
  1605. 'UPDATE <articles> SET <title> = :c0 , <body> = :c1',
  1606. $result,
  1607. true
  1608. );
  1609. $this->assertQuotedQuery(' WHERE <id> = :c2$', $result, true);
  1610. $result = $query->execute();
  1611. $this->assertCount(1, $result);
  1612. }
  1613. /**
  1614. * Test updating multiple fields with an array.
  1615. *
  1616. * @return void
  1617. */
  1618. public function testUpdateMultipleFieldsArray() {
  1619. $query = new Query($this->connection);
  1620. $query->update('articles')
  1621. ->set([
  1622. 'title' => 'mark',
  1623. 'body' => 'some text'
  1624. ], ['title' => 'string', 'body' => 'string'])
  1625. ->where(['id' => 1]);
  1626. $result = $query->sql();
  1627. $this->assertQuotedQuery(
  1628. 'UPDATE <articles> SET <title> = :c0 , <body> = :c1',
  1629. $result,
  1630. true
  1631. );
  1632. $this->assertQuotedQuery('WHERE <id> = :', $result, true);
  1633. $result = $query->execute();
  1634. $this->assertCount(1, $result);
  1635. }
  1636. /**
  1637. * Test updates with an expression.
  1638. *
  1639. * @return void
  1640. */
  1641. public function testUpdateWithExpression() {
  1642. $query = new Query($this->connection);
  1643. $expr = $query->newExpr();
  1644. $expr->add('title = author_id');
  1645. $query->update('articles')
  1646. ->set($expr)
  1647. ->where(['id' => 1]);
  1648. $result = $query->sql();
  1649. $this->assertQuotedQuery(
  1650. 'UPDATE <articles> SET title = author_id WHERE <id> = :',
  1651. $result,
  1652. true
  1653. );
  1654. $result = $query->execute();
  1655. $this->assertCount(1, $result);
  1656. }
  1657. /**
  1658. * Test update with array fields and types.
  1659. *
  1660. * @return void
  1661. */
  1662. public function testUpdateArrayFields() {
  1663. $query = new Query($this->connection);
  1664. $date = new \DateTime;
  1665. $query->update('comments')
  1666. ->set(['comment' => 'mark', 'created' => $date], ['created' => 'date'])
  1667. ->where(['id' => 1]);
  1668. $result = $query->sql();
  1669. $this->assertQuotedQuery(
  1670. 'UPDATE <comments> SET <comment> = :c0 , <created> = :c1',
  1671. $result,
  1672. true
  1673. );
  1674. $this->assertQuotedQuery(' WHERE <id> = :c2$', $result, true);
  1675. $result = $query->execute();
  1676. $this->assertCount(1, $result);
  1677. $query = new Query($this->connection);
  1678. $result = $query->select('created')->from('comments')->where(['id' => 1])->execute();
  1679. $result = $result->fetchAll('assoc')[0]['created'];
  1680. $this->assertStringStartsWith($date->format('Y-m-d'), $result);
  1681. }
  1682. /**
  1683. * You cannot call values() before insert() it causes all sorts of pain.
  1684. *
  1685. * @expectedException \Cake\Error\Exception
  1686. * @return void
  1687. */
  1688. public function testInsertValuesBeforeInsertFailure() {
  1689. $query = new Query($this->connection);
  1690. $query->select('*')->values([
  1691. 'id' => 1,
  1692. 'title' => 'mark',
  1693. 'body' => 'test insert'
  1694. ]);
  1695. }
  1696. /**
  1697. * Inserting nothing should not generate an error.
  1698. *
  1699. * @expectedException RuntimeException
  1700. * @expectedExceptionMessage At least 1 column is required to perform an insert.
  1701. * @return void
  1702. */
  1703. public function testInsertNothing() {
  1704. $query = new Query($this->connection);
  1705. $query->insert([]);
  1706. }
  1707. /**
  1708. * Test inserting a single row.
  1709. *
  1710. * @return void
  1711. */
  1712. public function testInsertSimple() {
  1713. $query = new Query($this->connection);
  1714. $query->insert(['title', 'body'])
  1715. ->into('articles')
  1716. ->values([
  1717. 'title' => 'mark',
  1718. 'body' => 'test insert'
  1719. ]);
  1720. $result = $query->sql();
  1721. $this->assertQuotedQuery(
  1722. 'INSERT INTO <articles> \(<title>, <body>\) ' .
  1723. 'VALUES \(\?, \?\)',
  1724. $result,
  1725. true
  1726. );
  1727. $result = $query->execute();
  1728. $this->assertCount(1, $result, '1 row should be inserted');
  1729. $expected = [
  1730. [
  1731. 'id' => 4,
  1732. 'author_id' => null,
  1733. 'title' => 'mark',
  1734. 'body' => 'test insert',
  1735. 'published' => 'N',
  1736. ]
  1737. ];
  1738. $this->assertTable('articles', 1, $expected, ['id >=' => 4]);
  1739. }
  1740. /**
  1741. * Test an insert when not all the listed fields are provided.
  1742. * Columns should be matched up where possible.
  1743. *
  1744. * @return void
  1745. */
  1746. public function testInsertSparseRow() {
  1747. $query = new Query($this->connection);
  1748. $query->insert(['title', 'body'])
  1749. ->into('articles')
  1750. ->values([
  1751. 'title' => 'mark',
  1752. ]);
  1753. $result = $query->sql();
  1754. $this->assertQuotedQuery(
  1755. 'INSERT INTO <articles> \(<title>, <body>\) ' .
  1756. 'VALUES \(\?, \?\)',
  1757. $result,
  1758. true
  1759. );
  1760. $result = $query->execute();
  1761. $this->assertCount(1, $result, '1 row should be inserted');
  1762. $expected = [
  1763. [
  1764. 'id' => 4,
  1765. 'author_id' => null,
  1766. 'title' => 'mark',
  1767. 'body' => null,
  1768. 'published' => 'N',
  1769. ]
  1770. ];
  1771. $this->assertTable('articles', 1, $expected, ['id >= 4']);
  1772. }
  1773. /**
  1774. * Test inserting multiple rows with sparse data.
  1775. *
  1776. * @return void
  1777. */
  1778. public function testInsertMultipleRowsSparse() {
  1779. $query = new Query($this->connection);
  1780. $query->insert(['title', 'body'])
  1781. ->into('articles')
  1782. ->values([
  1783. 'body' => 'test insert'
  1784. ])
  1785. ->values([
  1786. 'title' => 'jose',
  1787. ]);
  1788. $result = $query->execute();
  1789. $this->assertCount(2, $result, '2 rows should be inserted');
  1790. $expected = [
  1791. [
  1792. 'id' => 4,
  1793. 'author_id' => null,
  1794. 'title' => null,
  1795. 'body' => 'test insert',
  1796. 'published' => 'N',
  1797. ],
  1798. [
  1799. 'id' => 5,
  1800. 'author_id' => null,
  1801. 'title' => 'jose',
  1802. 'body' => null,
  1803. 'published' => 'N',
  1804. ],
  1805. ];
  1806. $this->assertTable('articles', 2, $expected, ['id >=' => 4]);
  1807. }
  1808. /**
  1809. * Test that INSERT INTO ... SELECT works.
  1810. *
  1811. * @return void
  1812. */
  1813. public function testInsertFromSelect() {
  1814. $select = (new Query($this->connection))->select(['name', "'some text'", 99])
  1815. ->from('authors')
  1816. ->where(['id' => 1]);
  1817. $query = new Query($this->connection);
  1818. $query->insert(
  1819. ['title', 'body', 'author_id'],
  1820. ['title' => 'string', 'body' => 'string', 'author_id' => 'integer']
  1821. )
  1822. ->into('articles')
  1823. ->values($select);
  1824. $result = $query->sql();
  1825. $this->assertQuotedQuery(
  1826. 'INSERT INTO <articles> \(<title>, <body>, <author_id>\) SELECT',
  1827. $result,
  1828. true
  1829. );
  1830. $this->assertQuotedQuery(
  1831. 'SELECT <name>, \'some text\', 99 FROM <authors>', $result, true);
  1832. $result = $query->execute();
  1833. $this->assertCount(1, $result);
  1834. $result = (new Query($this->connection))->select('*')
  1835. ->from('articles')
  1836. ->where(['author_id' => 99])
  1837. ->execute();
  1838. $this->assertCount(1, $result);
  1839. $expected = [
  1840. 'id' => 4,
  1841. 'title' => 'mariano',
  1842. 'body' => 'some text',
  1843. 'author_id' => 99,
  1844. 'published' => 'N',
  1845. ];
  1846. $this->assertEquals($expected, $result->fetch('assoc'));
  1847. }
  1848. /**
  1849. * Test that an exception is raised when mixing query + array types.
  1850. *
  1851. * @expectedException \Cake\Error\Exception
  1852. */
  1853. public function testInsertFailureMixingTypesArrayFirst() {
  1854. $query = new Query($this->connection);
  1855. $query->insert(['name'])
  1856. ->into('articles')
  1857. ->values(['name' => 'mark'])
  1858. ->values(new Query($this->connection));
  1859. }
  1860. /**
  1861. * Test that an exception is raised when mixing query + array types.
  1862. *
  1863. * @expectedException \Cake\Error\Exception
  1864. */
  1865. public function testInsertFailureMixingTypesQueryFirst() {
  1866. $query = new Query($this->connection);
  1867. $query->insert(['name'])
  1868. ->into('articles')
  1869. ->values(new Query($this->connection))
  1870. ->values(['name' => 'mark']);
  1871. }
  1872. /**
  1873. * Tests that functions are correctly transformed and their parameters are bound
  1874. *
  1875. * @group FunctionExpression
  1876. * @return void
  1877. */
  1878. public function testSQLFunctions() {
  1879. $query = new Query($this->connection);
  1880. $result = $query->select(
  1881. function($q) {
  1882. return ['total' => $q->func()->count('*')];
  1883. }
  1884. )
  1885. ->from('articles')
  1886. ->execute();
  1887. $expected = [['total' => 3]];
  1888. $this->assertEquals($expected, $result->fetchAll('assoc'));
  1889. $query = new Query($this->connection);
  1890. $result = $query->select([
  1891. 'c' => $query->func()->concat(['title' => 'literal', ' is appended'])
  1892. ])
  1893. ->from('articles')
  1894. ->order(['c' => 'ASC'])
  1895. ->execute();
  1896. $expected = [
  1897. ['c' => 'First Article is appended'],
  1898. ['c' => 'Second Article is appended'],
  1899. ['c' => 'Third Article is appended']
  1900. ];
  1901. $this->assertEquals($expected, $result->fetchAll('assoc'));
  1902. $query = new Query($this->connection);
  1903. $result = $query
  1904. ->select(['d' => $query->func()->dateDiff(['2012-01-05', '2012-01-02'])])
  1905. ->execute()
  1906. ->fetchAll('assoc');
  1907. $this->assertEquals(3, abs($result[0]['d']));
  1908. $query = new Query($this->connection);
  1909. $result = $query
  1910. ->select(['d' => $query->func()->now('date')])
  1911. ->execute();
  1912. $this->assertEquals([['d' => date('Y-m-d')]], $result->fetchAll('assoc'));
  1913. $query = new Query($this->connection);
  1914. $result = $query
  1915. ->select(['d' => $query->func()->now('time')])
  1916. ->execute();
  1917. $this->assertWithinMargin(
  1918. date('U'),
  1919. (new \DateTime($result->fetchAll('assoc')[0]['d']))->format('U'),
  1920. 1
  1921. );
  1922. $query = new Query($this->connection);
  1923. $result = $query
  1924. ->select(['d' => $query->func()->now()])
  1925. ->execute();
  1926. $this->assertWithinMargin(
  1927. date('U'),
  1928. (new \DateTime($result->fetchAll('assoc')[0]['d']))->format('U'),
  1929. 1
  1930. );
  1931. }
  1932. /**
  1933. * Tests that default types are passed to functions accepting a $types param
  1934. *
  1935. * @return void
  1936. */
  1937. public function testDefaultTypes() {
  1938. $query = new Query($this->connection);
  1939. $this->assertEquals([], $query->defaultTypes());
  1940. $types = ['id' => 'integer', 'created' => 'datetime'];
  1941. $this->assertSame($query, $query->defaultTypes($types));
  1942. $this->assertSame($types, $query->defaultTypes());
  1943. $results = $query->select(['id', 'comment'])
  1944. ->from('comments')
  1945. ->where(['created >=' => new \DateTime('2007-03-18 10:55:00')])
  1946. ->execute();
  1947. $expected = [['id' => '6', 'comment' => 'Second Comment for Second Article']];
  1948. $this->assertEquals($expected, $results->fetchAll('assoc'));
  1949. // Now test default can be overridden
  1950. $types = ['created' => 'date'];
  1951. $results = $query
  1952. ->where(['created >=' => new \DateTime('2007-03-18 10:50:00')], $types, true)
  1953. ->execute();
  1954. $this->assertCount(6, $results, 'All 6 rows should match.');
  1955. }
  1956. /**
  1957. * Tests parameter binding
  1958. *
  1959. * @return void
  1960. */
  1961. public function testBind() {
  1962. $query = new Query($this->connection);
  1963. $results = $query->select(['id', 'comment'])
  1964. ->from('comments')
  1965. ->where(['created BETWEEN :foo AND :bar'])
  1966. ->bind(':foo', new \DateTime('2007-03-18 10:50:00'), 'datetime')
  1967. ->bind(':bar', new \DateTime('2007-03-18 10:52:00'), 'datetime')
  1968. ->execute();
  1969. $expected = [['id' => '4', 'comment' => 'Fourth Comment for First Article']];
  1970. $this->assertEquals($expected, $results->fetchAll('assoc'));
  1971. $query = new Query($this->connection);
  1972. $results = $query->select(['id', 'comment'])
  1973. ->from('comments')
  1974. ->where(['created BETWEEN :foo AND :bar'])
  1975. ->bind(':foo', '2007-03-18 10:50:00')
  1976. ->bind(':bar', '2007-03-18 10:52:00')
  1977. ->execute();
  1978. $this->assertEquals($expected, $results->fetchAll('assoc'));
  1979. }
  1980. /**
  1981. * Test that epilog() will actually append a string to a select query
  1982. *
  1983. * @return void
  1984. */
  1985. public function testAppendSelect() {
  1986. $query = new Query($this->connection);
  1987. $sql = $query
  1988. ->select(['id', 'title'])
  1989. ->from('articles')
  1990. ->where(['id' => 1])
  1991. ->epilog('FOR UPDATE')
  1992. ->sql();
  1993. $this->assertContains('SELECT', $sql);
  1994. $this->assertContains('FROM', $sql);
  1995. $this->assertContains('WHERE', $sql);
  1996. $this->assertEquals(' FOR UPDATE', substr($sql, -11));
  1997. }
  1998. /**
  1999. * Test that epilog() will actually append a string to an insert query
  2000. *
  2001. * @return void
  2002. */
  2003. public function testAppendInsert() {
  2004. $query = new Query($this->connection);
  2005. $sql = $query
  2006. ->insert(['id', 'title'])
  2007. ->into('articles')
  2008. ->values([1, 'a title'])
  2009. ->epilog('RETURNING id')
  2010. ->sql();
  2011. $this->assertContains('INSERT', $sql);
  2012. $this->assertContains('INTO', $sql);
  2013. $this->assertContains('VALUES', $sql);
  2014. $this->assertEquals(' RETURNING id', substr($sql, -13));
  2015. }
  2016. /**
  2017. * Test that epilog() will actually append a string to an update query
  2018. *
  2019. * @return void
  2020. */
  2021. public function testAppendUpdate() {
  2022. $query = new Query($this->connection);
  2023. $sql = $query
  2024. ->update('articles')
  2025. ->set(['title' => 'foo'])
  2026. ->where(['id' => 1])
  2027. ->epilog('RETURNING id')
  2028. ->sql();
  2029. $this->assertContains('UPDATE', $sql);
  2030. $this->assertContains('SET', $sql);
  2031. $this->assertContains('WHERE', $sql);
  2032. $this->assertEquals(' RETURNING id', substr($sql, -13));
  2033. }
  2034. /**
  2035. * Test that epilog() will actually append a string to a delete query
  2036. *
  2037. * @return void
  2038. */
  2039. public function testAppendDelete() {
  2040. $query = new Query($this->connection);
  2041. $sql = $query
  2042. ->delete('articles')
  2043. ->where(['id' => 1])
  2044. ->epilog('RETURNING id')
  2045. ->sql();
  2046. $this->assertContains('DELETE FROM', $sql);
  2047. $this->assertContains('WHERE', $sql);
  2048. $this->assertEquals(' RETURNING id', substr($sql, -13));
  2049. }
  2050. /**
  2051. * Tests automatic identifier quoting in the select clause
  2052. *
  2053. * @return void
  2054. */
  2055. public function testQuotingSelectFieldsAndAlias() {
  2056. $this->connection->driver()->autoQuoting(true);
  2057. $query = new Query($this->connection);
  2058. $sql = $query->select(['something'])->sql();
  2059. $this->assertQuotedQuery('SELECT <something>$', $sql);
  2060. $query = new Query($this->connection);
  2061. $sql = $query->select(['foo' => 'something'])->sql();
  2062. $this->assertQuotedQuery('SELECT <something> AS <foo>$', $sql);
  2063. $query = new Query($this->connection);
  2064. $sql = $query->select(['foo' => 1])->sql();
  2065. $this->assertQuotedQuery('SELECT 1 AS <foo>$', $sql);
  2066. $query = new Query($this->connection);
  2067. $sql = $query->select(['foo' => '1 + 1'])->sql();
  2068. $this->assertQuotedQuery('SELECT <1 \+ 1> AS <foo>$', $sql);
  2069. $query = new Query($this->connection);
  2070. $sql = $query->select(['foo' => $query->newExpr()->add('1 + 1')])->sql();
  2071. $this->assertQuotedQuery('SELECT \(1 \+ 1\) AS <foo>$', $sql);
  2072. $query = new Query($this->connection);
  2073. $sql = $query->select(['foo' => new IdentifierExpression('bar')])->sql();
  2074. $this->assertQuotedQuery('<bar>', $sql);
  2075. }
  2076. /**
  2077. * Tests automatic identifier quoting in the from clause
  2078. *
  2079. * @return void
  2080. */
  2081. public function testQuotingFromAndAlias() {
  2082. $this->connection->driver()->autoQuoting(true);
  2083. $query = new Query($this->connection);
  2084. $sql = $query->select('*')->from(['something'])->sql();
  2085. $this->assertQuotedQuery('FROM <something>', $sql);
  2086. $query = new Query($this->connection);
  2087. $sql = $query->select('*')->from(['foo' => 'something'])->sql();
  2088. $this->assertQuotedQuery('FROM <something> AS <foo>$', $sql);
  2089. $query = new Query($this->connection);
  2090. $sql = $query->select('*')->from(['foo' => $query->newExpr()->add('bar')])->sql();
  2091. $this->assertQuotedQuery('FROM \(bar\) AS <foo>$', $sql);
  2092. }
  2093. /**
  2094. * Tests automatic identifier quoting for DISTINCT ON
  2095. *
  2096. * @return void
  2097. */
  2098. public function testQuotingDistinctOn() {
  2099. $this->connection->driver()->autoQuoting(true);
  2100. $query = new Query($this->connection);
  2101. $sql = $query->select('*')->distinct(['something'])->sql();
  2102. $this->assertQuotedQuery('<something>', $sql);
  2103. }
  2104. /**
  2105. * Tests automatic identifier quoting in the join clause
  2106. *
  2107. * @return void
  2108. */
  2109. public function testQuotingJoinsAndAlias() {
  2110. $this->connection->driver()->autoQuoting(true);
  2111. $query = new Query($this->connection);
  2112. $sql = $query->select('*')->join(['something'])->sql();
  2113. $this->assertQuotedQuery('JOIN <something>', $sql);
  2114. $query = new Query($this->connection);
  2115. $sql = $query->select('*')->join(['foo' => 'something'])->sql();
  2116. $this->assertQuotedQuery('JOIN <something> <foo>', $sql);
  2117. $query = new Query($this->connection);
  2118. $sql = $query->select('*')->join(['foo' => $query->newExpr()->add('bar')])->sql();
  2119. $this->assertQuotedQuery('JOIN \(bar\) <foo>', $sql);
  2120. }
  2121. /**
  2122. * Tests automatic identifier quoting in the group by clause
  2123. *
  2124. * @return void
  2125. */
  2126. public function testQuotingGroupBy() {
  2127. $this->connection->driver()->autoQuoting(true);
  2128. $query = new Query($this->connection);
  2129. $sql = $query->select('*')->group(['something'])->sql();
  2130. $this->assertQuotedQuery('GROUP BY <something>', $sql);
  2131. $query = new Query($this->connection);
  2132. $sql = $query->select('*')->group([$query->newExpr()->add('bar')])->sql();
  2133. $this->assertQuotedQuery('GROUP BY \(bar\)', $sql);
  2134. $query = new Query($this->connection);
  2135. $sql = $query->select('*')->group([new IdentifierExpression('bar')])->sql();
  2136. $this->assertQuotedQuery('GROUP BY \(<bar>\)', $sql);
  2137. }
  2138. /**
  2139. * Tests automatic identifier quoting strings inside conditions expressions
  2140. *
  2141. * @return void
  2142. */
  2143. public function testQuotingExpressions() {
  2144. $this->connection->driver()->autoQuoting(true);
  2145. $query = new Query($this->connection);
  2146. $sql = $query->select('*')
  2147. ->where(['something' => 'value'])
  2148. ->sql();
  2149. $this->assertQuotedQuery('WHERE <something> = :c0', $sql);
  2150. $query = new Query($this->connection);
  2151. $sql = $query->select('*')
  2152. ->where([
  2153. 'something' => 'value',
  2154. 'OR' => ['foo' => 'bar', 'baz' => 'cake']
  2155. ])
  2156. ->sql();
  2157. $this->assertQuotedQuery('<something> = :c0 AND', $sql);
  2158. $this->assertQuotedQuery('\(<foo> = :c1 OR <baz> = :c2\)', $sql);
  2159. }
  2160. /**
  2161. * Tests that insert query parts get quoted automatically
  2162. *
  2163. * @return void
  2164. */
  2165. public function testQuotingInsert() {
  2166. $this->connection->driver()->autoQuoting(true);
  2167. $query = new Query($this->connection);
  2168. $sql = $query->insert(['bar', 'baz'])
  2169. ->into('foo')
  2170. ->where(['something' => 'value'])
  2171. ->sql();
  2172. $this->assertQuotedQuery('INSERT INTO <foo> \(<bar>, <baz>\)', $sql);
  2173. $query = new Query($this->connection);
  2174. $sql = $query->insert([$query->newExpr()->add('bar')])
  2175. ->into('foo')
  2176. ->where(['something' => 'value'])
  2177. ->sql();
  2178. $this->assertQuotedQuery('INSERT INTO <foo> \(\(bar\)\)', $sql);
  2179. }
  2180. /**
  2181. * Tests converting a query to a string
  2182. *
  2183. * @return void
  2184. */
  2185. public function testToString() {
  2186. $query = new Query($this->connection);
  2187. $query
  2188. ->select(['title'])
  2189. ->from('articles');
  2190. $result = (string)$query;
  2191. $this->assertQuotedQuery('SELECT <title> FROM <articles>', $result, true);
  2192. }
  2193. /**
  2194. * Tests __debugInfo
  2195. *
  2196. * @return void
  2197. */
  2198. public function testDebugInfo() {
  2199. $query = (new Query($this->connection))->select('*')
  2200. ->from('articles')
  2201. ->defaultTypes(['id' => 'integer'])
  2202. ->where(['id' => '1']);
  2203. $expected = [
  2204. 'sql' => $query->sql(),
  2205. 'params' => [
  2206. ':c0' => ['value' => '1', 'type' => 'integer', 'placeholder' => 'c0']
  2207. ],
  2208. 'defaultTypes' => ['id' => 'integer'],
  2209. 'decorators' => 0,
  2210. 'executed' => false
  2211. ];
  2212. $result = $query->__debugInfo();
  2213. $this->assertEquals($expected, $result);
  2214. $query->execute();
  2215. $expected = [
  2216. 'sql' => $query->sql(),
  2217. 'params' => [
  2218. ':c0' => ['value' => '1', 'type' => 'integer', 'placeholder' => 'c0']
  2219. ],
  2220. 'defaultTypes' => ['id' => 'integer'],
  2221. 'decorators' => 0,
  2222. 'executed' => true
  2223. ];
  2224. $result = $query->__debugInfo();
  2225. }
  2226. /**
  2227. * Assertion for comparing a table's contents with what is in it.
  2228. *
  2229. * @param string $table
  2230. * @param integer $count
  2231. * @param array $rows
  2232. * @param array $conditions
  2233. * @return void
  2234. */
  2235. public function assertTable($table, $count, $rows, $conditions = []) {
  2236. $result = (new Query($this->connection))->select('*')
  2237. ->from($table)
  2238. ->where($conditions)
  2239. ->execute();
  2240. $this->assertCount($count, $result, 'Row count is incorrect');
  2241. $this->assertEquals($rows, $result->fetchAll('assoc'));
  2242. }
  2243. /**
  2244. * Assertion for comparing a regex pattern against a query having its indentifiers
  2245. * quoted. It accepts queries quoted with the characters `<` and `>`. If the third
  2246. * parameter is set to true, it will alter the pattern to both accept quoted and
  2247. * unquoted queries
  2248. *
  2249. * @param string $pattern
  2250. * @param string $query the result to compare against
  2251. * @param boolean $optional
  2252. * @return void
  2253. */
  2254. public function assertQuotedQuery($pattern, $query, $optional = false) {
  2255. if ($optional) {
  2256. $optional = '?';
  2257. }
  2258. $pattern = str_replace('<', '[`"\[]' . $optional, $pattern);
  2259. $pattern = str_replace('>', '[`"\]]' . $optional, $pattern);
  2260. $this->assertRegExp('#' . $pattern . '#', $query);
  2261. }
  2262. }