ConnectionManagerTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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://cakephp.org CakePHP(tm) Project
  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\Plugin;
  14. use Cake\Datasource\ConnectionManager;
  15. use Cake\TestSuite\TestCase;
  16. class FakeConnection
  17. {
  18. }
  19. /**
  20. * ConnectionManager Test
  21. */
  22. class ConnectionManagerTest extends TestCase
  23. {
  24. /**
  25. * tearDown method
  26. *
  27. * @return void
  28. */
  29. public function tearDown()
  30. {
  31. parent::tearDown();
  32. Plugin::unload();
  33. ConnectionManager::drop('test_variant');
  34. ConnectionManager::dropAlias('other_name');
  35. }
  36. /**
  37. * Data provider for valid config data sets.
  38. *
  39. * @return array
  40. */
  41. public static function configProvider()
  42. {
  43. return [
  44. 'Array of data using classname key.' => [[
  45. 'className' => __NAMESPACE__ . '\FakeConnection',
  46. 'instance' => 'Sqlite',
  47. 'database' => ':memory:',
  48. ]],
  49. 'Direct instance' => [new FakeConnection],
  50. ];
  51. }
  52. /**
  53. * Test the various valid config() calls.
  54. *
  55. * @dataProvider configProvider
  56. * @return void
  57. */
  58. public function testConfigVariants($settings)
  59. {
  60. $this->assertNotContains('test_variant', ConnectionManager::configured(), 'test_variant config should not exist.');
  61. ConnectionManager::config('test_variant', $settings);
  62. $ds = ConnectionManager::get('test_variant');
  63. $this->assertInstanceOf(__NAMESPACE__ . '\FakeConnection', $ds);
  64. $this->assertContains('test_variant', ConnectionManager::configured());
  65. }
  66. /**
  67. * Test invalid classes cause exceptions
  68. *
  69. * @expectedException \Cake\Datasource\Exception\MissingDatasourceException
  70. */
  71. public function testConfigInvalidOptions()
  72. {
  73. ConnectionManager::config('test_variant', [
  74. 'className' => 'Herp\Derp'
  75. ]);
  76. ConnectionManager::get('test_variant');
  77. }
  78. /**
  79. * Test for errors on duplicate config.
  80. *
  81. * @expectedException \BadMethodCallException
  82. * @expectedExceptionMessage Cannot reconfigure existing key "test_variant"
  83. * @return void
  84. */
  85. public function testConfigDuplicateConfig()
  86. {
  87. $settings = [
  88. 'className' => __NAMESPACE__ . '\FakeConnection',
  89. 'database' => ':memory:',
  90. ];
  91. ConnectionManager::config('test_variant', $settings);
  92. ConnectionManager::config('test_variant', $settings);
  93. }
  94. /**
  95. * Test get() failing on missing config.
  96. *
  97. * @expectedException \Cake\Core\Exception\Exception
  98. * @expectedExceptionMessage The datasource configuration "test_variant" was not found.
  99. * @return void
  100. */
  101. public function testGetFailOnMissingConfig()
  102. {
  103. ConnectionManager::get('test_variant');
  104. }
  105. /**
  106. * Test loading configured connections.
  107. *
  108. * @return void
  109. */
  110. public function testGet()
  111. {
  112. $config = ConnectionManager::config('test');
  113. $this->skipIf(empty($config), 'No test config, skipping');
  114. $ds = ConnectionManager::get('test');
  115. $this->assertSame($ds, ConnectionManager::get('test'));
  116. $this->assertInstanceOf('Cake\Database\Connection', $ds);
  117. $this->assertEquals('test', $ds->configName());
  118. }
  119. /**
  120. * Test loading connections without aliases
  121. *
  122. * @expectedException \Cake\Core\Exception\Exception
  123. * @expectedExceptionMessage The datasource configuration "other_name" was not found.
  124. * @return void
  125. */
  126. public function testGetNoAlias()
  127. {
  128. $config = ConnectionManager::config('test');
  129. $this->skipIf(empty($config), 'No test config, skipping');
  130. ConnectionManager::alias('test', 'other_name');
  131. ConnectionManager::get('other_name', false);
  132. }
  133. /**
  134. * Test that configured() finds configured sources.
  135. *
  136. * @return void
  137. */
  138. public function testConfigured()
  139. {
  140. ConnectionManager::config('test_variant', [
  141. 'className' => __NAMESPACE__ . '\FakeConnection',
  142. 'database' => ':memory:'
  143. ]);
  144. $results = ConnectionManager::configured();
  145. $this->assertContains('test_variant', $results);
  146. }
  147. /**
  148. * testGetPluginDataSource method
  149. *
  150. * @return void
  151. */
  152. public function testGetPluginDataSource()
  153. {
  154. Plugin::load('TestPlugin');
  155. $name = 'test_variant';
  156. $config = ['className' => 'TestPlugin.TestSource', 'foo' => 'bar'];
  157. ConnectionManager::config($name, $config);
  158. $connection = ConnectionManager::get($name);
  159. $this->assertInstanceOf('TestPlugin\Datasource\TestSource', $connection);
  160. unset($config['className']);
  161. $this->assertSame($config + ['name' => 'test_variant'], $connection->config());
  162. }
  163. /**
  164. * Tests that a connection configuration can be deleted in runtime
  165. *
  166. * @return void
  167. */
  168. public function testDrop()
  169. {
  170. ConnectionManager::config('test_variant', [
  171. 'className' => __NAMESPACE__ . '\FakeConnection',
  172. 'database' => ':memory:'
  173. ]);
  174. $result = ConnectionManager::configured();
  175. $this->assertContains('test_variant', $result);
  176. $this->assertTrue(ConnectionManager::drop('test_variant'));
  177. $result = ConnectionManager::configured();
  178. $this->assertNotContains('test_variant', $result);
  179. $this->assertFalse(ConnectionManager::drop('probably_does_not_exist'), 'Should return false on failure.');
  180. }
  181. /**
  182. * Test aliasing connections.
  183. *
  184. * @return void
  185. */
  186. public function testAlias()
  187. {
  188. ConnectionManager::config('test_variant', [
  189. 'className' => __NAMESPACE__ . '\FakeConnection',
  190. 'database' => ':memory:'
  191. ]);
  192. ConnectionManager::alias('test_variant', 'other_name');
  193. $result = ConnectionManager::get('test_variant');
  194. $this->assertSame($result, ConnectionManager::get('other_name'));
  195. }
  196. /**
  197. * Test alias() raises an error when aliasing an undefined connection.
  198. *
  199. * @expectedException \Cake\Datasource\Exception\MissingDatasourceConfigException
  200. * @return void
  201. */
  202. public function testAliasError()
  203. {
  204. $this->assertNotContains('test_kaboom', ConnectionManager::configured());
  205. ConnectionManager::alias('test_kaboom', 'other_name');
  206. }
  207. /**
  208. * Test parseDsn method.
  209. *
  210. * @return void
  211. */
  212. public function testParseDsn()
  213. {
  214. $result = ConnectionManager::parseDsn('mysql://root:secret@localhost:3306/database?log=1');
  215. $expected = [
  216. 'scheme' => 'mysql',
  217. 'className' => 'Cake\Database\Connection',
  218. 'driver' => 'Cake\Database\Driver\Mysql',
  219. 'host' => 'localhost',
  220. 'username' => 'root',
  221. 'password' => 'secret',
  222. 'port' => 3306,
  223. 'database' => 'database',
  224. 'log' => '1'
  225. ];
  226. $this->assertEquals($expected, $result);
  227. }
  228. /**
  229. * Tests that directly setting an instance in a config, will not return a different
  230. * instance later on
  231. *
  232. * @return void
  233. */
  234. public function testConfigWithObject()
  235. {
  236. $connection = new FakeConnection;
  237. ConnectionManager::config('test_variant', $connection);
  238. $this->assertSame($connection, ConnectionManager::get('test_variant'));
  239. }
  240. /**
  241. * Tests configuring an instance with a callable
  242. *
  243. * @return void
  244. */
  245. public function testConfigWithCallable()
  246. {
  247. $connection = new FakeConnection;
  248. $callable = function ($alias) use ($connection) {
  249. $this->assertEquals('test_variant', $alias);
  250. return $connection;
  251. };
  252. ConnectionManager::config('test_variant', $callable);
  253. $this->assertSame($connection, ConnectionManager::get('test_variant'));
  254. }
  255. }