ConnectionTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. use ActiveRecord\Connection;
  3. include 'helpers/config.php';
  4. // Only use this to test static methods in Connection that are not specific
  5. // to any database adapter.
  6. class ConnectionTest extends SnakeCase_PHPUnit_Framework_TestCase
  7. {
  8. /**
  9. * @expectedException ActiveRecord\DatabaseException
  10. */
  11. public function test_connection_info_from_should_throw_exception_when_no_host()
  12. {
  13. ActiveRecord\Connection::parse_connection_url('mysql://user:pass@');
  14. }
  15. public function test_connection_info()
  16. {
  17. $info = ActiveRecord\Connection::parse_connection_url('mysql://user:[email protected]:3306/dbname');
  18. $this->assert_equals('mysql',$info->protocol);
  19. $this->assert_equals('user',$info->user);
  20. $this->assert_equals('pass',$info->pass);
  21. $this->assert_equals('127.0.0.1',$info->host);
  22. $this->assert_equals(3306,$info->port);
  23. $this->assert_equals('dbname',$info->db);
  24. }
  25. public function test_gh_103_sqlite_connection_string_relative()
  26. {
  27. $info = ActiveRecord\Connection::parse_connection_url('sqlite://../some/path/to/file.db');
  28. $this->assert_equals('../some/path/to/file.db', $info->host);
  29. }
  30. /**
  31. * @expectedException ActiveRecord\DatabaseException
  32. */
  33. public function test_gh_103_sqlite_connection_string_absolute()
  34. {
  35. $info = ActiveRecord\Connection::parse_connection_url('sqlite:///some/path/to/file.db');
  36. }
  37. public function test_gh_103_sqlite_connection_string_unix()
  38. {
  39. $info = ActiveRecord\Connection::parse_connection_url('sqlite://unix(/some/path/to/file.db)');
  40. $this->assert_equals('/some/path/to/file.db', $info->host);
  41. $info = ActiveRecord\Connection::parse_connection_url('sqlite://unix(/some/path/to/file.db)/');
  42. $this->assert_equals('/some/path/to/file.db', $info->host);
  43. $info = ActiveRecord\Connection::parse_connection_url('sqlite://unix(/some/path/to/file.db)/dummy');
  44. $this->assert_equals('/some/path/to/file.db', $info->host);
  45. }
  46. public function test_gh_103_sqlite_connection_string_windows()
  47. {
  48. $info = ActiveRecord\Connection::parse_connection_url('sqlite://windows(c%3A/some/path/to/file.db)');
  49. $this->assert_equals('c:/some/path/to/file.db', $info->host);
  50. }
  51. public function test_parse_connection_url_with_unix_sockets()
  52. {
  53. $info = ActiveRecord\Connection::parse_connection_url('mysql://user:password@unix(/tmp/mysql.sock)/database');
  54. $this->assert_equals('/tmp/mysql.sock',$info->host);
  55. }
  56. public function test_parse_connection_url_with_decode_option()
  57. {
  58. $info = ActiveRecord\Connection::parse_connection_url('mysql://h%20az:h%[email protected]/test?decode=true');
  59. $this->assert_equals('h az',$info->user);
  60. $this->assert_equals('h@i',$info->pass);
  61. }
  62. public function test_encoding()
  63. {
  64. $info = ActiveRecord\Connection::parse_connection_url('mysql://test:[email protected]/test?charset=utf8');
  65. $this->assert_equals('utf8', $info->charset);
  66. }
  67. }
  68. ?>