queryTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * Generated by PHPUnit_SkeletonGenerator 1.2.0 on 2013-02-01 at 06:23:13.
  4. */
  5. class Query_Mysql_DriverTest extends PHPUnit_Framework_TestCase
  6. {
  7. /**
  8. * @var Query_Mysql_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\Mysql\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_Mysql_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_Mysql_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_Mysql_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_Mysql_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_Mysql_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_Mysql_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_Mysql_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_Mysql_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_Mysql_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_Mysql_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. }