ConnectionManagerTest.php 5.9 KB

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