sql.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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(__sql::$login_data['host'], 'a');
  16. $this->assertEquals(__sql::$login_data['username'], 'b');
  17. $this->assertEquals(__sql::$login_data['password'], 'c');
  18. $this->assertEquals(__sql::$login_data['dbname'], 'd');
  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($sql->variable, 'foobar');
  30. $this->assertEquals($sql->query, 'foobar');
  31. }
  32. function test_variable_query() {
  33. $sql = _sql("select id from foo where bar = '[bar]'")
  34. ->set(array('bar' => 'test1'));
  35. $this->assertEquals($sql->query, "select id from foo where bar = 'test1'");
  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($sql->query, "select id from foo where bar = 'select id from foo where bar = 'test1''");
  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($sql->query, "select id from foo where bar = 'select id from foo where bar = 'test1''");
  46. }
  47. function test_constructor_simple() {
  48. $sql = _sql("select id from foo where bar = '[0]'", 'test1');
  49. $this->assertEquals($sql->query, "select id from foo where bar = 'test1'");
  50. }
  51. function test_constructor_advanced() {
  52. $sql = _sql("[0] [bar] [foo] [2]", '1', array('bar' => '2', 'foo' => '3'), '4');
  53. $this->assertEquals($sql->query, "1 2 3 4");
  54. }
  55. function test_num_rows() {
  56. $sql = _sql("select bar from foo where id in (1, 2)");
  57. $this->assertEquals($sql->num_rows(), 2);
  58. }
  59. function test_escape_column_simple() {
  60. $this->assertEquals(__sql::escape_column('foo'), '`foo`');
  61. }
  62. function test_escape_column_escaped() {
  63. $this->assertEquals(__sql::escape_column('`foo`'), '`foo`');
  64. }
  65. function test_escape_column_table() {
  66. $this->assertEquals(__sql::escape_column('foo.bar'), '`foo`.`bar`');
  67. }
  68. function test_escape_column_aggregate() {
  69. $this->assertEquals(__sql::escape_column('count(foo)'), 'COUNT(`foo`)');
  70. }
  71. function test_escape_column_aggregate_escaped() {
  72. $this->assertEquals(__sql::escape_column('count(`foo`)'), 'COUNT(`foo`)');
  73. }
  74. function test_escape_value() {
  75. $this->assertEquals("'foo'", __sql::escape_value("foo"));
  76. }
  77. function test_escape_value_escaped() {
  78. $this->assertEquals("'foo'", __sql::escape_value("'foo'"));
  79. }
  80. function test_parse_columns_star() {
  81. $sql = __sql::select('foo', '*', '', false);
  82. $this->assertEquals($sql->query, "SELECT * FROM `foo` WHERE 1;");
  83. }
  84. function test_parse_columns_simple() {
  85. $sql = __sql::select('foo', array('id', 'bar'), '', false);
  86. $this->assertEquals($sql->query, "SELECT `id`, `bar` FROM `foo` WHERE 1;");
  87. }
  88. function test_parse_columns_as() {
  89. $sql = __sql::select('foo', array('id' => 'foo_id'), '', false);
  90. $this->assertEquals($sql->query, "SELECT `id` AS `foo_id` FROM `foo` WHERE 1;");
  91. }
  92. function test_parse_constraints_empty() {
  93. $this->assertSame(__sql::parse_constraints(null, false), "1");
  94. }
  95. function test_parse_constraints_string() {
  96. $constraints = "foo LIKE '%bar%'";
  97. $this->assertEquals(__sql::parse_constraints($constraints, false), $constraints);
  98. }
  99. function test_parse_constraints_simple() {
  100. $this->assertEquals(__sql::parse_constraints(
  101. array('id' => 1, 'bar' => 'test1'), false),
  102. "`id` = '1' AND `bar` = 'test1'");
  103. }
  104. function test_parse_constraints_value_list() {
  105. $this->assertEquals(__sql::parse_constraints(
  106. array('id' => range(1, 3)), false),
  107. "`id` IN ('1', '2', '3')");
  108. }
  109. function test_select_query() {
  110. $sql = __sql::select('foo', '*', array('bar' => 'test1'), false);
  111. $this->assertEquals($sql->query, "SELECT * FROM `foo` WHERE `bar` = 'test1';");
  112. }
  113. function test_update_query() {
  114. $sql = __sql::update('foo', array('bar' => 'test4'), array('id' => 1), false);
  115. $this->assertEquals($sql->query, "UPDATE `foo` SET `bar` = 'test4' WHERE `id` = '1';");
  116. }
  117. function test_insert_query() {
  118. $sql = __sql::insert_row('foo', array('bar' => 'test3'), false);
  119. $this->assertEquals($sql->query, "INSERT INTO `foo`(`bar`) VALUES('test3');");
  120. }
  121. function test_delete_query() {
  122. $sql = __sql::delete('foo', array('bar' => 'test3'), false);
  123. $this->assertEquals($sql->query, "DELETE FROM `foo` WHERE `bar` = 'test3';");
  124. }
  125. /**
  126. * @depends test_select_query
  127. */
  128. function test_select() {
  129. $sql = _sql("select bar from foo where id = 1");
  130. $result = $sql->fetch('object');
  131. $this->assertEquals('test1', $result->bar);
  132. $this->assertSame($sql->fetch(), false);
  133. }
  134. /**
  135. * @depends test_update_query
  136. */
  137. function test_update() {
  138. $update = __sql::update('foo', array('bar' => 'test1'),
  139. array('id' => 1), false)->execute();
  140. // Do not continue unless the value has been updated
  141. $this->assertSame($update->result, true);
  142. }
  143. /**
  144. * @depends test_insert_query
  145. */
  146. function test_insert() {
  147. $insert = __sql::insert_row('foo', array('bar' => 'test3'))->execute();
  148. $this->assertSame($insert->result, true);
  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->assertSame($delete->result, true);
  157. }
  158. }
  159. ?>