sql.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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_parse_columns_star() {
  75. $sql = __sql::select('foo', '*', '', false);
  76. $this->assertEquals($sql->query, "SELECT * FROM `foo` WHERE 1;");
  77. }
  78. function test_parse_columns_simple() {
  79. $sql = __sql::select('foo', array('id', 'bar'), '', false);
  80. $this->assertEquals($sql->query, "SELECT `id`, `bar` FROM `foo` WHERE 1;");
  81. }
  82. function test_parse_columns_as() {
  83. $sql = __sql::select('foo', array('id' => 'foo_id'), '', false);
  84. $this->assertEquals($sql->query, "SELECT `id` AS `foo_id` FROM `foo` WHERE 1;");
  85. }
  86. function test_parse_constraints_empty() {
  87. $this->assertSame(__sql::parse_constraints(null, false), "1");
  88. }
  89. function test_parse_constraints_string() {
  90. $constraints = "foo LIKE '%bar%'";
  91. $this->assertEquals(__sql::parse_constraints($constraints, false), $constraints);
  92. }
  93. function test_parse_constraints_simple() {
  94. $this->assertEquals(__sql::parse_constraints(
  95. array('id' => 1, 'bar' => 'test1'), false),
  96. "`id` = '1' AND `bar` = 'test1'");
  97. }
  98. function test_parse_constraints_value_list() {
  99. $this->assertEquals(__sql::parse_constraints(
  100. array('id' => range(1, 3)), false),
  101. "`id` IN ('1', '2', '3')");
  102. }
  103. function test_select_query() {
  104. $sql = __sql::select('foo', '*', array('bar' => 'test1'), false);
  105. $this->assertEquals($sql->query, "SELECT * FROM `foo` WHERE `bar` = 'test1';");
  106. }
  107. function test_update_query() {
  108. $sql = __sql::update('foo', array('bar' => 'test4'), array('id' => 1), false);
  109. $this->assertEquals($sql->query, "UPDATE `foo` SET `bar` = 'test4' WHERE `id` = '1';");
  110. }
  111. function test_insert_query() {
  112. $sql = __sql::insert_row('foo', array('bar' => 'test3'), false);
  113. $this->assertEquals($sql->query, "INSERT INTO `foo`(`bar`) VALUES('test3');");
  114. }
  115. function test_delete_query() {
  116. $sql = __sql::delete('foo', array('bar' => 'test3'), false);
  117. $this->assertEquals($sql->query, "DELETE FROM `foo` WHERE `bar` = 'test3';");
  118. }
  119. /**
  120. * @depends test_select_query
  121. */
  122. function test_select() {
  123. $sql = _sql("select bar from foo where id = 1");
  124. $result = $sql->fetch('object');
  125. $this->assertEquals('test1', $result->bar);
  126. $this->assertSame($sql->fetch(), false);
  127. }
  128. /**
  129. * @depends test_update_query
  130. */
  131. function test_update() {
  132. $update = __sql::update('foo', array('bar' => 'test1'),
  133. array('id' => 1), false)->execute();
  134. // Do not continue unless the value has been updated
  135. $this->assertSame($update->result, true);
  136. }
  137. /**
  138. * @depends test_insert_query
  139. */
  140. function test_insert() {
  141. $insert = __sql::insert_row('foo', array('bar' => 'test3'))->execute();
  142. $this->assertSame($insert->result, true);
  143. }
  144. /**
  145. * @depends test_delete_query
  146. * @depends test_insert
  147. */
  148. function test_delete() {
  149. $delete = __sql::delete('foo', array('bar' => 'test3'))->execute();
  150. $this->assertSame($delete->result, true);
  151. }
  152. }
  153. ?>