ConfigTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace Elgg\Database;
  3. class ConfigTest extends \PHPUnit_Framework_TestCase {
  4. public function testGetTablePrefix() {
  5. $CONFIG = new \stdClass();
  6. $CONFIG->dbprefix = "foo";
  7. $conf = new \Elgg\Database\Config($CONFIG);
  8. $this->assertEquals($CONFIG->dbprefix, $conf->getTablePrefix());
  9. }
  10. public function testIsDatabaseSplitNotSet() {
  11. $CONFIG = new \stdClass();
  12. $conf = new \Elgg\Database\Config($CONFIG);
  13. $this->assertFalse($conf->isDatabaseSplit());
  14. }
  15. public function testIsDatabaseSplitInSettings() {
  16. $CONFIG = new \stdClass();
  17. $CONFIG->db['split'] = true;
  18. $conf = new \Elgg\Database\Config($CONFIG);
  19. $this->assertTrue($conf->isDatabaseSplit());
  20. }
  21. public function testGetConnectionConfigNormalSetup() {
  22. $ans = array(
  23. 'host' => 'foo',
  24. 'user' => 'user',
  25. 'password' => 'xxxx',
  26. 'database' => 'elgg',
  27. );
  28. $CONFIG = new \stdClass();
  29. $CONFIG->dbhost = $ans['host'];
  30. $CONFIG->dbuser = $ans['user'];
  31. $CONFIG->dbpass = $ans['password'];
  32. $CONFIG->dbname = $ans['database'];
  33. $conf = new \Elgg\Database\Config($CONFIG);
  34. $this->assertEquals($ans, $conf->getConnectionConfig());
  35. }
  36. public function testGetConnectionConfigWithSingleWrite() {
  37. $ans = array(
  38. 'host' => 'foo',
  39. 'user' => 'user',
  40. 'password' => 'xxxx',
  41. 'database' => 'elgg',
  42. );
  43. $CONFIG = new \stdClass();
  44. $CONFIG->db['write']['dbhost'] = $ans['host'];
  45. $CONFIG->db['write']['dbuser'] = $ans['user'];
  46. $CONFIG->db['write']['dbpass'] = $ans['password'];
  47. $CONFIG->db['write']['dbname'] = $ans['database'];
  48. $conf = new \Elgg\Database\Config($CONFIG);
  49. $this->assertEquals($ans, $conf->getConnectionConfig(\Elgg\Database\Config::WRITE));
  50. }
  51. public function testGetConnectionConfigWithMultipleRead() {
  52. $ans = array(
  53. 0 => array(
  54. 'host' => 0,
  55. 'user' => 'user0',
  56. 'password' => 'xxxx0',
  57. 'database' => 'elgg0',
  58. ),
  59. 1 => array(
  60. 'host' => 1,
  61. 'user' => 'user1',
  62. 'password' => 'xxxx1',
  63. 'database' => 'elgg1',
  64. ),
  65. );
  66. $CONFIG = new \stdClass();
  67. $CONFIG->db['read'][0]['dbhost'] = $ans[0]['host'];
  68. $CONFIG->db['read'][0]['dbuser'] = $ans[0]['user'];
  69. $CONFIG->db['read'][0]['dbpass'] = $ans[0]['password'];
  70. $CONFIG->db['read'][0]['dbname'] = $ans[0]['database'];
  71. $CONFIG->db['read'][1]['dbhost'] = $ans[1]['host'];
  72. $CONFIG->db['read'][1]['dbuser'] = $ans[1]['user'];
  73. $CONFIG->db['read'][1]['dbpass'] = $ans[1]['password'];
  74. $CONFIG->db['read'][1]['dbname'] = $ans[1]['database'];
  75. $conf = new \Elgg\Database\Config($CONFIG);
  76. $connConf = $conf->getConnectionConfig(\Elgg\Database\Config::READ);
  77. $this->assertEquals($ans[$connConf['host']], $connConf);
  78. }
  79. // Elgg < 1.9 used objects to store the config
  80. public function testGetConnectionConfigWithSingleWriteOldStyle() {
  81. $ans = array(
  82. 'host' => 'foo',
  83. 'user' => 'user',
  84. 'password' => 'xxxx',
  85. 'database' => 'elgg',
  86. );
  87. $CONFIG = new \stdClass();
  88. $CONFIG->db['write'] = new \stdClass();
  89. $CONFIG->db['write']->dbhost = $ans['host'];
  90. $CONFIG->db['write']->dbuser = $ans['user'];
  91. $CONFIG->db['write']->dbpass = $ans['password'];
  92. $CONFIG->db['write']->dbname = $ans['database'];
  93. $conf = new \Elgg\Database\Config($CONFIG);
  94. $this->assertEquals($ans, $conf->getConnectionConfig(\Elgg\Database\Config::WRITE));
  95. }
  96. // Elgg < 1.9 used objects to store the config
  97. public function testGetConnectionConfigWithMultipleReadOldStyle() {
  98. $ans = array(
  99. 0 => array(
  100. 'host' => 0,
  101. 'user' => 'user0',
  102. 'password' => 'xxxx0',
  103. 'database' => 'elgg0',
  104. ),
  105. 1 => array(
  106. 'host' => 1,
  107. 'user' => 'user1',
  108. 'password' => 'xxxx1',
  109. 'database' => 'elgg1',
  110. ),
  111. );
  112. $CONFIG = new \stdClass();
  113. $CONFIG->db['read'][0] = new \stdClass();
  114. $CONFIG->db['read'][0]->dbhost = $ans[0]['host'];
  115. $CONFIG->db['read'][0]->dbuser = $ans[0]['user'];
  116. $CONFIG->db['read'][0]->dbpass = $ans[0]['password'];
  117. $CONFIG->db['read'][0]->dbname = $ans[0]['database'];
  118. $CONFIG->db['read'][1] = new \stdClass();
  119. $CONFIG->db['read'][1]->dbhost = $ans[1]['host'];
  120. $CONFIG->db['read'][1]->dbuser = $ans[1]['user'];
  121. $CONFIG->db['read'][1]->dbpass = $ans[1]['password'];
  122. $CONFIG->db['read'][1]->dbname = $ans[1]['database'];
  123. $conf = new \Elgg\Database\Config($CONFIG);
  124. $connConf = $conf->getConnectionConfig(\Elgg\Database\Config::READ);
  125. $this->assertEquals($ans[$connConf['host']], $connConf);
  126. }
  127. }