QueryTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\data\model;
  9. use lithium\data\model\Query;
  10. use lithium\data\entity\Record;
  11. use lithium\tests\mocks\data\MockPostObject;
  12. use lithium\tests\mocks\data\model\MockDatabase;
  13. use lithium\tests\mocks\data\model\MockQueryPost;
  14. use lithium\tests\mocks\data\model\MockQueryComment;
  15. class QueryTest extends \lithium\test\Unit {
  16. protected $_model = 'lithium\tests\mocks\data\model\MockQueryPost';
  17. protected $_configs = array();
  18. protected $_queryArr = array(
  19. 'model' => 'lithium\tests\mocks\data\model\MockQueryPost',
  20. 'type' => 'read',
  21. 'order' => 'created DESC',
  22. 'limit' => 10,
  23. 'page' => 1,
  24. 'fields' => array('id', 'author_id', 'title'),
  25. 'conditions' => array('author_id' => 12),
  26. 'comment' => 'Find all posts by author 12'
  27. );
  28. public function setUp() {
  29. $this->db = new MockDatabase();
  30. MockQueryPost::$connection = $this->db;
  31. MockQueryComment::$connection = $this->db;
  32. }
  33. public function tearDown() {
  34. MockQueryPost::reset();
  35. MockQueryComment::reset();
  36. }
  37. /**
  38. * Tests that configuration settings are delegating to matching method names
  39. */
  40. public function testObjectConstruction() {
  41. $query = new Query();
  42. $this->assertFalse($query->conditions());
  43. $query = new Query(array('conditions' => 'foo', 'limit' => '10'));
  44. $this->assertEqual(array('foo'), $query->conditions());
  45. $this->assertIdentical(10, $query->limit());
  46. }
  47. public function testModel() {
  48. $query = new Query($this->_queryArr);
  49. $this->assertEqual($this->_model, $query->model());
  50. $query->model('lithium\tests\mocks\data\model\MockQueryComment');
  51. $expected = 'lithium\tests\mocks\data\model\MockQueryComment';
  52. $result = $query->model();
  53. $this->assertEqual($expected, $result);
  54. }
  55. public function testFields() {
  56. $query = new Query($this->_queryArr);
  57. $expected = array('id','author_id','title');
  58. $result = $query->fields();
  59. $this->assertEqual($expected, $result);
  60. $query->fields('content');
  61. $expected = array('id','author_id','title','content');
  62. $result = $query->fields();
  63. $this->assertEqual($expected, $result);
  64. $query->fields(array('updated','created'));
  65. $expected = array('id','author_id','title','content','updated','created');
  66. $result = $query->fields();
  67. $this->assertEqual($expected, $result);
  68. $query->fields(false);
  69. $query->fields(array('id', 'title'));
  70. $expected = array('id','title');
  71. $result = $query->fields();
  72. $this->assertEqual($expected, $result);
  73. $query->fields(false);
  74. $expected = array(
  75. array(array('count(MockDatabasePost.id)')),
  76. array('count(MockDatabasePost.id)'),
  77. array((object) 'count(MockDatabasePost.id)'),
  78. (object) 'count(MockDatabasePost.id)'
  79. );
  80. $query->fields($expected);
  81. $result = $query->fields();
  82. $this->assertEqual($expected, $result);
  83. }
  84. public function testFieldsWithArray() {
  85. $query = new Query(array(
  86. 'model' => 'lithium\tests\mocks\data\model\MockQueryPost',
  87. 'type' => 'read',
  88. 'with' => 'MockQueryComment'
  89. ));
  90. $query->fields(array('MockQueryPost', 'MockQueryPost' => array('id')));
  91. $result = $query->fields();
  92. $expected = array('MockQueryPost', 'MockQueryPost.id');
  93. $this->assertEqual($expected, $result);
  94. $query->fields(false);
  95. $query->fields(array(
  96. 'MockQueryPost' => array('id'),
  97. 'title',
  98. 'MockQueryComment' => array('comment', 'title'),
  99. 'MockQueryComment'
  100. ));
  101. $result = $query->fields();
  102. $expected = array(
  103. 'MockQueryPost.id',
  104. 'title',
  105. 'MockQueryComment.comment',
  106. 'MockQueryComment.title',
  107. 'MockQueryComment'
  108. );
  109. $this->assertEqual($expected, $result);
  110. }
  111. public function testLimit() {
  112. $query = new Query($this->_queryArr);
  113. $expected = 10;
  114. $result = $query->limit();
  115. $this->assertEqual($expected, $result);
  116. $query->limit(5);
  117. $expected = 5;
  118. $result = $query->limit();
  119. $this->assertEqual($expected, $result);
  120. $query->limit(false);
  121. $this->assertNull($query->limit());
  122. }
  123. public function testPage() {
  124. $query = new Query($this->_queryArr);
  125. $expected = 1;
  126. $result = $query->page();
  127. $this->assertEqual($expected, $result);
  128. $query->page(5);
  129. $expected = 5;
  130. $result = $query->page();
  131. $this->assertEqual($expected, $result);
  132. }
  133. public function testOrder() {
  134. $query = new Query($this->_queryArr);
  135. $expected = 'created DESC';
  136. $result = $query->order();
  137. $this->assertEqual($expected, $result);
  138. $query->order('updated ASC');
  139. $expected = 'updated ASC';
  140. $result = $query->order();
  141. $this->assertEqual($expected, $result);
  142. }
  143. public function testRecord() {
  144. $query = new Query($this->_queryArr);
  145. $result = $query->entity();
  146. $this->assertNull($result);
  147. $record = (object) array('id' => 12);
  148. $record->title = 'Lorem Ipsum';
  149. $query->entity($record);
  150. $queryRecord = $query->entity();
  151. $expected = 12;
  152. $result = $queryRecord->id;
  153. $this->assertEqual($expected, $result);
  154. $expected = 'Lorem Ipsum';
  155. $result = $queryRecord->title;
  156. $this->assertEqual($expected, $result);
  157. $this->assertTrue($record === $query->entity());
  158. }
  159. public function testComment() {
  160. $query = new Query($this->_queryArr);
  161. $expected = 'Find all posts by author 12';
  162. $result = $query->comment();
  163. $this->assertEqual($expected, $result);
  164. $query->comment('Comment lorem');
  165. $expected = 'Comment lorem';
  166. $result = $query->comment();
  167. $this->assertEqual($expected, $result);
  168. }
  169. public function testData() {
  170. $query = new Query($this->_queryArr);
  171. $expected = array();
  172. $result = $query->data();
  173. $this->assertEqual($expected, $result);
  174. $record = new Record();
  175. $record->id = 12;
  176. $record->title = 'Lorem Ipsum';
  177. $query->entity($record);
  178. $expected = array('id' => 12, 'title' => 'Lorem Ipsum');
  179. $result = $query->data();
  180. $this->assertEqual($expected, $result);
  181. $query->data(array('id' => 35, 'title' => 'Nix', 'body' => 'Prix'));
  182. $expected = array('id' => 35, 'title' => 'Nix', 'body' => 'Prix');
  183. $result = $query->data();
  184. $this->assertEqual($expected, $result);
  185. }
  186. public function testConditions() {
  187. $query = new Query($this->_queryArr);
  188. $expected = array('author_id' => 12);
  189. $result = $query->conditions();
  190. $this->assertEqual($expected, $result);
  191. $query->conditions(array('author_id' => 13, 'title LIKE' => 'Lorem%'));
  192. $expected = array('author_id' => 13, 'title LIKE' => 'Lorem%');
  193. $result = $query->conditions();
  194. $this->assertEqual($expected, $result);
  195. }
  196. public function testHaving() {
  197. $query = new Query($this->_queryArr);
  198. $expected = array();
  199. $result = $query->having();
  200. $this->assertEqual($expected, $result);
  201. $query->having(array('count' => 5));
  202. $expected = array('count' => 5);
  203. $result = $query->having();
  204. $this->assertEqual($expected, $result);
  205. }
  206. public function testConditionFromRecord() {
  207. $entity = new Record();
  208. $entity->id = 12;
  209. $query = new Query(compact('entity') + array('model' => $this->_model));
  210. $expected = array('id' => 12);
  211. $result = $query->conditions();
  212. $this->assertEqual($expected, $result);
  213. }
  214. public function testExtra() {
  215. $object = new MockPostObject(array('id' => 1, 'data' => 'test'));
  216. $query = new Query(array(
  217. 'conditions' => 'foo', 'extra' => 'value', 'extraObject' => $object
  218. ));
  219. $this->assertEqual(array('foo'), $query->conditions());
  220. $this->assertEqual('value', $query->extra());
  221. $this->assertEqual($object, $query->extraObject());
  222. $this->assertNull($query->extra2());
  223. }
  224. public function testExport() {
  225. MockQueryPost::meta('source', 'foo');
  226. $query = new Query($this->_queryArr);
  227. $ds = new MockDatabase();
  228. $export = $query->export($ds);
  229. $this->assertTrue(is_array($export));
  230. $this->skipIf(!is_array($export), 'Query::export() does not return an array');
  231. $expected = array(
  232. 'alias',
  233. 'calculate',
  234. 'comment',
  235. 'conditions',
  236. 'having',
  237. 'data',
  238. 'fields',
  239. 'group',
  240. 'joins',
  241. 'limit',
  242. 'map',
  243. 'model',
  244. 'offset',
  245. 'order',
  246. 'page',
  247. 'schema',
  248. 'source',
  249. 'type',
  250. 'whitelist',
  251. 'with',
  252. 'relationships'
  253. );
  254. $result = array_keys($export);
  255. sort($expected);
  256. sort($result);
  257. $this->assertEqual($expected, $result);
  258. $expected = '{MockQueryPost}.{id}, {MockQueryPost}.{author_id}, {MockQueryPost}.{title}';
  259. $result = $export['fields'];
  260. $this->assertEqual($expected, $result);
  261. $result = $export['source'];
  262. $this->assertEqual("{foo}", $result);
  263. }
  264. public function testRestrictedKeyExport() {
  265. $options = array(
  266. 'type' => 'update',
  267. 'data' => array('title' => 'Bar'),
  268. 'conditions' => array('title' => 'Foo'),
  269. 'model' => $this->_model
  270. );
  271. $query = new Query($options);
  272. $result = $query->export($this->db, array(
  273. 'keys' => array('data', 'conditions')
  274. ));
  275. $expected = array(
  276. 'type' => 'update',
  277. 'data' => array('title' => 'Bar'),
  278. 'conditions' => "WHERE {title} = 'Foo'"
  279. );
  280. $this->assertEqual($expected, $result);
  281. }
  282. public function testPagination() {
  283. $query = new Query(array('limit' => 5, 'page' => 1));
  284. $this->assertEqual(0, $query->offset());
  285. $query = new Query(array('limit' => 5, 'page' => 2));
  286. $this->assertEqual(5, $query->offset());
  287. $query->page(1);
  288. $this->assertEqual(0, $query->offset());
  289. }
  290. public function testJoins() {
  291. $query = new Query(array('joins' => array(array('foo' => 'bar'))));
  292. $query->joins(array('bar' => 'baz'));
  293. $expected = array(array('foo' => 'bar'), array('bar' => 'baz'));
  294. $joins = $query->joins();
  295. $this->assertEqual($expected, $joins);
  296. $this->assertEqual('bar', $joins[0]['foo']);
  297. $this->assertFalse(isset($joins[0]['bar']));
  298. $this->assertEqual('baz', $joins[1]['bar']);
  299. $this->assertFalse(isset($joins[1]['foo']));
  300. $query->joins('zim', array('dib' => 'gir'));
  301. $joins = $query->joins();
  302. $this->assertEqual(3, count($joins));
  303. $this->assertEqual('gir', $joins['zim']['dib']);
  304. $expected = array(
  305. array('foo' => 'bar'),
  306. array('bar' => 'baz'),
  307. 'zim' => array('dib' => 'gir')
  308. );
  309. $this->assertEqual($expected, $joins);
  310. }
  311. public function testWithAssociation() {
  312. $model = $this->_model;
  313. $model::meta('source', 'foo');
  314. $model::bind('hasMany', 'MockQueryComment');
  315. $query = new Query(compact('model') + array('with' => 'MockQueryComment'));
  316. $export = $query->export(new MockDatabase());
  317. $expected = array('MockQueryComment' => array(
  318. 'type' => 'hasMany',
  319. 'model' => 'lithium\tests\mocks\data\model\MockQueryComment',
  320. 'fieldName' => 'mock_query_comments',
  321. 'alias' => 'MockQueryComment'
  322. ));
  323. $keyExists = isset($export['relationships']);
  324. $this->assertTrue($keyExists);
  325. $this->skipIf(!$keyExists);
  326. $this->assertEqual($expected, $export['relationships']);
  327. $query = new Query(compact('model') + array(
  328. 'type' => 'read',
  329. 'with' => 'MockQueryComment',
  330. 'limit' => 3,
  331. 'order' => 'author_id ASC',
  332. 'group' => 'author_id'
  333. ));
  334. $expected = 'SELECT * FROM {foo} AS {MockQueryPost} LEFT JOIN AS ';
  335. $expected .= '{MockQueryComment} ON {MockQueryPost}.{id} = {MockQueryComment}';
  336. $expected .= '.{mock_query_post_id} GROUP BY author_id ORDER BY author_id ASC ';
  337. $expected .= 'LIMIT 3;';
  338. $this->assertEqual($expected, $this->db->renderCommand($query));
  339. }
  340. /**
  341. * Tests that assigning a whitelist to a query properly restricts the list of data fields that
  342. * the query exposes.
  343. *
  344. * @return void
  345. */
  346. public function testWhitelisting() {
  347. $data = array('foo' => 1, 'bar' => 2, 'baz' => 3);
  348. $query = new Query(compact('data'));
  349. $this->assertEqual($data, $query->data());
  350. $query = new Query(compact('data') + array('whitelist' => array('foo', 'bar')));
  351. $this->assertEqual(array('foo' => 1, 'bar' => 2), $query->data());
  352. }
  353. /**
  354. * Tests basic property accessors and mutators.
  355. *
  356. * @return void
  357. */
  358. public function testBasicAssignments() {
  359. $query = new Query();
  360. $group = array('key' => 'hits', 'reduce' => 'function() {}');
  361. $calculate = 'count';
  362. $this->assertNull($query->group());
  363. $query->group($group);
  364. $this->assertEqual($group, $query->group());
  365. $this->assertNull($query->calculate());
  366. $query->calculate($calculate);
  367. $this->assertEqual($calculate, $query->calculate());
  368. $query = new Query(compact('calculate', 'group'));
  369. $this->assertEqual($group, $query->group());
  370. $this->assertEqual($calculate, $query->calculate());
  371. $query->group(false);
  372. $this->assertNull($query->group());
  373. }
  374. public function testInstantiationWithConditionsAndData() {
  375. $options = array(
  376. 'type' => 'update',
  377. 'data' => array('title' => '..'),
  378. 'conditions' => array('title' => 'FML'),
  379. 'model' => 'lithium\tests\mocks\data\model\MockQueryPost'
  380. );
  381. $query = new Query($options);
  382. $result = $query->export($this->db);
  383. $this->assertEqual(array('title' => '..'), $result['data']);
  384. $this->assertEqual("WHERE {title} = 'FML'", $result['conditions']);
  385. }
  386. public function testEntityConditions() {
  387. $entity = new Record(array('model' => $this->_model, 'exists' => true));
  388. $entity->id = 13;
  389. $query = new Query(compact('entity'));
  390. $this->assertEqual(array('id' => 13), $query->conditions());
  391. }
  392. public function testInvalidEntityCondition() {
  393. $entity = new Record(array('model' => $this->_model, 'exists' => true));
  394. $entity->_id = 13;
  395. $query = new Query(compact('entity'));
  396. $this->expectException('/No matching primary key found/');
  397. $query->conditions();
  398. }
  399. public function testAutomaticAliasing() {
  400. $query = new Query(array('model' => $this->_model));
  401. $this->assertEqual('MockQueryPost', $query->alias());
  402. }
  403. public function testFluentInterface() {
  404. $query = new Query();
  405. $conditions = array('foo' => 'bar');
  406. $fields = array('foo', 'bar', 'baz', 'created');
  407. $order = array('created' => 'ASC');
  408. $result = $query->conditions($conditions)->fields($fields)->order($order);
  409. $this->assertEqual($result, $query);
  410. $this->assertEqual($conditions, $query->conditions());
  411. $this->assertEqual($fields, $query->fields());
  412. $this->assertEqual($order, $query->order());
  413. }
  414. /**
  415. * The `Query` object shouldn't overwrite custom values with model-supplied values.
  416. */
  417. public function testQueryWithCustomAlias() {
  418. $model = 'lithium\tests\mocks\data\model\MockQueryComment';
  419. $query = new Query(compact('model') + array(
  420. 'source' => 'my_custom_table',
  421. 'alias' => 'MyCustomAlias'
  422. ));
  423. $result = $query->export($this->db);
  424. $this->assertEqual('{my_custom_table}', $result['source']);
  425. $this->assertEqual('AS {MyCustomAlias}', $result['alias']);
  426. }
  427. public function testRelationships() {
  428. $query = new Query(array('relationships' => array('Model1' => array('foo' => 'bar'))));
  429. $query->relationships('Model1.Model2', array('bar' => 'baz'));
  430. $expected = array(
  431. 'Model1' => array('foo' => 'bar'),
  432. 'Model1.Model2' => array('bar' => 'baz')
  433. );
  434. $relationships = $query->relationships();
  435. $this->assertEqual($expected, $relationships);
  436. $query = new Query();
  437. $query->relationships('Model1', array('foo' => 'bar'));
  438. $query->relationships('Model1.Model2', array('bar' => 'baz'));
  439. $relationships = $query->relationships();
  440. $this->assertEqual($expected, $relationships);
  441. }
  442. public function testAliasAndPaths() {
  443. $model = 'lithium\tests\mocks\data\model\MockQueryComment';
  444. $query = new Query(compact('model'));
  445. $this->assertIdentical('MockQueryComment', $query->alias());
  446. $this->assertIdentical('MockQueryComment', $query->alias(true));
  447. $this->assertIdentical('MockQueryComment2', $query->alias('MockQueryComment2'));
  448. $this->assertIdentical('MockQueryComment2', $query->alias());
  449. $this->assertIdentical('MockQueryComment2', $query->alias(true));
  450. $result = $query->alias('MockQueryComment', 'Model1');
  451. $this->assertIdentical('MockQueryComment__2', $result);
  452. $result = $query->alias('MockQueryComment2', 'Model2');
  453. $this->assertIdentical('MockQueryComment2__2', $result);
  454. $this->assertIdentical('MockQueryComment__2', $query->alias(true, 'Model1'));
  455. $this->assertIdentical('MockQueryComment2__2', $query->alias(true, 'Model2'));
  456. $query = new Query(compact('model') + array(
  457. 'source' => 'my_custom_table',
  458. 'alias' => 'MyCustomAlias'
  459. ));
  460. $result = $query->export($this->db);
  461. $this->assertIdentical('{my_custom_table}', $result['source']);
  462. $this->assertIdentical('AS {MyCustomAlias}', $result['alias']);
  463. $result = $query->alias('MyCustomAlias', 'Relation1');
  464. $this->assertIdentical('MyCustomAlias__2', $result);
  465. $result = $query->alias('MyCustomAlias', 'Other.Relation2');
  466. $this->assertIdentical('MyCustomAlias__3', $result);
  467. $result = $query->alias('MyCustomAlias2', 'Other.Other.Relation3');
  468. $this->assertIdentical('MyCustomAlias2', $result);
  469. $this->assertIdentical('MyCustomAlias', $query->alias());
  470. $this->assertIdentical('MyCustomAlias__2', $query->alias(true, 'Relation1'));
  471. $this->assertIdentical('MyCustomAlias__3', $query->alias(true, 'Other.Relation2'));
  472. $this->assertIdentical('MyCustomAlias2', $query->alias(true, 'Other.Other.Relation3'));
  473. $this->assertIdentical('Relation4', $query->alias(null, 'Relation4'));
  474. $this->assertIdentical('Relation5', $query->alias(null, 'Other.Relation5'));
  475. $this->assertIdentical('Relation5__2', $query->alias(null, 'Other.Other.Relation5'));
  476. $this->assertIdentical('Relation4', $query->alias(true, 'Relation4'));
  477. $this->assertIdentical('Relation5', $query->alias(true, 'Other.Relation5'));
  478. $this->assertIdentical('Relation5__2', $query->alias(true, 'Other.Other.Relation5'));
  479. $expected = array (
  480. 'MyCustomAlias' => null,
  481. 'MyCustomAlias__2' => 'Relation1',
  482. 'MyCustomAlias__3' => 'Other.Relation2',
  483. 'MyCustomAlias2' => 'Other.Other.Relation3',
  484. 'Relation4' => 'Relation4',
  485. 'Relation5' => 'Other.Relation5',
  486. 'Relation5__2' => 'Other.Other.Relation5'
  487. );
  488. $this->assertEqual($expected, $query->paths($this->db));
  489. $model = 'lithium\tests\mocks\data\model\MockQueryPost';
  490. $query = new Query(compact('model'));
  491. $query->alias(null, 'MockQueryComment');
  492. $query->alias('MockQueryPost2', 'MockQueryComment.MockQueryPost');
  493. $expected = array(
  494. 'MockQueryPost' => null,
  495. 'MockQueryComment' => 'MockQueryComment',
  496. 'MockQueryPost2' => 'MockQueryComment.MockQueryPost'
  497. );
  498. $this->assertEqual($expected, $query->paths($this->db));
  499. }
  500. public function testModels() {
  501. $model = 'lithium\tests\mocks\data\model\MockQueryPost';
  502. $query = new Query(array(
  503. 'model' => $model,
  504. 'with' => 'MockQueryComment'
  505. ));
  506. $expected = array(
  507. 'MockQueryPost' => 'lithium\tests\mocks\data\model\MockQueryPost',
  508. 'MockQueryComment' => 'lithium\tests\mocks\data\model\MockQueryComment'
  509. );
  510. $this->assertEqual($expected, $query->models($this->db));
  511. $query = new Query(array(
  512. 'model' => $model,
  513. 'alias' => 'Post',
  514. 'with' => array(
  515. 'MockQueryComment' => array('alias' => 'Comment'),
  516. 'MockQueryComment.MockQueryPost' => array('alias' => 'Post2'),
  517. )
  518. ));
  519. $expected = array(
  520. 'Post' => 'lithium\tests\mocks\data\model\MockQueryPost',
  521. 'Comment' => 'lithium\tests\mocks\data\model\MockQueryComment',
  522. 'Post2' => 'lithium\tests\mocks\data\model\MockQueryPost'
  523. );
  524. $this->assertEqual($expected, $query->models($this->db));
  525. }
  526. public function testExportWithJoinedStrategy() {
  527. $query = new Query(array(
  528. 'alias' => 'MyAlias',
  529. 'model' => 'lithium\tests\mocks\data\model\MockGallery',
  530. 'calculate' => 'MyCalculate',
  531. 'comment' => 'No comment',
  532. 'conditions' => array('id' => 2),
  533. 'fields' => array('Tag'),
  534. 'type' => 'read',
  535. 'with' => array('Image.ImageTag.Tag', 'Image', 'Image.ImageTag')
  536. ));
  537. $export = $query->export($this->db);
  538. $joins = 'LEFT JOIN {mock_image} AS {Image} ON {MyAlias}.{id} = {Image}.{gallery_id} ';
  539. $joins .= 'LEFT JOIN {mock_image_tag} AS {ImageTag} ON {Image}.{id} = ';
  540. $joins .= '{ImageTag}.{image_id} LEFT JOIN {mock_tag} AS {Tag} ON {ImageTag}.{tag_id} = ';
  541. $joins .= '{Tag}.{id}';
  542. $expected = array(
  543. 'type' => 'read',
  544. 'alias' => 'AS {MyAlias}',
  545. 'comment' => '/* No comment */',
  546. 'conditions' => 'WHERE {MyAlias}.{id} = 2',
  547. 'fields' => '{MyAlias}.{id}, {Tag}.*, {Image}.{id}, {ImageTag}.{id}',
  548. 'having' => '',
  549. 'group' => null,
  550. 'order' => null,
  551. 'limit' => null,
  552. 'joins' => $joins,
  553. 'model' => 'lithium\tests\mocks\data\model\MockGallery',
  554. 'calculate' => 'MyCalculate',
  555. 'with' => array(
  556. 'Image.ImageTag.Tag' => null,
  557. 'Image' => null,
  558. 'Image.ImageTag' => null
  559. ),
  560. 'source' => '{mock_gallery}',
  561. 'offset' => null,
  562. 'page' => null,
  563. 'data' => array(),
  564. 'whitelist' => array(),
  565. 'schema' => null,
  566. 'map' => array(),
  567. 'relationships' => array(
  568. 'Image' => array(
  569. 'type' => 'hasMany',
  570. 'model' => 'lithium\tests\mocks\data\model\MockImage',
  571. 'fieldName' => 'images',
  572. 'alias' => 'Image'
  573. ),
  574. 'Image.ImageTag' => array(
  575. 'type' => 'hasMany',
  576. 'model' => 'lithium\tests\mocks\data\model\MockImageTag',
  577. 'fieldName' => 'image_tags',
  578. 'alias' => 'ImageTag'
  579. ),
  580. 'Image.ImageTag.Tag' => array(
  581. 'type' => 'belongsTo',
  582. 'model' => 'lithium\tests\mocks\data\model\MockTag',
  583. 'fieldName' => 'tag',
  584. 'alias' => 'Tag'
  585. )
  586. )
  587. );
  588. $this->assertEqual($expected, $export);
  589. }
  590. public function testExportWithUndefinedStrategy() {
  591. $query = new Query(array(
  592. 'alias' => 'MyAlias',
  593. 'model' => 'lithium\tests\mocks\data\model\MockGallery',
  594. 'calculate' => 'MyCalculate',
  595. 'comment' => 'No comment',
  596. 'conditions' => array('id' => 2),
  597. 'fields' => array('Image.ImageTag.Tag'),
  598. 'type' => 'read',
  599. 'with' => array('Image.ImageTag.Tag', 'Image', 'Image.ImageTag'),
  600. 'strategy' => 'custom'
  601. ));
  602. $this->expectException('Undefined query strategy `custom`.');
  603. $export = $query->export($this->db);
  604. }
  605. public function testRespondsTo() {
  606. $query = new Query();
  607. $this->assertTrue($query->respondsTo('calculate'));
  608. $this->assertFalse($query->respondsTo('foobarbaz'));
  609. }
  610. }
  611. ?>