queryTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /**
  3. * Generated by PHPUnit_SkeletonGenerator 1.2.0 on 2013-02-01 at 06:23:13.
  4. */
  5. class PDO_Query_Test extends PHPUnit_Framework_TestCase
  6. {
  7. /**
  8. * @var Query_PDO_Driver
  9. */
  10. protected $object;
  11. /**
  12. * Sets up the fixture, for example, opens a network connection.
  13. * This method is called before a test is executed.
  14. */
  15. protected function setUp()
  16. {
  17. $stub = (object) array('db_type' => 'mysql');
  18. $this->object = new \PHPixie\DB\PDO\Query($stub, 'select');
  19. }
  20. /**
  21. * Tears down the fixture, for example, closes a network connection.
  22. * This method is called after a test is executed.
  23. */
  24. protected function tearDown()
  25. {
  26. }
  27. /**
  28. * @covers Query_PDO_Driver::escape_field
  29. * @todo Implement testEscape_field().
  30. */
  31. public function testEscape_field()
  32. {
  33. $this->object->add_alias();
  34. $this->assertEquals('test', $this->object->escape_field(new \PHPixie\DB\Expression('test')));
  35. $this->assertEquals('`a0`.*', $this->object->escape_field('*'));
  36. $this->assertEquals('`a0`.`test`', $this->object->escape_field('test'));
  37. $this->assertEquals('`test`.`test`', $this->object->escape_field('test.test'));
  38. }
  39. /**
  40. * @covers Query_PDO_Driver::escape_value
  41. * @todo Implement testEscape_value().
  42. */
  43. public function testEscape_value()
  44. {
  45. $params = array();
  46. $this->assertEquals('test', $this->object->escape_value(new \PHPixie\DB\Expression('test'), $params));
  47. $this->assertEquals('?', $this->object->escape_value('korova', $params));
  48. $this->assertArrayHasKey(0, $params);
  49. $this->assertEquals('korova', $params[0]);
  50. }
  51. /**
  52. * @covers Query_PDO_Driver::query
  53. * @todo Implement testQuery().
  54. */
  55. public function testQuerySelect1()
  56. {
  57. $query = $this->object
  58. ->table('fairies')
  59. ->where('a', 7)
  60. ->where('b', '<', 8)
  61. ->where('c', '>', 3)
  62. ->where('or', array('d', '>', 11))
  63. ->where(array(
  64. array('e', 9),
  65. array('or', array(
  66. array('f', 10),
  67. array('g', 11),
  68. )),
  69. array('or', array(
  70. array('h', 12),
  71. array('or', array('i', 13)),
  72. ))
  73. ))
  74. ->order_by('id', 'desc')
  75. ->group_by('id')
  76. ->having('j', '<', new \PHPixie\DB\Expression('korova'))
  77. ->having('or', array('l', '>', 11))
  78. ->having(array(
  79. array('m', 9),
  80. array('or', array(
  81. array('n', 10),
  82. array('o', 11),
  83. ))
  84. ))
  85. ->limit(5)
  86. ->offset(6)
  87. ->query();
  88. $this->assertEquals("SELECT * FROM `fairies` WHERE `fairies`.`a` = ? AND `fairies`.`b` < ? AND `fairies`.`c` > ? OR `fairies`.`d` > ? AND ( `fairies`.`e` = ? OR ( `fairies`.`f` = ? AND `fairies`.`g` = ? ) OR ( `fairies`.`h` = ? OR `fairies`.`i` = ? ) ) GROUP BY `fairies`.`id` HAVING `fairies`.`j` < korova OR `fairies`.`l` > ? AND ( `fairies`.`m` = ? OR ( `fairies`.`n` = ? AND `fairies`.`o` = ? ) ) ORDER BY `fairies`.`id` DESC LIMIT 5 OFFSET 6 ", current($query));
  89. }
  90. /**
  91. * @covers Query_PDO_Driver::query
  92. * @todo Implement testQuery().
  93. */
  94. public function testQuerySelect2()
  95. {
  96. $query = $this->object
  97. ->table('fairies')
  98. ->where('a', 7)
  99. ->join('test', array('fairies.test_id', 'test.id'))
  100. ->join('test2', array(
  101. array('fairies.test2_id', 'test.test_id'),
  102. array('fairies.test3_id', 'test.id')
  103. ), 'inner')
  104. ->order_by('id', 'desc')
  105. ->query();
  106. $this->assertEquals("SELECT * FROM `fairies` LEFT JOIN `test` ON `fairies`.`test_id` = `test`.`id` INNER JOIN `test2` ON ( `fairies`.`test2_id` = `test`.`test_id` AND `fairies`.`test3_id` = `test`.`id` ) WHERE `fairies`.`a` = ? ORDER BY `fairies`.`id` DESC ", current($query));
  107. }
  108. /**
  109. * @covers Query_PDO_Driver::query
  110. * @todo Implement testQuery().
  111. */
  112. public function testQueryDelete()
  113. {
  114. $query = $this->object
  115. ->type('delete')
  116. ->table('fairies')
  117. ->where('id', 1)
  118. ->query();
  119. $this->assertEquals("DELETE fairies.* FROM `fairies` WHERE `fairies`.`id` = ? ", current($query));
  120. }
  121. /**
  122. * @covers Query_PDO_Driver::query
  123. * @todo Implement testQuery().
  124. */
  125. public function testQueryInsert()
  126. {
  127. $query = $this->object
  128. ->type('insert')
  129. ->table('fairies')
  130. ->data(array('id' => 1, 'name' => 'Trixie'))
  131. ->query();
  132. $this->assertEquals("INSERT INTO `fairies` (`id`, `name`) VALUES(?, ?)", current($query));
  133. }
  134. /**
  135. * @covers Query_PDO_Driver::query
  136. * @todo Implement testQuery().
  137. */
  138. public function testQueryUpdate()
  139. {
  140. $query = $this->object
  141. ->type('update')
  142. ->table('fairies')
  143. ->data(array('id' => 1, 'name' => 'Trixie'))
  144. ->query();
  145. $this->assertEquals("UPDATE `fairies` SET `id` = ?, `name` = ? ", current($query));
  146. }
  147. /**
  148. * @covers Query_PDO_Driver::query
  149. * @todo Implement testQuery().
  150. */
  151. public function testQueryCount()
  152. {
  153. $query = $this->object
  154. ->type('count')
  155. ->table('fairies')
  156. ->where('id', 8)
  157. ->query();
  158. $this->assertEquals("SELECT COUNT(*) as `count` FROM `fairies` WHERE `fairies`.`id` = ? ", current($query));
  159. }
  160. /**
  161. * @covers Query_PDO_Driver::add_alias
  162. * @todo Implement testQuery().
  163. */
  164. public function testAlias()
  165. {
  166. $this->object->table('fairies');
  167. $this->assertEquals('fairies', $this->object->last_alias());
  168. $this->object->add_alias();
  169. $this->assertEquals('a0', $this->object->last_alias());
  170. }
  171. /**
  172. * @covers Query_PDO_Driver::__call
  173. * @todo Implement testQuery().
  174. */
  175. public function testCall()
  176. {
  177. $this->object->table('fairies');
  178. $this->assertEquals('fairies', $this->object->table());
  179. $except = false;
  180. try {
  181. $this->object->limit('fairies');
  182. } catch (Exception $e) {
  183. $except = true;
  184. }
  185. $this->assertEquals(true, $except);
  186. }
  187. public function testUnion()
  188. {
  189. $stub = (object) array('db_type' => 'mysql');
  190. $subquery = new \PHPixie\DB\PDO\Query($stub, 'select');
  191. $subquery->table('fairies');
  192. $this->object->table('fairies');
  193. $this->object->where('id', '>', 7);
  194. $this->object->union($subquery);
  195. $this->object->union($subquery);
  196. $this->assertEquals('(SELECT * FROM `fairies` WHERE `fairies`.`id` > ? ) UNION ALL (SELECT * FROM `fairies` ) UNION ALL (SELECT * FROM `fairies` ) ', current($this->object->query()));
  197. }
  198. public function testSubselect()
  199. {
  200. $stub = (object) array('db_type' => 'mysql');
  201. $subquery = new \PHPixie\DB\PDO\Query($stub, 'select');
  202. $subquery->fields('id')->table('fairies');
  203. $this->object->table('fairies')->where('id', 7)->where('id', 'in', $subquery);
  204. $this->assertEquals('SELECT * FROM `fairies` WHERE `fairies`.`id` = ? AND `fairies`.`id` in (SELECT `fairies`.`id` FROM `fairies` ) ', current($this->object->query()));
  205. }
  206. public function testSubtable()
  207. {
  208. $stub = (object) array('db_type' => 'mysql');
  209. $subquery = new \PHPixie\DB\PDO\Query($stub, 'select');
  210. $subquery->fields('id')->table('fairies');
  211. $this->object->table($subquery)->where('id', 7);
  212. $this->assertEquals('SELECT * FROM (SELECT `fairies`.`id` FROM `fairies` ) AS a0 WHERE `a0`.`id` = ? ', current($this->object->query()));
  213. }
  214. public function testJoinSubtable()
  215. {
  216. $stub = (object) array('db_type' => 'mysql');
  217. $subquery = new \PHPixie\DB\PDO\Query($stub, 'select');
  218. $subquery->table('fairies');
  219. $this->object->table($subquery);
  220. $this->object->join('pixies', array('fairies.id', '=', 'pixie.id'));
  221. $this->object->join(array('fairies', 'fae'), array('fairies.id', '=', 'fae.id'));
  222. $this->object->join(array($subquery, 'fae2'), array('fairies.id', '=', 'fae2.id'));
  223. $this->assertEquals('SELECT * FROM (SELECT * FROM `fairies` ) AS a0 LEFT JOIN `pixies` ON `fairies`.`id` = `pixie`.`id` LEFT JOIN `fairies` AS fae ON `fairies`.`id` = `fae`.`id` LEFT JOIN (SELECT * FROM `fairies` ) AS fae2 ON `fairies`.`id` = `fae2`.`id` ', current($this->object->query()));
  224. }
  225. public function testExpressionSelect()
  226. {
  227. $this->object->table('fairies')->where('id', 'in', new \PHPixie\DB\Expression("(SELECT id from fairies)"));
  228. $this->assertEquals('SELECT * FROM `fairies` WHERE `fairies`.`id` in (SELECT id from fairies) ', current($this->object->query()));
  229. }
  230. public function testExpressionQuery()
  231. {
  232. $this->object->table('fairies')->where(new \PHPixie\DB\Expression('fairies.id'), 5);
  233. $this->assertEquals('SELECT * FROM `fairies` WHERE fairies.id = ? ', current($this->object->query()));
  234. }
  235. }