PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/lithium/0.9.9/libraries/lithium/tests/cases/data/model/QueryTest.php

http://github.com/pmjones/php-framework-benchmarks
PHP | 393 lines | 287 code | 84 blank | 22 comment | 1 complexity | 0a440d2ca3e269674bb6419c7e50a15f MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2010, 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\Connections;
  10. use lithium\data\model\Query;
  11. use lithium\data\entity\Record;
  12. use lithium\tests\mocks\data\MockPostObject;
  13. use lithium\tests\mocks\data\model\MockDatabase;
  14. use lithium\tests\mocks\data\model\MockQueryPost;
  15. use lithium\tests\mocks\data\model\MockQueryComment;
  16. class QueryTest extends \lithium\test\Unit {
  17. protected $_model = 'lithium\tests\mocks\data\model\MockQueryPost';
  18. protected $_configs = array();
  19. protected $_queryArr = array(
  20. 'model' => 'lithium\tests\mocks\data\model\MockQueryPost',
  21. 'type' => 'read',
  22. 'order' => 'created DESC',
  23. 'limit' => 10,
  24. 'page' => 1,
  25. 'fields' => array('id', 'author_id', 'title'),
  26. 'conditions' => array('author_id' => 12),
  27. 'comment' => 'Find all posts by author 12'
  28. );
  29. public function setUp() {
  30. $this->db = new MockDatabase();
  31. $this->_configs = Connections::config();
  32. Connections::reset();
  33. Connections::config(array('mock-database-connection' => array(
  34. 'object' => &$this->db,
  35. 'adapter' => 'MockDatabase'
  36. )));
  37. MockQueryPost::config();
  38. MockQueryComment::config();
  39. }
  40. public function tearDown() {
  41. Connections::reset();
  42. Connections::config($this->_configs);
  43. }
  44. /**
  45. * Tests that configuration settings are delegating to matching method names
  46. *
  47. * @return void
  48. */
  49. public function testObjectConstruction() {
  50. $query = new Query();
  51. $this->assertFalse($query->conditions());
  52. $query = new Query(array('conditions' => 'foo', 'fields' => array('id')));
  53. $this->assertEqual($query->conditions(), array('foo'));
  54. }
  55. public function testModel() {
  56. $query = new Query($this->_queryArr);
  57. $this->assertEqual($this->_model, $query->model());
  58. $query->model('lithium\tests\mocks\data\model\MockQueryComment');
  59. $expected = 'lithium\tests\mocks\data\model\MockQueryComment';
  60. $result = $query->model();
  61. $this->assertEqual($expected, $result);
  62. }
  63. public function testFields() {
  64. $query = new Query($this->_queryArr);
  65. $expected = array('id','author_id','title');
  66. $result = $query->fields();
  67. $this->assertEqual($expected, $result);
  68. $query->fields('content');
  69. $expected = array('id','author_id','title','content');
  70. $result = $query->fields();
  71. $this->assertEqual($expected, $result);
  72. $query->fields(array('updated','created'));
  73. $expected = array('id','author_id','title','content','updated','created');
  74. $result = $query->fields();
  75. $this->assertEqual($expected, $result);
  76. $query->fields(false);
  77. $query->fields(array('id', 'title'));
  78. $expected = array('id','title');
  79. $result = $query->fields();
  80. $this->assertEqual($expected, $result);
  81. }
  82. public function testLimit() {
  83. $query = new Query($this->_queryArr);
  84. $expected = 10;
  85. $result = $query->limit();
  86. $this->assertEqual($expected, $result);
  87. $query->limit(5);
  88. $expected = 5;
  89. $result = $query->limit();
  90. $this->assertEqual($expected, $result);
  91. }
  92. public function testPage() {
  93. $query = new Query($this->_queryArr);
  94. $expected = 1;
  95. $result = $query->page();
  96. $this->assertEqual($expected, $result);
  97. $query->page(5);
  98. $expected = 5;
  99. $result = $query->page();
  100. $this->assertEqual($expected, $result);
  101. }
  102. public function testOrder() {
  103. $query = new Query($this->_queryArr);
  104. $expected = 'created DESC';
  105. $result = $query->order();
  106. $this->assertEqual($expected, $result);
  107. $query->order('updated ASC');
  108. $expected = 'updated ASC';
  109. $result = $query->order();
  110. $this->assertEqual($expected, $result);
  111. }
  112. public function testRecord() {
  113. $query = new Query($this->_queryArr);
  114. $result = $query->entity();
  115. $this->assertNull($result);
  116. $record = (object) array('id' => 12);
  117. $record->title = 'Lorem Ipsum';
  118. $query->entity($record);
  119. $query_record = $query->entity();
  120. $expected = 12;
  121. $result = $query_record->id;
  122. $this->assertEqual($expected, $result);
  123. $expected = 'Lorem Ipsum';
  124. $result = $query_record->title;
  125. $this->assertEqual($expected, $result);
  126. $this->assertTrue($record == $query->entity());
  127. }
  128. public function testComment() {
  129. $query = new Query($this->_queryArr);
  130. $expected = 'Find all posts by author 12';
  131. $result = $query->comment();
  132. $this->assertEqual($expected, $result);
  133. $query->comment('Comment lorem');
  134. $expected = 'Comment lorem';
  135. $result = $query->comment();
  136. $this->assertEqual($expected, $result);
  137. }
  138. public function testData() {
  139. $query = new Query($this->_queryArr);
  140. $expected = array();
  141. $result = $query->data();
  142. $this->assertEqual($expected, $result);
  143. $record = new Record();
  144. $record->id = 12;
  145. $record->title = 'Lorem Ipsum';
  146. $query->entity($record);
  147. $expected = array('id' => 12, 'title' => 'Lorem Ipsum');
  148. $result = $query->data();
  149. $this->assertEqual($expected, $result);
  150. $query->data(array('id' => 35, 'title' => 'Nix', 'body' => 'Prix'));
  151. $expected = array('id' => 35, 'title' => 'Nix', 'body' => 'Prix');
  152. $result = $query->data();
  153. $this->assertEqual($expected, $result);
  154. }
  155. public function testConditions() {
  156. $query = new Query($this->_queryArr);
  157. $expected = array('author_id' => 12);
  158. $result = $query->conditions();
  159. $this->assertEqual($expected, $result);
  160. $query->conditions(array('author_id' => 13, 'title LIKE' => 'Lorem%'));
  161. $expected = array('author_id' => 13, 'title LIKE' => 'Lorem%');
  162. $result = $query->conditions();
  163. $this->assertEqual($expected, $result);
  164. }
  165. public function testConditionFromRecord() {
  166. $entity = new Record();
  167. $entity->id = 12;
  168. $query = new Query(compact('entity') + array(
  169. 'model' => $this->_model,
  170. ));
  171. $expected = array('id' => 12);
  172. $result = $query->conditions();
  173. $this->assertEqual($expected, $result);
  174. }
  175. public function testExtra() {
  176. $object = new MockPostObject(array('id' => 1, 'data' => 'test'));
  177. $query = new Query(array(
  178. 'conditions' => 'foo', 'extra' => 'value', 'extraObject' => $object
  179. ));
  180. $this->assertEqual(array('foo'), $query->conditions());
  181. $this->assertEqual('value', $query->extra());
  182. $this->assertEqual($object, $query->extraObject());
  183. $this->assertNull($query->extra2());
  184. }
  185. public function testExport() {
  186. $query = new Query($this->_queryArr);
  187. $ds = new MockDatabase();
  188. $export = $query->export($ds);
  189. $this->assertTrue(is_array($export));
  190. $this->skipIf(!is_array($export), 'Query::export() does not return an array');
  191. $expected = array(
  192. 'alias',
  193. 'calculate',
  194. 'comment',
  195. 'conditions',
  196. 'data',
  197. 'fields',
  198. 'group',
  199. 'joins',
  200. 'limit',
  201. 'map',
  202. 'model',
  203. 'name',
  204. 'offset',
  205. 'order',
  206. 'page',
  207. 'source',
  208. 'type',
  209. 'whitelist'
  210. );
  211. $result = array_keys($export);
  212. sort($expected);
  213. sort($result);
  214. $this->assertEqual($expected, $result);
  215. $expected = 'id, author_id, title';
  216. $result = $export['fields'];
  217. $this->assertEqual($expected, $result);
  218. $expected = MockQueryPost::meta('source');
  219. $result = $export['source'];
  220. $this->assertEqual("{{$expected}}", $result);
  221. }
  222. public function testPagination() {
  223. $query = new Query(array('limit' => 5, 'page' => 1));
  224. $this->assertEqual(0, $query->offset());
  225. $query = new Query(array('limit' => 5, 'page' => 2));
  226. $this->assertEqual(5, $query->offset());
  227. $query->page(1);
  228. $this->assertEqual(0, $query->offset());
  229. }
  230. public function testJoin() {
  231. $query = new Query(array('joins' => array(array('foo' => 'bar'))));
  232. $query->join(array('bar' => 'baz'));
  233. $expected = array(array('foo' => 'bar'), array('bar' => 'baz'));
  234. $this->assertEqual($expected, $query->join());
  235. $query->join('zim', array('dib' => 'gir'));
  236. $expected = array(
  237. array('foo' => 'bar'),
  238. array('bar' => 'baz'),
  239. 'zim' => array('dib' => 'gir')
  240. );
  241. $this->assertEqual($expected, $query->join());
  242. }
  243. /**
  244. * Tests that assigning a whitelist to a query properly restricts the list of data fields that
  245. * the query exposes.
  246. *
  247. * @return void
  248. */
  249. public function testWhitelisting() {
  250. $data = array('foo' => 1, 'bar' => 2, 'baz' => 3);
  251. $query = new Query(compact('data'));
  252. $this->assertEqual($data, $query->data());
  253. $query = new Query(compact('data') + array('whitelist' => array('foo', 'bar')));
  254. $this->assertEqual(array('foo' => 1, 'bar' => 2), $query->data());
  255. }
  256. /**
  257. * Tests basic property accessors and mutators.
  258. *
  259. * @return void
  260. */
  261. public function testBasicAssignments() {
  262. $query = new Query();
  263. $group = array('key' => 'hits', 'reduce' => 'function() {}');
  264. $calculate = 'count';
  265. $this->assertNull($query->group());
  266. $query->group($group);
  267. $this->assertEqual($group, $query->group());
  268. $this->assertNull($query->calculate());
  269. $query->calculate($calculate);
  270. $this->assertEqual($calculate, $query->calculate());
  271. $query = new Query(compact('calculate', 'group'));
  272. $this->assertEqual($group, $query->group());
  273. $this->assertEqual($calculate, $query->calculate());
  274. }
  275. public function testInstantiationWithConditionsAndData() {
  276. $options = array(
  277. 'type' => 'update',
  278. 'data' => array('title' => '..'),
  279. 'conditions' => array('title' => 'FML'),
  280. 'model' => 'lithium\tests\mocks\data\model\MockQueryPost'
  281. );
  282. $query = new Query($options);
  283. $result = $query->export(Connections::get('mock-database-connection'));
  284. $this->assertEqual(array('title' => '..'), $result['data']);
  285. $this->assertEqual("WHERE title = 'FML'", $result['conditions']);
  286. }
  287. public function testEntityConditions() {
  288. $entity = new Record(array('model' => $this->_model, 'exists' => true));
  289. $entity->id = 13;
  290. $query = new Query(compact('entity'));
  291. $this->assertEqual(array('id' => 13), $query->conditions());
  292. }
  293. public function testAutomaticAliasing() {
  294. $query = new Query(array('model' => $this->_model));
  295. $this->assertEqual('MockQueryPost', $query->alias());
  296. }
  297. public function testFluentInterface() {
  298. $query = new Query();
  299. $conditions = array('foo' => 'bar');
  300. $fields = array('foo', 'bar', 'baz', 'created');
  301. $order = array('created' => 'ASC');
  302. $result = $query->conditions($conditions)->fields($fields)->order($order);
  303. $this->assertEqual($result, $query);
  304. $this->assertEqual($conditions, $query->conditions());
  305. $this->assertEqual($fields, $query->fields());
  306. $this->assertEqual($order, $query->order());
  307. }
  308. }
  309. ?>