ConnectionManagerTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * PHP 5
  4. *
  5. * Licensed under The MIT License
  6. * For full copyright and license information, please see the LICENSE.txt
  7. * Redistributions of files must retain the above copyright notice
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  11. * @since CakePHP(tm) v 1.2.0.5550
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Database;
  15. use Cake\Core\App;
  16. use Cake\Core\Plugin;
  17. use Cake\Database\ConnectionManager;
  18. use Cake\Database\Driver\Sqlite;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * ConnectionManager Test
  22. */
  23. class ConnectionManagerTest extends TestCase {
  24. /**
  25. * tearDown method
  26. *
  27. * @return void
  28. */
  29. public function tearDown() {
  30. parent::tearDown();
  31. Plugin::unload();
  32. ConnectionManager::drop('test_variant');
  33. ConnectionManager::dropAlias('other_name');
  34. }
  35. /**
  36. * Data provider for valid config data sets.
  37. *
  38. * @return array
  39. */
  40. public static function configProvider() {
  41. return [
  42. 'Array of data using classname key.' => [[
  43. 'className' => 'Sqlite',
  44. 'database' => ':memory:',
  45. ]],
  46. 'Direct instance' => [new Sqlite(['database' => ':memory:'])],
  47. ];
  48. }
  49. /**
  50. * Test the various valid config() calls.
  51. *
  52. * @dataProvider configProvider
  53. * @return void
  54. */
  55. public function testConfigVariants($settings) {
  56. $this->assertNotContains('test_variant', ConnectionManager::configured(), 'test_variant config should not exist.');
  57. ConnectionManager::config('test_variant', $settings);
  58. $ds = ConnectionManager::get('test_variant');
  59. $this->assertInstanceOf('Cake\Database\Connection', $ds);
  60. $this->assertContains('test_variant', ConnectionManager::configured());
  61. }
  62. /**
  63. * Test invalid classes cause exceptions
  64. *
  65. * @expectedException Cake\Database\Exception\MissingDriverException
  66. */
  67. public function testConfigInvalidOptions() {
  68. ConnectionManager::config('test_variant', [
  69. 'className' => 'HerpDerp'
  70. ]);
  71. ConnectionManager::get('test_variant');
  72. }
  73. /**
  74. * Test for errors on duplicate config.
  75. *
  76. * @expectedException Cake\Error\Exception
  77. * @expectedExceptionMessage Cannot reconfigure existing key "test_variant"
  78. * @return void
  79. */
  80. public function testConfigDuplicateConfig() {
  81. $settings = [
  82. 'className' => 'Sqlite',
  83. 'database' => ':memory:',
  84. ];
  85. ConnectionManager::config('test_variant', $settings);
  86. ConnectionManager::config('test_variant', $settings);
  87. }
  88. /**
  89. * Test get() failing on missing config.
  90. *
  91. * @expectedException Cake\Error\Exception
  92. * @expectedExceptionMessage The datasource configuration "test_variant" was not found.
  93. * @return void
  94. */
  95. public function testGetFailOnMissingConfig() {
  96. ConnectionManager::get('test_variant');
  97. }
  98. /**
  99. * Test loading configured connections.
  100. *
  101. * @return void
  102. */
  103. public function testGet() {
  104. $config = ConnectionManager::config('test');
  105. $this->skipIf(empty($config), 'No test config, skipping');
  106. $ds = ConnectionManager::get('test');
  107. $this->assertSame($ds, ConnectionManager::get('test'));
  108. $this->assertInstanceOf('Cake\Database\Connection', $ds);
  109. $this->assertEquals('test', $ds->configName());
  110. }
  111. /**
  112. * Test loading connections without aliases
  113. *
  114. * @expectedException Cake\Error\Exception
  115. * @expectedExceptionMessage The datasource configuration "other_name" was not found.
  116. * @return void
  117. */
  118. public function testGetNoAlias() {
  119. $config = ConnectionManager::config('test');
  120. $this->skipIf(empty($config), 'No test config, skipping');
  121. ConnectionManager::alias('test', 'other_name');
  122. ConnectionManager::get('other_name', false);
  123. }
  124. /**
  125. * Test that configured() finds configured sources.
  126. *
  127. * @return void
  128. */
  129. public function testConfigured() {
  130. ConnectionManager::config('test_variant', [
  131. 'className' => 'Sqlite',
  132. 'database' => ':memory:'
  133. ]);
  134. $results = ConnectionManager::configured();
  135. $this->assertContains('test_variant', $results);
  136. }
  137. /**
  138. * testGetPluginDataSource method
  139. *
  140. * @return void
  141. */
  142. public function testGetPluginDataSource() {
  143. Plugin::load('TestPlugin');
  144. $name = 'test_variant';
  145. $config = array('className' => 'TestPlugin.TestSource');
  146. ConnectionManager::config($name, $config);
  147. $connection = ConnectionManager::get($name);
  148. $this->assertInstanceOf('Cake\Database\Connection', $connection);
  149. $this->assertInstanceOf('TestPlugin\Database\Driver\TestSource', $connection->driver());
  150. }
  151. /**
  152. * Tests that a connection configuration can be deleted in runtime
  153. *
  154. * @return void
  155. */
  156. public function testDrop() {
  157. ConnectionManager::config('test_variant', [
  158. 'className' => 'Sqlite',
  159. 'database' => ':memory:'
  160. ]);
  161. $result = ConnectionManager::configured();
  162. $this->assertContains('test_variant', $result);
  163. $this->assertTrue(ConnectionManager::drop('test_variant'));
  164. $result = ConnectionManager::configured();
  165. $this->assertNotContains('test_variant', $result);
  166. $this->assertFalse(ConnectionManager::drop('probably_does_not_exist'), 'Should return false on failure.');
  167. }
  168. /**
  169. * Test aliasing connections.
  170. *
  171. * @return void
  172. */
  173. public function testAlias() {
  174. ConnectionManager::config('test_variant', [
  175. 'className' => 'Sqlite',
  176. 'database' => ':memory:'
  177. ]);
  178. ConnectionManager::alias('test_variant', 'other_name');
  179. $result = ConnectionManager::get('test_variant');
  180. $this->assertSame($result, ConnectionManager::get('other_name'));
  181. }
  182. /**
  183. * Test alias() raises an error when aliasing an undefined connection.
  184. *
  185. * @expectedException Cake\Error\MissingDatasourceConfigException
  186. * @return void
  187. */
  188. public function testAliasError() {
  189. $this->assertNotContains('test_kaboom', ConnectionManager::configured());
  190. ConnectionManager::alias('test_kaboom', 'other_name');
  191. }
  192. }