ConnectionManagerTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. (https://cakefoundation.org)
  8. * @link https://cakephp.org CakePHP(tm) Project
  9. * @since 1.2.0
  10. * @license https://opensource.org/licenses/mit-license.php MIT License
  11. */
  12. namespace Cake\Test\TestCase\Datasource;
  13. use Cake\Datasource\ConnectionManager;
  14. use Cake\TestSuite\TestCase;
  15. class FakeConnection
  16. {
  17. protected $_config = [];
  18. /**
  19. * Constructor.
  20. *
  21. * @param array $config configuration for connecting to database
  22. */
  23. public function __construct($config = [])
  24. {
  25. $this->_config = $config;
  26. }
  27. /**
  28. * Returns the set config
  29. *
  30. * @return array
  31. */
  32. public function config()
  33. {
  34. return $this->_config;
  35. }
  36. /**
  37. * Returns the set name
  38. *
  39. * @return string
  40. */
  41. public function configName()
  42. {
  43. if (empty($this->_config['name'])) {
  44. return '';
  45. }
  46. return $this->_config['name'];
  47. }
  48. }
  49. /**
  50. * ConnectionManager Test
  51. */
  52. class ConnectionManagerTest extends TestCase
  53. {
  54. /**
  55. * tearDown method
  56. *
  57. * @return void
  58. */
  59. public function tearDown()
  60. {
  61. parent::tearDown();
  62. $this->clearPlugins();
  63. ConnectionManager::drop('test_variant');
  64. ConnectionManager::dropAlias('other_name');
  65. }
  66. /**
  67. * Data provider for valid config data sets.
  68. *
  69. * @return array
  70. */
  71. public static function configProvider()
  72. {
  73. return [
  74. 'Array of data using classname key.' => [[
  75. 'className' => __NAMESPACE__ . '\FakeConnection',
  76. 'instance' => 'Sqlite',
  77. 'database' => ':memory:',
  78. ]],
  79. 'Direct instance' => [new FakeConnection()],
  80. ];
  81. }
  82. /**
  83. * Test the various valid config() calls.
  84. *
  85. * @dataProvider configProvider
  86. * @return void
  87. */
  88. public function testConfigVariants($settings)
  89. {
  90. $this->assertNotContains('test_variant', ConnectionManager::configured(), 'test_variant config should not exist.');
  91. ConnectionManager::setConfig('test_variant', $settings);
  92. $ds = ConnectionManager::get('test_variant');
  93. $this->assertInstanceOf(__NAMESPACE__ . '\FakeConnection', $ds);
  94. $this->assertContains('test_variant', ConnectionManager::configured());
  95. }
  96. /**
  97. * Test invalid classes cause exceptions
  98. */
  99. public function testConfigInvalidOptions()
  100. {
  101. $this->expectException(\Cake\Datasource\Exception\MissingDatasourceException::class);
  102. ConnectionManager::setConfig('test_variant', [
  103. 'className' => 'Herp\Derp',
  104. ]);
  105. ConnectionManager::get('test_variant');
  106. }
  107. /**
  108. * Test for errors on duplicate config.
  109. *
  110. * @return void
  111. */
  112. public function testConfigDuplicateConfig()
  113. {
  114. $this->expectException(\BadMethodCallException::class);
  115. $this->expectExceptionMessage('Cannot reconfigure existing key "test_variant"');
  116. $settings = [
  117. 'className' => __NAMESPACE__ . '\FakeConnection',
  118. 'database' => ':memory:',
  119. ];
  120. ConnectionManager::setConfig('test_variant', $settings);
  121. ConnectionManager::setConfig('test_variant', $settings);
  122. }
  123. /**
  124. * Test get() failing on missing config.
  125. *
  126. * @return void
  127. */
  128. public function testGetFailOnMissingConfig()
  129. {
  130. $this->expectException(\Cake\Core\Exception\Exception::class);
  131. $this->expectExceptionMessage('The datasource configuration "test_variant" was not found.');
  132. ConnectionManager::get('test_variant');
  133. }
  134. /**
  135. * Test loading configured connections.
  136. *
  137. * @return void
  138. */
  139. public function testGet()
  140. {
  141. $config = ConnectionManager::getConfig('test');
  142. $this->skipIf(empty($config), 'No test config, skipping');
  143. $ds = ConnectionManager::get('test');
  144. $this->assertSame($ds, ConnectionManager::get('test'));
  145. $this->assertInstanceOf('Cake\Database\Connection', $ds);
  146. $this->assertEquals('test', $ds->configName());
  147. }
  148. /**
  149. * Test loading connections without aliases
  150. *
  151. * @return void
  152. */
  153. public function testGetNoAlias()
  154. {
  155. $this->expectException(\Cake\Core\Exception\Exception::class);
  156. $this->expectExceptionMessage('The datasource configuration "other_name" was not found.');
  157. $config = ConnectionManager::getConfig('test');
  158. $this->skipIf(empty($config), 'No test config, skipping');
  159. ConnectionManager::alias('test', 'other_name');
  160. ConnectionManager::get('other_name', false);
  161. }
  162. /**
  163. * Test that configured() finds configured sources.
  164. *
  165. * @return void
  166. */
  167. public function testConfigured()
  168. {
  169. ConnectionManager::setConfig('test_variant', [
  170. 'className' => __NAMESPACE__ . '\FakeConnection',
  171. 'database' => ':memory:',
  172. ]);
  173. $results = ConnectionManager::configured();
  174. $this->assertContains('test_variant', $results);
  175. }
  176. /**
  177. * testGetPluginDataSource method
  178. *
  179. * @return void
  180. */
  181. public function testGetPluginDataSource()
  182. {
  183. $this->loadPlugins(['TestPlugin']);
  184. $name = 'test_variant';
  185. $config = ['className' => 'TestPlugin.TestSource', 'foo' => 'bar'];
  186. ConnectionManager::setConfig($name, $config);
  187. $connection = ConnectionManager::get($name);
  188. $this->assertInstanceOf('TestPlugin\Datasource\TestSource', $connection);
  189. unset($config['className']);
  190. $this->assertSame($config + ['name' => 'test_variant'], $connection->config());
  191. }
  192. /**
  193. * Tests that a connection configuration can be deleted in runtime
  194. *
  195. * @return void
  196. */
  197. public function testDrop()
  198. {
  199. ConnectionManager::setConfig('test_variant', [
  200. 'className' => __NAMESPACE__ . '\FakeConnection',
  201. 'database' => ':memory:',
  202. ]);
  203. $result = ConnectionManager::configured();
  204. $this->assertContains('test_variant', $result);
  205. $this->assertTrue(ConnectionManager::drop('test_variant'));
  206. $result = ConnectionManager::configured();
  207. $this->assertNotContains('test_variant', $result);
  208. $this->assertFalse(ConnectionManager::drop('probably_does_not_exist'), 'Should return false on failure.');
  209. }
  210. /**
  211. * Test aliasing connections.
  212. *
  213. * @return void
  214. */
  215. public function testAlias()
  216. {
  217. ConnectionManager::setConfig('test_variant', [
  218. 'className' => __NAMESPACE__ . '\FakeConnection',
  219. 'database' => ':memory:',
  220. ]);
  221. ConnectionManager::alias('test_variant', 'other_name');
  222. $result = ConnectionManager::get('test_variant');
  223. $this->assertSame($result, ConnectionManager::get('other_name'));
  224. }
  225. /**
  226. * Test alias() raises an error when aliasing an undefined connection.
  227. *
  228. * @return void
  229. */
  230. public function testAliasError()
  231. {
  232. $this->expectException(\Cake\Datasource\Exception\MissingDatasourceConfigException::class);
  233. $this->assertNotContains('test_kaboom', ConnectionManager::configured());
  234. ConnectionManager::alias('test_kaboom', 'other_name');
  235. }
  236. /**
  237. * provider for DSN strings.
  238. *
  239. * @return array
  240. */
  241. public function dsnProvider()
  242. {
  243. return [
  244. 'no user' => [
  245. 'mysql://localhost:3306/database',
  246. [
  247. 'className' => 'Cake\Database\Connection',
  248. 'driver' => 'Cake\Database\Driver\Mysql',
  249. 'host' => 'localhost',
  250. 'database' => 'database',
  251. 'port' => 3306,
  252. 'scheme' => 'mysql',
  253. ],
  254. ],
  255. 'subdomain host' => [
  256. 'mysql://my.host-name.com:3306/database',
  257. [
  258. 'className' => 'Cake\Database\Connection',
  259. 'driver' => 'Cake\Database\Driver\Mysql',
  260. 'host' => 'my.host-name.com',
  261. 'database' => 'database',
  262. 'port' => 3306,
  263. 'scheme' => 'mysql',
  264. ],
  265. ],
  266. 'user & pass' => [
  267. 'mysql://root:secret@localhost:3306/database?log=1',
  268. [
  269. 'scheme' => 'mysql',
  270. 'className' => 'Cake\Database\Connection',
  271. 'driver' => 'Cake\Database\Driver\Mysql',
  272. 'host' => 'localhost',
  273. 'username' => 'root',
  274. 'password' => 'secret',
  275. 'port' => 3306,
  276. 'database' => 'database',
  277. 'log' => '1',
  278. ],
  279. ],
  280. 'no password' => [
  281. 'mysql://user@localhost:3306/database',
  282. [
  283. 'className' => 'Cake\Database\Connection',
  284. 'driver' => 'Cake\Database\Driver\Mysql',
  285. 'host' => 'localhost',
  286. 'database' => 'database',
  287. 'port' => 3306,
  288. 'scheme' => 'mysql',
  289. 'username' => 'user',
  290. ],
  291. ],
  292. 'empty password' => [
  293. 'mysql://user:@localhost:3306/database',
  294. [
  295. 'className' => 'Cake\Database\Connection',
  296. 'driver' => 'Cake\Database\Driver\Mysql',
  297. 'host' => 'localhost',
  298. 'database' => 'database',
  299. 'port' => 3306,
  300. 'scheme' => 'mysql',
  301. 'username' => 'user',
  302. 'password' => '',
  303. ],
  304. ],
  305. 'sqlite memory' => [
  306. 'sqlite:///:memory:',
  307. [
  308. 'className' => 'Cake\Database\Connection',
  309. 'driver' => 'Cake\Database\Driver\Sqlite',
  310. 'database' => ':memory:',
  311. 'scheme' => 'sqlite',
  312. ],
  313. ],
  314. 'sqlite path' => [
  315. 'sqlite:////absolute/path',
  316. [
  317. 'className' => 'Cake\Database\Connection',
  318. 'driver' => 'Cake\Database\Driver\Sqlite',
  319. 'database' => '/absolute/path',
  320. 'scheme' => 'sqlite',
  321. ],
  322. ],
  323. 'sqlite database query' => [
  324. 'sqlite:///?database=:memory:',
  325. [
  326. 'className' => 'Cake\Database\Connection',
  327. 'driver' => 'Cake\Database\Driver\Sqlite',
  328. 'database' => ':memory:',
  329. 'scheme' => 'sqlite',
  330. ],
  331. ],
  332. 'sqlserver' => [
  333. 'sqlserver://sa:Password12!@.\SQL2012SP1/cakephp?MultipleActiveResultSets=false',
  334. [
  335. 'className' => 'Cake\Database\Connection',
  336. 'driver' => 'Cake\Database\Driver\Sqlserver',
  337. 'host' => '.\SQL2012SP1',
  338. 'MultipleActiveResultSets' => false,
  339. 'password' => 'Password12!',
  340. 'database' => 'cakephp',
  341. 'scheme' => 'sqlserver',
  342. 'username' => 'sa',
  343. ],
  344. ],
  345. 'sqllocaldb' => [
  346. 'sqlserver://username:password@(localdb)\.\DeptSharedLocalDB/database',
  347. [
  348. 'className' => 'Cake\Database\Connection',
  349. 'driver' => 'Cake\Database\Driver\Sqlserver',
  350. 'host' => '(localdb)\.\DeptSharedLocalDB',
  351. 'password' => 'password',
  352. 'database' => 'database',
  353. 'scheme' => 'sqlserver',
  354. 'username' => 'username',
  355. ],
  356. ],
  357. 'classname query arg' => [
  358. 'mysql://localhost/database?className=Custom\Driver',
  359. [
  360. 'className' => 'Cake\Database\Connection',
  361. 'database' => 'database',
  362. 'driver' => 'Custom\Driver',
  363. 'host' => 'localhost',
  364. 'scheme' => 'mysql',
  365. ],
  366. ],
  367. 'classname and port' => [
  368. 'mysql://localhost:3306/database?className=Custom\Driver',
  369. [
  370. 'className' => 'Cake\Database\Connection',
  371. 'database' => 'database',
  372. 'driver' => 'Custom\Driver',
  373. 'host' => 'localhost',
  374. 'scheme' => 'mysql',
  375. 'port' => 3306,
  376. ],
  377. ],
  378. 'custom connection class' => [
  379. 'Cake\Database\Connection://localhost:3306/database?driver=Cake\Database\Driver\Mysql',
  380. [
  381. 'className' => 'Cake\Database\Connection',
  382. 'database' => 'database',
  383. 'driver' => 'Cake\Database\Driver\Mysql',
  384. 'host' => 'localhost',
  385. 'scheme' => 'Cake\Database\Connection',
  386. 'port' => 3306,
  387. ],
  388. ],
  389. 'complex password' => [
  390. 'mysql://user:/?#][{}$%20@!@localhost:3306/database?log=1&quoteIdentifiers=1',
  391. [
  392. 'className' => 'Cake\Database\Connection',
  393. 'database' => 'database',
  394. 'driver' => 'Cake\Database\Driver\Mysql',
  395. 'host' => 'localhost',
  396. 'password' => '/?#][{}$%20@!',
  397. 'port' => 3306,
  398. 'scheme' => 'mysql',
  399. 'username' => 'user',
  400. 'log' => 1,
  401. 'quoteIdentifiers' => 1,
  402. ],
  403. ],
  404. ];
  405. }
  406. /**
  407. * Test parseDsn method.
  408. *
  409. * @dataProvider dsnProvider
  410. * @return void
  411. */
  412. public function testParseDsn($dsn, $expected)
  413. {
  414. $result = ConnectionManager::parseDsn($dsn);
  415. $this->assertEquals($expected, $result);
  416. }
  417. /**
  418. * Test parseDsn invalid.
  419. *
  420. * @return void
  421. */
  422. public function testParseDsnInvalid()
  423. {
  424. $this->expectException(\InvalidArgumentException::class);
  425. $this->expectExceptionMessage('The DSN string \'bagof:nope\' could not be parsed.');
  426. $result = ConnectionManager::parseDsn('bagof:nope');
  427. }
  428. /**
  429. * Tests that directly setting an instance in a config, will not return a different
  430. * instance later on
  431. *
  432. * @return void
  433. */
  434. public function testConfigWithObject()
  435. {
  436. $connection = new FakeConnection();
  437. ConnectionManager::setConfig('test_variant', $connection);
  438. $this->assertSame($connection, ConnectionManager::get('test_variant'));
  439. }
  440. /**
  441. * Tests configuring an instance with a callable
  442. *
  443. * @return void
  444. */
  445. public function testConfigWithCallable()
  446. {
  447. $connection = new FakeConnection();
  448. $callable = function ($alias) use ($connection) {
  449. $this->assertEquals('test_variant', $alias);
  450. return $connection;
  451. };
  452. ConnectionManager::setConfig('test_variant', $callable);
  453. $this->assertSame($connection, ConnectionManager::get('test_variant'));
  454. }
  455. /**
  456. * Tests that setting a config will also correctly set the name for the connection
  457. *
  458. * @return void
  459. */
  460. public function testSetConfigName()
  461. {
  462. //Set with explicit name
  463. ConnectionManager::setConfig('test_variant', [
  464. 'className' => __NAMESPACE__ . '\FakeConnection',
  465. 'database' => ':memory:',
  466. ]);
  467. $result = ConnectionManager::get('test_variant');
  468. $this->assertSame('test_variant', $result->configName());
  469. ConnectionManager::drop('test_variant');
  470. ConnectionManager::setConfig([
  471. 'test_variant' => [
  472. 'className' => __NAMESPACE__ . '\FakeConnection',
  473. 'database' => ':memory:',
  474. ],
  475. ]);
  476. $result = ConnectionManager::get('test_variant');
  477. $this->assertSame('test_variant', $result->configName());
  478. }
  479. }