ConnectionTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\DB;
  8. use Doctrine\DBAL\Configuration;
  9. use Doctrine\DBAL\Driver;
  10. use Doctrine\DBAL\Driver\Connection as DriverConnection;
  11. use Doctrine\DBAL\Platforms\MySQLPlatform;
  12. use OC\DB\Adapter;
  13. use OC\DB\Connection;
  14. use Test\TestCase;
  15. /**
  16. * @group DB
  17. */
  18. class ConnectionTest extends TestCase {
  19. public function testSingleNodeConnectsToPrimaryOnly(): void {
  20. $connectionParams = [
  21. 'user' => 'test',
  22. 'password' => 'topsecret',
  23. 'host' => 'test',
  24. ];
  25. $adapter = $this->createMock(Adapter::class);
  26. $driver = $this->createMock(Driver::class);
  27. $configuration = $this->createMock(Configuration::class);
  28. $connection = $this->getMockBuilder(Connection::class)
  29. ->onlyMethods(['connectTo'])
  30. ->setConstructorArgs([
  31. [
  32. 'adapter' => $adapter,
  33. 'platform' => new MySQLPlatform(),
  34. 'tablePrefix' => 'nctest',
  35. 'primary' => $connectionParams,
  36. 'replica' => [
  37. $connectionParams,
  38. ],
  39. ],
  40. $driver,
  41. $configuration,
  42. ])
  43. ->getMock();
  44. $driverConnection = $this->createMock(DriverConnection::class);
  45. $connection->expects(self::once())
  46. ->method('connectTo')
  47. ->with('primary')
  48. ->willReturn($driverConnection);
  49. $connection->ensureConnectedToReplica();
  50. $connection->ensureConnectedToPrimary();
  51. $connection->ensureConnectedToReplica();
  52. }
  53. public function testClusterConnectsToPrimaryAndReplica(): void {
  54. $connectionParamsPrimary = [
  55. 'user' => 'test',
  56. 'password' => 'topsecret',
  57. 'host' => 'testprimary',
  58. ];
  59. $connectionParamsReplica = [
  60. 'user' => 'test',
  61. 'password' => 'topsecret',
  62. 'host' => 'testreplica',
  63. ];
  64. $adapter = $this->createMock(Adapter::class);
  65. $driver = $this->createMock(Driver::class);
  66. $configuration = $this->createMock(Configuration::class);
  67. $connection = $this->getMockBuilder(Connection::class)
  68. ->onlyMethods(['connectTo'])
  69. ->setConstructorArgs([
  70. [
  71. 'adapter' => $adapter,
  72. 'platform' => new MySQLPlatform(),
  73. 'tablePrefix' => 'nctest',
  74. 'primary' => $connectionParamsPrimary,
  75. 'replica' => [
  76. $connectionParamsReplica,
  77. ],
  78. ],
  79. $driver,
  80. $configuration,
  81. ])
  82. ->getMock();
  83. $driverConnection = $this->createMock(DriverConnection::class);
  84. $connection->expects(self::exactly(2))
  85. ->method('connectTo')
  86. ->willReturn($driverConnection);
  87. $connection->ensureConnectedToReplica();
  88. $connection->ensureConnectedToPrimary();
  89. $connection->ensureConnectedToReplica();
  90. }
  91. }