ConnectionManagerTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. /**
  22. * ConnectionManager Test
  23. */
  24. class ConnectionManagerTest extends TestCase
  25. {
  26. /**
  27. * tearDown method
  28. *
  29. * @return void
  30. */
  31. public function tearDown()
  32. {
  33. parent::tearDown();
  34. Plugin::unload();
  35. ConnectionManager::drop('test_variant');
  36. ConnectionManager::dropAlias('other_name');
  37. }
  38. /**
  39. * Data provider for valid config data sets.
  40. *
  41. * @return array
  42. */
  43. public static function configProvider()
  44. {
  45. return [
  46. 'Array of data using classname key.' => [[
  47. 'className' => __NAMESPACE__ . '\FakeConnection',
  48. 'instance' => 'Sqlite',
  49. 'database' => ':memory:',
  50. ]],
  51. 'Direct instance' => [new FakeConnection],
  52. ];
  53. }
  54. /**
  55. * Test the various valid config() calls.
  56. *
  57. * @dataProvider configProvider
  58. * @return void
  59. */
  60. public function testConfigVariants($settings)
  61. {
  62. $this->assertNotContains('test_variant', ConnectionManager::configured(), 'test_variant config should not exist.');
  63. ConnectionManager::config('test_variant', $settings);
  64. $ds = ConnectionManager::get('test_variant');
  65. $this->assertInstanceOf(__NAMESPACE__ . '\FakeConnection', $ds);
  66. $this->assertContains('test_variant', ConnectionManager::configured());
  67. }
  68. /**
  69. * Test invalid classes cause exceptions
  70. *
  71. * @expectedException \Cake\Datasource\Exception\MissingDatasourceException
  72. */
  73. public function testConfigInvalidOptions()
  74. {
  75. ConnectionManager::config('test_variant', [
  76. 'className' => 'Herp\Derp'
  77. ]);
  78. ConnectionManager::get('test_variant');
  79. }
  80. /**
  81. * Test for errors on duplicate config.
  82. *
  83. * @expectedException \BadMethodCallException
  84. * @expectedExceptionMessage Cannot reconfigure existing key "test_variant"
  85. * @return void
  86. */
  87. public function testConfigDuplicateConfig()
  88. {
  89. $settings = [
  90. 'className' => __NAMESPACE__ . '\FakeConnection',
  91. 'database' => ':memory:',
  92. ];
  93. ConnectionManager::config('test_variant', $settings);
  94. ConnectionManager::config('test_variant', $settings);
  95. }
  96. /**
  97. * Test get() failing on missing config.
  98. *
  99. * @expectedException \Cake\Core\Exception\Exception
  100. * @expectedExceptionMessage The datasource configuration "test_variant" was not found.
  101. * @return void
  102. */
  103. public function testGetFailOnMissingConfig()
  104. {
  105. ConnectionManager::get('test_variant');
  106. }
  107. /**
  108. * Test loading configured connections.
  109. *
  110. * @return void
  111. */
  112. public function testGet()
  113. {
  114. $config = ConnectionManager::config('test');
  115. $this->skipIf(empty($config), 'No test config, skipping');
  116. $ds = ConnectionManager::get('test');
  117. $this->assertSame($ds, ConnectionManager::get('test'));
  118. $this->assertInstanceOf('Cake\Database\Connection', $ds);
  119. $this->assertEquals('test', $ds->configName());
  120. }
  121. /**
  122. * Test loading connections without aliases
  123. *
  124. * @expectedException \Cake\Core\Exception\Exception
  125. * @expectedExceptionMessage The datasource configuration "other_name" was not found.
  126. * @return void
  127. */
  128. public function testGetNoAlias()
  129. {
  130. $config = ConnectionManager::config('test');
  131. $this->skipIf(empty($config), 'No test config, skipping');
  132. ConnectionManager::alias('test', 'other_name');
  133. ConnectionManager::get('other_name', false);
  134. }
  135. /**
  136. * Test that configured() finds configured sources.
  137. *
  138. * @return void
  139. */
  140. public function testConfigured()
  141. {
  142. ConnectionManager::config('test_variant', [
  143. 'className' => __NAMESPACE__ . '\FakeConnection',
  144. 'database' => ':memory:'
  145. ]);
  146. $results = ConnectionManager::configured();
  147. $this->assertContains('test_variant', $results);
  148. }
  149. /**
  150. * testGetPluginDataSource method
  151. *
  152. * @return void
  153. */
  154. public function testGetPluginDataSource()
  155. {
  156. Plugin::load('TestPlugin');
  157. $name = 'test_variant';
  158. $config = ['className' => 'TestPlugin.TestSource', 'foo' => 'bar'];
  159. ConnectionManager::config($name, $config);
  160. $connection = ConnectionManager::get($name);
  161. $this->assertInstanceOf('TestPlugin\Datasource\TestSource', $connection);
  162. unset($config['className']);
  163. $this->assertSame($config + ['name' => 'test_variant'], $connection->config());
  164. }
  165. /**
  166. * Tests that a connection configuration can be deleted in runtime
  167. *
  168. * @return void
  169. */
  170. public function testDrop()
  171. {
  172. ConnectionManager::config('test_variant', [
  173. 'className' => __NAMESPACE__ . '\FakeConnection',
  174. 'database' => ':memory:'
  175. ]);
  176. $result = ConnectionManager::configured();
  177. $this->assertContains('test_variant', $result);
  178. $this->assertTrue(ConnectionManager::drop('test_variant'));
  179. $result = ConnectionManager::configured();
  180. $this->assertNotContains('test_variant', $result);
  181. $this->assertFalse(ConnectionManager::drop('probably_does_not_exist'), 'Should return false on failure.');
  182. }
  183. /**
  184. * Test aliasing connections.
  185. *
  186. * @return void
  187. */
  188. public function testAlias()
  189. {
  190. ConnectionManager::config('test_variant', [
  191. 'className' => __NAMESPACE__ . '\FakeConnection',
  192. 'database' => ':memory:'
  193. ]);
  194. ConnectionManager::alias('test_variant', 'other_name');
  195. $result = ConnectionManager::get('test_variant');
  196. $this->assertSame($result, ConnectionManager::get('other_name'));
  197. }
  198. /**
  199. * Test alias() raises an error when aliasing an undefined connection.
  200. *
  201. * @expectedException \Cake\Datasource\Exception\MissingDatasourceConfigException
  202. * @return void
  203. */
  204. public function testAliasError()
  205. {
  206. $this->assertNotContains('test_kaboom', ConnectionManager::configured());
  207. ConnectionManager::alias('test_kaboom', 'other_name');
  208. }
  209. }