|
29 | 29 |
|
30 | 30 | namespace VuFindTest\Db;
|
31 | 31 |
|
| 32 | +use PHPUnit\Framework\MockObject\MockObject; |
32 | 33 | use VuFind\Config\Version;
|
33 | 34 | use VuFind\Db\Connection;
|
34 | 35 | use VuFind\Db\ConnectionFactory;
|
|
48 | 49 | */
|
49 | 50 | class DbBuilderTest extends \PHPUnit\Framework\TestCase
|
50 | 51 | {
|
| 52 | + /** |
| 53 | + * Get a mock database connection with a working quote method. |
| 54 | + * |
| 55 | + * @return MockObject&Connection |
| 56 | + */ |
| 57 | + protected function getMockConnectionWithQuote(): MockObject&Connection |
| 58 | + { |
| 59 | + $mockConnection = $this->createMock(Connection::class); |
| 60 | + $mockConnection->expects($this->once())->method('quote')->willReturnCallback(fn ($str) => "'$str'"); |
| 61 | + return $mockConnection; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Data provider for testPortHandling(). |
| 66 | + * |
| 67 | + * @return array[] |
| 68 | + */ |
| 69 | + public static function portHandlingProvider(): array |
| 70 | + { |
| 71 | + return [ |
| 72 | + 'port' => ['localhost:1234', 'localhost', '1234'], |
| 73 | + 'no port' => ['localhost', 'localhost', null], |
| 74 | + ]; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Test port number processing. |
| 79 | + * |
| 80 | + * @param string $host Host string |
| 81 | + * @param string $expectedHost Expected hostname parsed from string |
| 82 | + * @param ?string $expectedPort Expected port number (or null) parsed from string |
| 83 | + * |
| 84 | + * @return void |
| 85 | + * |
| 86 | + * @dataProvider portHandlingProvider |
| 87 | + */ |
| 88 | + public function testPortHandling(string $host, string $expectedHost, ?string $expectedPort): void |
| 89 | + { |
| 90 | + $mockConnectionFactory = $this->createMock(ConnectionFactory::class); |
| 91 | + $mockLoader = $this->createMock(MigrationLoader::class); |
| 92 | + $builder = $this->getMockBuilder(DbBuilder::class)->onlyMethods(['getRootDatabaseConnection']) |
| 93 | + ->setConstructorArgs([$mockConnectionFactory, $mockLoader]) |
| 94 | + ->getMock(); |
| 95 | + $builder->expects($this->exactly(2))->method('getRootDatabaseConnection') |
| 96 | + ->with('mysql', $expectedHost, 'root', '', $expectedPort) |
| 97 | + ->willReturn($this->getMockConnectionWithQuote()); |
| 98 | + $builder->build('newName', 'newUser', 'newPass', 'mysql', $host); |
| 99 | + } |
| 100 | + |
51 | 101 | /**
|
52 | 102 | * Data provider for testPreCommands().
|
53 | 103 | *
|
@@ -95,10 +145,8 @@ public function testPreCommands(string $driver, array $expectedCommands, bool $s
|
95 | 145 | if ($sqlOnly) {
|
96 | 146 | $factory->expects($this->never())->method('getConnectionFromOptions');
|
97 | 147 | } else {
|
98 |
| - $mockConnection = $this->createMock(Connection::class); |
| 148 | + $mockConnection = $this->getMockConnectionWithQuote(); |
99 | 149 | $mockConnection->expects($this->exactly(count($expectedCommands)))->method('executeQuery');
|
100 |
| - $mockConnection->expects($this->exactly(1))->method('quote') |
101 |
| - ->willReturnCallback(fn ($str) => "'$str'"); |
102 | 150 | $factory->expects($this->exactly(1))->method('getConnectionFromOptions')->willReturn($mockConnection);
|
103 | 151 | }
|
104 | 152 | $builder = new DbBuilder($factory, $this->createMock(MigrationLoader::class));
|
|
0 commit comments