ConnectionManagerTest.php 5.9 KB

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