ConnectionManagerTest.php 16 KB

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