ConnectionHelperTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @since 4.3.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\TestSuite;
  16. use Cake\Database\Connection;
  17. use Cake\Datasource\ConnectionManager;
  18. use Cake\Datasource\Exception\MissingDatasourceConfigException;
  19. use Cake\TestSuite\ConnectionHelper;
  20. use Cake\TestSuite\TestCase;
  21. use TestApp\Database\Driver\TestDriver;
  22. class ConnectionHelperTest extends TestCase
  23. {
  24. protected function tearDown(): void
  25. {
  26. parent::tearDown();
  27. ConnectionManager::drop('query_logging');
  28. ConnectionManager::drop('something');
  29. ConnectionManager::drop('test_something');
  30. ConnectionManager::dropAlias('something');
  31. }
  32. public function testAliasConnections(): void
  33. {
  34. ConnectionManager::dropAlias('default');
  35. ConnectionHelper::addTestAliases();
  36. $this->assertSame(
  37. ConnectionManager::get('test'),
  38. ConnectionManager::get('default')
  39. );
  40. }
  41. public function testAliasNonDefaultConnections(): void
  42. {
  43. $connection = new Connection(['driver' => TestDriver::class]);
  44. ConnectionManager::setConfig('test_something', $connection);
  45. ConnectionHelper::addTestAliases();
  46. // Having a test_ alias defined will generate an alias for the unprefixed
  47. // connection for simpler CI configuration
  48. $this->assertSame(
  49. ConnectionManager::get('test_something'),
  50. ConnectionManager::get('something')
  51. );
  52. }
  53. public function testAliasNoTestClass(): void
  54. {
  55. $connection = new Connection(['driver' => TestDriver::class]);
  56. ConnectionManager::setConfig('something', $connection);
  57. (new ConnectionHelper())->addTestAliases();
  58. // Should raise as no test connection was defined.
  59. $this->expectException(MissingDatasourceConfigException::class);
  60. ConnectionManager::get('test_something');
  61. }
  62. public function testAliasNonDefaultConnectionWithTestConnection(): void
  63. {
  64. $testConnection = new Connection(['driver' => TestDriver::class]);
  65. $connection = new Connection(['driver' => TestDriver::class]);
  66. ConnectionManager::setConfig('something', $connection);
  67. ConnectionManager::setConfig('test_something', $testConnection);
  68. (new ConnectionHelper())->addTestAliases();
  69. // Development connections that have test_ prefix connections defined
  70. // should have an alias defined for the test_ prefixed name. This allows
  71. // access to the development connection to resolve to the test prefixed name
  72. // in tests.
  73. $this->assertSame($testConnection, ConnectionManager::get('test_something'));
  74. $this->assertSame($testConnection, ConnectionManager::get('something'));
  75. }
  76. public function testEnableQueryLogging(): void
  77. {
  78. $connection = new Connection(['driver' => TestDriver::class]);
  79. ConnectionManager::setConfig('query_logging', $connection);
  80. $this->assertFalse($connection->getDriver()->log(''));
  81. ConnectionHelper::enableQueryLogging(['query_logging']);
  82. $this->assertTrue($connection->getDriver()->log(''));
  83. }
  84. }