test_sql.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. __p::load_plugin('sql');
  3. class pQuerySqlTest extends UnitTestCase {
  4. function __construct() {
  5. parent::__construct('pQuery MySQL plugin');
  6. }
  7. function setUp() {
  8. __sql::set_login_data('localhost', 'root', '', 'pquery_test');
  9. }
  10. function test_set_login_data() {
  11. __sql::set_login_data('a', 'b', 'c', 'd');
  12. $this->assertEqual(__sql::$login_data['host'], 'a');
  13. $this->assertEqual(__sql::$login_data['username'], 'b');
  14. $this->assertEqual(__sql::$login_data['password'], 'c');
  15. $this->assertEqual(__sql::$login_data['dbname'], 'd');
  16. }
  17. function test_no_login_data() {
  18. $this->expectException('pQueryException');
  19. __sql::$login_data = array();
  20. __sql::assert_login_data_exist();
  21. }
  22. function test_query_getter() {
  23. $sql = _sql('foobar');
  24. $this->assertEqual($sql->variable, 'foobar');
  25. $this->assertEqual($sql->query, 'foobar');
  26. }
  27. function test_variable_query() {
  28. $sql = _sql("select id from foo where bar = '[bar]'")
  29. ->set(array('bar' => 'test1'));
  30. $this->assertEqual($sql->query, "select id from foo where bar = 'test1'");
  31. }
  32. function test_unescaped_query() {
  33. $sql = _sql("select id from foo where bar = '[bar]'")
  34. ->set_unescaped(array('bar' => "select id from foo where bar = 'test1'"));
  35. $this->assertEqual($sql->query, "select id from foo where bar = 'select id from foo where bar = 'test1''");
  36. }
  37. function test_escaped_query() {
  38. $sql = _sql("select id from foo where bar = '[bar]'")
  39. ->set(array('bar' => "select id from foo where bar = 'test1'"));
  40. $this->assertNotEqual($sql->query, "select id from foo where bar = 'select id from foo where bar = 'test1''");
  41. }
  42. function test_select_simple() {
  43. $sql = _sql("select bar from foo where id = 1");
  44. $result = $sql->fetch('object');
  45. $this->assertEqual($result->bar, 'test1');
  46. $this->assertIdentical($sql->fetch(), false);
  47. }
  48. function test_result_count() {
  49. $sql = _sql("select bar from foo where id in (1, 2)");
  50. $this->assertEqual($sql->result_count(), 2);
  51. }
  52. }
  53. ?>