sql.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. /**
  127. * @depends test_select_query
  128. */
  129. function test_select() {
  130. $sql = _sql("select bar from foo where id = 1");
  131. $result = $sql->fetch('object');
  132. $this->assertEquals('test1', $result->bar);
  133. $this->assertFalse($sql->fetch());
  134. }
  135. /**
  136. * @depends test_update_query
  137. */
  138. function test_update() {
  139. $update = __sql::update('foo', array('bar' => 'test1'),
  140. array('id' => 1), false)->execute();
  141. $this->assertTrue($update->result);
  142. }
  143. /**
  144. * @depends test_insert_query
  145. */
  146. function test_insert() {
  147. $insert = __sql::insert_row('foo', array('bar' => 'test3'))->execute();
  148. $this->assertTrue($insert->result);
  149. }
  150. /**
  151. * @depends test_delete_query
  152. * @depends test_insert
  153. */
  154. function test_delete() {
  155. $delete = __sql::delete('foo', array('bar' => 'test3'))->execute();
  156. $this->assertTrue($delete->result);
  157. $this->assertEquals($delete->affected_rows());
  158. }
  159. }
  160. ?>