DatabaseTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. require_once 'DatabaseLoader.php';
  3. class DatabaseTest extends SnakeCase_PHPUnit_Framework_TestCase
  4. {
  5. protected $conn;
  6. public static $log = false;
  7. public function set_up($connection_name=null)
  8. {
  9. ActiveRecord\Table::clear_cache();
  10. $config = ActiveRecord\Config::instance();
  11. $this->original_default_connection = $config->get_default_connection();
  12. if ($connection_name)
  13. $config->set_default_connection($connection_name);
  14. if ($connection_name == 'sqlite' || $config->get_default_connection() == 'sqlite')
  15. {
  16. // need to create the db. the adapter specifically does not create it for us.
  17. $this->db = substr(ActiveRecord\Config::instance()->get_connection('sqlite'),9);
  18. new SQLite3($this->db);
  19. }
  20. $this->connection_name = $connection_name;
  21. try {
  22. $this->conn = ActiveRecord\ConnectionManager::get_connection($connection_name);
  23. } catch (ActiveRecord\DatabaseException $e) {
  24. $this->mark_test_skipped($connection_name . ' failed to connect. '.$e->getMessage());
  25. }
  26. $GLOBALS['ACTIVERECORD_LOG'] = false;
  27. $loader = new DatabaseLoader($this->conn);
  28. $loader->reset_table_data();
  29. if (self::$log)
  30. $GLOBALS['ACTIVERECORD_LOG'] = true;
  31. }
  32. public function tear_down()
  33. {
  34. if ($this->original_default_connection)
  35. ActiveRecord\Config::instance()->set_default_connection($this->original_default_connection);
  36. }
  37. public function assert_exception_message_contains($contains, $closure)
  38. {
  39. $message = "";
  40. try {
  41. $closure();
  42. } catch (ActiveRecord\UndefinedPropertyException $e) {
  43. $message = $e->getMessage();
  44. }
  45. $this->assert_true(strpos($message,$contains) !== false);
  46. }
  47. /**
  48. * Returns true if $regex matches $actual.
  49. *
  50. * Takes database specific quotes into account by removing them. So, this won't
  51. * work if you have actual quotes in your strings.
  52. */
  53. public function assert_sql_has($needle, $haystack)
  54. {
  55. $needle = str_replace(array('"','`'),'',$needle);
  56. $haystack = str_replace(array('"','`'),'',$haystack);
  57. return $this->assert_true(strpos($haystack,$needle) !== false);
  58. }
  59. public function assert_sql_doesnt_has($needle, $haystack)
  60. {
  61. $needle = str_replace(array('"','`'),'',$needle);
  62. $haystack = str_replace(array('"','`'),'',$haystack);
  63. return $this->assert_false(strpos($haystack,$needle) !== false);
  64. }
  65. }
  66. ?>