VPN licensing server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.6KB

  1. <?php
  2. function new_error_callback($params) {
  3. global $error_callback_worked;
  4. if (substr_count($params['error'], 'You have an error in your SQL syntax')) $error_callback_worked = 1;
  5. }
  6. function my_debug_handler($params) {
  7. global $debug_callback_worked;
  8. if (substr_count($params['query'], 'SELECT')) $debug_callback_worked = 1;
  9. }
  10. class ErrorTest extends SimpleTest {
  11. function test_1_error_handler() {
  12. global $error_callback_worked, $static_error_callback_worked, $nonstatic_error_callback_worked;
  13. DB::$error_handler = 'new_error_callback';
  14. DB::query("SELET * FROM accounts");
  15. $this->assert($error_callback_worked === 1);
  16. DB::$error_handler = array('ErrorTest', 'static_error_callback');
  17. DB::query("SELET * FROM accounts");
  18. $this->assert($static_error_callback_worked === 1);
  19. DB::$error_handler = array($this, 'nonstatic_error_callback');
  20. DB::query("SELET * FROM accounts");
  21. $this->assert($nonstatic_error_callback_worked === 1);
  22. }
  23. public static function static_error_callback($params) {
  24. global $static_error_callback_worked;
  25. if (substr_count($params['error'], 'You have an error in your SQL syntax')) $static_error_callback_worked = 1;
  26. }
  27. public function nonstatic_error_callback($params) {
  28. global $nonstatic_error_callback_worked;
  29. if (substr_count($params['error'], 'You have an error in your SQL syntax')) $nonstatic_error_callback_worked = 1;
  30. }
  31. function test_2_exception_catch() {
  32. $dbname = DB::$dbName;
  33. DB::$error_handler = '';
  34. DB::$throw_exception_on_error = true;
  35. try {
  36. DB::query("SELET * FROM accounts");
  37. } catch(MeekroDBException $e) {
  38. $this->assert(substr_count($e->getMessage(), 'You have an error in your SQL syntax'));
  39. $this->assert($e->getQuery() === 'SELET * FROM accounts');
  40. $exception_was_caught = 1;
  41. }
  42. $this->assert($exception_was_caught === 1);
  43. try {
  44. DB::insert("`$dbname`.`accounts`", array(
  45. 'id' => 2,
  46. 'username' => 'Another Dude\'s \'Mom"',
  47. 'password' => 'asdfsdse',
  48. 'age' => 35,
  49. 'height' => 555.23
  50. ));
  51. } catch(MeekroDBException $e) {
  52. $this->assert(substr_count($e->getMessage(), 'Duplicate entry'));
  53. $exception_was_caught = 2;
  54. }
  55. $this->assert($exception_was_caught === 2);
  56. }
  57. function test_3_debugmode_handler() {
  58. global $debug_callback_worked;
  59. DB::debugMode('my_debug_handler');
  60. DB::query("SELECT * FROM accounts WHERE username!=%s", "Charlie's Friend");
  61. $this->assert($debug_callback_worked === 1);
  62. DB::debugMode(false);
  63. }
  64. }
  65. ?>