sql.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. include_once 'config.php';
  3. __p::load_plugin('sql');
  4. class pQuerySqlTest extends PHPUnit_Framework_TestCase {
  5. function setUp() {
  6. __sql::set_login_data('localhost', 'root', '', 'pquery_test');
  7. }
  8. function tearDown() {
  9. __sql::disconnect();
  10. __sql::$login_data = array();
  11. }
  12. function test_set_login_data() {
  13. $this->tearDown();
  14. __sql::set_login_data('a', 'b', 'c', 'd');
  15. $this->assertEquals('a', __sql::$login_data['host']);
  16. $this->assertEquals('b', __sql::$login_data['username']);
  17. $this->assertEquals('c', __sql::$login_data['password']);
  18. $this->assertEquals('d', __sql::$login_data['dbname']);
  19. }
  20. /**
  21. * @expectedException pQueryException
  22. */
  23. function test_no_login_data() {
  24. $this->tearDown();
  25. __sql::assert_login_data_exist();
  26. }
  27. function test_query_getter() {
  28. $sql = _sql('foobar');
  29. $this->assertEquals('foobar', $sql->variable);
  30. $this->assertEquals('foobar', $sql->query);
  31. }
  32. function test_variable_query() {
  33. $sql = _sql("select id from foo where bar = '[bar]'")
  34. ->set(array('bar' => 'test1'));
  35. $this->assertEquals("select id from foo where bar = 'test1'", $sql->query);
  36. }
  37. function test_unescaped_query() {
  38. $sql = _sql("select id from foo where bar = '[bar]'")
  39. ->set_unescaped(array('bar' => "select id from foo where bar = 'test1'"));
  40. $this->assertEquals("select id from foo where bar = 'select id from foo where bar = 'test1''", $sql->query);
  41. }
  42. function test_escaped_query() {
  43. $sql = _sql("select id from foo where bar = '[bar]'")
  44. ->set(array('bar' => "select id from foo where bar = 'test1'"));
  45. $this->assertNotEquals("select id from foo where bar = 'select id from foo where bar = 'test1''", $sql->query);
  46. }
  47. function test_constructor() {
  48. $this->assertInstanceOf('pQuerySql', _sql("foo"), 'constructor does not return pQuerySql object');
  49. }
  50. function test_constructor_simple() {
  51. $sql = _sql("select id from foo where bar = '[0]'", 'test1');
  52. $this->assertEquals("select id from foo where bar = 'test1'", $sql->query);
  53. }
  54. function test_constructor_advanced() {
  55. $sql = _sql("[0] [bar] [foo] [2]", '1', array('bar' => '2', 'foo' => '3'), '4');
  56. $this->assertEquals("1 2 3 4", $sql->query);
  57. }
  58. function test_num_rows() {
  59. $sql = _sql("select bar from foo where id in (1, 2)");
  60. $this->assertEquals(2, $sql->num_rows());
  61. }
  62. function test_escape_column_simple() {
  63. $this->assertEquals('`foo`', __sql::escape_column('foo'));
  64. }
  65. function test_escape_column_escaped() {
  66. $this->assertEquals('`foo`', __sql::escape_column('`foo`'));
  67. }
  68. function test_escape_column_table() {
  69. $this->assertEquals('`foo`.`bar`', __sql::escape_column('foo.bar'));
  70. }
  71. function test_escape_column_aggregate() {
  72. $this->assertEquals('COUNT(`foo`)', __sql::escape_column('count(foo)'));
  73. }
  74. function test_escape_column_aggregate_escaped() {
  75. $this->assertEquals('COUNT(`foo`)', __sql::escape_column('count(`foo`)'));
  76. }
  77. function test_escape_value() {
  78. $this->assertEquals("'foo'", __sql::escape_value("foo"));
  79. }
  80. function test_escape_value_escaped() {
  81. $this->assertEquals("'foo'", __sql::escape_value("'foo'"));
  82. }
  83. function test_parse_columns_star() {
  84. $sql = __sql::select('foo', '*', '', false);
  85. $this->assertEquals("SELECT * FROM `foo` WHERE 1;", $sql->query);
  86. }
  87. function test_parse_columns_simple() {
  88. $sql = __sql::select('foo', array('id', 'bar'), '', false);
  89. $this->assertEquals("SELECT `id`, `bar` FROM `foo` WHERE 1;", $sql->query);
  90. }
  91. function test_parse_columns_as() {
  92. $sql = __sql::select('foo', array('id' => 'foo_id'), '', false);
  93. $this->assertEquals("SELECT `id` AS `foo_id` FROM `foo` WHERE 1;", $sql->query);
  94. }
  95. function test_parse_constraints_empty() {
  96. $this->assertSame("1", __sql::parse_constraints(null, false));
  97. }
  98. function test_parse_constraints_string() {
  99. $constraints = "foo LIKE '%bar%'";
  100. $this->assertEquals($constraints, __sql::parse_constraints($constraints, false));
  101. }
  102. function test_parse_constraints_simple() {
  103. $this->assertEquals("`id` = '1' AND `bar` = 'test1'",
  104. __sql::parse_constraints(array('id' => 1, 'bar' => 'test1'), false));
  105. }
  106. function test_parse_constraints_value_list() {
  107. $this->assertEquals("`id` IN ('1', '2', '3')",
  108. __sql::parse_constraints(array('id' => range(1, 3)), false));
  109. }
  110. function test_select_query() {
  111. $sql = __sql::select('foo', '*', array('bar' => 'test1'), false);
  112. $this->assertEquals("SELECT * FROM `foo` WHERE `bar` = 'test1';", $sql->query);
  113. }
  114. function test_update_query() {
  115. $sql = __sql::update('foo', array('bar' => 'test4'), array('id' => 1), false);
  116. $this->assertEquals("UPDATE `foo` SET `bar` = 'test4' WHERE `id` = '1';", $sql->query);
  117. }
  118. function test_insert_query() {
  119. $sql = __sql::insert_row('foo', array('bar' => 'test3'), false);
  120. $this->assertEquals("INSERT INTO `foo`(`bar`) VALUES('test3');", $sql->query);
  121. }
  122. function test_delete_query() {
  123. $sql = __sql::delete('foo', array('bar' => 'test3'), false);
  124. $this->assertEquals("DELETE FROM `foo` WHERE `bar` = 'test3';", $sql->query);
  125. }
  126. function test_fetch_assoc() {
  127. $sql = _sql("select bar from foo where id = 1");
  128. $result = $sql->fetch('assoc');
  129. $this->assertEquals(array('bar' => 'test1'), $result);
  130. $this->assertFalse($sql->fetch());
  131. }
  132. function test_fetch_array() {
  133. $sql = _sql("select bar from foo where id = 1");
  134. $result = $sql->fetch('array');
  135. $this->assertEquals(array(0 => 'test1', 'bar' => 'test1'), $result);
  136. $this->assertFalse($sql->fetch());
  137. }
  138. function test_fetch_object() {
  139. $sql = _sql("select bar from foo where id = 1");
  140. $result = $sql->fetch('object');
  141. $this->assertEquals('test1', $result->bar);
  142. $this->assertFalse($sql->fetch());
  143. }
  144. /**
  145. * @depends test_fetch_assoc
  146. */
  147. function test_fetch_all() {
  148. $sql = _sql("select bar from foo where id = 1");
  149. $results = $sql->fetch_all('assoc');
  150. $this->assertEquals(array(array('bar' => 'test1')), $results);
  151. }
  152. /*
  153. * @depends test_select_query
  154. * @depends test_fetch_assoc
  155. */
  156. function test_select_all() {
  157. $select = __sql::select('foo', '*', array('bar' => 'test1'))->execute();
  158. $result = $select->fetch('assoc');
  159. $this->assertEquals(array('id' => 1, 'bar' => 'test1'), $result);
  160. }
  161. /*
  162. * @depends test_select_query
  163. * @depends test_fetch_assoc
  164. */
  165. function test_select_single() {
  166. $select = __sql::select('foo', 'bar', array('bar' => 'test1'))->execute();
  167. $result = $select->fetch('assoc');
  168. $this->assertEquals(array('bar' => 'test1'), $result);
  169. }
  170. /*
  171. * @depends test_select_query
  172. * @depends test_fetch_assoc
  173. */
  174. function test_select_multiple() {
  175. $select = __sql::select('foo', array('id', 'bar'), array('bar' => 'test1'))->execute();
  176. $result = $select->fetch('assoc');
  177. $this->assertEquals(array('id' => 1, 'bar' => 'test1'), $result);
  178. }
  179. /**
  180. * @depends test_update_query
  181. */
  182. function test_update() {
  183. $update = __sql::update('foo', array('bar' => 'test1'),
  184. array('id' => 1), false)->execute();
  185. $this->assertTrue($update->result);
  186. }
  187. /**
  188. * @depends test_insert_query
  189. */
  190. function test_insert() {
  191. $insert = __sql::insert_row('foo', array('bar' => 'test3'))->execute();
  192. $this->assertTrue($insert->result);
  193. $this->assertEquals(1, $insert->affected_rows());
  194. }
  195. /**
  196. * @depends test_delete_query
  197. * @depends test_insert
  198. */
  199. function test_delete() {
  200. $delete = __sql::delete('foo', array('bar' => 'test3'))->execute();
  201. $this->assertTrue($delete->result);
  202. $this->assertEquals(1, $delete->affected_rows());
  203. }
  204. }
  205. ?>